monolish  0.14.0
MONOlithic LIner equation Solvers for Highly-parallel architecture
monolish_common.hpp
Go to the documentation of this file.
1 #pragma once
2 #include "monolish_dense.hpp"
3 #include "monolish_logger.hpp"
4 #include "monolish_matrix.hpp"
5 #include "monolish_vector.hpp"
6 #include "monolish_view1D.hpp"
7 #include <initializer_list>
8 
9 // error code
10 #define MONOLISH_SOLVER_SUCCESS 0
11 #define MONOLISH_SOLVER_SIZE_ERROR -1
12 #define MONOLISH_SOLVER_MAXITER -2
13 #define MONOLISH_SOLVER_BREAKDOWN -3
14 #define MONOLISH_SOLVER_RESIDUAL_NAN -4
15 #define MONOLISH_SOLVER_NOT_IMPL -10
16 
17 namespace monolish {
21 namespace util {
22 
34 double get_residual_l2(const matrix::Dense<double> &A, const vector<double> &x,
35  const vector<double> &y);
36 double get_residual_l2(const matrix::Dense<double> &A, const vector<double> &x,
37  const view1D<vector<double>, double> &y);
38 double get_residual_l2(const matrix::Dense<double> &A, const vector<double> &x,
39  const view1D<matrix::Dense<double>, double> &y);
41  const view1D<vector<double>, double> &x,
42  const vector<double> &y);
44  const view1D<vector<double>, double> &x,
45  const view1D<vector<double>, double> &y);
47  const view1D<vector<double>, double> &x,
48  const view1D<matrix::Dense<double>, double> &y);
50  const view1D<matrix::Dense<double>, double> &x,
51  const vector<double> &y);
53  const view1D<matrix::Dense<double>, double> &x,
54  const view1D<vector<double>, double> &y);
56  const view1D<matrix::Dense<double>, double> &x,
57  const view1D<matrix::Dense<double>, double> &y);
58 float get_residual_l2(const matrix::Dense<float> &A, const vector<float> &x,
59  const vector<float> &y);
60 float get_residual_l2(const matrix::Dense<float> &A, const vector<float> &x,
61  const view1D<vector<float>, float> &y);
62 float get_residual_l2(const matrix::Dense<float> &A, const vector<float> &x,
63  const view1D<matrix::Dense<float>, float> &y);
65  const view1D<vector<float>, float> &x,
66  const vector<float> &y);
68  const view1D<vector<float>, float> &x,
69  const view1D<vector<float>, float> &y);
71  const view1D<vector<float>, float> &x,
72  const view1D<matrix::Dense<float>, float> &y);
74  const view1D<matrix::Dense<float>, float> &x,
75  const vector<float> &y);
77  const view1D<matrix::Dense<float>, float> &x,
78  const view1D<vector<float>, float> &y);
80  const view1D<matrix::Dense<float>, float> &x,
81  const view1D<matrix::Dense<float>, float> &y);
82 
94 double get_residual_l2(const matrix::CRS<double> &A, const vector<double> &x,
95  const vector<double> &y);
96 double get_residual_l2(const matrix::CRS<double> &A, const vector<double> &x,
97  const view1D<vector<double>, double> &y);
98 double get_residual_l2(const matrix::CRS<double> &A, const vector<double> &x,
99  const view1D<matrix::Dense<double>, double> &y);
100 double get_residual_l2(const matrix::CRS<double> &A,
101  const view1D<vector<double>, double> &x,
102  const vector<double> &y);
103 double get_residual_l2(const matrix::CRS<double> &A,
104  const view1D<vector<double>, double> &x,
105  const view1D<vector<double>, double> &y);
106 double get_residual_l2(const matrix::CRS<double> &A,
107  const view1D<vector<double>, double> &x,
108  const view1D<matrix::Dense<double>, double> &y);
109 double get_residual_l2(const matrix::CRS<double> &A,
110  const view1D<matrix::Dense<double>, double> &x,
111  const vector<double> &y);
112 double get_residual_l2(const matrix::CRS<double> &A,
113  const view1D<matrix::Dense<double>, double> &x,
114  const view1D<vector<double>, double> &y);
115 double get_residual_l2(const matrix::CRS<double> &A,
116  const view1D<matrix::Dense<double>, double> &x,
117  const view1D<matrix::Dense<double>, double> &y);
118 float get_residual_l2(const matrix::CRS<float> &A, const vector<float> &x,
119  const vector<float> &y);
120 float get_residual_l2(const matrix::CRS<float> &A, const vector<float> &x,
121  const view1D<vector<float>, float> &y);
122 float get_residual_l2(const matrix::CRS<float> &A, const vector<float> &x,
123  const view1D<matrix::Dense<float>, float> &y);
124 float get_residual_l2(const matrix::CRS<float> &A,
125  const view1D<vector<float>, float> &x,
126  const vector<float> &y);
127 float get_residual_l2(const matrix::CRS<float> &A,
128  const view1D<vector<float>, float> &x,
129  const view1D<vector<float>, float> &y);
130 float get_residual_l2(const matrix::CRS<float> &A,
131  const view1D<vector<float>, float> &x,
132  const view1D<matrix::Dense<float>, float> &y);
133 float get_residual_l2(const matrix::CRS<float> &A,
134  const view1D<matrix::Dense<float>, float> &x,
135  const vector<float> &y);
136 float get_residual_l2(const matrix::CRS<float> &A,
137  const view1D<matrix::Dense<float>, float> &x,
138  const view1D<vector<float>, float> &y);
139 float get_residual_l2(const matrix::CRS<float> &A,
140  const view1D<matrix::Dense<float>, float> &x,
141  const view1D<matrix::Dense<float>, float> &y);
142 
155  const vector<double> &x, const vector<double> &y);
157  const vector<double> &x,
158  const view1D<vector<double>, double> &y);
160  const vector<double> &x,
161  const view1D<matrix::Dense<double>, double> &y);
163  const view1D<vector<double>, double> &x,
164  const vector<double> &y);
166  const view1D<vector<double>, double> &x,
167  const view1D<vector<double>, double> &y);
169  const view1D<vector<double>, double> &x,
170  const view1D<matrix::Dense<double>, double> &y);
172  const view1D<matrix::Dense<double>, double> &x,
173  const vector<double> &y);
175  const view1D<matrix::Dense<double>, double> &x,
176  const view1D<vector<double>, double> &y);
178  const view1D<matrix::Dense<double>, double> &x,
179  const view1D<matrix::Dense<double>, double> &y);
181  const vector<float> &x, const vector<float> &y);
183  const vector<float> &x,
184  const view1D<vector<float>, float> &y);
186  const vector<float> &x,
187  const view1D<matrix::Dense<float>, float> &y);
189  const view1D<vector<float>, float> &x,
190  const vector<float> &y);
192  const view1D<vector<float>, float> &x,
193  const view1D<vector<float>, float> &y);
195  const view1D<vector<float>, float> &x,
196  const view1D<matrix::Dense<float>, float> &y);
198  const view1D<matrix::Dense<float>, float> &x,
199  const vector<float> &y);
201  const view1D<matrix::Dense<float>, float> &x,
202  const view1D<vector<float>, float> &y);
204  const view1D<matrix::Dense<float>, float> &x,
205  const view1D<matrix::Dense<float>, float> &y);
217 bool solver_check(const int err);
218 
220 
230 void set_log_level(const size_t Level);
231 
236 void set_log_filename(const std::string filename);
237 
238 // create typical data///////////////////////////
239 
251 template <typename T>
252 void random_vector(vector<T> &vec, const T min, const T max) {
253  // rand (0~1)
254  std::random_device random;
255  std::mt19937 mt(random());
256  std::uniform_real_distribution<> rand(min, max);
257 
258  for (size_t i = 0; i < vec.size(); i++) {
259  vec[i] = rand(mt);
260  }
261 }
262 
263 // is_same //////////////////
264 
268 template <typename T, typename U> bool is_same_structure(const T A, const U B) {
269  return false;
270 }
271 
282 template <typename T>
283 bool is_same_structure(const vector<T> &x, const vector<T> &y) {
284  return x.size() == y.size();
285 }
286 
297 template <typename T>
298 bool is_same_structure(const matrix::Dense<T> &A, const matrix::Dense<T> &B);
299 
310 template <typename T>
311 bool is_same_structure(const matrix::COO<T> &A, const matrix::COO<T> &B);
312 
323 template <typename T>
324 bool is_same_structure(const matrix::CRS<T> &A, const matrix::CRS<T> &B);
325 
336 template <typename T>
338  const matrix::LinearOperator<T> &B);
339 
343 template <typename T, typename... types>
344 bool is_same_structure(const T &A, const T &B, const types &... args) {
345  return is_same_structure(A, B) && is_same_structure(A, args...);
346 }
347 
358 template <typename T, typename U> bool is_same_size(const T &x, const U &y) {
359  return x.size() == y.size();
360 }
361 
372 template <typename T>
373 bool is_same_size(const matrix::Dense<T> &A, const matrix::Dense<T> &B);
374 
385 template <typename T>
386 bool is_same_size(const matrix::COO<T> &A, const matrix::COO<T> &B);
387 
398 template <typename T>
399 bool is_same_size(const matrix::CRS<T> &A, const matrix::CRS<T> &B);
400 
411 template <typename T>
413  const matrix::LinearOperator<T> &B);
414 
418 template <typename T, typename U, typename... types>
419 bool is_same_size(const T &arg1, const U &arg2, const types &... args) {
420  return is_same_size(arg1, arg2) && is_same_size(arg1, args...);
421 }
422 
430 template <typename T, typename U>
431 bool is_same_device_mem_stat(const T &arg1, const U &arg2) {
432  return arg1.get_device_mem_stat() == arg2.get_device_mem_stat();
433 }
434 
442 template <typename T, typename U, typename... types>
443 bool is_same_device_mem_stat(const T &arg1, const U &arg2,
444  const types &... args) {
445  return is_same_device_mem_stat(arg1, arg2) &&
446  is_same_device_mem_stat(arg1, args...);
447 }
448 
449 // create matrix //////////////////
450 
463 template <typename T>
464 matrix::COO<T> band_matrix(const int M, const int N, const int W,
465  const T diag_val, const T val);
466 
478 template <typename T>
479 matrix::COO<T> random_structure_matrix(const int M, const int N,
480  const int nnzrow, const T val);
481 
490 template <typename T> matrix::COO<T> eye(const int M);
491 
500 template <typename T> matrix::COO<T> frank_matrix(const int &M);
501 
511 template <typename T> T frank_matrix_eigenvalue(const int &M, const int &N);
512 
523 template <typename T>
524 matrix::COO<T> tridiagonal_toeplitz_matrix(const int &M, T a, T b);
525 
537 template <typename T>
538 T tridiagonal_toeplitz_matrix_eigenvalue(const int &M, int N, T a, T b);
539 
548 template <typename T> matrix::COO<T> laplacian_matrix_1D(const int &M);
549 
559 template <typename T> T laplacian_matrix_1D_eigenvalue(const int &M, int N);
560 
574 template <typename T>
575 matrix::COO<T> toeplitz_plus_hankel_matrix(const int &M, T a0, T a1, T a2);
576 
589 template <typename T>
590 T toeplitz_plus_hankel_matrix_eigenvalue(const int &M, int N, T a0, T a1, T a2,
591  T b0, T b1, T b2);
592 
593 // send///////////////////
594 
598 template <typename T> auto send(T &x) { x.send(); }
599 
603 template <typename T, typename... Types> auto send(T &x, Types &... args) {
604  x.send();
605  send(args...);
606 }
607 
608 // recv///////////////////
612 template <typename T> auto recv(T &x) { x.recv(); }
613 
617 template <typename T, typename... Types> auto recv(T &x, Types &... args) {
618  x.recv();
619  recv(args...);
620 }
621 
622 // device_free///////////////////
623 
627 template <typename T> auto device_free(T &x) { x.device_free(); }
628 
632 template <typename T, typename... Types>
633 auto device_free(T &x, Types &... args) {
634  x.device_free();
635  device_free(args...);
636 }
637 
641 bool build_with_avx();
642 
646 bool build_with_avx2();
647 
651 bool build_with_avx512();
652 
656 bool build_with_gpu();
657 
661 bool build_with_mkl();
662 
667 bool build_with_lapack();
668 
673 bool build_with_cblas();
674 
675 } // namespace util
676 } // namespace monolish
monolish::util::is_same_size
bool is_same_size(const T &x, const U &y)
compare size of vector or 1Dview (same as is_same_structure())
Definition: monolish_common.hpp:358
monolish::util::build_with_avx
bool build_with_avx()
get build option (true: with avx, false: without avx)
Definition: system_util.cpp:6
monolish::matrix::LinearOperator
Linear Operator imitating Matrix.
Definition: monolish_coo.hpp:30
monolish::vml::min
void min(const matrix::CRS< double > &A, const matrix::CRS< double > &B, matrix::CRS< double > &C)
Create a new CRS matrix with smallest elements of two matrices (C[0:nnz] = min(A[0:nnz],...
Definition: matrix_vml.cpp:390
monolish::util::build_with_gpu
bool build_with_gpu()
get build option (true: enable gpu, false: disable gpu)
Definition: system_util.cpp:30
monolish::util::tridiagonal_toeplitz_matrix
matrix::COO< T > tridiagonal_toeplitz_matrix(const int &M, T a, T b)
create tridiagonal Toeplitz matrix
Definition: tridiagonal_toeplitz.cpp:7
monolish::util::random_structure_matrix
matrix::COO< T > random_structure_matrix(const int M, const int N, const int nnzrow, const T val)
create random structure matrix (column number is decided by random)
Definition: random_stracture.cpp:8
monolish::util::build_with_cblas
bool build_with_cblas()
get build option (true: with cblas, false: without cblas (=with intel mkl))
Definition: system_util.cpp:54
monolish::util::frank_matrix
matrix::COO< T > frank_matrix(const int &M)
create Frank matrix
Definition: frank.cpp:6
monolish::util::is_same_structure
bool is_same_structure(const T A, const U B)
compare matrix structure
Definition: monolish_common.hpp:268
monolish::vml::max
void max(const matrix::CRS< double > &A, const matrix::CRS< double > &B, matrix::CRS< double > &C)
Create a new CRS matrix with greatest elements of two matrices (C[0:nnz] = max(A[0:nnz],...
Definition: matrix_vml.cpp:382
monolish::util::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::util::frank_matrix_eigenvalue
T frank_matrix_eigenvalue(const int &M, const int &N)
Nth eigenvalue from the bottom of MxM Frank matrix.
Definition: frank.cpp:26
monolish_matrix.hpp
declare matrix class
monolish::matrix::Dense
Dense format Matrix.
Definition: monolish_coo.hpp:28
monolish::util::tridiagonal_toeplitz_matrix_eigenvalue
T tridiagonal_toeplitz_matrix_eigenvalue(const int &M, int N, T a, T b)
Nth smallest eigenvalue of MxM tridiagonal Toeplitz matrix.
Definition: tridiagonal_toeplitz.cpp:31
monolish::util::device_free
auto device_free(T &x)
free data of GPU
Definition: monolish_common.hpp:627
monolish::vector::size
auto size() const
get vector size
Definition: monolish_vector.hpp:306
monolish::util::build_with_lapack
bool build_with_lapack()
get build option (true: with lapack, false: without lapack (=with intel mkl))
Definition: system_util.cpp:46
monolish::util::is_same_device_mem_stat
bool is_same_device_mem_stat(const T &arg1, const U &arg2)
compare same device memory status
Definition: monolish_common.hpp:431
monolish_view1D.hpp
monolish::util::band_matrix
matrix::COO< T > band_matrix(const int M, const int N, const int W, const T diag_val, const T val)
create band matrix
Definition: band_matrix.cpp:7
monolish::util::eye
matrix::COO< T > eye(const int M)
create band matrix
Definition: eye.cpp:6
monolish::util::toeplitz_plus_hankel_matrix_eigenvalue
T toeplitz_plus_hankel_matrix_eigenvalue(const int &M, int N, T a0, T a1, T a2, T b0, T b1, T b2)
Nth smallest eigenvalue of GEVP Ax=lBx of Toeplitz-plus-Hankel matrixes A, B.
Definition: toeplitz_plus_hankel.cpp:46
monolish::util::toeplitz_plus_hankel_matrix
matrix::COO< T > toeplitz_plus_hankel_matrix(const int &M, T a0, T a1, T a2)
create Toeplitz-plus-Hankel matrix
Definition: toeplitz_plus_hankel.cpp:7
monolish
Definition: monolish_matrix_blas.hpp:9
monolish_vector.hpp
monolish_logger.hpp
monolish::matrix::COO
Coodinate (COO) format Matrix (need to sort)
Definition: monolish_coo.hpp:38
monolish_dense.hpp
monolish::util::set_log_filename
void set_log_filename(const std::string filename)
Specifying the log finename.
Definition: logger_utils.cpp:161
monolish::util::recv
auto recv(T &x)
recv. and free data from GPU
Definition: monolish_common.hpp:612
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::util::random_vector
void random_vector(vector< T > &vec, const T min, const T max)
create random vector
Definition: monolish_common.hpp:252
monolish::util::build_with_avx2
bool build_with_avx2()
get build option (true: with avx2, false: without avx2)
Definition: system_util.cpp:14
monolish::util::set_log_level
void set_log_level(const size_t Level)
Logger utils ///////////////////////////////.
Definition: logger_utils.cpp:156
monolish::vector
vector class
Definition: monolish_coo.hpp:25
monolish::util::laplacian_matrix_1D_eigenvalue
T laplacian_matrix_1D_eigenvalue(const int &M, int N)
Nth smallest eigenvalue of 1D Laplacian matrix.
Definition: laplacian.cpp:13
monolish::util::send
auto send(T &x)
send data to GPU
Definition: monolish_common.hpp:598
monolish::util::laplacian_matrix_1D
matrix::COO< T > laplacian_matrix_1D(const int &M)
create 1D Laplacian matrix
Definition: laplacian.cpp:6
monolish::matrix::CRS
Compressed Row Storage (CRS) format Matrix.
Definition: monolish_coo.hpp:29
monolish::util::build_with_avx512
bool build_with_avx512()
get build option (true: with avx512, false: without avx512)
Definition: system_util.cpp:22
monolish::util::build_with_mkl
bool build_with_mkl()
get build option (true: with intel mkl, false: without intel mkl)
Definition: system_util.cpp:38