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
RSS