The following tutorial shows how to use the iterative solvers in ViennaCL with objects from the MTL4 Library directly.
- Note
- MTL4 provides iterative solvers through the ITK library. You might also want to check these.
We begin with including the necessary headers:
#include <iostream>
#include <boost/numeric/mtl/mtl.hpp>
#include <boost/numeric/itl/itl.hpp>
#define VIENNACL_WITH_MTL4 1
In the following we run the CG method, the BiCGStab method, and the GMRES method with MTL4 types directly. First, the matrices are set up, then the respective solvers are called.
{
mtl::compressed2D<ScalarType> mtl4_matrix;
mtl4_matrix.change_dim(65025, 65025);
set_to_zero(mtl4_matrix);
mtl::dense_vector<ScalarType> mtl4_rhs(65025, 1.0);
mtl::dense_vector<ScalarType> mtl4_result(65025, 0.0);
mtl::dense_vector<ScalarType> mtl4_residual(65025, 0.0);
Read system from file
mtl::io::matrix_market_istream("../examples/testdata/mat65k.mtx") >> mtl4_matrix;
Conjugate Gradient (CG) solver:
std::cout << "----- Running CG -----" << std::endl;
mtl4_residual = mtl4_matrix * mtl4_result - mtl4_rhs;
Stabilized Bi-Conjugate Gradient (BiCGStab) solver:
std::cout << "----- Running BiCGStab -----" << std::endl;
mtl4_residual = mtl4_matrix * mtl4_result - mtl4_rhs;
Generalized Minimum Residual (GMRES) solver:
std::cout << "----- Running GMRES -----" << std::endl;
mtl4_residual = mtl4_matrix * mtl4_result - mtl4_rhs;
That's it. Print a success message and exit.
std::cout << std::endl;
std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
std::cout << std::endl;
}
Full Example Code
#include <iostream>
#include <boost/numeric/mtl/mtl.hpp>
#include <boost/numeric/itl/itl.hpp>
#define VIENNACL_WITH_MTL4 1
{
mtl::compressed2D<ScalarType> mtl4_matrix;
mtl4_matrix.change_dim(65025, 65025);
set_to_zero(mtl4_matrix);
mtl::dense_vector<ScalarType> mtl4_rhs(65025, 1.0);
mtl::dense_vector<ScalarType> mtl4_result(65025, 0.0);
mtl::dense_vector<ScalarType> mtl4_residual(65025, 0.0);
mtl::io::matrix_market_istream("../examples/testdata/mat65k.mtx") >> mtl4_matrix;
std::cout << "----- Running CG -----" << std::endl;
mtl4_residual = mtl4_matrix * mtl4_result - mtl4_rhs;
std::cout << "----- Running BiCGStab -----" << std::endl;
mtl4_residual = mtl4_matrix * mtl4_result - mtl4_rhs;
std::cout << "----- Running GMRES -----" << std::endl;
mtl4_residual = mtl4_matrix * mtl4_result - mtl4_rhs;
std::cout << std::endl;
std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
std::cout << std::endl;
}