monolish  0.15.2
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 {
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 
276 // is_same //////////////////
277 
281 template <typename T, typename U>
282 [[nodiscard]] bool is_same_structure(const T A, const U B) {
283  return false;
284 }
285 
296 template <typename T>
297 [[nodiscard]] bool is_same_structure(const vector<T> &x, const vector<T> &y) {
298  return x.size() == y.size();
299 }
300 
311 template <typename T>
312 [[nodiscard]] bool is_same_structure(const matrix::Dense<T> &A,
313  const matrix::Dense<T> &B);
314 
325 template <typename T>
326 [[nodiscard]] bool is_same_structure(const matrix::COO<T> &A,
327  const matrix::COO<T> &B);
328 
339 template <typename T>
340 [[nodiscard]] bool is_same_structure(const matrix::CRS<T> &A,
341  const matrix::CRS<T> &B);
342 
353 template <typename T>
355  const matrix::LinearOperator<T> &B);
356 
360 template <typename T, typename... types>
361 [[nodiscard]] bool is_same_structure(const T &A, const T &B,
362  const types &... args) {
363  return is_same_structure(A, B) && is_same_structure(A, args...);
364 }
365 
376 template <typename T, typename U>
377 [[nodiscard]] bool is_same_size(const T &x, const U &y) {
378  return x.size() == y.size();
379 }
380 
391 template <typename T>
392 [[nodiscard]] bool is_same_size(const matrix::Dense<T> &A,
393  const matrix::Dense<T> &B);
394 
405 template <typename T>
406 [[nodiscard]] bool is_same_size(const matrix::COO<T> &A,
407  const matrix::COO<T> &B);
408 
419 template <typename T>
420 [[nodiscard]] bool is_same_size(const matrix::CRS<T> &A,
421  const matrix::CRS<T> &B);
422 
433 template <typename T>
434 [[nodiscard]] bool is_same_size(const matrix::LinearOperator<T> &A,
435  const matrix::LinearOperator<T> &B);
436 
440 template <typename T, typename U, typename... types>
441 [[nodiscard]] bool is_same_size(const T &arg1, const U &arg2,
442  const types &... args) {
443  return is_same_size(arg1, arg2) && is_same_size(arg1, args...);
444 }
445 
453 template <typename T, typename U>
454 [[nodiscard]] bool is_same_device_mem_stat(const T &arg1, const U &arg2) {
455  return arg1.get_device_mem_stat() == arg2.get_device_mem_stat();
456 }
457 
465 template <typename T, typename U, typename... types>
466 [[nodiscard]] bool is_same_device_mem_stat(const T &arg1, const U &arg2,
467  const types &... args) {
468  return is_same_device_mem_stat(arg1, arg2) &&
469  is_same_device_mem_stat(arg1, args...);
470 }
471 
472 // create matrix //////////////////
473 
486 template <typename T>
487 [[nodiscard]] matrix::COO<T> band_matrix(const int M, const int N, const int W,
488  const T diag_val, const T val);
489 
501 template <typename T>
502 [[nodiscard]] matrix::COO<T> random_structure_matrix(const int M, const int N,
503  const int nnzrow,
504  const T val);
505 
514 template <typename T>[[nodiscard]] matrix::COO<T> eye(const int M);
515 
524 template <typename T>[[nodiscard]] matrix::COO<T> frank_matrix(const int &M);
525 
535 template <typename T>
536 [[nodiscard]] T frank_matrix_eigenvalue(const int &M, const int &N);
537 
548 template <typename T>
549 [[nodiscard]] matrix::COO<T> tridiagonal_toeplitz_matrix(const int &M, T a,
550  T b);
551 
563 template <typename T>
564 [[nodiscard]] T tridiagonal_toeplitz_matrix_eigenvalue(const int &M, int N, T a,
565  T b);
566 
575 template <typename T>
576 [[nodiscard]] matrix::COO<T> laplacian_matrix_1D(const int &M);
577 
587 template <typename T>
588 [[nodiscard]] T laplacian_matrix_1D_eigenvalue(const int &M, int N);
589 
600 template <typename T>
601 [[nodiscard]] matrix::COO<T> laplacian_matrix_2D_5p(const int M, const int N);
602 
616 template <typename T>
617 [[nodiscard]] matrix::COO<T> toeplitz_plus_hankel_matrix(const int &M, T a0,
618  T a1, T a2);
619 
632 template <typename T>
633 [[nodiscard]] T toeplitz_plus_hankel_matrix_eigenvalue(const int &M, int N,
634  T a0, T a1, T a2, T b0,
635  T b1, T b2);
636 
637 // send///////////////////
638 
642 template <typename T> auto send(T &x) { x.send(); }
643 
647 template <typename T, typename... Types> auto send(T &x, Types &... args) {
648  x.send();
649  send(args...);
650 }
651 
652 // recv///////////////////
656 template <typename T> auto recv(T &x) { x.recv(); }
657 
661 template <typename T, typename... Types> auto recv(T &x, Types &... args) {
662  x.recv();
663  recv(args...);
664 }
665 
666 // device_free///////////////////
667 
671 template <typename T> auto device_free(T &x) { x.device_free(); }
672 
676 template <typename T, typename... Types>
677 auto device_free(T &x, Types &... args) {
678  x.device_free();
679  device_free(args...);
680 }
681 
685 [[nodiscard]] bool build_with_avx();
686 
690 [[nodiscard]] bool build_with_avx2();
691 
695 [[nodiscard]] bool build_with_avx512();
696 
700 [[nodiscard]] bool build_with_mpi();
701 
705 [[nodiscard]] bool build_with_gpu();
706 
710 [[nodiscard]] bool build_with_mkl();
711 
716 [[nodiscard]] bool build_with_lapack();
717 
722 [[nodiscard]] bool build_with_cblas();
723 
724 } // namespace monolish::util
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::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:377
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::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],...
monolish::util::build_with_gpu
bool build_with_gpu()
get build option (true: enable gpu, false: disable gpu)
monolish::util::laplacian_matrix_1D
matrix::COO< T > laplacian_matrix_1D(const int &M)
create 1D Laplacian matrix
monolish::util::build_with_cblas
bool build_with_cblas()
get build option (true: with cblas, false: without cblas (=with intel mkl))
monolish::util::is_same_structure
bool is_same_structure(const T A, const U B)
compare matrix structure
Definition: monolish_common.hpp:282
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::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_matrix.hpp
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.
monolish::util::device_free
auto device_free(T &x)
free data of GPU
Definition: monolish_common.hpp:671
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::vector::size
auto size() const
get vector size
Definition: monolish_vector.hpp:305
monolish::util::build_with_lapack
bool build_with_lapack()
get build option (true: with lapack, false: without lapack (=with intel mkl))
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:454
monolish_view1D.hpp
monolish::util
monolish utilities
Definition: monolish_common.hpp:23
monolish::util::frank_matrix
matrix::COO< T > frank_matrix(const int &M)
create Frank matrix
monolish::util::tridiagonal_toeplitz_matrix
matrix::COO< T > tridiagonal_toeplitz_matrix(const int &M, T a, T b)
create tridiagonal Toeplitz 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::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::util::set_default_device
bool set_default_device(size_t device_num)
set default device number
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_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.
monolish::util::recv
auto recv(T &x)
recv. and free data from GPU
Definition: monolish_common.hpp:656
monolish::view1D
1D view class
Definition: monolish_coo.hpp:26
monolish::util::solver_check
bool solver_check(const int err)
check error
monolish::util::random_vector
void random_vector(vector< T > &vec, const T min, const T max)
create random vector
monolish::util::build_with_avx2
bool build_with_avx2()
get build option (true: with avx2, false: without avx2)
monolish::util::set_log_level
void set_log_level(const size_t Level)
Logger utils ///////////////////////////////.
monolish::util::get_default_device
int get_default_device()
get default device number
monolish::vector< double >
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::build_with_mpi
bool build_with_mpi()
get build option (true: enable MPI, false: disable MPI)
monolish::util::get_num_devices
int get_num_devices()
get the number of devices
monolish::util::send
auto send(T &x)
send data to GPU
Definition: monolish_common.hpp:642
monolish::util::eye
matrix::COO< T > eye(const int M)
create band matrix
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)
monolish::util::build_with_mkl
bool build_with_mkl()
get build option (true: with intel mkl, false: without intel mkl)