All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
iterative-armadillo.cpp
Go to the documentation of this file.
/* =========================================================================
Copyright (c) 2010-2016, Institute for Microelectronics,
Institute for Analysis and Scientific Computing,
TU Wien.
Portions of this software are copyright by UChicago Argonne, LLC.
-----------------
ViennaCL - The Vienna Computing Library
-----------------
Project Head: Karl Rupp rupp@iue.tuwien.ac.at
(A list of authors and contributors can be found in the PDF manual)
License: MIT (X11), see file LICENSE in the base directory
============================================================================= */
// System headers
#include <iostream>
#ifndef NDEBUG
#define NDEBUG
#endif
// Armadillo headers (disable BLAS and LAPACK to avoid linking issues)
#define ARMA_DONT_USE_BLAS
#define ARMA_DONT_USE_LAPACK
#include <armadillo>
// IMPORTANT: Must be set prior to any ViennaCL includes if you want to use ViennaCL algorithms on Armadillo objects
#define VIENNACL_WITH_ARMADILLO 1
// ViennaCL headers
#include "viennacl/linalg/cg.hpp"
#include "viennacl/linalg/bicgstab.hpp"
#include "viennacl/linalg/gmres.hpp"
#include "viennacl/io/matrix_market.hpp"
// Some helper functions for this tutorial:
#include "vector-io.hpp"
int main(int, char *[])
{
typedef float ScalarType;
std::vector<std::map<unsigned int, ScalarType> > stl_matrix;
std::cout << "Reading matrix (this might take some time)..." << std::endl;
if (!viennacl::io::read_matrix_market_file(stl_matrix, "../examples/testdata/mat65k.mtx"))
{
std::cout << "Error reading Matrix file. Make sure you run from the build/-folder." << std::endl;
return EXIT_FAILURE;
}
// Copy over to Armadillo sparse matrix by putting the indices into a matrix and the values into a vector:
std::size_t num_nnz = 0;
for (std::size_t i=0; i<stl_matrix.size(); ++i)
num_nnz += stl_matrix[i].size();
arma::Mat<arma::uword> arma_indices(2, num_nnz);
arma::Col<ScalarType> arma_values(num_nnz);
std::size_t index = 0;
for (std::size_t i=0; i<stl_matrix.size(); ++i)
{
for (std::map<unsigned int, ScalarType>::const_iterator it = stl_matrix[i].begin(); it != stl_matrix[i].end(); ++it)
{
arma_indices(0, index) = i;
arma_indices(1, index) = it->first;
arma_values(index) = it->second;
++index;
}
}
std::cout << "Done: reading matrix" << std::endl;
arma::SpMat<ScalarType> arma_matrix(arma_indices, arma_values, 65025, 65025);
arma::Col<ScalarType> arma_rhs;
arma::Col<ScalarType> arma_result;
arma::Col<ScalarType> residual;
if (!readVectorFromFile("../examples/testdata/rhs65025.txt", arma_rhs))
{
std::cout << "Error reading RHS file" << std::endl;
return EXIT_FAILURE;
}
if (!readVectorFromFile("../examples/testdata/result65025.txt", arma_result))
{
std::cout << "Error reading Result file" << std::endl;
return EXIT_FAILURE;
}
std::cout << "----- Running CG -----" << std::endl;
arma_result = viennacl::linalg::solve(arma_matrix, arma_rhs, viennacl::linalg::cg_tag());
residual = arma_matrix * arma_result - arma_rhs;
std::cout << "Relative residual: " << viennacl::linalg::norm_2(residual) / viennacl::linalg::norm_2(arma_rhs) << std::endl;
std::cout << "----- Running BiCGStab -----" << std::endl;
arma_result = viennacl::linalg::solve(arma_matrix, arma_rhs, viennacl::linalg::bicgstab_tag());
residual = arma_matrix * arma_result - arma_rhs;
std::cout << "Relative residual: " << viennacl::linalg::norm_2(residual) / viennacl::linalg::norm_2(arma_rhs) << std::endl;
std::cout << "----- Running GMRES -----" << std::endl;
arma_result = viennacl::linalg::solve(arma_matrix, arma_rhs, viennacl::linalg::gmres_tag());
residual = arma_matrix * arma_result - arma_rhs;
std::cout << "Relative residual: " << viennacl::linalg::norm_2(residual) / viennacl::linalg::norm_2(arma_rhs) << std::endl;
std::cout << std::endl;
std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
std::cout << std::endl;
}
viennacl::linalg::norm_2
T norm_2(std::vector< T, A > const &v1)
Definition: norm_2.hpp:96
matrix_market.hpp
A reader and writer for the matrix market format is implemented here.
main
int main()
Definition: bisect.cpp:91
bicgstab.hpp
The stabilized bi-conjugate gradient method is implemented here.
vector-io.hpp
viennacl::linalg::gmres_tag
A tag for the solver GMRES. Used for supplying solver parameters and for dispatching the solve() func...
Definition: gmres.hpp:49
viennacl::linalg::solve
VectorT solve(MatrixT const &matrix, VectorT const &rhs, bicgstab_tag const &tag, PreconditionerT const &precond)
Definition: bicgstab.hpp:496
viennacl::traits::size
vcl_size_t size(VectorType const &vec)
Generic routine for obtaining the size of a vector (ViennaCL, uBLAS, etc.)
Definition: size.hpp:239
gmres.hpp
Implementations of the generalized minimum residual method are in this file.
readVectorFromFile
bool readVectorFromFile(const std::string &filename, VectorType &vec)
Definition: vector-io.hpp:104
cg.hpp
The conjugate gradient method is implemented here.
ScalarType
float ScalarType
Definition: fft_1d.cpp:42
viennacl::linalg::cg_tag
A tag for the conjugate gradient Used for supplying solver parameters and for dispatching the solve()...
Definition: cg.hpp:48
viennacl::linalg::bicgstab_tag
A tag for the stabilized Bi-conjugate gradient solver. Used for supplying solver parameters and for d...
Definition: bicgstab.hpp:47
viennacl::io::read_matrix_market_file
long read_matrix_market_file(MatrixT &mat, const char *file, long index_base=1)
Reads a sparse matrix from a file (MatrixMarket format)
Definition: matrix_market.hpp:339