monolish  0.15.3
MONOlithic LInear 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 {
35 int get_num_devices();
36 
42 bool set_default_device(size_t device_num);
43 
50 int get_default_device();
51 
68 double get_residual_l2(const matrix::Dense<double> &A, const vector<double> &x,
69  const vector<double> &y);
70 double get_residual_l2(const matrix::Dense<double> &A, const vector<double> &x,
71  const view1D<vector<double>, double> &y);
72 double get_residual_l2(const matrix::Dense<double> &A, const vector<double> &x,
73  const view1D<matrix::Dense<double>, double> &y);
75  const view1D<vector<double>, double> &x,
76  const vector<double> &y);
78  const view1D<vector<double>, double> &x,
79  const view1D<vector<double>, double> &y);
81  const view1D<vector<double>, double> &x,
82  const view1D<matrix::Dense<double>, double> &y);
84  const view1D<matrix::Dense<double>, double> &x,
85  const vector<double> &y);
87  const view1D<matrix::Dense<double>, double> &x,
88  const view1D<vector<double>, double> &y);
90  const view1D<matrix::Dense<double>, double> &x,
91  const view1D<matrix::Dense<double>, double> &y);
92 float get_residual_l2(const matrix::Dense<float> &A, const vector<float> &x,
93  const vector<float> &y);
94 float get_residual_l2(const matrix::Dense<float> &A, const vector<float> &x,
95  const view1D<vector<float>, float> &y);
96 float get_residual_l2(const matrix::Dense<float> &A, const vector<float> &x,
97  const view1D<matrix::Dense<float>, float> &y);
99  const view1D<vector<float>, float> &x,
100  const vector<float> &y);
102  const view1D<vector<float>, float> &x,
103  const view1D<vector<float>, float> &y);
105  const view1D<vector<float>, float> &x,
106  const view1D<matrix::Dense<float>, float> &y);
108  const view1D<matrix::Dense<float>, float> &x,
109  const vector<float> &y);
111  const view1D<matrix::Dense<float>, float> &x,
112  const view1D<vector<float>, float> &y);
114  const view1D<matrix::Dense<float>, float> &x,
115  const view1D<matrix::Dense<float>, float> &y);
116 
128 double get_residual_l2(const matrix::CRS<double> &A, const vector<double> &x,
129  const vector<double> &y);
130 double get_residual_l2(const matrix::CRS<double> &A, const vector<double> &x,
131  const view1D<vector<double>, double> &y);
132 double get_residual_l2(const matrix::CRS<double> &A, const vector<double> &x,
133  const view1D<matrix::Dense<double>, double> &y);
134 double get_residual_l2(const matrix::CRS<double> &A,
135  const view1D<vector<double>, double> &x,
136  const vector<double> &y);
137 double get_residual_l2(const matrix::CRS<double> &A,
138  const view1D<vector<double>, double> &x,
139  const view1D<vector<double>, double> &y);
140 double get_residual_l2(const matrix::CRS<double> &A,
141  const view1D<vector<double>, double> &x,
142  const view1D<matrix::Dense<double>, double> &y);
143 double get_residual_l2(const matrix::CRS<double> &A,
144  const view1D<matrix::Dense<double>, double> &x,
145  const vector<double> &y);
146 double get_residual_l2(const matrix::CRS<double> &A,
147  const view1D<matrix::Dense<double>, double> &x,
148  const view1D<vector<double>, double> &y);
149 double get_residual_l2(const matrix::CRS<double> &A,
150  const view1D<matrix::Dense<double>, double> &x,
151  const view1D<matrix::Dense<double>, double> &y);
152 float get_residual_l2(const matrix::CRS<float> &A, const vector<float> &x,
153  const vector<float> &y);
154 float get_residual_l2(const matrix::CRS<float> &A, const vector<float> &x,
155  const view1D<vector<float>, float> &y);
156 float get_residual_l2(const matrix::CRS<float> &A, const vector<float> &x,
157  const view1D<matrix::Dense<float>, float> &y);
158 float get_residual_l2(const matrix::CRS<float> &A,
159  const view1D<vector<float>, float> &x,
160  const vector<float> &y);
161 float get_residual_l2(const matrix::CRS<float> &A,
162  const view1D<vector<float>, float> &x,
163  const view1D<vector<float>, float> &y);
164 float get_residual_l2(const matrix::CRS<float> &A,
165  const view1D<vector<float>, float> &x,
166  const view1D<matrix::Dense<float>, float> &y);
167 float get_residual_l2(const matrix::CRS<float> &A,
168  const view1D<matrix::Dense<float>, float> &x,
169  const vector<float> &y);
170 float get_residual_l2(const matrix::CRS<float> &A,
171  const view1D<matrix::Dense<float>, float> &x,
172  const view1D<vector<float>, float> &y);
173 float get_residual_l2(const matrix::CRS<float> &A,
174  const view1D<matrix::Dense<float>, float> &x,
175  const view1D<matrix::Dense<float>, float> &y);
176 
189  const vector<double> &x, const vector<double> &y);
191  const vector<double> &x,
192  const view1D<vector<double>, double> &y);
194  const vector<double> &x,
195  const view1D<matrix::Dense<double>, double> &y);
197  const view1D<vector<double>, double> &x,
198  const vector<double> &y);
200  const view1D<vector<double>, double> &x,
201  const view1D<vector<double>, double> &y);
203  const view1D<vector<double>, double> &x,
204  const view1D<matrix::Dense<double>, double> &y);
206  const view1D<matrix::Dense<double>, double> &x,
207  const vector<double> &y);
209  const view1D<matrix::Dense<double>, double> &x,
210  const view1D<vector<double>, double> &y);
212  const view1D<matrix::Dense<double>, double> &x,
213  const view1D<matrix::Dense<double>, double> &y);
215  const vector<float> &x, const vector<float> &y);
217  const vector<float> &x,
218  const view1D<vector<float>, float> &y);
220  const vector<float> &x,
221  const view1D<matrix::Dense<float>, float> &y);
223  const view1D<vector<float>, float> &x,
224  const vector<float> &y);
226  const view1D<vector<float>, float> &x,
227  const view1D<vector<float>, float> &y);
229  const view1D<vector<float>, float> &x,
230  const view1D<matrix::Dense<float>, float> &y);
232  const view1D<matrix::Dense<float>, float> &x,
233  const vector<float> &y);
235  const view1D<matrix::Dense<float>, float> &x,
236  const view1D<vector<float>, float> &y);
238  const view1D<matrix::Dense<float>, float> &x,
239  const view1D<matrix::Dense<float>, float> &y);
254 [[nodiscard]] bool solver_check(const int err);
255 
256 // logger
257 
269 void set_log_level(const size_t Level);
270 
276 void set_log_filename(const std::string filename);
277 
278 // create typical data///////////////////////////
279 
292 template <typename T>
293 void random_vector(vector<T> &vec, const T min, const T max);
294 
295 // is_same //////////////////
296 
301 template <typename T, typename U>
302 [[nodiscard]] bool is_same_structure(const T A, const U B) {
303  return false;
304 }
305 
317 template <typename T>
318 [[nodiscard]] bool is_same_structure(const vector<T> &x, const vector<T> &y) {
319  return x.size() == y.size();
320 }
321 
333 template <typename T>
334 [[nodiscard]] bool is_same_structure(const matrix::Dense<T> &A,
335  const matrix::Dense<T> &B);
336 
348 template <typename T>
349 [[nodiscard]] bool is_same_structure(const matrix::COO<T> &A,
350  const matrix::COO<T> &B);
351 
363 template <typename T>
364 [[nodiscard]] bool is_same_structure(const matrix::CRS<T> &A,
365  const matrix::CRS<T> &B);
366 
378 template <typename T>
380  const matrix::LinearOperator<T> &B);
381 
386 template <typename T, typename... types>
387 [[nodiscard]] bool is_same_structure(const T &A, const T &B,
388  const types &... args) {
389  return is_same_structure(A, B) && is_same_structure(A, args...);
390 }
391 
403 template <typename T, typename U>
404 [[nodiscard]] bool is_same_size(const T &x, const U &y) {
405  return x.size() == y.size();
406 }
407 
419 template <typename T>
420 [[nodiscard]] bool is_same_size(const matrix::Dense<T> &A,
421  const matrix::Dense<T> &B);
422 
434 template <typename T>
435 [[nodiscard]] bool is_same_size(const matrix::COO<T> &A,
436  const matrix::COO<T> &B);
437 
449 template <typename T>
450 [[nodiscard]] bool is_same_size(const matrix::CRS<T> &A,
451  const matrix::CRS<T> &B);
452 
464 template <typename T>
465 [[nodiscard]] bool is_same_size(const matrix::LinearOperator<T> &A,
466  const matrix::LinearOperator<T> &B);
467 
472 template <typename T, typename U, typename... types>
473 [[nodiscard]] bool is_same_size(const T &arg1, const U &arg2,
474  const types &... args) {
475  return is_same_size(arg1, arg2) && is_same_size(arg1, args...);
476 }
477 
486 template <typename T, typename U>
487 [[nodiscard]] bool is_same_device_mem_stat(const T &arg1, const U &arg2) {
488  return arg1.get_device_mem_stat() == arg2.get_device_mem_stat();
489 }
490 
499 template <typename T, typename U, typename... types>
500 [[nodiscard]] bool is_same_device_mem_stat(const T &arg1, const U &arg2,
501  const types &... args) {
502  return is_same_device_mem_stat(arg1, arg2) &&
503  is_same_device_mem_stat(arg1, args...);
504 }
505 
506 // create matrix //////////////////
507 
521 template <typename T>
522 [[nodiscard]] matrix::COO<T> band_matrix(const int M, const int N, const int W,
523  const T diag_val, const T val);
524 
537 template <typename T>
538 [[nodiscard]] matrix::COO<T> random_structure_matrix(const int M, const int N,
539  const int nnzrow,
540  const T val);
541 
551 template <typename T>[[nodiscard]] matrix::COO<T> eye(const int M);
552 
562 template <typename T>[[nodiscard]] matrix::COO<T> frank_matrix(const int &M);
563 
574 template <typename T>
575 [[nodiscard]] T frank_matrix_eigenvalue(const int &M, const int &N);
576 
588 template <typename T>
589 [[nodiscard]] matrix::COO<T> tridiagonal_toeplitz_matrix(const int &M, T a,
590  T b);
591 
604 template <typename T>
605 [[nodiscard]] T tridiagonal_toeplitz_matrix_eigenvalue(const int &M, int N, T a,
606  T b);
607 
617 template <typename T>
618 [[nodiscard]] matrix::COO<T> laplacian_matrix_1D(const int &M);
619 
630 template <typename T>
631 [[nodiscard]] T laplacian_matrix_1D_eigenvalue(const int &M, int N);
632 
644 template <typename T>
645 [[nodiscard]] matrix::COO<T> laplacian_matrix_2D_5p(const int M, const int N);
646 
661 template <typename T>
662 [[nodiscard]] matrix::COO<T> toeplitz_plus_hankel_matrix(const int &M, T a0,
663  T a1, T a2);
664 
678 template <typename T>
679 [[nodiscard]] T toeplitz_plus_hankel_matrix_eigenvalue(const int &M, int N,
680  T a0, T a1, T a2, T b0,
681  T b1, T b2);
682 
683 // send///////////////////
684 
689 template <typename T> auto send(T &x) { x.send(); }
690 
695 template <typename T, typename... Types> auto send(T &x, Types &... args) {
696  x.send();
697  send(args...);
698 }
699 
700 // recv///////////////////
705 template <typename T> auto recv(T &x) { x.recv(); }
706 
711 template <typename T, typename... Types> auto recv(T &x, Types &... args) {
712  x.recv();
713  recv(args...);
714 }
715 
716 // device_free///////////////////
717 
722 template <typename T> auto device_free(T &x) { x.device_free(); }
723 
728 template <typename T, typename... Types>
729 auto device_free(T &x, Types &... args) {
730  x.device_free();
731  device_free(args...);
732 }
733 
738 [[nodiscard]] bool build_with_avx();
739 
744 [[nodiscard]] bool build_with_avx2();
745 
750 [[nodiscard]] bool build_with_avx512();
751 
756 [[nodiscard]] bool build_with_mpi();
757 
762 [[nodiscard]] bool build_with_gpu();
763 
768 [[nodiscard]] bool build_with_mkl();
769 
775 [[nodiscard]] bool build_with_lapack();
776 
782 [[nodiscard]] bool build_with_cblas();
785 } // namespace monolish::util
monolish::util::random_vector
void random_vector(vector< T > &vec, const T min, const T max)
create random vector
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)
monolish::matrix::LinearOperator
Linear Operator imitating Matrix.
Definition: monolish_coo.hpp:30
monolish::util::build_with_mkl
bool build_with_mkl()
get build option (true: with intel mkl, false: without intel mkl)
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],...
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],...
monolish::util::build_with_avx512
bool build_with_avx512()
get build option (true: with avx512, false: without avx512)
monolish::util::build_with_avx2
bool build_with_avx2()
get build option (true: with avx2, false: without avx2)
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
monolish::util::eye
matrix::COO< T > eye(const int M)
create band matrix
monolish_matrix.hpp
monolish::matrix::Dense
Dense format Matrix.
Definition: monolish_coo.hpp:28
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:404
monolish::util::frank_matrix
matrix::COO< T > frank_matrix(const int &M)
create Frank matrix
monolish::vector::size
auto size() const
get vector size
Definition: monolish_vector.hpp:310
monolish::util::tridiagonal_toeplitz_matrix
matrix::COO< T > tridiagonal_toeplitz_matrix(const int &M, T a, T b)
create tridiagonal Toeplitz matrix
monolish_view1D.hpp
monolish::util
monolish utilities
Definition: monolish_common.hpp:23
monolish::util::build_with_avx
bool build_with_avx()
get build option (true: with avx, false: without avx)
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
monolish::util::build_with_cblas
bool build_with_cblas()
get build option (true: with cblas, false: without cblas (=with intel mkl))
monolish::util::recv
auto recv(T &x)
recv. and free data from GPU
Definition: monolish_common.hpp:705
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
monolish::util::send
auto send(T &x)
send data to GPU
Definition: monolish_common.hpp:689
monolish::util::set_log_level
void set_log_level(const size_t Level)
Specifying the log level.
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
monolish::util::laplacian_matrix_1D_eigenvalue
T laplacian_matrix_1D_eigenvalue(const int &M, int N)
Nth smallest eigenvalue of 1D Laplacian matrix.
monolish_vector.hpp
monolish_logger.hpp
monolish::util::device_free
auto device_free(T &x)
free data of GPU
Definition: monolish_common.hpp:722
monolish::util::solver_check
bool solver_check(const int err)
check error
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.
monolish::matrix::COO
Coodinate (COO) format Matrix (need to sort)
Definition: monolish_coo.hpp:43
monolish_dense.hpp
monolish::view1D
1D view class
Definition: monolish_coo.hpp:26
monolish::util::build_with_mpi
bool build_with_mpi()
get build option (true: enable MPI, false: disable MPI)
monolish::vector< double >
monolish::util::build_with_lapack
bool build_with_lapack()
get build option (true: with lapack, false: without lapack (=with intel mkl))
monolish::util::build_with_gpu
bool build_with_gpu()
get build option (true: enable gpu, false: disable gpu)
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:487
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.
monolish::util::laplacian_matrix_1D
matrix::COO< T > laplacian_matrix_1D(const int &M)
create 1D Laplacian matrix
monolish::util::get_num_devices
int get_num_devices()
get the number of devices
monolish::util::is_same_structure
bool is_same_structure(const T A, const U B)
compare matrix structure
Definition: monolish_common.hpp:302
monolish::util::frank_matrix_eigenvalue
T frank_matrix_eigenvalue(const int &M, const int &N)
Nth eigenvalue from the bottom of MxM Frank matrix.
monolish::util::get_default_device
int get_default_device()
get default device number
monolish::matrix::CRS
Compressed Row Storage (CRS) format Matrix.
Definition: monolish_coo.hpp:29
monolish::util::set_log_filename
void set_log_filename(const std::string filename)
Specifying the log finename.
monolish::util::set_default_device
bool set_default_device(size_t device_num)
set default device number