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