Sample code in C++ to run TAUCS

Now let us employ Matrix and SparseMatrix to run TAUCS. The goal will be to develop a command line tool that will read a matrix and a right-hand side

run_taucs matrix rhs

in the Matrix Market format and then writes the solution of the system of linear equations in file rhs.solution also in the Matrix Market format.

The code is presented in run_taucs.cpp and is briefly explained below. It is assumed that you have already browsed the TAUCS documentation.

1) The SparseMatrix mat is initialized from the file matrix (first argument in the command line).

2a) The TAUCS matrix is created. TAUCS uses Compressed Column Storage (CCS), see for example http://netlib.org/linalg/html_templates/node92.html. If the matrix is symmetic, then no changes are required because in this case row storage chosen in SparseMatrix can be treated as column storage. For a symmetric matrix it is after all just a matter of interpreting the indices. However, if the matrix is unsymmetric it is necessary first to transpose it, and only after that treat rows as columns.

2b) The SparseMatrix is iterated through all nonzero elements and they are copied into the TAUCS CCS matrix. Finally the memory to keep SparseMatrix is freed up.

3) The right hand side is read in Matrix rhs. Note that it can contain several vectors in the case when we need to solve the linear system several times with different right-hand sides.

4) Before starting solution we initialize the matrix to keep it with the same dimensions as rhs.

4a) We call taucs_linsolve. Please not that it is buggy and if you need to try different options it is more reliably to use low-level TAUCS functions (see Section Working with TAUCS by using low-level functions at Compiling and Using TAUCS under Microsoft Visual Studio). The low-level TAUCS functions will be used later in the class LinearSolver.

4b) We check if solution has been successful if and if yes we go to next step.

4c) The norm | Ax - rhs | is computed and printed. After that the solution is saved into the file.

5) Clean up memory.

In order to compile the code we need three steps. I assume that you are in the directory where run_taucs.cpp is located. First we compile run_taucs.cpp:

$ cl -I path_to_taucs_header -I LinearSolver -O2 -EHsc -MD -D_SECURE_SCL=0 -c run_taucs.cpp

Here it is necessary to specify the correct path to the TAUCS header. The second -I gives the path to matrices.h and auxiliary.h that are in the subdirectory LinearSolver. After that we need to compile matrices.cpp, as functions from Matrix and SparseMatrix should be compiled as well.

$ cl -I LinearSolver -O2 -EHsc -MD -D_SECURE_SCL=0 -c LinearSolver/matrices.cpp

Finally it is necessary to link object files with each other as well as with TAUCS, METIS and BLAS libraries (in the example below I use MKL as BLAS):

$ cl run_taucs.obj matrices.obj libtaucs.lib libmetis.lib mkl_intel_c.lib mkl_intel_thread.lib mkl_core.lib libiomp5md.lib -link -LIBPATH:path_to_libraries

Here it is important to put the path to the libraries that the linker can find them. Note that the MS linker also searches the path specified by the LIB environment variables.

The three steps above are written within the makefile run_taucs.make and the command

$ make -f run_taucs.make

takes necessary steps to compile run_taucs provided that you have specified the paths to the headers and libraries in the makefile correctly.

It is possible to use files 3×3.mtx and 3×1.mtx for testing (they are from the discussion at matrixprogramming). The command

$ ./run_taucs 3x3.mtx 3x1.mtx

should write solution (9.25 8.5 5.75) in the file 3x1.mtx.solution. The command

$ ./run_taucs 3x3.mtx 3x3.mtx

should produce the identity matrix as the solution to AX = A.

Previous

Introduction
Class Matrix
Class SparseMatrix

Next

Sample code in C++ to run MUMPS
Class LinearSolver
  Class UMFPACK
  Class TAUCS
  Class MUMPS and PARDISO
  Gluing all together: LinearSolver::create
Sample driver to run a sparse solver


Comments

6 responses to “Sample code in C++ to run TAUCS”

