monolish  0.17.1
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_tensor.hpp"
9 #include "monolish_vector.hpp"
10 #include "monolish_view1D.hpp"
11 #include <initializer_list>
12 
13 // error code
14 #define MONOLISH_SOLVER_SUCCESS 0
15 #define MONOLISH_SOLVER_SIZE_ERROR -1
16 #define MONOLISH_SOLVER_MAXITER -2
17 #define MONOLISH_SOLVER_BREAKDOWN -3
18 #define MONOLISH_SOLVER_RESIDUAL_NAN -4
19 #define MONOLISH_SOLVER_NOT_IMPL -10
20 
24 namespace monolish {
28 namespace util {
41 
47 bool set_default_device(size_t device_num);
48 
56 
74  const vector<double> &y);
76  const view1D<vector<double>, double> &y);
78  const view1D<matrix::Dense<double>, double> &y);
80  const view1D<tensor::tensor_Dense<double>, double> &y);
82  const view1D<vector<double>, double> &x,
83  const vector<double> &y);
85  const view1D<vector<double>, double> &x,
86  const view1D<vector<double>, double> &y);
88  const view1D<vector<double>, double> &x,
89  const view1D<matrix::Dense<double>, double> &y);
91  const view1D<vector<double>, double> &x,
92  const view1D<tensor::tensor_Dense<double>, double> &y);
94  const view1D<matrix::Dense<double>, double> &x,
95  const vector<double> &y);
97  const view1D<matrix::Dense<double>, double> &x,
98  const view1D<vector<double>, double> &y);
100  const view1D<matrix::Dense<double>, double> &x,
101  const view1D<matrix::Dense<double>, double> &y);
103  const view1D<matrix::Dense<double>, double> &x,
104  const view1D<tensor::tensor_Dense<double>, double> &y);
106  const view1D<tensor::tensor_Dense<double>, double> &x,
107  const vector<double> &y);
109  const view1D<tensor::tensor_Dense<double>, double> &x,
110  const view1D<vector<double>, double> &y);
112  const view1D<tensor::tensor_Dense<double>, double> &x,
113  const view1D<matrix::Dense<double>, double> &y);
115  const view1D<tensor::tensor_Dense<double>, double> &x,
116  const view1D<tensor::tensor_Dense<double>, double> &y);
117 
119  const vector<float> &y);
121  const view1D<vector<float>, float> &y);
123  const view1D<matrix::Dense<float>, float> &y);
125  const view1D<tensor::tensor_Dense<float>, float> &y);
127  const view1D<vector<float>, float> &x,
128  const vector<float> &y);
130  const view1D<vector<float>, float> &x,
131  const view1D<vector<float>, float> &y);
133  const view1D<vector<float>, float> &x,
134  const view1D<matrix::Dense<float>, float> &y);
136  const view1D<vector<float>, float> &x,
137  const view1D<tensor::tensor_Dense<float>, float> &y);
139  const view1D<matrix::Dense<float>, float> &x,
140  const vector<float> &y);
142  const view1D<matrix::Dense<float>, float> &x,
143  const view1D<vector<float>, float> &y);
145  const view1D<matrix::Dense<float>, float> &x,
146  const view1D<matrix::Dense<float>, float> &y);
148  const view1D<matrix::Dense<float>, float> &x,
149  const view1D<tensor::tensor_Dense<float>, float> &y);
151  const view1D<tensor::tensor_Dense<float>, float> &x,
152  const vector<float> &y);
154  const view1D<tensor::tensor_Dense<float>, float> &x,
155  const view1D<vector<float>, float> &y);
157  const view1D<tensor::tensor_Dense<float>, float> &x,
158  const view1D<matrix::Dense<float>, float> &y);
160  const view1D<tensor::tensor_Dense<float>, float> &x,
161  const view1D<tensor::tensor_Dense<float>, float> &y);
162 
175  const vector<double> &y);
177  const view1D<vector<double>, double> &y);
179  const view1D<matrix::Dense<double>, double> &y);
181  const view1D<tensor::tensor_Dense<double>, double> &y);
183  const view1D<vector<double>, double> &x,
184  const vector<double> &y);
186  const view1D<vector<double>, double> &x,
187  const view1D<vector<double>, double> &y);
189  const view1D<vector<double>, double> &x,
190  const view1D<matrix::Dense<double>, double> &y);
192  const view1D<vector<double>, double> &x,
193  const view1D<tensor::tensor_Dense<double>, double> &y);
195  const view1D<matrix::Dense<double>, double> &x,
196  const vector<double> &y);
198  const view1D<matrix::Dense<double>, double> &x,
199  const view1D<vector<double>, double> &y);
201  const view1D<matrix::Dense<double>, double> &x,
202  const view1D<matrix::Dense<double>, double> &y);
204  const view1D<matrix::Dense<double>, double> &x,
205  const view1D<tensor::tensor_Dense<double>, double> &y);
207  const view1D<tensor::tensor_Dense<double>, double> &x,
208  const vector<double> &y);
210  const view1D<tensor::tensor_Dense<double>, double> &x,
211  const view1D<vector<double>, double> &y);
213  const view1D<tensor::tensor_Dense<double>, double> &x,
214  const view1D<matrix::Dense<double>, double> &y);
216  const view1D<tensor::tensor_Dense<double>, double> &x,
217  const view1D<tensor::tensor_Dense<double>, double> &y);
218 
220  const vector<float> &y);
222  const view1D<vector<float>, float> &y);
224  const view1D<matrix::Dense<float>, float> &y);
226  const view1D<tensor::tensor_Dense<float>, float> &y);
228  const view1D<vector<float>, float> &x,
229  const vector<float> &y);
231  const view1D<vector<float>, float> &x,
232  const view1D<vector<float>, float> &y);
234  const view1D<vector<float>, float> &x,
235  const view1D<matrix::Dense<float>, float> &y);
237  const view1D<vector<float>, float> &x,
238  const view1D<tensor::tensor_Dense<float>, float> &y);
240  const view1D<matrix::Dense<float>, float> &x,
241  const vector<float> &y);
243  const view1D<matrix::Dense<float>, float> &x,
244  const view1D<vector<float>, float> &y);
246  const view1D<matrix::Dense<float>, float> &x,
247  const view1D<matrix::Dense<float>, float> &y);
249  const view1D<matrix::Dense<float>, float> &x,
250  const view1D<tensor::tensor_Dense<float>, float> &y);
252  const view1D<tensor::tensor_Dense<float>, float> &x,
253  const vector<float> &y);
255  const view1D<tensor::tensor_Dense<float>, float> &x,
256  const view1D<vector<float>, float> &y);
258  const view1D<tensor::tensor_Dense<float>, float> &x,
259  const view1D<matrix::Dense<float>, float> &y);
261  const view1D<tensor::tensor_Dense<float>, float> &x,
262  const view1D<tensor::tensor_Dense<float>, float> &y);
263 
276  const vector<double> &x, const vector<double> &y);
278  const vector<double> &x,
279  const view1D<vector<double>, double> &y);
281  const vector<double> &x,
282  const view1D<matrix::Dense<double>, double> &y);
284  const vector<double> &x,
285  const view1D<tensor::tensor_Dense<double>, double> &y);
287  const view1D<vector<double>, double> &x,
288  const vector<double> &y);
290  const view1D<vector<double>, double> &x,
291  const view1D<vector<double>, double> &y);
293  const view1D<vector<double>, double> &x,
294  const view1D<matrix::Dense<double>, double> &y);
296  const view1D<vector<double>, double> &x,
297  const view1D<tensor::tensor_Dense<double>, double> &y);
299  const view1D<matrix::Dense<double>, double> &x,
300  const vector<double> &y);
302  const view1D<matrix::Dense<double>, double> &x,
303  const view1D<vector<double>, double> &y);
305  const view1D<matrix::Dense<double>, double> &x,
306  const view1D<matrix::Dense<double>, double> &y);
308  const view1D<matrix::Dense<double>, double> &x,
309  const view1D<tensor::tensor_Dense<double>, double> &y);
311  const view1D<tensor::tensor_Dense<double>, double> &x,
312  const vector<double> &y);
314  const view1D<tensor::tensor_Dense<double>, double> &x,
315  const view1D<vector<double>, double> &y);
317  const view1D<tensor::tensor_Dense<double>, double> &x,
318  const view1D<matrix::Dense<double>, double> &y);
320  const view1D<tensor::tensor_Dense<double>, double> &x,
321  const view1D<tensor::tensor_Dense<double>, double> &y);
322 
337 [[nodiscard]] bool solver_check(const int err);
338 
339 // logger
340 
352 void set_log_level(const size_t Level);
353 
359 void set_log_filename(const std::string filename);
360 
361 // create typical data///////////////////////////
362 
375 template <typename T>
376 void random_vector(vector<T> &vec, const T min, const T max);
377 
391 template <typename T>
392 void random_vector(vector<T> &vec, const T min, const T max,
393  const std::uint32_t seed);
394 
395 // is_same //////////////////
396 
401 template <typename T, typename U>
402 [[nodiscard]] bool is_same_structure(const T A, const U B) {
403  return false;
404 }
405 
417 template <typename T>
418 [[nodiscard]] bool is_same_structure(const vector<T> &x, const vector<T> &y) {
419  return x.size() == y.size();
420 }
421 
433 template <typename T>
434 [[nodiscard]] bool is_same_structure(const matrix::Dense<T> &A,
435  const matrix::Dense<T> &B);
436 
448 template <typename T>
449 [[nodiscard]] bool is_same_structure(const matrix::COO<T> &A,
450  const matrix::COO<T> &B);
451 
463 template <typename T>
464 [[nodiscard]] bool is_same_structure(const matrix::CRS<T> &A,
465  const matrix::CRS<T> &B);
466 
478 template <typename T>
480  const matrix::LinearOperator<T> &B);
481 
493 template <typename T>
494 [[nodiscard]] bool is_same_structure(const tensor::tensor_Dense<T> &A,
495  const tensor::tensor_Dense<T> &B);
496 
508 template <typename T>
509 [[nodiscard]] bool is_same_structure(const tensor::tensor_COO<T> &A,
510  const tensor::tensor_COO<T> &B);
511 
516 template <typename T, typename... types>
517 [[nodiscard]] bool is_same_structure(const T &A, const T &B,
518  const types &...args) {
519  return is_same_structure(A, B) && is_same_structure(A, args...);
520 }
521 
533 template <typename T, typename U>
534 [[nodiscard]] bool is_same_size(const T &x, const U &y) {
535  return x.size() == y.size();
536 }
537 
549 template <typename T>
550 [[nodiscard]] bool is_same_size(const matrix::Dense<T> &A,
551  const matrix::Dense<T> &B);
552 
564 template <typename T>
565 [[nodiscard]] bool is_same_size(const matrix::COO<T> &A,
566  const matrix::COO<T> &B);
567 
579 template <typename T>
580 [[nodiscard]] bool is_same_size(const matrix::CRS<T> &A,
581  const matrix::CRS<T> &B);
582 
594 template <typename T>
595 [[nodiscard]] bool is_same_size(const matrix::LinearOperator<T> &A,
596  const matrix::LinearOperator<T> &B);
597 
609 template <typename T>
610 [[nodiscard]] bool is_same_size(const tensor::tensor_Dense<T> &A,
611  const tensor::tensor_Dense<T> &B);
612 
624 template <typename T>
625 [[nodiscard]] bool is_same_size(const tensor::tensor_COO<T> &A,
626  const tensor::tensor_COO<T> &B);
627 
632 template <typename T, typename U, typename... types>
633 [[nodiscard]] bool is_same_size(const T &arg1, const U &arg2,
634  const types &...args) {
635  return is_same_size(arg1, arg2) && is_same_size(arg1, args...);
636 }
637 
646 template <typename T, typename U>
647 [[nodiscard]] bool is_same_device_mem_stat(const T &arg1, const U &arg2) {
648  return arg1.get_device_mem_stat() == arg2.get_device_mem_stat();
649 }
650 
659 template <typename T, typename U, typename... types>
660 [[nodiscard]] bool is_same_device_mem_stat(const T &arg1, const U &arg2,
661  const types &...args) {
662  return is_same_device_mem_stat(arg1, arg2) &&
663  is_same_device_mem_stat(arg1, args...);
664 }
665 
666 // create matrix //////////////////
667 
681 template <typename T>
682 [[nodiscard]] matrix::COO<T> band_matrix(const int M, const int N, const int W,
683  const T diag_val, const T val);
684 
699 template <typename T>
700 [[nodiscard]] matrix::COO<T> asym_band_matrix(const int M, const int N,
701  const int W, const T diag_val,
702  const T Uval, const T Lval);
703 
716 template <typename T>
717 [[nodiscard]] matrix::COO<T> random_structure_matrix(const int M, const int N,
718  const int nnzrow,
719  const T val);
720 
733 template <typename T>
734 [[nodiscard]] tensor::tensor_COO<T>
735 random_structure_tensor(const size_t M, const size_t N, const size_t nnzrow,
736  const T val);
737 
751 template <typename T>
752 [[nodiscard]] tensor::tensor_COO<T>
753 random_structure_tensor(const size_t M, const size_t N, const size_t L,
754  const size_t nnzrow, const T val);
755 
769 template <typename T>
770 [[nodiscard]] tensor::tensor_COO<T>
771 random_structure_tensor(const size_t K, const size_t M, const size_t N,
772  const size_t L, const size_t nnzrow, const T val);
773 
783 template <typename T> [[nodiscard]] matrix::COO<T> eye(const int M);
784 
794 template <typename T> [[nodiscard]] matrix::COO<T> frank_matrix(const int &M);
795 
806 template <typename T>
807 [[nodiscard]] T frank_matrix_eigenvalue(const int &M, const int &N);
808 
820 template <typename T>
821 [[nodiscard]] matrix::COO<T> tridiagonal_toeplitz_matrix(const int &M, T a,
822  T b);
823 
836 template <typename T>
837 [[nodiscard]] T tridiagonal_toeplitz_matrix_eigenvalue(const int &M, int N, T a,
838  T b);
839 
849 template <typename T>
850 [[nodiscard]] matrix::COO<T> laplacian_matrix_1D(const int &M);
851 
862 template <typename T>
863 [[nodiscard]] T laplacian_matrix_1D_eigenvalue(const int &M, int N);
864 
876 template <typename T>
877 [[nodiscard]] matrix::COO<T> laplacian_matrix_2D_5p(const int M, const int N);
878 
893 template <typename T>
894 [[nodiscard]] matrix::COO<T> toeplitz_plus_hankel_matrix(const int &M, T a0,
895  T a1, T a2);
896 
910 template <typename T>
911 [[nodiscard]] T toeplitz_plus_hankel_matrix_eigenvalue(const int &M, int N,
912  T a0, T a1, T a2, T b0,
913  T b1, T b2);
914 
915 // send///////////////////
916 
921 template <typename T> void send(T &x) { x.send(); }
922 
927 template <typename T, typename... Types> void send(T &x, Types &...args) {
928  x.send();
929  send(args...);
930 }
931 
932 // recv///////////////////
937 template <typename T> void recv(T &x) { x.recv(); }
938 
943 template <typename T, typename... Types> void recv(T &x, Types &...args) {
944  x.recv();
945  recv(args...);
946 }
947 
948 // device_free///////////////////
949 
954 template <typename T> void device_free(T &x) { x.device_free(); }
955 
960 template <typename T, typename... Types>
961 void device_free(T &x, Types &...args) {
962  x.device_free();
963  device_free(args...);
964 }
965 
970 [[nodiscard]] bool build_with_avx();
971 
976 [[nodiscard]] bool build_with_avx2();
977 
982 [[nodiscard]] bool build_with_avx512();
983 
988 [[nodiscard]] bool build_with_mpi();
989 
994 [[nodiscard]] bool build_with_gpu();
995 
1000 [[nodiscard]] bool build_with_mkl();
1001 
1007 [[nodiscard]] bool build_with_lapack();
1008 
1014 [[nodiscard]] bool build_with_cblas();
1017 } // namespace util
1018 } // namespace monolish
Coodinate (COO) format Matrix (need to sort)
Compressed Row Storage (CRS) format Matrix.
Dense format Matrix.
Linear Operator imitating Matrix.
size_t size() const
get vector size
int get_num_devices()
get the number of devices
bool set_default_device(size_t device_num)
set default device number
int get_default_device()
get default device number
void send(T &x)
send data to GPU
void recv(T &x)
recv. and free data from GPU
void device_free(T &x)
free data of GPU
double get_residual_l2(const matrix::Dense< double > &A, const vector< double > &x, const vector< double > &y)
get nrm |b-Ax|_2
bool build_with_mpi()
get build option (true: enable MPI, false: disable MPI)
bool build_with_gpu()
get build option (true: enable gpu, false: disable gpu)
bool build_with_avx2()
get build option (true: with avx2, false: without avx2)
bool build_with_lapack()
get build option (true: with lapack, false: without lapack (=with intel mkl))
bool build_with_cblas()
get build option (true: with cblas, false: without cblas (=with intel mkl))
bool build_with_avx()
get build option (true: with avx, false: without avx)
bool build_with_mkl()
get build option (true: with intel mkl, false: without intel mkl)
bool build_with_avx512()
get build option (true: with avx512, false: without avx512)
bool is_same_structure(const T A, const U B)
compare matrix structure
bool is_same_size(const T &x, const U &y)
compare size of vector or 1Dview (same as is_same_structure())
bool is_same_device_mem_stat(const T &arg1, const U &arg2)
compare same device memory status
bool solver_check(const int err)
check error
tensor::tensor_COO< T > random_structure_tensor(const size_t M, const size_t N, const size_t nnzrow, const T val)
create random structure tensor (index is decided by random)
void random_vector(vector< T > &vec, const T min, const T max)
create random vector
matrix::COO< T > tridiagonal_toeplitz_matrix(const int &M, T a, T b)
create tridiagonal Toeplitz matrix
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
matrix::COO< T > toeplitz_plus_hankel_matrix(const int &M, T a0, T a1, T a2)
create Toeplitz-plus-Hankel matrix
T tridiagonal_toeplitz_matrix_eigenvalue(const int &M, int N, T a, T b)
Nth smallest eigenvalue of MxM tridiagonal Toeplitz matrix.
T laplacian_matrix_1D_eigenvalue(const int &M, int N)
Nth smallest eigenvalue of 1D Laplacian matrix.
matrix::COO< T > laplacian_matrix_1D(const int &M)
create 1D Laplacian matrix
matrix::COO< T > asym_band_matrix(const int M, const int N, const int W, const T diag_val, const T Uval, const T Lval)
create asymmetric band 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)
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.
matrix::COO< T > frank_matrix(const int &M)
create Frank 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
T frank_matrix_eigenvalue(const int &M, const int &N)
Nth eigenvalue from the bottom of MxM Frank matrix.
matrix::COO< T > eye(const int M)
create band matrix
void set_log_filename(const std::string filename)
Specifying the log finename.
void set_log_level(const size_t Level)
Specifying the log level.
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],...
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 namespaces