monolish  0.17.3-dev.16
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 "monolish_view_dense.hpp"
13 #include <initializer_list>
14 
15 // error code
16 #define MONOLISH_SOLVER_SUCCESS 0
17 #define MONOLISH_SOLVER_SIZE_ERROR -1
18 #define MONOLISH_SOLVER_MAXITER -2
19 #define MONOLISH_SOLVER_BREAKDOWN -3
20 #define MONOLISH_SOLVER_RESIDUAL_NAN -4
21 #define MONOLISH_SOLVER_NOT_IMPL -10
22 
26 namespace monolish {
30 namespace util {
43 
49 bool set_default_device(size_t device_num);
50 
58 
76  const vector<double> &y);
78  const view1D<vector<double>, double> &y);
80  const view1D<matrix::Dense<double>, double> &y);
82  const view1D<tensor::tensor_Dense<double>, double> &y);
84  const view1D<vector<double>, double> &x,
85  const vector<double> &y);
87  const view1D<vector<double>, double> &x,
88  const view1D<vector<double>, double> &y);
90  const view1D<vector<double>, double> &x,
91  const view1D<matrix::Dense<double>, double> &y);
93  const view1D<vector<double>, double> &x,
94  const view1D<tensor::tensor_Dense<double>, double> &y);
96  const view1D<matrix::Dense<double>, double> &x,
97  const vector<double> &y);
99  const view1D<matrix::Dense<double>, double> &x,
100  const view1D<vector<double>, double> &y);
102  const view1D<matrix::Dense<double>, double> &x,
103  const view1D<matrix::Dense<double>, double> &y);
105  const view1D<matrix::Dense<double>, double> &x,
106  const view1D<tensor::tensor_Dense<double>, double> &y);
108  const view1D<tensor::tensor_Dense<double>, double> &x,
109  const vector<double> &y);
111  const view1D<tensor::tensor_Dense<double>, double> &x,
112  const view1D<vector<double>, double> &y);
114  const view1D<tensor::tensor_Dense<double>, double> &x,
115  const view1D<matrix::Dense<double>, double> &y);
117  const view1D<tensor::tensor_Dense<double>, double> &x,
118  const view1D<tensor::tensor_Dense<double>, double> &y);
119 
121  const vector<float> &y);
123  const view1D<vector<float>, float> &y);
125  const view1D<matrix::Dense<float>, float> &y);
127  const view1D<tensor::tensor_Dense<float>, float> &y);
129  const view1D<vector<float>, float> &x,
130  const vector<float> &y);
132  const view1D<vector<float>, float> &x,
133  const view1D<vector<float>, float> &y);
135  const view1D<vector<float>, float> &x,
136  const view1D<matrix::Dense<float>, float> &y);
138  const view1D<vector<float>, float> &x,
139  const view1D<tensor::tensor_Dense<float>, float> &y);
141  const view1D<matrix::Dense<float>, float> &x,
142  const vector<float> &y);
144  const view1D<matrix::Dense<float>, float> &x,
145  const view1D<vector<float>, float> &y);
147  const view1D<matrix::Dense<float>, float> &x,
148  const view1D<matrix::Dense<float>, float> &y);
150  const view1D<matrix::Dense<float>, float> &x,
151  const view1D<tensor::tensor_Dense<float>, float> &y);
153  const view1D<tensor::tensor_Dense<float>, float> &x,
154  const vector<float> &y);
156  const view1D<tensor::tensor_Dense<float>, float> &x,
157  const view1D<vector<float>, float> &y);
159  const view1D<tensor::tensor_Dense<float>, float> &x,
160  const view1D<matrix::Dense<float>, float> &y);
162  const view1D<tensor::tensor_Dense<float>, float> &x,
163  const view1D<tensor::tensor_Dense<float>, float> &y);
164 
177  const vector<double> &y);
179  const view1D<vector<double>, double> &y);
181  const view1D<matrix::Dense<double>, double> &y);
183  const view1D<tensor::tensor_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<vector<double>, double> &x,
195  const view1D<tensor::tensor_Dense<double>, double> &y);
197  const view1D<matrix::Dense<double>, double> &x,
198  const vector<double> &y);
200  const view1D<matrix::Dense<double>, double> &x,
201  const view1D<vector<double>, double> &y);
203  const view1D<matrix::Dense<double>, double> &x,
204  const view1D<matrix::Dense<double>, double> &y);
206  const view1D<matrix::Dense<double>, double> &x,
207  const view1D<tensor::tensor_Dense<double>, double> &y);
209  const view1D<tensor::tensor_Dense<double>, double> &x,
210  const vector<double> &y);
212  const view1D<tensor::tensor_Dense<double>, double> &x,
213  const view1D<vector<double>, double> &y);
215  const view1D<tensor::tensor_Dense<double>, double> &x,
216  const view1D<matrix::Dense<double>, double> &y);
218  const view1D<tensor::tensor_Dense<double>, double> &x,
219  const view1D<tensor::tensor_Dense<double>, double> &y);
220 
222  const vector<float> &y);
224  const view1D<vector<float>, float> &y);
226  const view1D<matrix::Dense<float>, float> &y);
228  const view1D<tensor::tensor_Dense<float>, float> &y);
230  const view1D<vector<float>, float> &x,
231  const vector<float> &y);
233  const view1D<vector<float>, float> &x,
234  const view1D<vector<float>, float> &y);
236  const view1D<vector<float>, float> &x,
237  const view1D<matrix::Dense<float>, float> &y);
239  const view1D<vector<float>, float> &x,
240  const view1D<tensor::tensor_Dense<float>, float> &y);
242  const view1D<matrix::Dense<float>, float> &x,
243  const vector<float> &y);
245  const view1D<matrix::Dense<float>, float> &x,
246  const view1D<vector<float>, float> &y);
248  const view1D<matrix::Dense<float>, float> &x,
249  const view1D<matrix::Dense<float>, float> &y);
251  const view1D<matrix::Dense<float>, float> &x,
252  const view1D<tensor::tensor_Dense<float>, float> &y);
254  const view1D<tensor::tensor_Dense<float>, float> &x,
255  const vector<float> &y);
257  const view1D<tensor::tensor_Dense<float>, float> &x,
258  const view1D<vector<float>, float> &y);
260  const view1D<tensor::tensor_Dense<float>, float> &x,
261  const view1D<matrix::Dense<float>, float> &y);
263  const view1D<tensor::tensor_Dense<float>, float> &x,
264  const view1D<tensor::tensor_Dense<float>, float> &y);
265 
278  const vector<double> &x, const vector<double> &y);
280  const vector<double> &x,
281  const view1D<vector<double>, double> &y);
283  const vector<double> &x,
284  const view1D<matrix::Dense<double>, double> &y);
286  const vector<double> &x,
287  const view1D<tensor::tensor_Dense<double>, double> &y);
289  const view1D<vector<double>, double> &x,
290  const vector<double> &y);
292  const view1D<vector<double>, double> &x,
293  const view1D<vector<double>, double> &y);
295  const view1D<vector<double>, double> &x,
296  const view1D<matrix::Dense<double>, double> &y);
298  const view1D<vector<double>, double> &x,
299  const view1D<tensor::tensor_Dense<double>, double> &y);
301  const view1D<matrix::Dense<double>, double> &x,
302  const vector<double> &y);
304  const view1D<matrix::Dense<double>, double> &x,
305  const view1D<vector<double>, double> &y);
307  const view1D<matrix::Dense<double>, double> &x,
308  const view1D<matrix::Dense<double>, double> &y);
310  const view1D<matrix::Dense<double>, double> &x,
311  const view1D<tensor::tensor_Dense<double>, double> &y);
313  const view1D<tensor::tensor_Dense<double>, double> &x,
314  const vector<double> &y);
316  const view1D<tensor::tensor_Dense<double>, double> &x,
317  const view1D<vector<double>, double> &y);
319  const view1D<tensor::tensor_Dense<double>, double> &x,
320  const view1D<matrix::Dense<double>, double> &y);
322  const view1D<tensor::tensor_Dense<double>, double> &x,
323  const view1D<tensor::tensor_Dense<double>, double> &y);
324 
339 [[nodiscard]] bool solver_check(const int err);
340 
341 // logger
342 
354 void set_log_level(const size_t Level);
355 
361 void set_log_filename(const std::string filename);
362 
363 // create typical data///////////////////////////
364 
377 template <typename T>
378 void random_vector(vector<T> &vec, const T min, const T max);
379 
393 template <typename T>
394 void random_vector(vector<T> &vec, const T min, const T max,
395  const std::uint32_t seed);
396 
397 // is_same //////////////////
398 
403 template <typename T, typename U>
404 [[nodiscard]] bool is_same_structure(const T A, const U B) {
405  return false;
406 }
407 
419 template <typename T>
420 [[nodiscard]] bool is_same_structure(const vector<T> &x, const vector<T> &y) {
421  return x.size() == y.size();
422 }
423 
435 template <typename T, typename U>
436 [[nodiscard]] bool is_same_structure(const view1D<T, U> &x,
437  const vector<U> &y) {
438  return x.size() == y.size();
439 }
440 
452 template <typename T, typename U>
453 [[nodiscard]] bool is_same_structure(const vector<U> &x,
454  const view1D<T, U> &y) {
455  return x.size() == y.size();
456 }
457 
469 template <typename T, typename U, typename V>
470 [[nodiscard]] bool is_same_structure(const view1D<T, V> &x,
471  const view1D<U, V> &y) {
472  return x.size() == y.size();
473 }
474 
486 template <typename T>
487 [[nodiscard]] bool is_same_structure(const matrix::Dense<T> &A,
488  const matrix::Dense<T> &B);
489 
501 template <typename T>
502 [[nodiscard]] bool is_same_structure(const matrix::COO<T> &A,
503  const matrix::COO<T> &B);
504 
516 template <typename T>
517 [[nodiscard]] bool is_same_structure(const matrix::CRS<T> &A,
518  const matrix::CRS<T> &B);
519 
531 template <typename T>
532 [[nodiscard]] bool is_same_structure(const matrix::LinearOperator<T> &A,
533  const matrix::LinearOperator<T> &B);
534 
546 template <typename T, typename U>
547 [[nodiscard]] bool is_same_structure(const view_Dense<T, U> &A,
548  const matrix::Dense<U> &B);
549 
561 template <typename T, typename U>
562 [[nodiscard]] bool is_same_structure(const matrix::Dense<U> &A,
563  const view_Dense<T, U> &B);
564 
576 template <typename T, typename U, typename V>
577 [[nodiscard]] bool is_same_structure(const view_Dense<T, V> &A,
578  const view_Dense<U, V> &B);
579 
591 template <typename T>
592 [[nodiscard]] bool is_same_structure(const tensor::tensor_Dense<T> &A,
593  const tensor::tensor_Dense<T> &B);
594 
606 template <typename T>
607 [[nodiscard]] bool is_same_structure(const tensor::tensor_COO<T> &A,
608  const tensor::tensor_COO<T> &B);
609 
621 template <typename T, typename U>
622 [[nodiscard]] bool is_same_structure(const view_tensor_Dense<T, U> &A,
623  const tensor::tensor_Dense<U> &B);
624 
636 template <typename T, typename U>
637 [[nodiscard]] bool is_same_structure(const tensor::tensor_Dense<U> &A,
638  const view_tensor_Dense<T, U> &B);
639 
651 template <typename T, typename U, typename V>
652 [[nodiscard]] bool is_same_structure(const view_tensor_Dense<T, V> &A,
653  const view_tensor_Dense<U, V> &B);
654 
659 template <typename T, typename U, typename V, typename... types>
660 [[nodiscard]] bool is_same_structure(const T &A, const U &B, const V &C,
661  const types &...args) {
662  return is_same_structure(A, B) && is_same_structure(A, C, args...);
663 }
664 
676 template <typename T, typename U>
677 [[nodiscard]] bool is_same_size(const T &x, const U &y) {
678  return x.size() == y.size();
679 }
680 
692 template <typename T>
693 [[nodiscard]] bool is_same_size(const matrix::Dense<T> &A,
694  const matrix::Dense<T> &B);
695 
707 template <typename T>
708 [[nodiscard]] bool is_same_size(const matrix::COO<T> &A,
709  const matrix::COO<T> &B);
710 
722 template <typename T>
723 [[nodiscard]] bool is_same_size(const matrix::CRS<T> &A,
724  const matrix::CRS<T> &B);
725 
737 template <typename T>
738 [[nodiscard]] bool is_same_size(const matrix::LinearOperator<T> &A,
739  const matrix::LinearOperator<T> &B);
740 
752 template <typename T, typename U>
753 [[nodiscard]] bool is_same_size(const view_Dense<T, U> &A,
754  const matrix::Dense<U> &B);
766 template <typename T, typename U>
767 [[nodiscard]] bool is_same_size(const matrix::Dense<U> &A,
768  const view_Dense<T, U> &B);
780 template <typename T, typename U, typename V>
781 [[nodiscard]] bool is_same_size(const view_Dense<T, V> &A,
782  const view_Dense<U, V> &B);
783 
795 template <typename T>
796 [[nodiscard]] bool is_same_size(const tensor::tensor_Dense<T> &A,
797  const tensor::tensor_Dense<T> &B);
798 
810 template <typename T>
811 [[nodiscard]] bool is_same_size(const tensor::tensor_COO<T> &A,
812  const tensor::tensor_COO<T> &B);
813 
825 template <typename T, typename U>
826 [[nodiscard]] bool is_same_size(const view_tensor_Dense<T, U> &A,
827  const tensor::tensor_Dense<U> &B);
839 template <typename T, typename U>
840 [[nodiscard]] bool is_same_size(const tensor::tensor_Dense<U> &A,
841  const view_tensor_Dense<T, U> &B);
853 template <typename T, typename U, typename V>
854 [[nodiscard]] bool is_same_size(const view_tensor_Dense<T, V> &A,
855  const view_tensor_Dense<U, V> &B);
856 
861 template <typename T, typename U, typename V, typename... types>
862 [[nodiscard]] bool is_same_size(const T &arg1, const U &arg2, const V &arg3,
863  const types &...args) {
864  return is_same_size(arg1, arg2) && is_same_size(arg1, arg3, args...);
865 }
866 
875 template <typename T, typename U>
876 [[nodiscard]] bool is_same_device_mem_stat(const T &arg1, const U &arg2) {
877  return arg1.get_device_mem_stat() == arg2.get_device_mem_stat();
878 }
879 
888 template <typename T, typename U, typename... types>
889 [[nodiscard]] bool is_same_device_mem_stat(const T &arg1, const U &arg2,
890  const types &...args) {
891  return is_same_device_mem_stat(arg1, arg2) &&
892  is_same_device_mem_stat(arg1, args...);
893 }
894 
895 // create matrix //////////////////
896 
910 template <typename T>
911 [[nodiscard]] matrix::COO<T> band_matrix(const int M, const int N, const int W,
912  const T diag_val, const T val);
913 
928 template <typename T>
929 [[nodiscard]] matrix::COO<T> asym_band_matrix(const int M, const int N,
930  const int W, const T diag_val,
931  const T Uval, const T Lval);
932 
945 template <typename T>
946 [[nodiscard]] matrix::COO<T> random_structure_matrix(const int M, const int N,
947  const int nnzrow,
948  const T val);
949 
962 template <typename T>
963 [[nodiscard]] tensor::tensor_COO<T>
964 random_structure_tensor(const size_t M, const size_t N, const size_t nnzrow,
965  const T val);
966 
980 template <typename T>
981 [[nodiscard]] tensor::tensor_COO<T>
982 random_structure_tensor(const size_t M, const size_t N, const size_t L,
983  const size_t nnzrow, const T val);
984 
998 template <typename T>
999 [[nodiscard]] tensor::tensor_COO<T>
1000 random_structure_tensor(const size_t K, const size_t M, const size_t N,
1001  const size_t L, const size_t nnzrow, const T val);
1002 
1012 template <typename T> [[nodiscard]] matrix::COO<T> eye(const int M);
1013 
1023 template <typename T> [[nodiscard]] matrix::COO<T> frank_matrix(const int &M);
1024 
1035 template <typename T>
1036 [[nodiscard]] T frank_matrix_eigenvalue(const int &M, const int &N);
1037 
1049 template <typename T>
1050 [[nodiscard]] matrix::COO<T> tridiagonal_toeplitz_matrix(const int &M, T a,
1051  T b);
1052 
1065 template <typename T>
1066 [[nodiscard]] T tridiagonal_toeplitz_matrix_eigenvalue(const int &M, int N, T a,
1067  T b);
1068 
1078 template <typename T>
1079 [[nodiscard]] matrix::COO<T> laplacian_matrix_1D(const int &M);
1080 
1091 template <typename T>
1092 [[nodiscard]] T laplacian_matrix_1D_eigenvalue(const int &M, int N);
1093 
1105 template <typename T>
1106 [[nodiscard]] matrix::COO<T> laplacian_matrix_2D_5p(const int M, const int N);
1107 
1122 template <typename T>
1123 [[nodiscard]] matrix::COO<T> toeplitz_plus_hankel_matrix(const int &M, T a0,
1124  T a1, T a2);
1125 
1139 template <typename T>
1140 [[nodiscard]] T toeplitz_plus_hankel_matrix_eigenvalue(const int &M, int N,
1141  T a0, T a1, T a2, T b0,
1142  T b1, T b2);
1143 
1144 // send///////////////////
1145 
1150 template <typename T> void send(T &x) { x.send(); }
1151 
1156 template <typename T, typename... Types> void send(T &x, Types &...args) {
1157  x.send();
1158  send(args...);
1159 }
1160 
1161 // recv///////////////////
1166 template <typename T> void recv(T &x) { x.recv(); }
1167 
1172 template <typename T, typename... Types> void recv(T &x, Types &...args) {
1173  x.recv();
1174  recv(args...);
1175 }
1176 
1177 // device_free///////////////////
1178 
1183 template <typename T> void device_free(T &x) { x.device_free(); }
1184 
1189 template <typename T, typename... Types>
1190 void device_free(T &x, Types &...args) {
1191  x.device_free();
1192  device_free(args...);
1193 }
1194 
1199 [[nodiscard]] bool build_with_avx();
1200 
1205 [[nodiscard]] bool build_with_avx2();
1206 
1211 [[nodiscard]] bool build_with_avx512();
1212 
1217 [[nodiscard]] bool build_with_mpi();
1218 
1223 [[nodiscard]] bool build_with_gpu();
1224 
1229 [[nodiscard]] bool build_with_mkl();
1230 
1236 [[nodiscard]] bool build_with_lapack();
1237 
1243 [[nodiscard]] bool build_with_cblas();
1246 } // namespace util
1247 } // 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
size_t size() const
get view1D size (range)
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