monolish  0.14.0
MONOlithic LIner equation Solvers for Highly-parallel architecture
dense_constructor.cpp
Go to the documentation of this file.
1 #include "../../include/common/monolish_dense.hpp"
2 #include "../../include/common/monolish_logger.hpp"
3 #include "../../include/common/monolish_matrix.hpp"
4 #include "../../include/common/monolish_vector.hpp"
5 #include "../internal/monolish_internal.hpp"
6 
7 namespace monolish {
8 namespace matrix {
9 
10 // matrix constructor ///
11 
12 template <typename T> Dense<T>::Dense(const size_t M, const size_t N) {
13  Logger &logger = Logger::get_instance();
14  logger.util_in(monolish_func);
15  set_row(M);
16  set_col(N);
17  set_nnz(N * M);
18 
19  val.resize(nnz);
20  logger.util_out();
21 }
22 template Dense<double>::Dense(const size_t M, const size_t N);
23 template Dense<float>::Dense(const size_t M, const size_t N);
24 
25 template <typename T>
26 Dense<T>::Dense(const size_t M, const size_t N, const T *value) {
27  Logger &logger = Logger::get_instance();
28  logger.util_in(monolish_func);
29  set_row(M);
30  set_col(N);
31  set_nnz(M * N);
32 
33  val.resize(nnz);
34  std::copy(value, value + nnz, val.begin());
35  logger.util_out();
36 }
37 template Dense<double>::Dense(const size_t M, const size_t N,
38  const double *value);
39 template Dense<float>::Dense(const size_t M, const size_t N,
40  const float *value);
41 
42 template <typename T>
43 Dense<T>::Dense(const size_t M, const size_t N, const std::vector<T> &value) {
44  Logger &logger = Logger::get_instance();
45  logger.util_in(monolish_func);
46  set_row(M);
47  set_col(N);
48  set_nnz(M * N);
49 
50  val.resize(nnz);
51  std::copy(value.begin(), value.end(), val.begin());
52  logger.util_out();
53 }
54 template Dense<double>::Dense(const size_t M, const size_t N,
55  const std::vector<double> &value);
56 template Dense<float>::Dense(const size_t M, const size_t N,
57  const std::vector<float> &value);
58 
59 template <typename T>
60 Dense<T>::Dense(const size_t M, const size_t N, const vector<T> &value) {
61  Logger &logger = Logger::get_instance();
62  logger.util_in(monolish_func);
63  set_row(M);
64  set_col(N);
65  set_nnz(M * N);
66 
67  val.resize(nnz);
68  std::copy(value.data(), value.data() + nnz, val.begin());
69 
70  if (value.get_device_mem_stat() == true) {
71 #if MONOLISH_USE_GPU
72  send();
73  const T *data = value.data();
74  T *vald = val.data();
75 #pragma omp target teams distribute parallel for
76  for (size_t i = 0; i < get_nnz(); i++) {
77  vald[i] = data[i];
78  }
79 #else
80  throw std::runtime_error(
81  "error USE_GPU is false, but get_device_mem_stat() == true");
82 #endif
83  }
84 
85  logger.util_out();
86 }
87 template Dense<double>::Dense(const size_t M, const size_t N,
88  const vector<double> &value);
89 template Dense<float>::Dense(const size_t M, const size_t N,
90  const vector<float> &value);
91 
92 template <typename T>
93 Dense<T>::Dense(const size_t M, const size_t N,
94  const std::initializer_list<T> &list) {
95  Logger &logger = Logger::get_instance();
96  logger.util_in(monolish_func);
97  set_row(M);
98  set_col(N);
99  set_nnz(M * N);
100 
101  val.resize(nnz);
102  std::copy(list.begin(), list.end(), val.begin());
103  logger.util_out();
104 }
105 template Dense<double>::Dense(const size_t M, const size_t N,
106  const std::initializer_list<double> &list);
107 template Dense<float>::Dense(const size_t M, const size_t N,
108  const std::initializer_list<float> &list);
109 
110 template <typename T>
111 Dense<T>::Dense(const size_t M, const size_t N, const T min, const T max) {
112  Logger &logger = Logger::get_instance();
113  logger.util_in(monolish_func);
114  set_row(M);
115  set_col(N);
116  set_nnz(M * N);
117 
118  val.resize(nnz);
119 
120  std::random_device random;
121  std::mt19937 mt(random());
122  std::uniform_real_distribution<> rand(min, max);
123 
124 #pragma omp parallel for
125  for (size_t i = 0; i < val.size(); i++) {
126  val[i] = rand(mt);
127  }
128  logger.util_out();
129 }
130 template Dense<double>::Dense(const size_t M, const size_t N, const double min,
131  const double max);
132 template Dense<float>::Dense(const size_t M, const size_t N, const float min,
133  const float max);
134 
135 template <typename T>
136 Dense<T>::Dense(const size_t M, const size_t N, const T value) {
137  Logger &logger = Logger::get_instance();
138  logger.util_in(monolish_func);
139  set_row(M);
140  set_col(N);
141  set_nnz(M * N);
142 
143  val.resize(nnz);
144 
145 #pragma omp parallel for
146  for (size_t i = 0; i < val.size(); i++) {
147  val[i] = value;
148  }
149  logger.util_out();
150 }
151 template Dense<double>::Dense(const size_t M, const size_t N,
152  const double value);
153 template Dense<float>::Dense(const size_t M, const size_t N, const float value);
154 
155 // copy constructor///////////////////////////////
156 template <typename T> Dense<T>::Dense(const Dense<T> &mat) {
157  Logger &logger = Logger::get_instance();
158  logger.util_in(monolish_func);
159 
160  val.resize(mat.get_nnz());
161 
162  rowN = mat.get_row();
163  colN = mat.get_col();
164  nnz = mat.get_nnz();
165 
166 #if MONOLISH_USE_GPU
167  if (mat.get_device_mem_stat()) {
168  send();
169  internal::vcopy(get_nnz(), mat.val.data(), val.data(), true);
170  }
171 #endif
172  internal::vcopy(get_nnz(), mat.val.data(), val.data(), false);
173 
174  logger.util_out();
175 }
176 template Dense<double>::Dense(const Dense<double> &mat);
177 template Dense<float>::Dense(const Dense<float> &mat);
178 
179 } // namespace matrix
180 } // namespace monolish
monolish::matrix::Dense::Dense
Dense()
Definition: monolish_dense.hpp:49
monolish_func
#define monolish_func
Definition: monolish_logger.hpp:9
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],...
Definition: matrix_vml.cpp:390
monolish::Logger
logger class (singleton, for developper class)
Definition: monolish_logger.hpp:19
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],...
Definition: matrix_vml.cpp:382
monolish::Logger::util_out
void util_out()
Definition: logger_utils.cpp:123
monolish::Logger::util_in
void util_in(const std::string func_name)
Definition: logger_utils.cpp:113
monolish::blas::copy
void copy(const matrix::Dense< double > &A, matrix::Dense< double > &C)
Dense matrix copy (C=A)
Definition: dense_copy.cpp:25
monolish
Definition: monolish_matrix_blas.hpp:9
monolish::util::send
auto send(T &x)
send data to GPU
Definition: monolish_common.hpp:598
monolish::Logger::get_instance
static Logger & get_instance()
Definition: monolish_logger.hpp:42