-
Notifications
You must be signed in to change notification settings - Fork 550
Expand file tree
/
Copy pathdevice.cpp
More file actions
417 lines (374 loc) · 11.8 KB
/
device.cpp
File metadata and controls
417 lines (374 loc) · 11.8 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*******************************************************
* 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 <Array.hpp>
#include <backend.hpp>
#include <common/err_common.hpp>
#include <common/half.hpp>
#include <common/util.hpp>
#include <handle.hpp>
#include <platform.hpp>
#include <sparse_handle.hpp>
#include <af/backend.h>
#include <af/device.h>
#include <af/dim4.hpp>
#include <af/version.h>
#if defined(USE_MKL)
#include <mkl_service.h>
#endif
#include <cstring>
#include <string>
using af::dim4;
using arrayfire::getSparseArray;
using arrayfire::common::getCacheDirectory;
using arrayfire::common::getEnvVar;
using arrayfire::common::half;
using arrayfire::common::JIT_KERNEL_CACHE_DIRECTORY_ENV_NAME;
using detail::Array;
using detail::cdouble;
using detail::cfloat;
using detail::createEmptyArray;
using detail::devprop;
using detail::evalFlag;
using detail::getActiveDeviceId;
using detail::getBackend;
using detail::getDeviceCount;
using detail::getDeviceInfo;
using detail::init;
using detail::intl;
using detail::isDoubleSupported;
using detail::isHalfSupported;
using detail::schar;
using detail::setDevice;
using detail::uchar;
using detail::uint;
using detail::uintl;
using detail::ushort;
af_err af_set_backend(const af_backend bknd) {
try {
if (bknd != getBackend() && bknd != AF_BACKEND_DEFAULT) {
return AF_ERR_ARG;
}
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_get_backend_count(unsigned* num_backends) {
*num_backends = 1;
return AF_SUCCESS;
}
af_err af_get_available_backends(int* result) {
try {
*result = getBackend();
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_get_backend_id(af_backend* result, const af_array in) {
try {
if (in) {
const ArrayInfo& info = getInfo(in, false);
*result = info.getBackendId();
} else {
return AF_ERR_ARG;
}
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_get_device_id(int* device, const af_array in) {
try {
if (in) {
const ArrayInfo& info = getInfo(in, false);
*device = static_cast<int>(info.getDevId());
} else {
return AF_ERR_ARG;
}
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_get_active_backend(af_backend* result) {
*result = static_cast<af_backend>(getBackend());
return AF_SUCCESS;
}
af_err af_init() {
try {
thread_local std::once_flag flag;
std::call_once(flag, []() {
init();
#if defined(USE_MKL) && !defined(USE_STATIC_MKL)
int errCode = -1;
// Have used the AF_MKL_INTERFACE_SIZE as regular if's so that
// we will know if these are not defined when using MKL when a
// compilation error is generated.
if (AF_MKL_INTERFACE_SIZE == 4) {
errCode = mkl_set_interface_layer(MKL_INTERFACE_LP64);
} else if (AF_MKL_INTERFACE_SIZE == 8) {
errCode = mkl_set_interface_layer(MKL_INTERFACE_ILP64);
}
if (errCode == -1) {
AF_ERROR(
"Intel MKL Interface layer was not specified prior to the "
"call and the input parameter is incorrect.",
AF_ERR_RUNTIME);
}
switch (AF_MKL_THREAD_LAYER) {
case 0:
errCode = mkl_set_threading_layer(MKL_THREADING_SEQUENTIAL);
break;
case 1:
errCode = mkl_set_threading_layer(MKL_THREADING_GNU);
break;
case 2:
errCode = mkl_set_threading_layer(MKL_THREADING_INTEL);
break;
case 3:
errCode = mkl_set_threading_layer(MKL_THREADING_TBB);
break;
}
if (errCode == -1) {
AF_ERROR(
"Intel MKL Thread layer was not specified prior to the "
"call and the input parameter is incorrect.",
AF_ERR_RUNTIME);
}
#endif
});
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_info() {
try {
printf("%s", getDeviceInfo().c_str()); // NOLINT
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_info_string(char** str, const bool verbose) {
UNUSED(verbose); // TODO(umar): Add something useful
try {
std::string infoStr = getDeviceInfo();
void* halloc_ptr = nullptr;
af_alloc_host(&halloc_ptr, sizeof(char) * (infoStr.size() + 1));
memcpy(str, &halloc_ptr, sizeof(void*));
// Need to do a deep copy
// str.c_str wont cut it
infoStr.copy(*str, infoStr.size());
(*str)[infoStr.size()] = '\0';
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_device_info(char* d_name, char* d_platform, char* d_toolkit,
char* d_compute) {
try {
devprop(d_name, d_platform, d_toolkit, d_compute);
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_get_dbl_support(bool* available, const int device) {
try {
*available = isDoubleSupported(device);
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_get_half_support(bool* available, const int device) {
try {
*available = isHalfSupported(device);
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_get_device_count(int* nDevices) {
try {
*nDevices = getDeviceCount();
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_get_device(int* device) {
try {
*device = static_cast<int>(getActiveDeviceId());
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_set_device(const int device) {
try {
ARG_ASSERT(0, device >= 0);
if (setDevice(device) < 0) {
int ndevices = getDeviceCount();
if (ndevices == 0) {
AF_ERROR(
"No devices were found on this system. Ensure "
"you have installed the device driver as well as the "
"necessary runtime libraries for your platform.",
AF_ERR_RUNTIME);
} else {
char buf[512];
char err_msg[] =
"The device index of %d is out of range. Use a value "
"between 0 and %d.";
snprintf(buf, 512, err_msg, device, ndevices - 1); // NOLINT
AF_ERROR(buf, AF_ERR_ARG);
}
}
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_sync(const int device) {
try {
int dev = device == -1 ? static_cast<int>(getActiveDeviceId()) : device;
detail::sync(dev);
}
CATCHALL;
return AF_SUCCESS;
}
template<typename T>
static inline void eval(af_array arr) {
getArray<T>(arr).eval();
}
template<typename T>
static inline void sparseEval(af_array arr) {
getSparseArray<T>(arr).eval();
}
af_err af_eval(af_array arr) {
try {
const ArrayInfo& info = getInfo(arr, false);
af_dtype type = info.getType();
if (info.isSparse()) {
switch (type) {
case f32: sparseEval<float>(arr); break;
case f64: sparseEval<double>(arr); break;
case c32: sparseEval<cfloat>(arr); break;
case c64: sparseEval<cdouble>(arr); break;
default: TYPE_ERROR(0, type);
}
} else {
switch (type) {
case f32: eval<float>(arr); break;
case f64: eval<double>(arr); break;
case c32: eval<cfloat>(arr); break;
case c64: eval<cdouble>(arr); break;
case s32: eval<int>(arr); break;
case u32: eval<uint>(arr); break;
case s8: eval<schar>(arr); break;
case u8: eval<uchar>(arr); break;
case b8: eval<char>(arr); break;
case s64: eval<intl>(arr); break;
case u64: eval<uintl>(arr); break;
case s16: eval<short>(arr); break;
case u16: eval<ushort>(arr); break;
case f16: eval<half>(arr); break;
default: TYPE_ERROR(0, type);
}
}
}
CATCHALL;
return AF_SUCCESS;
}
template<typename T>
static inline void evalMultiple(int num, af_array* arrayPtrs) {
Array<T> empty = createEmptyArray<T>(dim4());
std::vector<Array<T>*> arrays(num, &empty);
for (int i = 0; i < num; i++) {
arrays[i] = reinterpret_cast<Array<T>*>(arrayPtrs[i]);
}
evalMultiple<T>(arrays);
}
af_err af_eval_multiple(int num, af_array* arrays) {
try {
const ArrayInfo& info = getInfo(arrays[0]);
af_dtype type = info.getType();
const dim4& dims = info.dims();
for (int i = 1; i < num; i++) {
const ArrayInfo& currInfo = getInfo(arrays[i]);
// FIXME: This needs to be removed when new functionality is added
if (type != currInfo.getType()) {
AF_ERROR("All arrays must be of same type", AF_ERR_TYPE);
}
if (dims != currInfo.dims()) {
AF_ERROR("All arrays must be of same size", AF_ERR_SIZE);
}
}
switch (type) {
case f32: evalMultiple<float>(num, arrays); break;
case f64: evalMultiple<double>(num, arrays); break;
case c32: evalMultiple<cfloat>(num, arrays); break;
case c64: evalMultiple<cdouble>(num, arrays); break;
case s32: evalMultiple<int>(num, arrays); break;
case u32: evalMultiple<uint>(num, arrays); break;
case s8: evalMultiple<schar>(num, arrays); break;
case u8: evalMultiple<uchar>(num, arrays); break;
case b8: evalMultiple<char>(num, arrays); break;
case s64: evalMultiple<intl>(num, arrays); break;
case u64: evalMultiple<uintl>(num, arrays); break;
case s16: evalMultiple<short>(num, arrays); break;
case u16: evalMultiple<ushort>(num, arrays); break;
case f16: evalMultiple<half>(num, arrays); break;
default: TYPE_ERROR(0, type);
}
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_set_manual_eval_flag(bool flag) {
try {
bool& backendFlag = evalFlag();
backendFlag = !flag;
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_get_manual_eval_flag(bool* flag) {
try {
bool backendFlag = evalFlag();
*flag = !backendFlag;
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_get_kernel_cache_directory(size_t* length, char* path) {
try {
std::string& cache_path = getCacheDirectory();
if (path == nullptr) {
ARG_ASSERT(length != nullptr, 1);
*length = cache_path.size();
} else {
size_t min_len = cache_path.size();
if (length) {
if (*length < cache_path.size()) {
AF_ERROR("Length not sufficient to store the path",
AF_ERR_SIZE);
}
min_len = std::min(*length, cache_path.size());
}
memcpy(path, cache_path.c_str(), min_len);
}
}
CATCHALL
return AF_SUCCESS;
}
af_err af_set_kernel_cache_directory(const char* path, int override_env) {
try {
ARG_ASSERT(path != nullptr, 1);
if (override_env) {
getCacheDirectory() = std::string(path);
} else {
auto env_path = getEnvVar(JIT_KERNEL_CACHE_DIRECTORY_ENV_NAME);
if (env_path.empty()) { getCacheDirectory() = std::string(path); }
}
}
CATCHALL
return AF_SUCCESS;
}