forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherr_common.cpp
More file actions
232 lines (194 loc) · 8.7 KB
/
err_common.cpp
File metadata and controls
232 lines (194 loc) · 8.7 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <common/err_common.hpp>
#include <common/graphics_common.hpp>
#include <common/util.hpp>
#include <type_util.hpp>
#include <af/device.h>
#include <af/exception.h>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <string>
#include <utility>
#ifdef AF_OPENCL
#include <errorcodes.hpp>
#include <platform.hpp>
#endif
using std::move;
using std::string;
using std::stringstream;
using common::is_stacktrace_enabled;
AfError::AfError(const char *const func, const char *const file, const int line,
const char *const message, af_err err,
boost::stacktrace::stacktrace st)
: logic_error(message)
, functionName(func)
, fileName(file)
, lineNumber(line)
, error(err)
, st_(move(st)) {}
AfError::AfError(string func, string file, const int line,
const string &message, af_err err,
boost::stacktrace::stacktrace st)
: logic_error(message)
, functionName(move(func))
, fileName(move(file))
, lineNumber(line)
, error(err)
, st_(move(st)) {}
const string &AfError::getFunctionName() const noexcept { return functionName; }
const string &AfError::getFileName() const noexcept { return fileName; }
int AfError::getLine() const noexcept { return lineNumber; }
af_err AfError::getError() const noexcept { return error; }
AfError::~AfError() noexcept = default;
TypeError::TypeError(const char *const func, const char *const file,
const int line, const int index, const af_dtype type,
boost::stacktrace::stacktrace st)
: AfError(func, file, line, "Invalid data type", AF_ERR_TYPE, move(st))
, argIndex(index)
, errTypeName(getName(type)) {}
const string &TypeError::getTypeName() const noexcept { return errTypeName; }
int TypeError::getArgIndex() const noexcept { return argIndex; }
ArgumentError::ArgumentError(const char *const func, const char *const file,
const int line, const int index,
const char *const expectString,
boost::stacktrace::stacktrace st)
: AfError(func, file, line, "Invalid argument", AF_ERR_ARG, move(st))
, argIndex(index)
, expected(expectString) {}
const string &ArgumentError::getExpectedCondition() const noexcept {
return expected;
}
int ArgumentError::getArgIndex() const noexcept { return argIndex; }
SupportError::SupportError(const char *const func, const char *const file,
const int line, const char *const back,
boost::stacktrace::stacktrace st)
: AfError(func, file, line, "Unsupported Error", AF_ERR_NOT_SUPPORTED,
move(st))
, backend(back) {}
const string &SupportError::getBackendName() const noexcept { return backend; }
DimensionError::DimensionError(const char *const func, const char *const file,
const int line, const int index,
const char *const expectString,
const boost::stacktrace::stacktrace &st)
: AfError(func, file, line, "Invalid size", AF_ERR_SIZE, st)
, argIndex(index)
, expected(expectString) {}
const string &DimensionError::getExpectedCondition() const noexcept {
return expected;
}
int DimensionError::getArgIndex() const noexcept { return argIndex; }
af_err set_global_error_string(const string &msg, af_err err) {
std::string perr = getEnvVar("AF_PRINT_ERRORS");
if (!perr.empty()) {
if (perr != "0") { fprintf(stderr, "%s\n", msg.c_str()); }
}
get_global_error_string() = msg;
return err;
}
af_err processException() {
stringstream ss;
af_err err = AF_ERR_INTERNAL;
try {
throw;
} catch (const DimensionError &ex) {
ss << "In function " << ex.getFunctionName() << "\n"
<< "In file " << ex.getFileName() << ":" << ex.getLine() << "\n"
<< "Invalid dimension for argument " << ex.getArgIndex() << "\n"
<< "Expected: " << ex.getExpectedCondition() << "\n";
if (is_stacktrace_enabled()) { ss << ex.getStacktrace(); }
err = set_global_error_string(ss.str(), AF_ERR_SIZE);
} catch (const ArgumentError &ex) {
ss << "In function " << ex.getFunctionName() << "\n"
<< "In file " << ex.getFileName() << ":" << ex.getLine() << "\n"
<< "Invalid argument at index " << ex.getArgIndex() << "\n"
<< "Expected: " << ex.getExpectedCondition() << "\n";
if (is_stacktrace_enabled()) { ss << ex.getStacktrace(); }
err = set_global_error_string(ss.str(), AF_ERR_ARG);
} catch (const SupportError &ex) {
ss << ex.getFunctionName() << " not supported for "
<< ex.getBackendName() << " backend\n";
if (is_stacktrace_enabled()) { ss << ex.getStacktrace(); }
err = set_global_error_string(ss.str(), AF_ERR_NOT_SUPPORTED);
} catch (const TypeError &ex) {
ss << "In function " << ex.getFunctionName() << "\n"
<< "In file " << ex.getFileName() << ":" << ex.getLine() << "\n"
<< "Invalid type for argument " << ex.getArgIndex() << "\n";
if (is_stacktrace_enabled()) { ss << ex.getStacktrace(); }
err = set_global_error_string(ss.str(), AF_ERR_TYPE);
} catch (const AfError &ex) {
ss << "In function " << ex.getFunctionName() << "\n"
<< "In file " << ex.getFileName() << ":" << ex.getLine() << "\n"
<< ex.what() << "\n";
if (is_stacktrace_enabled()) { ss << ex.getStacktrace(); }
err = set_global_error_string(ss.str(), ex.getError());
#ifdef AF_OPENCL
} catch (const cl::Error &ex) {
char opencl_err_msg[1024];
snprintf(opencl_err_msg, sizeof(opencl_err_msg),
"OpenCL Error (%d): %s when calling %s", ex.err(),
getErrorMessage(ex.err()).c_str(), ex.what());
if (ex.err() == CL_MEM_OBJECT_ALLOCATION_FAILURE) {
err = set_global_error_string(opencl_err_msg, AF_ERR_NO_MEM);
} else {
err = set_global_error_string(opencl_err_msg, AF_ERR_INTERNAL);
}
#endif
} catch (...) { err = set_global_error_string(ss.str(), AF_ERR_UNKNOWN); }
return err;
}
std::string &get_global_error_string() noexcept {
thread_local auto *global_error_string = new std::string("");
return *global_error_string;
}
const char *af_err_to_string(const af_err err) {
switch (err) {
case AF_SUCCESS: return "Success";
case AF_ERR_NO_MEM: return "Device out of memory";
case AF_ERR_DRIVER: return "Driver not available or incompatible";
case AF_ERR_RUNTIME: return "Runtime error ";
case AF_ERR_INVALID_ARRAY: return "Invalid array";
case AF_ERR_ARG: return "Invalid input argument";
case AF_ERR_SIZE: return "Invalid input size";
case AF_ERR_TYPE: return "Function does not support this data type";
case AF_ERR_DIFF_TYPE: return "Input types are not the same";
case AF_ERR_BATCH: return "Invalid batch configuration";
case AF_ERR_DEVICE:
return "Input does not belong to the current device.";
case AF_ERR_NOT_SUPPORTED: return "Function not supported";
case AF_ERR_NOT_CONFIGURED: return "Function not configured to build";
case AF_ERR_NONFREE:
return "Function unavailable. "
"ArrayFire compiled without Non-Free algorithms support";
case AF_ERR_NO_DBL:
return "Double precision not supported for this device";
case AF_ERR_NO_GFX:
return "Graphics functionality unavailable. "
"ArrayFire compiled without Graphics support";
case AF_ERR_NO_HALF:
return "Half precision floats not supported for this device";
case AF_ERR_LOAD_LIB: return "Failed to load dynamic library. ";
case AF_ERR_LOAD_SYM: return "Failed to load symbol";
case AF_ERR_ARR_BKND_MISMATCH:
return "There was a mismatch between an array and the current "
"backend";
case AF_ERR_INTERNAL: return "Internal error";
case AF_ERR_UNKNOWN: return "Unknown error";
}
return "Unknown error. Please open an issue and add this error code to the "
"case in af_err_to_string.";
}
namespace common {
bool &is_stacktrace_enabled() noexcept {
static bool stacktrace_enabled = true;
return stacktrace_enabled;
}
} // namespace common