Comments are now closed
  1. JBGAO says:

    matrices.h includes a header file, named “auxiliary.h”. Where can I find it?

    jbgao

  2. In the same directory

    http://matrixprogramming.com/files/code/benchmark/LinearSolver/

    Sorry, it is unclear from the text. I have changed now the text a bit.

  3. jbgao says:

    Hi Evgenii

    Thanks for your answer. I will test it soon.

    Now I have another question. I installed Taucs ver 2.2 and successfully run configure.bat and nmake in MVC 2010 Express command window. I obtained a lib file libtaucs.lib and other executable files like taucs_run.exe, test_linsolve.exe etc. All of them can be run.

    So I want to build test_linsolve.exe my own, thus I can know how to write my own programs in MVC++ 2010 Express. However after setting up a project, compiling test_linsolve is going well, while I have the following linking errors. What is the program? I did include all the necessary libraries etc.

    Thanks in advance

    jbgao

    1>—— Build started: Project: Taucs, Configuration: Debug Win32 ——
    1> test_linsolve.c
    1>LINK : warning LNK4098: defaultlib ‘LIBCMT’ conflicts with use of other libs; use /NODEFAULTLIB:library
    1>test_linsolve.obj : error LNK2019: unresolved external symbol _taucs_linsolve referenced in function _test_spd_orderings
    1>test_linsolve.obj : error LNK2019: unresolved external symbol _taucs_ccs_generate_mesh3d referenced in function _main
    1>libtaucs.lib(taucs_vec_base.obj) : error LNK2019: unresolved external symbol _taucs_get_nan referenced in function _taucs_vec_norm2
    1>C:\Users\Junbin\Documents\GaoFiles\C++Workplaces\Taucs\Debug\Taucs.exe : fatal error LNK1120: 3 unresolved externals
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  4. jbgao says:

    Hi Evgenii

    I got plenty of errors when compiling run_taucs.cpp under MVC++ 2010 Express (I built a project).

    jbgao

    Hi Evgenii

    ======================

    1>—— Build started: Project: Taucs, Configuration: Debug Win32 ——
    1> solvers.cpp
    1>c:\users\junbin\documents\gaofiles\c++workplaces\taucs\taucs\solvers.cpp(52): error C2275: ‘func’ : illegal use of this type as an expression
    1> c:\users\junbin\documents\gaofiles\c++workplaces\taucs\taucs\solvers.cpp(33) : see declaration of ‘func’
    1>c:\users\junbin\documents\gaofiles\c++workplaces\taucs\taucs\solvers.cpp(52): error C2275: ‘func’ : illegal use of this type as an expression
    1> c:\users\junbin\documents\gaofiles\c++workplaces\taucs\taucs\solvers.cpp(33) : see declaration of ‘func’
    1>c:\users\junbin\documents\gaofiles\c++workplaces\taucs\taucs\solvers.cpp(52): error C2955: ‘std::map’ : use of class template requires template argument list
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\map(80) : see declaration of ‘std::map’
    1>c:\users\junbin\documents\gaofiles\c++workplaces\taucs\taucs\solvers.cpp(52): error C2819: type ‘std::map’ does not have an overloaded member ‘operator ->’
    1> with
    1> [
    1> _Kty=std::string,
    1> _Ty=func
    1> ]
    1> did you intend to use ‘.’ instead?
    1>c:\users\junbin\documents\gaofiles\c++workplaces\taucs\taucs\solvers.cpp(52): error C2232: ‘->std::_Tree::find’ : left operand has ‘class’ type, use ‘.’
    1> with
    1> [
    1> _Traits=std::_Tmap_traits<std::string,func ,std::less,std::allocator<std::pair>,false>
    1> ]
    1>c:\users\junbin\documents\gaofiles\c++workplaces\taucs\taucs\solvers.cpp(52): error C2512: ‘std::_Tree_const_iterator<std::_Tree_val>’ : no appropriate default constructor available
    1>c:\users\junbin\documents\gaofiles\c++workplaces\taucs\taucs\solvers.cpp(53): error C2065: ‘types’ : undeclared identifier
    1>c:\users\junbin\documents\gaofiles\c++workplaces\taucs\taucs\solvers.cpp(53): error C2227: left of ‘->end’ must point to class/struct/union/generic type
    1> type is ”unknown-type”
    1>c:\users\junbin\documents\gaofiles\c++workplaces\taucs\taucs\solvers.cpp(55): fatal error C1903: unable to recover from previous error(s); stopping compilation
    1> solver_taucs.cpp
    1>c:\users\junbin\documents\gaofiles\c++workplaces\taucs\taucs\solver_taucs.cpp(32): warning C4800: ‘int’ : forcing value to bool ‘true’ or ‘false’ (performance warning)
    1>c:\users\junbin\documents\gaofiles\c++workplaces\taucs\taucs\solver_taucs.cpp(149): error C3861: ‘mor_dcopy’: identifier not found
    1> Generating Code…
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  5. jbgao says:

    I noted that libtaucs.lib is only 13K in size, so I doubt that libtaucs.lib has not been successfully established? What is the problem with MVC++ 2010 Express?

    jbgao

  6. 1) You need to make sure that you compile TAUCS with the same run time library, please look at

    http://matrixprogramming.com/2009/03/taucs-md

    2) solvers.cpp is not ready yet and you do not need to compile run_taucs.cpp. Please use only files that you need.

    3) I do not know. The best if you follow instructions from the command line, as I do not work with GUI and cannot help you in this respect.