monolish  0.14.0
MONOlithic LIner equation Solvers for Highly-parallel architecture
equation_utils.cpp
Go to the documentation of this file.
1 #include "../../include/monolish_blas.hpp"
2 #include "../../include/monolish_vml.hpp"
3 #include "../internal/monolish_internal.hpp"
4 
5 namespace monolish {
6 
7 bool util::solver_check(const int err) {
8  switch (err) {
10  return 0;
12  std::runtime_error("equation error, maxiter\n");
13  return false;
15  std::runtime_error("equation error, breakdown\n");
16  return false;
18  std::runtime_error("equation error, size error\n");
19  return false;
21  std::runtime_error("equation error, resudual is nan\n");
22  return false;
24  std::runtime_error("equation error, this solver is not impl.\n");
25  return false;
26  default:
27  return 0;
28  }
29 }
30 namespace {
31 
32 template <typename T, typename V1, typename V2>
33 T get_residual_l2_core(const matrix::Dense<T> &A, const V1 &x, const V2 &y) {
34  Logger &logger = Logger::get_instance();
35  logger.util_in(monolish_func);
36  vector<T> tmp(x.size());
37  tmp.send();
38 
39  blas::matvec(A, x, tmp); // tmp=Ax
40  vml::sub(y, tmp, tmp);
41  logger.util_out();
42  return blas::nrm2(tmp);
43 }
44 
45 template <typename T, typename V1, typename V2>
46 T get_residual_l2_core(const matrix::CRS<T> &A, const V1 &x, const V2 &y) {
47  Logger &logger = Logger::get_instance();
48  logger.util_in(monolish_func);
49  vector<T> tmp(x.size());
50  tmp.send();
51 
52  blas::matvec(A, x, tmp); // tmp=Ax
53  vml::sub(y, tmp, tmp);
54  logger.util_out();
55  return blas::nrm2(tmp);
56 }
57 
58 template <typename T, typename V1, typename V2>
59 T get_residual_l2_core(const matrix::LinearOperator<T> &A, const V1 &x,
60  const V2 &y) {
61  Logger &logger = Logger::get_instance();
62  logger.util_in(monolish_func);
63  vector<T> tmp(x.size());
64  tmp.send();
65 
66  blas::matvec(A, x, tmp); // tmp=Ax
67  vml::sub(y, tmp, tmp);
68  logger.util_out();
69  return blas::nrm2(tmp);
70 }
71 } // namespace
73  const vector<double> &y) {
74  return get_residual_l2_core(A, x, y);
75 }
77  const view1D<vector<double>, double> &y) {
78  return get_residual_l2_core(A, x, y);
79 }
81  const view1D<matrix::Dense<double>, double> &y) {
82  return get_residual_l2_core(A, x, y);
83 }
85  const view1D<vector<double>, double> &x,
86  const vector<double> &y) {
87  return get_residual_l2_core(A, x, y);
88 }
90  const view1D<vector<double>, double> &x,
91  const view1D<vector<double>, double> &y) {
92  return get_residual_l2_core(A, x, y);
93 }
95  const view1D<vector<double>, double> &x,
96  const view1D<matrix::Dense<double>, double> &y) {
97  return get_residual_l2_core(A, x, y);
98 }
100  const view1D<matrix::Dense<double>, double> &x,
101  const vector<double> &y) {
102  return get_residual_l2_core(A, x, y);
103 }
105  const view1D<matrix::Dense<double>, double> &x,
106  const view1D<vector<double>, double> &y) {
107  return get_residual_l2_core(A, x, y);
108 }
110  const view1D<matrix::Dense<double>, double> &x,
111  const view1D<matrix::Dense<double>, double> &y) {
112  return get_residual_l2_core(A, x, y);
113 }
115  const vector<float> &y) {
116  return get_residual_l2_core(A, x, y);
117 }
119  const view1D<vector<float>, float> &y) {
120  return get_residual_l2_core(A, x, y);
121 }
123  const view1D<matrix::Dense<float>, float> &y) {
124  return get_residual_l2_core(A, x, y);
125 }
127  const view1D<vector<float>, float> &x,
128  const vector<float> &y) {
129  return get_residual_l2_core(A, x, y);
130 }
132  const view1D<vector<float>, float> &x,
133  const view1D<vector<float>, float> &y) {
134  return get_residual_l2_core(A, x, y);
135 }
137  const view1D<vector<float>, float> &x,
138  const view1D<matrix::Dense<float>, float> &y) {
139  return get_residual_l2_core(A, x, y);
140 }
142  const view1D<matrix::Dense<float>, float> &x,
143  const vector<float> &y) {
144  return get_residual_l2_core(A, x, y);
145 }
147  const view1D<matrix::Dense<float>, float> &x,
148  const view1D<vector<float>, float> &y) {
149  return get_residual_l2_core(A, x, y);
150 }
152  const view1D<matrix::Dense<float>, float> &x,
153  const view1D<matrix::Dense<float>, float> &y) {
154  return get_residual_l2_core(A, x, y);
155 }
156 
158  const vector<double> &y) {
159  return get_residual_l2_core(A, x, y);
160 }
162  const view1D<vector<double>, double> &y) {
163  return get_residual_l2_core(A, x, y);
164 }
166  const view1D<matrix::Dense<double>, double> &y) {
167  return get_residual_l2_core(A, x, y);
168 }
170  const view1D<vector<double>, double> &x,
171  const vector<double> &y) {
172  return get_residual_l2_core(A, x, y);
173 }
175  const view1D<vector<double>, double> &x,
176  const view1D<vector<double>, double> &y) {
177  return get_residual_l2_core(A, x, y);
178 }
180  const view1D<vector<double>, double> &x,
181  const view1D<matrix::Dense<double>, double> &y) {
182  return get_residual_l2_core(A, x, y);
183 }
185  const view1D<matrix::Dense<double>, double> &x,
186  const vector<double> &y) {
187  return get_residual_l2_core(A, x, y);
188 }
190  const view1D<matrix::Dense<double>, double> &x,
191  const view1D<vector<double>, double> &y) {
192  return get_residual_l2_core(A, x, y);
193 }
195  const view1D<matrix::Dense<double>, double> &x,
196  const view1D<matrix::Dense<double>, double> &y) {
197  return get_residual_l2_core(A, x, y);
198 }
200  const vector<float> &y) {
201  return get_residual_l2_core(A, x, y);
202 }
204  const view1D<vector<float>, float> &y) {
205  return get_residual_l2_core(A, x, y);
206 }
208  const view1D<matrix::Dense<float>, float> &y) {
209  return get_residual_l2_core(A, x, y);
210 }
212  const view1D<vector<float>, float> &x,
213  const vector<float> &y) {
214  return get_residual_l2_core(A, x, y);
215 }
217  const view1D<vector<float>, float> &x,
218  const view1D<vector<float>, float> &y) {
219  return get_residual_l2_core(A, x, y);
220 }
222  const view1D<vector<float>, float> &x,
223  const view1D<matrix::Dense<float>, float> &y) {
224  return get_residual_l2_core(A, x, y);
225 }
227  const view1D<matrix::Dense<float>, float> &x,
228  const vector<float> &y) {
229  return get_residual_l2_core(A, x, y);
230 }
232  const view1D<matrix::Dense<float>, float> &x,
233  const view1D<vector<float>, float> &y) {
234  return get_residual_l2_core(A, x, y);
235 }
237  const view1D<matrix::Dense<float>, float> &x,
238  const view1D<matrix::Dense<float>, float> &y) {
239  return get_residual_l2_core(A, x, y);
240 }
241 
243  const vector<double> &x, const vector<double> &y) {
244  return get_residual_l2_core(A, x, y);
245 }
247  const vector<double> &x,
248  const view1D<vector<double>, double> &y) {
249  return get_residual_l2_core(A, x, y);
250 }
252  const vector<double> &x,
253  const view1D<matrix::Dense<double>, double> &y) {
254  return get_residual_l2_core(A, x, y);
255 }
257  const view1D<vector<double>, double> &x,
258  const vector<double> &y) {
259  return get_residual_l2_core(A, x, y);
260 }
262  const view1D<vector<double>, double> &x,
263  const view1D<vector<double>, double> &y) {
264  return get_residual_l2_core(A, x, y);
265 }
267  const view1D<vector<double>, double> &x,
268  const view1D<matrix::Dense<double>, double> &y) {
269  return get_residual_l2_core(A, x, y);
270 }
272  const view1D<matrix::Dense<double>, double> &x,
273  const vector<double> &y) {
274  return get_residual_l2_core(A, x, y);
275 }
277  const view1D<matrix::Dense<double>, double> &x,
278  const view1D<vector<double>, double> &y) {
279  return get_residual_l2_core(A, x, y);
280 }
282  const view1D<matrix::Dense<double>, double> &x,
283  const view1D<matrix::Dense<double>, double> &y) {
284  return get_residual_l2_core(A, x, y);
285 }
287  const vector<float> &x, const vector<float> &y) {
288  return get_residual_l2_core(A, x, y);
289 }
291  const vector<float> &x,
292  const view1D<vector<float>, float> &y) {
293  return get_residual_l2_core(A, x, y);
294 }
296  const vector<float> &x,
297  const view1D<matrix::Dense<float>, float> &y) {
298  return get_residual_l2_core(A, x, y);
299 }
301  const view1D<vector<float>, float> &x,
302  const vector<float> &y) {
303  return get_residual_l2_core(A, x, y);
304 }
306  const view1D<vector<float>, float> &x,
307  const view1D<vector<float>, float> &y) {
308  return get_residual_l2_core(A, x, y);
309 }
311  const view1D<vector<float>, float> &x,
312  const view1D<matrix::Dense<float>, float> &y) {
313  return get_residual_l2_core(A, x, y);
314 }
316  const view1D<matrix::Dense<float>, float> &x,
317  const vector<float> &y) {
318  return get_residual_l2_core(A, x, y);
319 }
321  const view1D<matrix::Dense<float>, float> &x,
322  const view1D<vector<float>, float> &y) {
323  return get_residual_l2_core(A, x, y);
324 }
326  const view1D<matrix::Dense<float>, float> &x,
327  const view1D<matrix::Dense<float>, float> &y) {
328  return get_residual_l2_core(A, x, y);
329 }
330 
331 } // namespace monolish
monolish::matrix::LinearOperator
Linear Operator imitating Matrix.
Definition: monolish_coo.hpp:30
monolish_func
#define monolish_func
Definition: monolish_logger.hpp:9
monolish::blas::nrm2
void nrm2(const vector< double > &x, double &ans)
nrm2: ||x||_2
Definition: vector_blas.cpp:1080
MONOLISH_SOLVER_NOT_IMPL
#define MONOLISH_SOLVER_NOT_IMPL
Definition: monolish_common.hpp:15
monolish::Logger
logger class (singleton, for developper class)
Definition: monolish_logger.hpp:19
monolish::vml::sub
void sub(const matrix::CRS< double > &A, const matrix::CRS< double > &B, matrix::CRS< double > &C)
element by element subtract CRS matrix A and CRS matrix B.
Definition: matrix_vml.cpp:228
monolish::matrix::Dense
Dense format Matrix.
Definition: monolish_coo.hpp:28
MONOLISH_SOLVER_SUCCESS
#define MONOLISH_SOLVER_SUCCESS
Definition: monolish_common.hpp:10
MONOLISH_SOLVER_BREAKDOWN
#define MONOLISH_SOLVER_BREAKDOWN
Definition: monolish_common.hpp:13
monolish::Logger::util_out
void util_out()
Definition: logger_utils.cpp:123
MONOLISH_SOLVER_MAXITER
#define MONOLISH_SOLVER_MAXITER
Definition: monolish_common.hpp:12
monolish::Logger::util_in
void util_in(const std::string func_name)
Definition: logger_utils.cpp:113
monolish::get_residual_l2
double get_residual_l2(const matrix::Dense< double > &A, const vector< double > &x, const vector< double > &y)
get nrm |b-Ax|_2
Definition: equation_utils.cpp:72
monolish
Definition: monolish_matrix_blas.hpp:9
MONOLISH_SOLVER_SIZE_ERROR
#define MONOLISH_SOLVER_SIZE_ERROR
Definition: monolish_common.hpp:11
monolish::blas::matvec
void matvec(const matrix::Dense< double > &A, const vector< double > &x, vector< double > &y)
Dense matrix and vector multiplication: y = Ax.
Definition: matvec_blas.cpp:11
monolish::view1D
1D view class
Definition: monolish_coo.hpp:26
monolish::util::solver_check
bool solver_check(const int err)
check error
Definition: equation_utils.cpp:7
monolish::vector
vector class
Definition: monolish_coo.hpp:25
MONOLISH_SOLVER_RESIDUAL_NAN
#define MONOLISH_SOLVER_RESIDUAL_NAN
Definition: monolish_common.hpp:14
monolish::Logger::get_instance
static Logger & get_instance()
Definition: monolish_logger.hpp:42
monolish::matrix::CRS
Compressed Row Storage (CRS) format Matrix.
Definition: monolish_coo.hpp:29