Debian Codegraph @

packages/kokkos-kernels/blas/tpls/KokkosBlas_tpl_spec.hpp

raw
1 //@HEADER
2 // ************************************************************************
3 //
4 // Kokkos v. 4.0
5 // Copyright (2022) National Technology & Engineering
6 // Solutions of Sandia, LLC (NTESS).
7 //
8 // Under the terms of Contract DE-NA0003525 with NTESS,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12 // See https://kokkos.org/LICENSE for license information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //@HEADER
16
17 #ifndef KOKKOSBLAS_TPL_SPEC_HPP_
18 #define KOKKOSBLAS_TPL_SPEC_HPP_
19
20 #ifdef KOKKOSKERNELS_ENABLE_TPL_CUBLAS
21 #include "cuda_runtime.h"
22 #include "cublas_v2.h"
23
24 namespace KokkosBlas {
25 namespace Impl {
26
27 struct CudaBlasSingleton {
28 cublasHandle_t handle;
29
30 CudaBlasSingleton();
31
32 static bool is_initialized();
33 static CudaBlasSingleton& singleton();
34
35 private:
36 static std::unique_ptr<CudaBlasSingleton>& get_instance();
37 };
38
39 inline void cublas_internal_error_throw(cublasStatus_t cublasState, const char* name, const char* file,
40 const int line) {
41 std::ostringstream out;
42 // out << name << " error( " << cublasGetStatusName(cublasState)
43 // << "): " << cublasGetStatusString(cublasState);
44 out << name << " error( ";
45 switch (cublasState) {
46 case CUBLAS_STATUS_NOT_INITIALIZED:
47 out << "CUBLAS_STATUS_NOT_INITIALIZED): the library was not initialized.";
48 break;
49 case CUBLAS_STATUS_ALLOC_FAILED: out << "CUBLAS_STATUS_ALLOC_FAILED): the resource allocation failed."; break;
50 case CUBLAS_STATUS_INVALID_VALUE:
51 out << "CUBLAS_STATUS_INVALID_VALUE): an invalid numerical value was "
52 "used as an argument.";
53 break;
54 case CUBLAS_STATUS_ARCH_MISMATCH:
55 out << "CUBLAS_STATUS_ARCH_MISMATCH): an absent device architectural "
56 "feature is required.";
57 break;
58 case CUBLAS_STATUS_MAPPING_ERROR:
59 out << "CUBLAS_STATUS_MAPPING_ERROR): an access to GPU memory space "
60 "failed.";
61 break;
62 case CUBLAS_STATUS_EXECUTION_FAILED:
63 out << "CUBLAS_STATUS_EXECUTION_FAILED): the GPU program failed to "
64 "execute.";
65 break;
66 case CUBLAS_STATUS_INTERNAL_ERROR: out << "CUBLAS_STATUS_INTERNAL_ERROR): an internal operation failed."; break;
67 case CUBLAS_STATUS_NOT_SUPPORTED:
68 out << "CUBLAS_STATUS_NOT_SUPPORTED): the feature required is not "
69 "supported.";
70 break;
71 default: out << "unrecognized error code): this is bad!"; break;
72 }
73 if (file) {
74 out << " " << file << ":" << line;
75 }
76 throw std::runtime_error(out.str());
77 }
78
79 inline void cublas_internal_safe_call(cublasStatus_t cublasState, const char* name, const char* file = nullptr,
80 const int line = 0) {
81 if (CUBLAS_STATUS_SUCCESS != cublasState) {
82 cublas_internal_error_throw(cublasState, name, file, line);
83 }
84 }
85
86 // The macro below defines the interface for the safe cublas calls.
87 // The functions themselves are protected by impl namespace and this
88 // is not meant to be used by external application or libraries.
89 #define KOKKOSBLAS_IMPL_CUBLAS_SAFE_CALL(call) \
90 KokkosBlas::Impl::cublas_internal_safe_call(call, #call, __FILE__, __LINE__)
91
92 /// \brief This function converts KK transpose mode to cuBLAS transpose mode
93 inline cublasOperation_t trans_mode_kk_to_cublas(const char kkMode[]) {
94 cublasOperation_t trans;
95 if ((kkMode[0] == 'N') || (kkMode[0] == 'n'))
96 trans = CUBLAS_OP_N;
97 else if ((kkMode[0] == 'T') || (kkMode[0] == 't'))
98 trans = CUBLAS_OP_T;
99 else
100 trans = CUBLAS_OP_C;
101 return trans;
102 }
103
104 } // namespace Impl
105 } // namespace KokkosBlas
106 #endif // KOKKOSKERNELS_ENABLE_TPL_CUBLAS
107
108 #ifdef KOKKOSKERNELS_ENABLE_TPL_ROCBLAS
109 #include <rocblas/rocblas.h>
110
111 namespace KokkosBlas {
112 namespace Impl {
113
114 struct RocBlasSingleton {
115 rocblas_handle handle;
116
117 RocBlasSingleton();
118
119 static bool is_initialized();
120
121 static RocBlasSingleton& singleton();
122
123 private:
124 static std::unique_ptr<RocBlasSingleton>& get_instance();
125 };
126
127 inline void rocblas_internal_error_throw(rocblas_status rocblasState, const char* name, const char* file,
128 const int line) {
129 std::ostringstream out;
130 out << name << " error( ";
131 switch (rocblasState) {
132 case rocblas_status_invalid_handle:
133 out << "rocblas_status_invalid_handle): handle not initialized, invalid "
134 "or null.";
135 break;
136 case rocblas_status_not_implemented: out << "rocblas_status_not_implemented): function is not implemented."; break;
137 case rocblas_status_invalid_pointer: out << "rocblas_status_invalid_pointer): invalid pointer argument."; break;
138 case rocblas_status_invalid_size: out << "rocblas_status_invalid_size): invalid size argument."; break;
139 case rocblas_status_memory_error:
140 out << "rocblas_status_memory_error): failed internal memory allocation, "
141 "copy or dealloc.";
142 break;
143 case rocblas_status_internal_error: out << "rocblas_status_internal_error): other internal library failure."; break;
144 case rocblas_status_perf_degraded:
145 out << "rocblas_status_perf_degraded): performance degraded due to low "
146 "device memory.";
147 break;
148 case rocblas_status_size_query_mismatch: out << "unmatched start/stop size query): ."; break;
149 case rocblas_status_size_increased:
150 out << "rocblas_status_size_increased): queried device memory size "
151 "increased.";
152 break;
153 case rocblas_status_size_unchanged:
154 out << "rocblas_status_size_unchanged): queried device memory size "
155 "unchanged.";
156 break;
157 case rocblas_status_invalid_value: out << "rocblas_status_invalid_value): passed argument not valid."; break;
158 case rocblas_status_continue:
159 out << "rocblas_status_continue): nothing preventing function to "
160 "proceed.";
161 break;
162 case rocblas_status_check_numerics_fail:
163 out << "rocblas_status_check_numerics_fail): will be set if the "
164 "vector/matrix has a NaN or an Infinity.";
165 break;
166 default: out << "unrecognized error code): this is bad!"; break;
167 }
168 if (file) {
169 out << " " << file << ":" << line;
170 }
171 throw std::runtime_error(out.str());
172 }
173
174 inline void rocblas_internal_safe_call(rocblas_status rocblasState, const char* name, const char* file = nullptr,
175 const int line = 0) {
176 if (rocblas_status_success != rocblasState) {
177 rocblas_internal_error_throw(rocblasState, name, file, line);
178 }
179 }
180
181 // The macro below defines the interface for the safe rocblas calls.
182 // The functions themselves are protected by impl namespace and this
183 // is not meant to be used by external application or libraries.
184 #define KOKKOSBLAS_IMPL_ROCBLAS_SAFE_CALL(call) \
185 KokkosBlas::Impl::rocblas_internal_safe_call(call, #call, __FILE__, __LINE__)
186
187 /// \brief This function converts KK transpose mode to rocBLAS transpose mode
188 inline rocblas_operation trans_mode_kk_to_rocblas(const char kkMode[]) {
189 rocblas_operation trans;
190 if ((kkMode[0] == 'N') || (kkMode[0] == 'n'))
191 trans = rocblas_operation_none;
192 else if ((kkMode[0] == 'T') || (kkMode[0] == 't'))
193 trans = rocblas_operation_transpose;
194 else
195 trans = rocblas_operation_conjugate_transpose;
196 return trans;
197 }
198
199 } // namespace Impl
200 } // namespace KokkosBlas
201
202 #endif // KOKKOSKERNELS_ENABLE_TPL_ROCBLAS
203
204 #endif // KOKKOSBLAS_TPL_SPEC_HPP_