In this example we show how one can use the iterative solvers in ViennaCL with objects from Boost.uBLAS.
The first step is to include the necessary headers:
#include <iostream>
#ifndef NDEBUG
#define BOOST_UBLAS_NDEBUG
#endif
#include <boost/numeric/ublas/io.hpp>
#include <boost/numeric/ublas/triangular.hpp>
#include <boost/numeric/ublas/matrix_sparse.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/matrix_proxy.hpp>
#include <boost/numeric/ublas/operation.hpp>
#include <boost/numeric/ublas/operation_sparse.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/numeric/ublas/lu.hpp>
#define VIENNACL_WITH_UBLAS 1
A shortcut for convience (use ublas::
instead of boost::numeric::ublas::
)
In the main() routine we create matrices and vectors using Boost.uBLAS types, fill them with data and launch the iterative solvers.
{
ublas::vector<ScalarType> rhs;
ublas::vector<ScalarType> ref_result;
ublas::vector<ScalarType> result;
ublas::compressed_matrix<ScalarType> ublas_matrix;
Read system from matrix-market file
{
std::cout << "Error reading Matrix file" << std::endl;
return EXIT_FAILURE;
}
Read associated vectors from files
{
std::cout << "Error reading RHS file" << std::endl;
return EXIT_FAILURE;
}
{
std::cout << "Error reading Result file" << std::endl;
return EXIT_FAILURE;
}
Set up ILUT preconditioners for ViennaCL and ublas objects. Other preconditioners can also be used, see Preconditioners
First we run an conjugate gradient solver with different preconditioners:
std::cout << "----- CG Test -----" << std::endl;
std::cout <<
"Residual norm: " <<
norm_2(
prod(ublas_matrix, result) - rhs) << std::endl;
std::cout <<
"Residual norm: " <<
norm_2(
prod(ublas_matrix, result) - rhs) << std::endl;
std::cout <<
"Residual norm: " <<
norm_2(
prod(ublas_matrix, result) - rhs) << std::endl;
std::cout <<
"Residual norm: " <<
norm_2(
prod(ublas_matrix, result) - rhs) << std::endl;
Run the stabilized BiConjugate gradient solver without and with preconditioners (ILUT, ILU0)
std::cout << "----- BiCGStab Test -----" << std::endl;
Run the generalized minimum residual method witout and with preconditioners (ILUT, ILU0):
std::cout << "----- GMRES Test -----" << std::endl;
That's it.
std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
return EXIT_SUCCESS;
}
Full Example Code
#include <iostream>
#ifndef NDEBUG
#define BOOST_UBLAS_NDEBUG
#endif
#include <boost/numeric/ublas/io.hpp>
#include <boost/numeric/ublas/triangular.hpp>
#include <boost/numeric/ublas/matrix_sparse.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/matrix_proxy.hpp>
#include <boost/numeric/ublas/operation.hpp>
#include <boost/numeric/ublas/operation_sparse.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/numeric/ublas/lu.hpp>
#define VIENNACL_WITH_UBLAS 1
{
ublas::vector<ScalarType> rhs;
ublas::vector<ScalarType> ref_result;
ublas::vector<ScalarType> result;
ublas::compressed_matrix<ScalarType> ublas_matrix;
{
std::cout << "Error reading Matrix file" << std::endl;
return EXIT_FAILURE;
}
{
std::cout << "Error reading RHS file" << std::endl;
return EXIT_FAILURE;
}
{
std::cout << "Error reading Result file" << std::endl;
return EXIT_FAILURE;
}
std::cout << "----- CG Test -----" << std::endl;
std::cout <<
"Residual norm: " <<
norm_2(
prod(ublas_matrix, result) - rhs) << std::endl;
std::cout <<
"Residual norm: " <<
norm_2(
prod(ublas_matrix, result) - rhs) << std::endl;
std::cout <<
"Residual norm: " <<
norm_2(
prod(ublas_matrix, result) - rhs) << std::endl;
std::cout <<
"Residual norm: " <<
norm_2(
prod(ublas_matrix, result) - rhs) << std::endl;
std::cout << "----- BiCGStab Test -----" << std::endl;
std::cout << "----- GMRES Test -----" << std::endl;
std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
return EXIT_SUCCESS;
}