1#ifndef VIENNASHE_SOLVERS_VIENNACL_SERIAL_LINEAR_SOLVER_HPP
2#define VIENNASHE_SOLVERS_VIENNACL_SERIAL_LINEAR_SOLVER_HPP
27#include "viennacl/linalg/bicgstab.hpp"
28#include "viennacl/linalg/ilu.hpp"
29#include "viennacl/linalg/norm_2.hpp"
30#include "viennacl/linalg/prod.hpp"
31#include "viennacl/io/matrix_market.hpp"
44 template <
typename MatrixT,
typename NumericT>
45 void copy(MatrixT
const & assembled_matrix,
46 viennacl::compressed_matrix<NumericT> & vcl_matrix)
51 template <
typename NumericT>
53 viennacl::compressed_matrix<NumericT> & vcl_matrix)
55 std::size_t nonzeros = assembled_matrix.
nnz();
56 viennacl::backend::typesafe_host_array<unsigned int> row_buffer(vcl_matrix.handle1(), assembled_matrix.
size1() + 1);
57 viennacl::backend::typesafe_host_array<unsigned int> col_buffer(vcl_matrix.handle2(), nonzeros);
58 std::vector<NumericT> elements(nonzeros);
60 std::size_t data_index = 0;
62 for (std::size_t i = 0;
63 i != assembled_matrix.
size1();
69 row_buffer.set(i, data_index);
70 RowType
const & row_i = assembled_matrix.
row(i);
72 for (AlongRowIterator col_it = row_i.begin();
73 col_it != row_i.end();
76 col_buffer.set(data_index, col_it->first);
77 elements[data_index] = col_it->second;
81 row_buffer.set(assembled_matrix.
size1(), data_index);
83 vcl_matrix.set(row_buffer.get(),
86 assembled_matrix.
size1(),
87 assembled_matrix.
size2(),
99 template <
typename MatrixType,
101 VectorType
solve(MatrixType
const & system_matrix,
102 VectorType
const & rhs,
107 typedef typename VectorType::value_type NumericT;
112 viennacl::compressed_matrix<NumericT> A(system_matrix.size1(), system_matrix.size2());
113 viennacl::vector<NumericT> b(system_matrix.size1());
115 viennacl::fast_copy(&(rhs[0]), &(rhs[0]) + rhs.size(), b.begin());
122 log::info<log_linear_solver>() <<
"* solve(): Computing preconditioner (single-threaded)... " << std::endl;
125 viennacl::linalg::ilu0_tag precond_tag;
126 viennacl::linalg::ilu0_precond<viennacl::compressed_matrix<NumericT> > preconditioner(A, precond_tag);
128 log::info<log_linear_solver>() <<
"* solve(): Solving system (single-threaded)... " << std::endl;
129 viennacl::linalg::bicgstab_tag solver_tag(
config.tolerance(),
config.max_iters());
142 VectorType result(vcl_result.size());
143 viennacl::fast_copy(vcl_result.begin(), vcl_result.end(), &(result[0]));
150 log::info<log_linear_solver>() <<
"* solve(): residual: "
152 <<
" after " << solver_tag.iters() <<
" iterations." << std::endl;
Defines a set of checker functors for micro-tests within ViennaSHE.
The main SHE configuration class. To be adjusted by the user for his/her needs.
Sparse matrix class based on a vector of binary trees for holding the entries.
MapType const & row(size_t i) const
MapType::const_iterator const_iterator2
A configuration class holding options for use within the different linear solvers.
Internal tag used for the specification of a single-threaded linear solver.
Contains forward declarations and definition of small classes that must be defined at an early stage.
Implementation of various utilities related to linear algebra.
A logging facility providing fine-grained control over logging in ViennaSHE.
VectorType::value_type norm_2(VectorType const &v)
VectorType prod(sparse_matrix< NumericT > const &system_matrix, VectorType const &x)
Computes A * x for a sparse A and a vector x.
void copy(MatrixT const &assembled_matrix, viennacl::compressed_matrix< NumericT > &vcl_matrix)
void copy(viennashe::math::sparse_matrix< NumericT > const &assembled_matrix, viennacl::compressed_matrix< NumericT > &vcl_matrix)
std::vector< double > solve(viennashe::math::sparse_matrix< double > &A, std::vector< double > const &b, linear_solver_config const &config)
Public interface for solving a system of linear equations represented using a sparse matrix.
VectorType solve(MatrixType const &system_matrix, VectorType const &rhs, viennashe::solvers::linear_solver_config const &config, viennashe::solvers::serial_linear_solver_tag)
Solves the provided system using an iterative solver in a serial fashion (single-threaded execution)
void check_vector_for_valid_entries(VectorType const &vec, std::string message="Location not specified")
Checks a vector for valid entries (i.e. no NaN).
The main ViennaSHE namespace. All functionality resides inside this namespace.
The SHE configuration class is defined here.
Defines all the log keys used within the viennashe::solvers namespace.