The following tutorial shows how to use the iterative solvers in ViennaCL with objects from the Eigen Library directly.
- Note
- Eigen provides its own iterative solvers in the meanwhile. Check these first.
We begin with including the necessary headers:
#include <iostream>
#ifndef NDEBUG
#define NDEBUG
#endif
#include <Eigen/Core>
#include <Eigen/Sparse>
#define VIENNACL_WITH_EIGEN 1
In the following we run the CG method, the BiCGStab method, and the GMRES method with Eigen types directly. First, the matrices are set up, then the respective solvers are called.
{
Eigen::SparseMatrix<ScalarType, Eigen::RowMajor> eigen_matrix(65025, 65025);
Eigen::VectorXf eigen_rhs;
Eigen::VectorXf eigen_result;
Eigen::VectorXf ref_result;
Eigen::VectorXf residual;
Read system from file
std::cout << "Reading matrix (this might take some time)..." << std::endl;
eigen_matrix.reserve(65025 * 7);
{
std::cout << "Error reading Matrix file. Make sure you run from the build/-folder." << std::endl;
return EXIT_FAILURE;
}
std::cout << "Done: reading matrix" << std::endl;
{
std::cout << "Error reading RHS file" << std::endl;
return EXIT_FAILURE;
}
{
std::cout << "Error reading Result file" << std::endl;
return EXIT_FAILURE;
}
Conjugate Gradient (CG) solver:
std::cout << "----- Running CG -----" << std::endl;
residual = eigen_matrix * eigen_result - eigen_rhs;
Stabilized Bi-Conjugate Gradient (BiCGStab) solver:
std::cout << "----- Running BiCGStab -----" << std::endl;
residual = eigen_matrix * eigen_result - eigen_rhs;
Generalized Minimum Residual (GMRES) solver:
std::cout << "----- Running GMRES -----" << std::endl;
residual = eigen_matrix * eigen_result - eigen_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>
#ifndef NDEBUG
#define NDEBUG
#endif
#include <Eigen/Core>
#include <Eigen/Sparse>
#define VIENNACL_WITH_EIGEN 1
{
Eigen::SparseMatrix<ScalarType, Eigen::RowMajor> eigen_matrix(65025, 65025);
Eigen::VectorXf eigen_rhs;
Eigen::VectorXf eigen_result;
Eigen::VectorXf ref_result;
Eigen::VectorXf residual;
std::cout << "Reading matrix (this might take some time)..." << std::endl;
eigen_matrix.reserve(65025 * 7);
{
std::cout << "Error reading Matrix file. Make sure you run from the build/-folder." << std::endl;
return EXIT_FAILURE;
}
std::cout << "Done: reading matrix" << std::endl;
{
std::cout << "Error reading RHS file" << std::endl;
return EXIT_FAILURE;
}
{
std::cout << "Error reading Result file" << std::endl;
return EXIT_FAILURE;
}
std::cout << "----- Running CG -----" << std::endl;
residual = eigen_matrix * eigen_result - eigen_rhs;
std::cout << "----- Running BiCGStab -----" << std::endl;
residual = eigen_matrix * eigen_result - eigen_rhs;
std::cout << "----- Running GMRES -----" << std::endl;
residual = eigen_matrix * eigen_result - eigen_rhs;
std::cout << std::endl;
std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
std::cout << std::endl;
}