monolish  0.14.0
MONOlithic LIner equation Solvers for Highly-parallel architecture
crs_getvec.cpp
Go to the documentation of this file.
1 #include "../../../include/monolish_blas.hpp"
2 #include "../../internal/monolish_internal.hpp"
3 
4 namespace monolish {
5 namespace matrix {
6 
7 // diag
8 template <typename T> void CRS<T>::diag(vector<T> &vec) const {
9  Logger &logger = Logger::get_instance();
10  logger.func_in(monolish_func);
11 
12  size_t n = get_row() < get_col() ? rowN : colN;
13  T *vecd = vec.data();
14 
15  assert(n == vec.size());
16  assert(get_device_mem_stat() == vec.get_device_mem_stat());
17 
18  const T *vald = val.data();
19  const int *rowd = row_ptr.data();
20  const int *cold = col_ind.data();
21 
22  if (gpu_status == true) {
23 #if MONOLISH_USE_GPU // gpu
24 #pragma omp target teams distribute parallel for
25  for (size_t i = 0; i < n; i++) {
26  vecd[i] = 0;
27  }
28 #pragma omp target teams distribute parallel for
29  for (size_t i = 0; i < n; i++) {
30  for (int j = rowd[i]; j < rowd[i + 1]; j++) {
31  if ((int)i == cold[j]) {
32  vecd[i] = vald[j];
33  }
34  }
35  }
36 #else
37  throw std::runtime_error("error USE_GPU is false, but gpu_status == true");
38 #endif
39  } else {
40 #pragma omp parallel for
41  for (size_t i = 0; i < n; i++) {
42  vecd[i] = 0;
43  }
44 #pragma omp parallel for
45  for (size_t i = 0; i < n; i++) {
46  for (int j = rowd[i]; j < rowd[i + 1]; j++) {
47  if ((int)i == cold[j]) {
48  vecd[i] = vald[j];
49  }
50  }
51  }
52  }
53 
54  logger.func_out();
55 }
56 template void monolish::matrix::CRS<double>::diag(vector<double> &vec) const;
57 template void monolish::matrix::CRS<float>::diag(vector<float> &vec) const;
58 
59 template <typename T> void CRS<T>::diag(view1D<vector<T>, T> &vec) const {
60  Logger &logger = Logger::get_instance();
61  logger.func_in(monolish_func);
62 
63  size_t n = get_row() < get_col() ? rowN : colN;
64  T *vecd = vec.data();
65 
66  assert(n == vec.size());
67  assert(get_device_mem_stat() == vec.get_device_mem_stat());
68 
69  const T *vald = val.data();
70  const int *rowd = row_ptr.data();
71  const int *cold = col_ind.data();
72 
73  if (gpu_status == true) {
74 #if MONOLISH_USE_GPU // gpu
75 #pragma omp target teams distribute parallel for
76  for (size_t i = 0; i < n; i++) {
77  vecd[i] = 0;
78  }
79 #pragma omp target teams distribute parallel for
80  for (size_t i = 0; i < n; i++) {
81  for (int j = rowd[i]; j < rowd[i + 1]; j++) {
82  if ((int)i == cold[j]) {
83  vecd[i] = vald[j];
84  }
85  }
86  }
87 #else
88  throw std::runtime_error("error USE_GPU is false, but gpu_status == true");
89 #endif
90  } else {
91 #pragma omp parallel for
92  for (size_t i = 0; i < n; i++) {
93  vecd[i] = 0;
94  }
95 #pragma omp parallel for
96  for (size_t i = 0; i < n; i++) {
97  for (int j = rowd[i]; j < rowd[i + 1]; j++) {
98  if ((int)i == cold[j]) {
99  vecd[i] = vald[j];
100  }
101  }
102  }
103  }
104 
105  logger.func_out();
106 }
107 template void
108 monolish::matrix::CRS<double>::diag(view1D<vector<double>, double> &vec) const;
109 template void
110 monolish::matrix::CRS<float>::diag(view1D<vector<float>, float> &vec) const;
111 
112 template <typename T>
113 void CRS<T>::diag(view1D<matrix::Dense<T>, T> &vec) const {
114  Logger &logger = Logger::get_instance();
115  logger.func_in(monolish_func);
116 
117  size_t n = get_row() < get_col() ? rowN : colN;
118  T *vecd = vec.data();
119 
120  assert(n == vec.size());
121  assert(get_device_mem_stat() == vec.get_device_mem_stat());
122 
123  const T *vald = val.data();
124  const int *rowd = row_ptr.data();
125  const int *cold = col_ind.data();
126 
127  if (gpu_status == true) {
128 #if MONOLISH_USE_GPU // gpu
129 #pragma omp target teams distribute parallel for
130  for (size_t i = 0; i < n; i++) {
131  vecd[i] = 0;
132  }
133 #pragma omp target teams distribute parallel for
134  for (size_t i = 0; i < n; i++) {
135  for (int j = rowd[i]; j < rowd[i + 1]; j++) {
136  if ((int)i == cold[j]) {
137  vecd[i] = vald[j];
138  }
139  }
140  }
141 #else
142  throw std::runtime_error("error USE_GPU is false, but gpu_status == true");
143 #endif
144  } else {
145 #pragma omp parallel for
146  for (size_t i = 0; i < n; i++) {
147  vecd[i] = 0;
148  }
149 #pragma omp parallel for
150  for (size_t i = 0; i < n; i++) {
151  for (int j = rowd[i]; j < rowd[i + 1]; j++) {
152  if ((int)i == cold[j]) {
153  vecd[i] = vald[j];
154  }
155  }
156  }
157  }
158 
159  logger.func_out();
160 }
162  view1D<matrix::Dense<double>, double> &vec) const;
164  view1D<matrix::Dense<float>, float> &vec) const;
165 
166 // row
167 template <typename T> void CRS<T>::row(const size_t r, vector<T> &vec) const {
168  Logger &logger = Logger::get_instance();
169  logger.func_in(monolish_func);
170 
171  size_t n = get_row();
172  T *vecd = vec.data();
173 
174  const T *vald = val.data();
175  const int *rowd = row_ptr.data();
176 
177  assert(n == vec.size());
178  assert(get_device_mem_stat() == vec.get_device_mem_stat());
179 
180  if (gpu_status == true) {
181 #if MONOLISH_USE_GPU // gpu
182  const int *indexd = col_ind.data();
183 
184 #pragma omp target teams distribute parallel for
185  for (size_t i = 0; i < n; i++) {
186  vecd[i] = 0;
187  }
188 #pragma omp target teams distribute parallel for
189  for (int j = rowd[r]; j < rowd[r + 1]; j++) {
190  vecd[indexd[j]] = vald[j];
191  }
192 #else
193  throw std::runtime_error("error USE_GPU is false, but gpu_status == true");
194 #endif
195  } else {
196 #pragma omp parallel for
197  for (size_t i = 0; i < n; i++) {
198  vecd[i] = 0;
199  }
200 #pragma omp parallel for
201  for (int j = rowd[r]; j < rowd[r + 1]; j++) {
202  vecd[col_ind[j]] = vald[j];
203  }
204  }
205 
206  logger.func_out();
207 }
208 template void monolish::matrix::CRS<double>::row(const size_t r,
209  vector<double> &vec) const;
210 template void monolish::matrix::CRS<float>::row(const size_t r,
211  vector<float> &vec) const;
212 
213 template <typename T>
214 void CRS<T>::row(const size_t r, view1D<vector<T>, T> &vec) const {
215  Logger &logger = Logger::get_instance();
216  logger.func_in(monolish_func);
217 
218  size_t n = get_row();
219  T *vecd = vec.data();
220 
221  const T *vald = val.data();
222  const int *rowd = row_ptr.data();
223 
224  assert(n == vec.size());
225  assert(get_device_mem_stat() == vec.get_device_mem_stat());
226 
227  if (gpu_status == true) {
228 #if MONOLISH_USE_GPU // gpu
229  const int *indexd = col_ind.data();
230 
231 #pragma omp target teams distribute parallel for
232  for (size_t i = 0; i < n; i++) {
233  vecd[i] = 0;
234  }
235 #pragma omp target teams distribute parallel for
236  for (int j = rowd[r]; j < rowd[r + 1]; j++) {
237  vecd[indexd[j]] = vald[j];
238  }
239 #else
240  throw std::runtime_error("error USE_GPU is false, but gpu_status == true");
241 #endif
242  } else {
243 #pragma omp parallel for
244  for (size_t i = 0; i < n; i++) {
245  vecd[i] = 0;
246  }
247 #pragma omp parallel for
248  for (int j = rowd[r]; j < rowd[r + 1]; j++) {
249  vecd[col_ind[j]] = vald[j];
250  }
251  }
252 
253  logger.func_out();
254 }
255 template void
257  view1D<vector<double>, double> &vec) const;
258 template void
259 monolish::matrix::CRS<float>::row(const size_t r,
260  view1D<vector<float>, float> &vec) const;
261 
262 template <typename T>
263 void CRS<T>::row(const size_t r, view1D<matrix::Dense<T>, T> &vec) const {
264  Logger &logger = Logger::get_instance();
265  logger.func_in(monolish_func);
266 
267  size_t n = get_row();
268  T *vecd = vec.data();
269 
270  const T *vald = val.data();
271  const int *rowd = row_ptr.data();
272 
273  assert(n == vec.size());
274  assert(get_device_mem_stat() == vec.get_device_mem_stat());
275 
276  if (gpu_status == true) {
277 #if MONOLISH_USE_GPU // gpu
278  const int *indexd = col_ind.data();
279 
280 #pragma omp target teams distribute parallel for
281  for (size_t i = 0; i < n; i++) {
282  vecd[i] = 0;
283  }
284 #pragma omp target teams distribute parallel for
285  for (int j = rowd[r]; j < rowd[r + 1]; j++) {
286  vecd[indexd[j]] = vald[j];
287  }
288 #else
289  throw std::runtime_error("error USE_GPU is false, but gpu_status == true");
290 #endif
291  } else {
292 #pragma omp parallel for
293  for (size_t i = 0; i < n; i++) {
294  vecd[i] = 0;
295  }
296 #pragma omp parallel for
297  for (int j = rowd[r]; j < rowd[r + 1]; j++) {
298  vecd[col_ind[j]] = vald[j];
299  }
300  }
301 
302  logger.func_out();
303 }
305  const size_t r, view1D<matrix::Dense<double>, double> &vec) const;
307  const size_t r, view1D<matrix::Dense<float>, float> &vec) const;
308 
309 // col
310 template <typename T> void CRS<T>::col(const size_t c, vector<T> &vec) const {
311  Logger &logger = Logger::get_instance();
312  logger.func_in(monolish_func);
313 
314  size_t n = get_col();
315  T *vecd = vec.data();
316 
317  const T *vald = val.data();
318  const int *rowd = row_ptr.data();
319  const int *cold = col_ind.data();
320 
321  assert(n == vec.size());
322  assert(get_device_mem_stat() == vec.get_device_mem_stat());
323 
324  if (gpu_status == true) {
325 #if MONOLISH_USE_GPU // gpu
326 #pragma omp target teams distribute parallel for
327  for (size_t i = 0; i < n; i++) {
328  vecd[i] = 0;
329  }
330 #pragma omp target teams distribute parallel for
331  for (size_t i = 0; i < n; i++) {
332  for (int j = rowd[i]; j < rowd[i + 1]; j++) {
333  if ((int)c == cold[j]) {
334  vecd[i] = vald[j];
335  }
336  }
337  }
338 #else
339  throw std::runtime_error("error USE_GPU is false, but gpu_status == true");
340 #endif
341  } else {
342 #pragma omp parallel for
343  for (size_t i = 0; i < n; i++) {
344  vecd[i] = 0;
345  }
346 #pragma omp parallel for
347  for (size_t i = 0; i < n; i++) {
348  for (int j = rowd[i]; j < rowd[i + 1]; j++) {
349  if ((int)c == cold[j]) {
350  vecd[i] = vald[j];
351  }
352  }
353  }
354  }
355 
356  logger.func_out();
357 }
358 template void monolish::matrix::CRS<double>::col(const size_t c,
359  vector<double> &vec) const;
360 template void monolish::matrix::CRS<float>::col(const size_t c,
361  vector<float> &vec) const;
362 
363 template <typename T>
364 void CRS<T>::col(const size_t c, view1D<vector<T>, T> &vec) const {
365  Logger &logger = Logger::get_instance();
366  logger.func_in(monolish_func);
367 
368  size_t n = get_col();
369  T *vecd = vec.data();
370 
371  const T *vald = val.data();
372  const int *rowd = row_ptr.data();
373  const int *cold = col_ind.data();
374 
375  assert(n == vec.size());
376  assert(get_device_mem_stat() == vec.get_device_mem_stat());
377 
378  if (gpu_status == true) {
379 #if MONOLISH_USE_GPU // gpu
380 #pragma omp target teams distribute parallel for
381  for (size_t i = 0; i < n; i++) {
382  vecd[i] = 0;
383  }
384 #pragma omp target teams distribute parallel for
385  for (size_t i = 0; i < n; i++) {
386  for (int j = rowd[i]; j < rowd[i + 1]; j++) {
387  if ((int)c == cold[j]) {
388  vecd[i] = vald[j];
389  }
390  }
391  }
392 #else
393  throw std::runtime_error("error USE_GPU is false, but gpu_status == true");
394 #endif
395  } else {
396 #pragma omp parallel for
397  for (size_t i = 0; i < n; i++) {
398  vecd[i] = 0;
399  }
400 #pragma omp parallel for
401  for (size_t i = 0; i < n; i++) {
402  for (int j = rowd[i]; j < rowd[i + 1]; j++) {
403  if ((int)c == cold[j]) {
404  vecd[i] = vald[j];
405  }
406  }
407  }
408  }
409 
410  logger.func_out();
411 }
412 template void
414  view1D<vector<double>, double> &vec) const;
415 template void
416 monolish::matrix::CRS<float>::col(const size_t c,
417  view1D<vector<float>, float> &vec) const;
418 
419 template <typename T>
420 void CRS<T>::col(const size_t c, view1D<matrix::Dense<T>, T> &vec) const {
421  Logger &logger = Logger::get_instance();
422  logger.func_in(monolish_func);
423 
424  size_t n = get_col();
425  T *vecd = vec.data();
426 
427  const T *vald = val.data();
428  const int *rowd = row_ptr.data();
429  const int *cold = col_ind.data();
430 
431  assert(n == vec.size());
432  assert(get_device_mem_stat() == vec.get_device_mem_stat());
433 
434  if (gpu_status == true) {
435 #if MONOLISH_USE_GPU // gpu
436 #pragma omp target teams distribute parallel for
437  for (size_t i = 0; i < n; i++) {
438  vecd[i] = 0;
439  }
440 #pragma omp target teams distribute parallel for
441  for (size_t i = 0; i < n; i++) {
442  for (int j = rowd[i]; j < rowd[i + 1]; j++) {
443  if ((int)c == cold[j]) {
444  vecd[i] = vald[j];
445  }
446  }
447  }
448 #else
449  throw std::runtime_error("error USE_GPU is false, but gpu_status == true");
450 #endif
451  } else {
452 #pragma omp parallel for
453  for (size_t i = 0; i < n; i++) {
454  vecd[i] = 0;
455  }
456 #pragma omp parallel for
457  for (size_t i = 0; i < n; i++) {
458  for (int j = rowd[i]; j < rowd[i + 1]; j++) {
459  if ((int)c == cold[j]) {
460  vecd[i] = vald[j];
461  }
462  }
463  }
464  }
465 
466  logger.func_out();
467 }
469  const size_t c, view1D<matrix::Dense<double>, double> &vec) const;
471  const size_t c, view1D<matrix::Dense<float>, float> &vec) const;
472 
473 } // namespace matrix
474 } // namespace monolish
monolish_func
#define monolish_func
Definition: monolish_logger.hpp:9
monolish::matrix::CRS::col
void col(const size_t c, vector< Float > &vec) const
get column vector
monolish::matrix::CRS::row
void row(const size_t r, vector< Float > &vec) const
get row vector
monolish
Definition: monolish_matrix_blas.hpp:9
monolish::matrix::CRS::diag
void diag(vector< Float > &vec) const
get diag. vector
monolish::Logger::get_instance
static Logger & get_instance()
Definition: monolish_logger.hpp:42