monolish  0.14.1
MONOlithic LIner equation Solvers for Highly-parallel architecture
monolish_common.hpp
Go to the documentation of this file.
1 #pragma once
2 #if USE_SXAT
3 #undef _HAS_CPP17
4 #endif
5 #include "monolish_dense.hpp"
6 #include "monolish_logger.hpp"
7 #include "monolish_matrix.hpp"
8 #include "monolish_vector.hpp"
9 #include "monolish_view1D.hpp"
10 #include <initializer_list>
11 
12 // error code
13 #define MONOLISH_SOLVER_SUCCESS 0
14 #define MONOLISH_SOLVER_SIZE_ERROR -1
15 #define MONOLISH_SOLVER_MAXITER -2
16 #define MONOLISH_SOLVER_BREAKDOWN -3
17 #define MONOLISH_SOLVER_RESIDUAL_NAN -4
18 #define MONOLISH_SOLVER_NOT_IMPL -10
19 
23 namespace monolish::util {
24 
30 int get_num_devices();
31 
36 bool set_default_device(size_t device_num);
37 
43 int get_default_device();
44 
56 double get_residual_l2(const matrix::Dense<double> &A, const vector<double> &x,
57  const vector<double> &y);
58 double get_residual_l2(const matrix::Dense<double> &A, const vector<double> &x,
59  const view1D<vector<double>, double> &y);
60 double get_residual_l2(const matrix::Dense<double> &A, const vector<double> &x,
61  const view1D<matrix::Dense<double>, double> &y);
63  const view1D<vector<double>, double> &x,
64  const vector<double> &y);
66  const view1D<vector<double>, double> &x,
67  const view1D<vector<double>, double> &y);
69  const view1D<vector<double>, double> &x,
70  const view1D<matrix::Dense<double>, double> &y);
72  const view1D<matrix::Dense<double>, double> &x,
73  const vector<double> &y);
75  const view1D<matrix::Dense<double>, double> &x,
76  const view1D<vector<double>, double> &y);
78  const view1D<matrix::Dense<double>, double> &x,
79  const view1D<matrix::Dense<double>, double> &y);
80 float get_residual_l2(const matrix::Dense<float> &A, const vector<float> &x,
81  const vector<float> &y);
82 float get_residual_l2(const matrix::Dense<float> &A, const vector<float> &x,
83  const view1D<vector<float>, float> &y);
84 float get_residual_l2(const matrix::Dense<float> &A, const vector<float> &x,
85  const view1D<matrix::Dense<float>, float> &y);
87  const view1D<vector<float>, float> &x,
88  const vector<float> &y);
90  const view1D<vector<float>, float> &x,
91  const view1D<vector<float>, float> &y);
93  const view1D<vector<float>, float> &x,
94  const view1D<matrix::Dense<float>, float> &y);
96  const view1D<matrix::Dense<float>, float> &x,
97  const vector<float> &y);
99  const view1D<matrix::Dense<float>, float> &x,
100  const view1D<vector<float>, float> &y);
102  const view1D<matrix::Dense<float>, float> &x,
103  const view1D<matrix::Dense<float>, float> &y);
104 
116 double get_residual_l2(const matrix::CRS<double> &A, const vector<double> &x,
117  const vector<double> &y);
118 double get_residual_l2(const matrix::CRS<double> &A, const vector<double> &x,
119  const view1D<vector<double>, double> &y);
120 double get_residual_l2(const matrix::CRS<double> &A, const vector<double> &x,
121  const view1D<matrix::Dense<double>, double> &y);
122 double get_residual_l2(const matrix::CRS<double> &A,
123  const view1D<vector<double>, double> &x,
124  const vector<double> &y);
125 double get_residual_l2(const matrix::CRS<double> &A,
126  const view1D<vector<double>, double> &x,
127  const view1D<vector<double>, double> &y);
128 double get_residual_l2(const matrix::CRS<double> &A,
129  const view1D<vector<double>, double> &x,
130  const view1D<matrix::Dense<double>, double> &y);
131 double get_residual_l2(const matrix::CRS<double> &A,
132  const view1D<matrix::Dense<double>, double> &x,
133  const vector<double> &y);
134 double get_residual_l2(const matrix::CRS<double> &A,
135  const view1D<matrix::Dense<double>, double> &x,
136  const view1D<vector<double>, double> &y);
137 double get_residual_l2(const matrix::CRS<double> &A,
138  const view1D<matrix::Dense<double>, double> &x,
139  const view1D<matrix::Dense<double>, double> &y);
140 float get_residual_l2(const matrix::CRS<float> &A, const vector<float> &x,
141  const vector<float> &y);
142 float get_residual_l2(const matrix::CRS<float> &A, const vector<float> &x,
143  const view1D<vector<float>, float> &y);
144 float get_residual_l2(const matrix::CRS<float> &A, const vector<float> &x,
145  const view1D<matrix::Dense<float>, float> &y);
146 float get_residual_l2(const matrix::CRS<float> &A,
147  const view1D<vector<float>, float> &x,
148  const vector<float> &y);
149 float get_residual_l2(const matrix::CRS<float> &A,
150  const view1D<vector<float>, float> &x,
151  const view1D<vector<float>, float> &y);
152 float get_residual_l2(const matrix::CRS<float> &A,
153  const view1D<vector<float>, float> &x,
154  const view1D<matrix::Dense<float>, float> &y);
155 float get_residual_l2(const matrix::CRS<float> &A,
156  const view1D<matrix::Dense<float>, float> &x,
157  const vector<float> &y);
158 float get_residual_l2(const matrix::CRS<float> &A,
159  const view1D<matrix::Dense<float>, float> &x,
160  const view1D<vector<float>, float> &y);
161 float get_residual_l2(const matrix::CRS<float> &A,
162  const view1D<matrix::Dense<float>, float> &x,
163  const view1D<matrix::Dense<float>, float> &y);
164 
177  const vector<double> &x, const vector<double> &y);
179  const vector<double> &x,
180  const view1D<vector<double>, double> &y);
182  const vector<double> &x,
183  const view1D<matrix::Dense<double>, double> &y);
185  const view1D<vector<double>, double> &x,
186  const vector<double> &y);
188  const view1D<vector<double>, double> &x,
189  const view1D<vector<double>, double> &y);
191  const view1D<vector<double>, double> &x,
192  const view1D<matrix::Dense<double>, double> &y);
194  const view1D<matrix::Dense<double>, double> &x,
195  const vector<double> &y);
197  const view1D<matrix::Dense<double>, double> &x,
198  const view1D<vector<double>, double> &y);
200  const view1D<matrix::Dense<double>, double> &x,
201  const view1D<matrix::Dense<double>, double> &y);
203  const vector<float> &x, const vector<float> &y);
205  const vector<float> &x,
206  const view1D<vector<float>, float> &y);
208  const vector<float> &x,
209  const view1D<matrix::Dense<float>, float> &y);
211  const view1D<vector<float>, float> &x,
212  const vector<float> &y);
214  const view1D<vector<float>, float> &x,
215  const view1D<vector<float>, float> &y);
217  const view1D<vector<float>, float> &x,
218  const view1D<matrix::Dense<float>, float> &y);
220  const view1D<matrix::Dense<float>, float> &x,
221  const vector<float> &y);
223  const view1D<matrix::Dense<float>, float> &x,
224  const view1D<vector<float>, float> &y);
226  const view1D<matrix::Dense<float>, float> &x,
227  const view1D<matrix::Dense<float>, float> &y);
239 [[nodiscard]] bool solver_check(const int err);
240 
242 
252 void set_log_level(const size_t Level);
253 
258 void set_log_filename(const std::string filename);
259 
260 // create typical data///////////////////////////
261 
273 template <typename T>
274 void random_vector(vector<T> &vec, const T min, const T max) {
275  // rand (0~1)
276  std::random_device random;
277  std::mt19937 mt(random());
278  std::uniform_real_distribution<> rand(min, max);
279 
280  for (size_t i = 0; i < vec.size(); i++) {
281  vec[i] = rand(mt);
282  }
283 }
284 
285 // is_same //////////////////
286 
290 template <typename T, typename U>
291 [[nodiscard]] bool is_same_structure(const T A, const U B) {
292  return false;
293 }
294 
305 template <typename T>
306 [[nodiscard]] bool is_same_structure(const vector<T> &x, const vector<T> &y) {
307  return x.size() == y.size();
308 }
309 
320 template <typename T>
321 [[nodiscard]] bool is_same_structure(const matrix::Dense<T> &A,
322  const matrix::Dense<T> &B);
323 
334 template <typename T>
335 [[nodiscard]] bool is_same_structure(const matrix::COO<T> &A,
336  const matrix::COO<T> &B);
337 
348 template <typename T>
349 [[nodiscard]] bool is_same_structure(const matrix::CRS<T> &A,
350  const matrix::CRS<T> &B);
351 
362 template <typename T>
364  const matrix::LinearOperator<T> &B);
365 
369 template <typename T, typename... types>
370 [[nodiscard]] bool is_same_structure(const T &A, const T &B,
371  const types &... args) {
372  return is_same_structure(A, B) && is_same_structure(A, args...);
373 }
374 
385 template <typename T, typename U>
386 [[nodiscard]] bool is_same_size(const T &x, const U &y) {
387  return x.size() == y.size();
388 }
389 
400 template <typename T>
401 [[nodiscard]] bool is_same_size(const matrix::Dense<T> &A,
402  const matrix::Dense<T> &B);
403 
414 template <typename T>
415 [[nodiscard]] bool is_same_size(const matrix::COO<T> &A,
416  const matrix::COO<T> &B);
417 
428 template <typename T>
429 [[nodiscard]] bool is_same_size(const matrix::CRS<T> &A,
430  const matrix::CRS<T> &B);
431 
442 template <typename T>
443 [[nodiscard]] bool is_same_size(const matrix::LinearOperator<T> &A,
444  const matrix::LinearOperator<T> &B);
445 
449 template <typename T, typename U, typename... types>
450 [[nodiscard]] bool is_same_size(const T &arg1, const U &arg2,
451  const types &... args) {
452  return is_same_size(arg1, arg2) && is_same_size(arg1, args...);
453 }
454 
462 template <typename T, typename U>
463 [[nodiscard]] bool is_same_device_mem_stat(const T &arg1, const U &arg2) {
464  return arg1.get_device_mem_stat() == arg2.get_device_mem_stat();
465 }
466 
474 template <typename T, typename U, typename... types>
475 [[nodiscard]] bool is_same_device_mem_stat(const T &arg1, const U &arg2,
476  const types &... args) {
477  return is_same_device_mem_stat(arg1, arg2) &&
478  is_same_device_mem_stat(arg1, args...);
479 }
480 
481 // create matrix //////////////////
482 
495 template <typename T>
496 [[nodiscard]] matrix::COO<T> band_matrix(const int M, const int N, const int W,
497  const T diag_val, const T val);
498 
510 template <typename T>
511 [[nodiscard]] matrix::COO<T> random_structure_matrix(const int M, const int N,
512  const int nnzrow,
513  const T val);
514 
523 template <typename T>[[nodiscard]] matrix::COO<T> eye(const int M);
524 
533 template <typename T>[[nodiscard]] matrix::COO<T> frank_matrix(const int &M);
534 
544 template <typename T>
545 [[nodiscard]] T frank_matrix_eigenvalue(const int &M, const int &N);
546 
557 template <typename T>
558 [[nodiscard]] matrix::COO<T> tridiagonal_toeplitz_matrix(const int &M, T a,
559  T b);
560 
572 template <typename T>
573 [[nodiscard]] T tridiagonal_toeplitz_matrix_eigenvalue(const int &M, int N, T a,
574  T b);
575 
584 template <typename T>
585 [[nodiscard]] matrix::COO<T> laplacian_matrix_1D(const int &M);
586 
596 template <typename T>
597 [[nodiscard]] T laplacian_matrix_1D_eigenvalue(const int &M, int N);
598 
609 template <typename T>
610 [[nodiscard]] matrix::COO<T> laplacian_matrix_2D_5p(const int M, const int N);
611 
625 template <typename T>
626 [[nodiscard]] matrix::COO<T> toeplitz_plus_hankel_matrix(const int &M, T a0,
627  T a1, T a2);
628 
641 template <typename T>
642 [[nodiscard]] T toeplitz_plus_hankel_matrix_eigenvalue(const int &M, int N,
643  T a0, T a1, T a2, T b0,
644  T b1, T b2);
645 
646 // send///////////////////
647 
651 template <typename T> auto send(T &x) { x.send(); }
652 
656 template <typename T, typename... Types> auto send(T &x, Types &... args) {
657  x.send();
658  send(args...);
659 }
660 
661 // recv///////////////////
665 template <typename T> auto recv(T &x) { x.recv(); }
666 
670 template <typename T, typename... Types> auto recv(T &x, Types &... args) {
671  x.recv();
672  recv(args...);
673 }
674 
675 // device_free///////////////////
676 
680 template <typename T> auto device_free(T &x) { x.device_free(); }
681 
685 template <typename T, typename... Types>
686 auto device_free(T &x, Types &... args) {
687  x.device_free();
688  device_free(args...);
689 }
690 
694 [[nodiscard]] bool build_with_avx();
695 
699 [[nodiscard]] bool build_with_avx2();
700 
704 [[nodiscard]] bool build_with_avx512();
705 
709 [[nodiscard]] bool build_with_mpi();
710 
714 [[nodiscard]] bool build_with_gpu();
715 
719 [[nodiscard]] bool build_with_mkl();
720 
725 [[nodiscard]] bool build_with_lapack();
726 
731 [[nodiscard]] bool build_with_cblas();
732 
733 } // namespace monolish::util
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:386
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:37
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:38
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:62
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:291
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:35
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:680
monolish::vector::size
auto size() const
get vector size
Definition: monolish_vector.hpp:312
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:54
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:463
monolish_view1D.hpp
monolish::util
monolish utilities
Definition: monolish_common.hpp:23
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::set_default_device
bool set_default_device(size_t device_num)
set default device number
Definition: gpu_deivce.cpp:16
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_vector.hpp
monolish_logger.hpp
monolish::matrix::COO
Coodinate (COO) format Matrix (need to sort)
Definition: monolish_coo.hpp:45
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:665
monolish::view1D
1D view class
Definition: monolish_coo.hpp:33
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:274
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::util::get_default_device
int get_default_device()
get default device number
Definition: gpu_deivce.cpp:26
monolish::vector
vector class
Definition: monolish_coo.hpp:32
monolish::util::build_with_mpi
bool build_with_mpi()
get build option (true: enable MPI, false: disable MPI)
Definition: system_util.cpp:30
monolish::util::get_num_devices
int get_num_devices()
get the number of devices
Definition: gpu_deivce.cpp:6
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:651
monolish::util::laplacian_matrix_2D_5p
matrix::COO< T > laplacian_matrix_2D_5p(const int M, const int N)
create two dimensional Laplacian matrix using the five point central difference scheme
Definition: laplacian_2D5p.cpp:7
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:36
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:46