monolish  0.14.0
MONOlithic LIner equation Solvers for Highly-parallel architecture
axpy.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 namespace monolish {
4 
5 namespace {
6 template <typename F1, typename F2, typename F3>
7 void Daxpy_core(const F1 alpha, const F2 &x, F3 &y) {
8  Logger &logger = Logger::get_instance();
9  logger.func_in(monolish_func);
10 
11  // err
12  assert(util::is_same_size(x, y));
13  assert(util::is_same_device_mem_stat(x, y));
14 
15  const double *xd = x.data();
16  double *yd = y.data();
17  size_t size = x.size();
18  const size_t xoffset = x.get_offset();
19  const size_t yoffset = y.get_offset();
20 
21  if (x.get_device_mem_stat() == true) {
22 #if MONOLISH_USE_GPU
23  cublasHandle_t h;
24  internal::check_CUDA(cublasCreate(&h));
25 #pragma omp target data use_device_ptr(xd, yd)
26  {
27  internal::check_CUDA(
28  cublasDaxpy(h, size, &alpha, xd + xoffset, 1, yd + yoffset, 1));
29  }
30  cublasDestroy(h);
31 #else
32  throw std::runtime_error(
33  "error USE_GPU is false, but get_device_mem_stat() == true");
34 #endif
35  } else {
36  cblas_daxpy(size, alpha, xd + xoffset, 1, yd + yoffset, 1);
37  }
38  logger.func_out();
39 }
40 
41 template <typename F1, typename F2, typename F3>
42 void Saxpy_core(const F1 alpha, const F2 &x, F3 &y) {
43  Logger &logger = Logger::get_instance();
44  logger.func_in(monolish_func);
45 
46  // err
47  assert(util::is_same_size(x, y));
48  assert(util::is_same_device_mem_stat(x, y));
49 
50  const float *xd = x.data();
51  float *yd = y.data();
52  size_t size = x.size();
53  const size_t xoffset = x.get_offset();
54  const size_t yoffset = y.get_offset();
55 
56  if (x.get_device_mem_stat() == true) {
57 #if MONOLISH_USE_GPU
58  cublasHandle_t h;
59  internal::check_CUDA(cublasCreate(&h));
60 #pragma omp target data use_device_ptr(xd, yd)
61  {
62  internal::check_CUDA(
63  cublasSaxpy(h, size, &alpha, xd + xoffset, 1, yd + yoffset, 1));
64  }
65  cublasDestroy(h);
66 #else
67  throw std::runtime_error(
68  "error USE_GPU is false, but get_device_mem_stat() == true");
69 #endif
70  } else {
71  cblas_saxpy(size, alpha, xd + xoffset, 1, yd + yoffset, 1);
72  }
73  logger.func_out();
74 }
75 
76 } // namespace
77 
78 } // namespace monolish
monolish::util::is_same_size
bool is_same_size(const T &x, const U &y)
compare size of vector or 1Dview (same as is_same_structure())
Definition: monolish_common.hpp:358
monolish_func
#define monolish_func
Definition: monolish_logger.hpp:9
monolish::util::is_same_device_mem_stat
bool is_same_device_mem_stat(const T &arg1, const U &arg2)
compare same device memory status
Definition: monolish_common.hpp:431
monolish
Definition: monolish_matrix_blas.hpp:9
monolish::Logger::get_instance
static Logger & get_instance()
Definition: monolish_logger.hpp:42