Expression Templates Library (ETL)
cublas.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2014-2023 Baptiste Wicht
3 // Distributed under the terms of the MIT License.
4 // (See accompanying file LICENSE or copy at
5 // http://opensource.org/licenses/MIT)
6 //=======================================================================
7 
13 #pragma once
14 
15 #include "cublas_v2.h"
16 
17 namespace etl::impl::cublas {
18 
27 inline const char* cublas_str(cublasStatus_t code) {
28  switch (code) {
29  case CUBLAS_STATUS_NOT_INITIALIZED:
30  return "CUBLAS_STATUS_NOT_INITIALIZED";
31  case CUBLAS_STATUS_ALLOC_FAILED:
32  return "CUBLAS_STATUS_ALLOC_FAILED";
33  case CUBLAS_STATUS_INVALID_VALUE:
34  return "CUBLAS_STATUS_INVALID_VALUE";
35  case CUBLAS_STATUS_ARCH_MISMATCH:
36  return "CUBLAS_STATUS_ARCH_MISMATCH";
37  case CUBLAS_STATUS_MAPPING_ERROR:
38  return "CUBLAS_STATUS_MAPPING_ERROR";
39  case CUBLAS_STATUS_EXECUTION_FAILED:
40  return "CUBLAS_STATUS_EXECUTION_FAILED";
41  case CUBLAS_STATUS_INTERNAL_ERROR:
42  return "CUBLAS_STATUS_INTERNAL_ERROR";
43  default:
44  return "unknown CUBLAS error";
45  }
46 }
47 
48 #define cublas_check(call) \
49  { \
50  auto status = call; \
51  if (status != CUBLAS_STATUS_SUCCESS) { \
52  std::cerr << "CUDA error: " << etl::impl::cublas::cublas_str(status) << " from " << #call << std::endl \
53  << "from " << __FILE__ << ":" << __LINE__ << std::endl; \
54  } \
55  }
56 
60 struct cublas_handle {
61  cublasHandle_t handle;
62 
67  cublasCreate(&handle);
68  }
69 
74  explicit cublas_handle(cublasHandle_t handle) : handle(handle) {}
75 
76  cublas_handle(const cublas_handle& rhs) = delete;
77  cublas_handle& operator=(const cublas_handle& rhs) = delete;
78 
79  cublas_handle(cublas_handle&& rhs) noexcept = default;
80  cublas_handle& operator=(cublas_handle&& rhs) noexcept = default;
81 
86  cublasHandle_t get() {
87  return handle;
88  }
89 
94  cublasDestroy(handle);
95  }
96 };
97 
98 #ifndef ETL_CUBLAS_LOCAL_HANDLE
99 
105  static cublas_handle handle;
106  return handle;
107 }
108 
109 #else
110 
115 inline cublas_handle start_cublas() {
116  return {};
117 }
118 
119 #endif
120 
121 } //end of namespace etl::impl::cublas
cublas_handle & start_cublas()
Start cublas and return a RTTI helper over a raw cublas handle.
Definition: cublas.hpp:104
RTTI helper to manage CUBLAS handle.
Definition: cublas.hpp:60
Definition: axpy.hpp:22
cublas_handle(cublasHandle_t handle)
Construct the helper from the raw handle.
Definition: cublas.hpp:74
const char * cublas_str(cublasStatus_t code)
Returns the string representation of the given CUBLAS status code.
Definition: cublas.hpp:27
~cublas_handle()
Destruct the helper and release the raw cublas handle.
Definition: cublas.hpp:93
cublas_handle()
Construct the helper and create the handle directly.
Definition: cublas.hpp:66
cublasHandle_t handle
The raw cublas handle.
Definition: cublas.hpp:61