monolish  0.14.0
MONOlithic LIner equation Solvers for Highly-parallel architecture
dot.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 namespace monolish {
4 
5 namespace {
6 template <typename F1, typename F2> double Ddot_core(const F1 &x, const F2 &y) {
7  Logger &logger = Logger::get_instance();
8  logger.func_in(monolish_func);
9 
10  // err
11  assert(util::is_same_size(x, y));
12  assert(util::is_same_device_mem_stat(x, y));
13 
14  double ans = 0;
15  const double *xd = x.data();
16  const double *yd = y.data();
17  const 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  cublasDdot(h, size, xd + xoffset, 1, yd + yoffset, 1, &ans));
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  ans = cblas_ddot(size, xd + xoffset, 1, yd + yoffset, 1);
37  }
38  logger.func_out();
39  return ans;
40 }
41 
42 template <typename F1, typename F2> float Sdot_core(const F1 &x, const F2 &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  float ans = 0;
51  const float *xd = x.data();
52  const float *yd = y.data();
53  const size_t size = x.size();
54  const size_t xoffset = x.get_offset();
55  const size_t yoffset = y.get_offset();
56 
57  if (x.get_device_mem_stat() == true) {
58 #if MONOLISH_USE_GPU
59  cublasHandle_t h;
60  internal::check_CUDA(cublasCreate(&h));
61 #pragma omp target data use_device_ptr(xd, yd)
62  {
63  internal::check_CUDA(
64  cublasSdot(h, size, xd + xoffset, 1, yd + yoffset, 1, &ans));
65  }
66  cublasDestroy(h);
67 #else
68  throw std::runtime_error(
69  "error USE_GPU is false, but get_device_mem_stat() == true");
70 #endif
71  } else {
72  ans = cblas_sdot(size, xd + xoffset, 1, yd + yoffset, 1);
73  }
74  logger.func_out();
75  return ans;
76 }
77 
78 } // namespace
79 } // 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