forked from romulus0914/MLP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_helper.hpp
199 lines (155 loc) · 5.93 KB
/
error_helper.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#ifndef __ERROR_HANDLER_CUH__
#define __ERROR_HANDLER_CUH__
#include <type_traits>
#include <cuda_runtime.h>
#include <cublas_v2.h>
#include <cusparse.h>
namespace zex
{
// some simple meta programming
// ===== is_any =====
template<typename T, typename ... R>
struct is_any : std::false_type {};
template<typename T, typename F>
struct is_any<T, F> : std::is_same<T, F> {};
template<typename T, typename F, typename ...R>
struct is_any<T, F, R...> :
std::integral_constant<bool, std::is_same<T, F>::value
|| is_any<T, R...>::value>{};
// ===== type_index =====
template<int _case, typename T, typename ...R>
struct _type_index : std::false_type {};
template<int _case, typename T, typename F>
struct _type_index<_case, T, F> : std::conditional<std::is_same<T, F>::value,
std::integral_constant<int, _case>,
std::integral_constant<int, -1>>::type{};
template<int _case, typename T, typename F, typename ...R>
struct _type_index<_case, T, F, R...> : std::conditional<std::is_same<T, F>::value,
_type_index<_case, T, F>, _type_index<_case+1, T, R...>>::type{};
template<typename T, typename ...R>
struct type_index : _type_index<0, T, R...>{};
// ===== type_case =====
template<int _case, typename ...R>
struct _type_case : std::false_type{};
template<int _case, typename T>
struct _type_case<_case, T> { typedef T type; };
template<int _case, typename T, typename ...R>
struct _type_case<_case, T, R...> : std::conditional<_case==0, _type_case<_case, T>,
_type_case<_case-1, R...>>::type{};
template<int _case, typename ...T>
struct type_case : _type_case<_case, T...> {};
// ===== tv_pair =====
template<typename T, T V>
struct tv_pair {
typedef T type;
static constexpr T value=V;
};
// ===== pick/options =====
// recursive call
template<int _case>
struct pick{
constexpr static bool _options() { return false; }
template<typename T, typename ...Tail>
constexpr static auto _options(const T& head, Tail&&...tail)
-> decltype(pick<_case-1>::_options(std::forward<Tail>(tail)...))
{
return pick<_case-1>::_options(tail...);
}
template<typename T, typename ...Tail>
constexpr static auto options(const T& head, Tail&&...tail)
-> decltype(pick<_case-1>::_options(tail...))
{
return pick<_case-1>::_options(tail...);
}
};
// stopping state
template<>
struct pick<0>{
template<typename T, typename ...Tail>
constexpr static auto _options(const T& head, Tail&&...tail)
-> decltype(head)
{
return head;
}
template<typename T, typename ...Tail>
constexpr static auto options(const T& head, Tail&&...tail)
-> decltype(head)
{
return head;
}
};
// namespace zex {end}
}
// =========================================
#define error_check(err) error_handler(err, #err, __LINE__, __FILE__)
const char *error_message(cublasStatus_t err)
{
switch(err)
{
case CUBLAS_STATUS_NOT_INITIALIZED:
return "library not initialized";
case CUBLAS_STATUS_ALLOC_FAILED:
return "failed to allocate resources";
case CUBLAS_STATUS_INVALID_VALUE:
return "got invalid value";
case CUBLAS_STATUS_ARCH_MISMATCH:
return "device does not support double/half precision";
case CUBLAS_STATUS_EXECUTION_FAILED:
return "failed to launch on the GPU";
case CUBLAS_STATUS_MAPPING_ERROR:
return "access gpu error";
}
return "other error occurred";
}
const char *error_message(cusparseStatus_t err)
{
switch(err)
{
case CUSPARSE_STATUS_NOT_INITIALIZED:
return "library not initialized";
case CUSPARSE_STATUS_ALLOC_FAILED:
return "failed to allocate resources";
case CUSPARSE_STATUS_INVALID_VALUE:
return "got invalid value";
case CUSPARSE_STATUS_ARCH_MISMATCH:
return "device does not support double/half precision";
case CUSPARSE_STATUS_EXECUTION_FAILED:
return "failed to launch on the GPU";
}
return "other error occurred";
}
const char *error_message(cudaError_t err)
{
switch(err)
{
case cudaErrorMemoryAllocation:
return "failed to allocate resources";
case cudaErrorInitializationError:
return "failed to initialize cuda";
case cudaErrorInvalidValue:
return "got invalid value";
}
return "other error occurred";
}
template<typename t, t v>
using pair = zex::tv_pair<t, v>;
// some simple metaprogramming, ensure that the error type is one of those types in is_any list
template<typename errT>
void error_handler(errT err, const std::string& func, const int& line, const std::string& file)
{
static_assert(zex::is_any<errT, cudaError_t, cublasStatus_t, cusparseStatus_t>::value,
"Error type must be one of the types: cudaError_t, cublasStatus_t or cusparseStatue_t");
// type switch-case
using tidx = zex::type_index<errT, cudaError_t, cublasStatus_t, cusparseStatus_t>;
// pick the "tidx::value"th option from the options list
if( err != zex::pick<tidx::value>::options(cudaSuccess, CUBLAS_STATUS_SUCCESS, CUSPARSE_STATUS_SUCCESS))
{
// pick the "tidx::value"th option from the options list
std::cout << zex::pick<tidx::value>::options("[cuda ERROR]", "[cuBLAS ERROR]", "[cuSPARSE ERROR]")
<< " " << error_message(err) << std::endl
<< " in line [" << line << "] : " << func << std::endl
<< " in file " << file << std::endl;
exit(1);
}
}
#endif