ViennaSHE 1.3.0
Free open-source semiconductor device simulator using spherical harmonics expansions techniques.
serial_linear_solver.hpp
Go to the documentation of this file.
1#ifndef VIENNASHE_SOLVERS_VIENNACL_SERIAL_LINEAR_SOLVER_HPP
2#define VIENNASHE_SOLVERS_VIENNACL_SERIAL_LINEAR_SOLVER_HPP
3
4/* ============================================================================
5 Copyright (c) 2011-2022, Institute for Microelectronics,
6 Institute for Analysis and Scientific Computing,
7 TU Wien.
8
9 -----------------
10 ViennaSHE - The Vienna Spherical Harmonics Expansion Boltzmann Solver
11 -----------------
12
13 http://viennashe.sourceforge.net/
14
15 License: MIT (X11), see file LICENSE in the base directory
16=============================================================================== */
17
18// viennashe
19#include "viennashe/forwards.h"
22#include "viennashe/log/log.hpp"
25
26// viennacl
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"
32
33
38namespace viennashe
39{
40 namespace solvers
41 {
42 namespace detail
43 {
44 template <typename MatrixT, typename NumericT>
45 void copy(MatrixT const & assembled_matrix,
46 viennacl::compressed_matrix<NumericT> & vcl_matrix)
47 {
48 viennacl::copy(assembled_matrix, vcl_matrix);
49 }
50
51 template <typename NumericT>
52 void copy(viennashe::math::sparse_matrix<NumericT> const & assembled_matrix,
53 viennacl::compressed_matrix<NumericT> & vcl_matrix)
54 {
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);
59
60 std::size_t data_index = 0;
61
62 for (std::size_t i = 0;
63 i != assembled_matrix.size1();
64 ++i)
65 {
66 typedef typename viennashe::math::sparse_matrix<NumericT>::const_iterator2 AlongRowIterator;
68
69 row_buffer.set(i, data_index);
70 RowType const & row_i = assembled_matrix.row(i);
71
72 for (AlongRowIterator col_it = row_i.begin();
73 col_it != row_i.end();
74 ++col_it)
75 {
76 col_buffer.set(data_index, col_it->first);
77 elements[data_index] = col_it->second;
78 ++data_index;
79 }
80 }
81 row_buffer.set(assembled_matrix.size1(), data_index);
82
83 vcl_matrix.set(row_buffer.get(),
84 col_buffer.get(),
85 &elements[0],
86 assembled_matrix.size1(),
87 assembled_matrix.size2(),
88 nonzeros);
89 }
90 }
91
92
99 template <typename MatrixType,
100 typename VectorType>
101 VectorType solve(MatrixType const & system_matrix,
102 VectorType const & rhs,
105 )
106 {
107 typedef typename VectorType::value_type NumericT;
108
109 //
110 // Step 1: Convert data to ViennaCL types:
111 //
112 viennacl::compressed_matrix<NumericT> A(system_matrix.size1(), system_matrix.size2());
113 viennacl::vector<NumericT> b(system_matrix.size1());
114
115 viennacl::fast_copy(&(rhs[0]), &(rhs[0]) + rhs.size(), b.begin());
116 detail::copy(system_matrix, A);
117
118
119 //
120 // Step 2: Setup preconditioner and run solver
121 //
122 log::info<log_linear_solver>() << "* solve(): Computing preconditioner (single-threaded)... " << std::endl;
123 //viennacl::linalg::ilut_tag precond_tag(config.ilut_entries(),
124 // config.ilut_drop_tolerance());
125 viennacl::linalg::ilu0_tag precond_tag;
126 viennacl::linalg::ilu0_precond<viennacl::compressed_matrix<NumericT> > preconditioner(A, precond_tag);
127
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());
130
131 //log::debug<log_linear_solver>() << "Compressed matrix: " << system_matrix << std::endl;
132 //log::debug<log_linear_solver>() << "Compressed rhs: " << rhs << std::endl;
133 viennacl::vector<NumericT> vcl_result = viennacl::linalg::solve(A,
134 b,
135 solver_tag,
136 preconditioner);
137 //log::debug<log_linear_solver>() << "Number of iterations (ILUT): " << solver_tag.iters() << std::endl;
138
139 //
140 // Step 3: Convert data back:
141 //
142 VectorType result(vcl_result.size());
143 viennacl::fast_copy(vcl_result.begin(), vcl_result.end(), &(result[0]));
144
146
147 //
148 // As a check, compute residual:
149 //
150 log::info<log_linear_solver>() << "* solve(): residual: "
152 << " after " << solver_tag.iters() << " iterations." << std::endl;
153 //log::debug<log_linear_solver>() << "SHE result (compressed): " << compressed_result << std::endl;
154
155 return result;
156 }
157
158
159 } // solvers
160} // viennashe
161
162#endif
163
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.
Definition: config.hpp:124
Sparse matrix class based on a vector of binary trees for holding the entries.
Definition: linalg_util.hpp:69
MapType const & row(size_t i) const
MapType::const_iterator const_iterator2
Definition: linalg_util.hpp:82
A configuration class holding options for use within the different linear solvers.
Definition: config.hpp:57
Internal tag used for the specification of a single-threaded linear solver.
Definition: forwards.h:206
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)
Definition: linalg_util.hpp:37
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).
Definition: checks.hpp:111
The main ViennaSHE namespace. All functionality resides inside this namespace.
Definition: accessors.hpp:40
The SHE configuration class is defined here.
Defines all the log keys used within the viennashe::solvers namespace.