monolish  0.16.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 {
27 namespace util {
40 
46 bool set_default_device(size_t device_num);
47 
55 
73  const vector<double> &y);
75  const view1D<vector<double>, double> &y);
77  const view1D<matrix::Dense<double>, double> &y);
79  const view1D<vector<double>, double> &x,
80  const vector<double> &y);
82  const view1D<vector<double>, double> &x,
83  const view1D<vector<double>, double> &y);
85  const view1D<vector<double>, double> &x,
86  const view1D<matrix::Dense<double>, double> &y);
88  const view1D<matrix::Dense<double>, double> &x,
89  const vector<double> &y);
91  const view1D<matrix::Dense<double>, double> &x,
92  const view1D<vector<double>, double> &y);
94  const view1D<matrix::Dense<double>, double> &x,
95  const view1D<matrix::Dense<double>, double> &y);
97  const vector<float> &y);
99  const view1D<vector<float>, float> &y);
101  const view1D<matrix::Dense<float>, float> &y);
103  const view1D<vector<float>, float> &x,
104  const vector<float> &y);
106  const view1D<vector<float>, float> &x,
107  const view1D<vector<float>, float> &y);
109  const view1D<vector<float>, float> &x,
110  const view1D<matrix::Dense<float>, float> &y);
112  const view1D<matrix::Dense<float>, float> &x,
113  const vector<float> &y);
115  const view1D<matrix::Dense<float>, float> &x,
116  const view1D<vector<float>, float> &y);
118  const view1D<matrix::Dense<float>, float> &x,
119  const view1D<matrix::Dense<float>, float> &y);
120 
133  const vector<double> &y);
135  const view1D<vector<double>, double> &y);
137  const view1D<matrix::Dense<double>, double> &y);
139  const view1D<vector<double>, double> &x,
140  const vector<double> &y);
142  const view1D<vector<double>, double> &x,
143  const view1D<vector<double>, double> &y);
145  const view1D<vector<double>, double> &x,
146  const view1D<matrix::Dense<double>, double> &y);
148  const view1D<matrix::Dense<double>, double> &x,
149  const vector<double> &y);
151  const view1D<matrix::Dense<double>, double> &x,
152  const view1D<vector<double>, double> &y);
154  const view1D<matrix::Dense<double>, double> &x,
155  const view1D<matrix::Dense<double>, double> &y);
157  const vector<float> &y);
159  const view1D<vector<float>, float> &y);
161  const view1D<matrix::Dense<float>, float> &y);
163  const view1D<vector<float>, float> &x,
164  const vector<float> &y);
166  const view1D<vector<float>, float> &x,
167  const view1D<vector<float>, float> &y);
169  const view1D<vector<float>, float> &x,
170  const view1D<matrix::Dense<float>, float> &y);
172  const view1D<matrix::Dense<float>, float> &x,
173  const vector<float> &y);
175  const view1D<matrix::Dense<float>, float> &x,
176  const view1D<vector<float>, float> &y);
178  const view1D<matrix::Dense<float>, float> &x,
179  const view1D<matrix::Dense<float>, float> &y);
180 
193  const vector<double> &x, const vector<double> &y);
195  const vector<double> &x,
196  const view1D<vector<double>, double> &y);
198  const vector<double> &x,
199  const view1D<matrix::Dense<double>, double> &y);
201  const view1D<vector<double>, double> &x,
202  const vector<double> &y);
204  const view1D<vector<double>, double> &x,
205  const view1D<vector<double>, double> &y);
207  const view1D<vector<double>, double> &x,
208  const view1D<matrix::Dense<double>, double> &y);
210  const view1D<matrix::Dense<double>, double> &x,
211  const vector<double> &y);
213  const view1D<matrix::Dense<double>, double> &x,
214  const view1D<vector<double>, double> &y);
216  const view1D<matrix::Dense<double>, double> &x,
217  const view1D<matrix::Dense<double>, double> &y);
219  const vector<float> &x, const vector<float> &y);
221  const vector<float> &x,
222  const view1D<vector<float>, float> &y);
224  const vector<float> &x,
225  const view1D<matrix::Dense<float>, float> &y);
227  const view1D<vector<float>, float> &x,
228  const vector<float> &y);
230  const view1D<vector<float>, float> &x,
231  const view1D<vector<float>, float> &y);
233  const view1D<vector<float>, float> &x,
234  const view1D<matrix::Dense<float>, float> &y);
236  const view1D<matrix::Dense<float>, float> &x,
237  const vector<float> &y);
239  const view1D<matrix::Dense<float>, float> &x,
240  const view1D<vector<float>, float> &y);
242  const view1D<matrix::Dense<float>, float> &x,
243  const view1D<matrix::Dense<float>, float> &y);
258 [[nodiscard]] bool solver_check(const int err);
259 
260 // logger
261 
273 void set_log_level(const size_t Level);
274 
280 void set_log_filename(const std::string filename);
281 
282 // create typical data///////////////////////////
283 
296 template <typename T>
297 void random_vector(vector<T> &vec, const T min, const T max);
298 
312 template <typename T>
313 void random_vector(vector<T> &vec, const T min, const T max,
314  const std::uint32_t seed);
315 
316 // is_same //////////////////
317 
322 template <typename T, typename U>
323 [[nodiscard]] bool is_same_structure(const T A, const U B) {
324  return false;
325 }
326 
338 template <typename T>
339 [[nodiscard]] bool is_same_structure(const vector<T> &x, const vector<T> &y) {
340  return x.size() == y.size();
341 }
342 
354 template <typename T>
355 [[nodiscard]] bool is_same_structure(const matrix::Dense<T> &A,
356  const matrix::Dense<T> &B);
357 
369 template <typename T>
370 [[nodiscard]] bool is_same_structure(const matrix::COO<T> &A,
371  const matrix::COO<T> &B);
372 
384 template <typename T>
385 [[nodiscard]] bool is_same_structure(const matrix::CRS<T> &A,
386  const matrix::CRS<T> &B);
387 
399 template <typename T>
401  const matrix::LinearOperator<T> &B);
402 
407 template <typename T, typename... types>
408 [[nodiscard]] bool is_same_structure(const T &A, const T &B,
409  const types &...args) {
410  return is_same_structure(A, B) && is_same_structure(A, args...);
411 }
412 
424 template <typename T, typename U>
425 [[nodiscard]] bool is_same_size(const T &x, const U &y) {
426  return x.size() == y.size();
427 }
428 
440 template <typename T>
441 [[nodiscard]] bool is_same_size(const matrix::Dense<T> &A,
442  const matrix::Dense<T> &B);
443 
455 template <typename T>
456 [[nodiscard]] bool is_same_size(const matrix::COO<T> &A,
457  const matrix::COO<T> &B);
458 
470 template <typename T>
471 [[nodiscard]] bool is_same_size(const matrix::CRS<T> &A,
472  const matrix::CRS<T> &B);
473 
485 template <typename T>
486 [[nodiscard]] bool is_same_size(const matrix::LinearOperator<T> &A,
487  const matrix::LinearOperator<T> &B);
488 
493 template <typename T, typename U, typename... types>
494 [[nodiscard]] bool is_same_size(const T &arg1, const U &arg2,
495  const types &...args) {
496  return is_same_size(arg1, arg2) && is_same_size(arg1, args...);
497 }
498 
507 template <typename T, typename U>
508 [[nodiscard]] bool is_same_device_mem_stat(const T &arg1, const U &arg2) {
509  return arg1.get_device_mem_stat() == arg2.get_device_mem_stat();
510 }
511 
520 template <typename T, typename U, typename... types>
521 [[nodiscard]] bool is_same_device_mem_stat(const T &arg1, const U &arg2,
522  const types &...args) {
523  return is_same_device_mem_stat(arg1, arg2) &&
524  is_same_device_mem_stat(arg1, args...);
525 }
526 
527 // create matrix //////////////////
528 
542 template <typename T>
543 [[nodiscard]] matrix::COO<T> band_matrix(const int M, const int N, const int W,
544  const T diag_val, const T val);
545 
560 template <typename T>
561 [[nodiscard]] matrix::COO<T> asym_band_matrix(const int M, const int N,
562  const int W, const T diag_val,
563  const T Uval, const T Lval);
564 
577 template <typename T>
578 [[nodiscard]] matrix::COO<T> random_structure_matrix(const int M, const int N,
579  const int nnzrow,
580  const T val);
581 
591 template <typename T> [[nodiscard]] matrix::COO<T> eye(const int M);
592 
602 template <typename T> [[nodiscard]] matrix::COO<T> frank_matrix(const int &M);
603 
614 template <typename T>
615 [[nodiscard]] T frank_matrix_eigenvalue(const int &M, const int &N);
616 
628 template <typename T>
629 [[nodiscard]] matrix::COO<T> tridiagonal_toeplitz_matrix(const int &M, T a,
630  T b);
631 
644 template <typename T>
645 [[nodiscard]] T tridiagonal_toeplitz_matrix_eigenvalue(const int &M, int N, T a,
646  T b);
647 
657 template <typename T>
658 [[nodiscard]] matrix::COO<T> laplacian_matrix_1D(const int &M);
659 
670 template <typename T>
671 [[nodiscard]] T laplacian_matrix_1D_eigenvalue(const int &M, int N);
672 
684 template <typename T>
685 [[nodiscard]] matrix::COO<T> laplacian_matrix_2D_5p(const int M, const int N);
686 
701 template <typename T>
702 [[nodiscard]] matrix::COO<T> toeplitz_plus_hankel_matrix(const int &M, T a0,
703  T a1, T a2);
704 
718 template <typename T>
719 [[nodiscard]] T toeplitz_plus_hankel_matrix_eigenvalue(const int &M, int N,
720  T a0, T a1, T a2, T b0,
721  T b1, T b2);
722 
723 // send///////////////////
724 
729 template <typename T> void send(T &x) { x.send(); }
730 
735 template <typename T, typename... Types> void send(T &x, Types &...args) {
736  x.send();
737  send(args...);
738 }
739 
740 // recv///////////////////
745 template <typename T> void recv(T &x) { x.recv(); }
746 
751 template <typename T, typename... Types> void recv(T &x, Types &...args) {
752  x.recv();
753  recv(args...);
754 }
755 
756 // device_free///////////////////
757 
762 template <typename T> void device_free(T &x) { x.device_free(); }
763 
768 template <typename T, typename... Types>
769 void device_free(T &x, Types &...args) {
770  x.device_free();
771  device_free(args...);
772 }
773 
778 [[nodiscard]] bool build_with_avx();
779 
784 [[nodiscard]] bool build_with_avx2();
785 
790 [[nodiscard]] bool build_with_avx512();
791 
796 [[nodiscard]] bool build_with_mpi();
797 
802 [[nodiscard]] bool build_with_gpu();
803 
808 [[nodiscard]] bool build_with_mkl();
809 
815 [[nodiscard]] bool build_with_lapack();
816 
822 [[nodiscard]] bool build_with_cblas();
825 } // namespace util
826 } // 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
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