monolish  0.14.2
MONOlithic LIner equation Solvers for Highly-parallel architecture
asum.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 namespace monolish {
4 namespace {
5 template <typename F1> double Dasum_core(const F1 &x) {
6  Logger &logger = Logger::get_instance();
7  logger.func_in(monolish_func);
8 
9  double ans = 0;
10  const double *xd = x.data();
11  size_t size = x.size();
12  const size_t xoffset = x.get_offset();
13 
14  if (x.get_device_mem_stat() == true) {
15 #if MONOLISH_USE_NVIDIA_GPU
16  cublasHandle_t h;
17  internal::check_CUDA(cublasCreate(&h));
18 #pragma omp target data use_device_ptr(xd)
19  { internal::check_CUDA(cublasDasum(h, size, xd + xoffset, 1, &ans)); }
20  cublasDestroy(h);
21 #else
22  throw std::runtime_error(
23  "error USE_GPU is false, but get_device_mem_stat() == true");
24 #endif
25  } else {
26  ans = cblas_dasum(size, xd + xoffset, 1);
27  }
28 
29 #if MONOLISH_USE_MPI
30  mpi::comm &comm = mpi::comm::get_instance();
31  ans = comm.Allreduce(ans);
32 #endif
33 
34  logger.func_out();
35  return ans;
36 }
37 
38 template <typename F1> float Sasum_core(const F1 &x) {
39  Logger &logger = Logger::get_instance();
40  logger.func_in(monolish_func);
41 
42  float ans = 0;
43  const float *xd = x.data();
44  size_t size = x.size();
45  const size_t xoffset = x.get_offset();
46 
47  if (x.get_device_mem_stat() == true) {
48 #if MONOLISH_USE_NVIDIA_GPU
49  cublasHandle_t h;
50  internal::check_CUDA(cublasCreate(&h));
51 #pragma omp target data use_device_ptr(xd)
52  { internal::check_CUDA(cublasSasum(h, size, xd + xoffset, 1, &ans)); }
53  cublasDestroy(h);
54 #else
55  throw std::runtime_error(
56  "error USE_GPU is false, but get_device_mem_stat() == true");
57 #endif
58  } else {
59  ans = cblas_sasum(size, xd + xoffset, 1);
60  }
61 
62 #if MONOLISH_USE_MPI
63  mpi::comm &comm = mpi::comm::get_instance();
64  ans = comm.Allreduce(ans);
65 #endif
66 
67  logger.func_out();
68  return ans;
69 }
70 
71 } // namespace
72 } // namespace monolish
monolish_func
#define monolish_func
Definition: monolish_logger.hpp:9
monolish
Definition: monolish_matrix_blas.hpp:10
monolish::mpi::comm::get_instance
static comm & get_instance()
Definition: monolish_mpi_core.hpp:33
monolish::Logger::get_instance
static Logger & get_instance()
Definition: monolish_logger.hpp:42