forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice.cpp
More file actions
180 lines (148 loc) · 5.13 KB
/
device.cpp
File metadata and controls
180 lines (148 loc) · 5.13 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
/*******************************************************
* 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 <af/array.h>
#include <af/backend.h>
#include <af/compatible.h>
#include <af/device.h>
#include <af/traits.hpp>
#include "error.hpp"
#include "type_util.hpp"
namespace af {
void setBackend(const Backend bknd) { AF_THROW(af_set_backend(bknd)); }
unsigned getBackendCount() {
unsigned temp = 1;
AF_THROW(af_get_backend_count(&temp));
return temp;
}
int getAvailableBackends() {
int result = 0;
AF_THROW(af_get_available_backends(&result));
return result;
}
af::Backend getBackendId(const array &in) {
af::Backend result = (af::Backend)0;
AF_THROW(af_get_backend_id(&result, in.get()));
return result;
}
int getDeviceId(const array &in) {
int device = getDevice();
;
AF_THROW(af_get_device_id(&device, in.get()));
return device;
}
af::Backend getActiveBackend() {
af::Backend result = (af::Backend)0;
AF_THROW(af_get_active_backend(&result));
return result;
}
void info() { AF_THROW(af_info()); }
const char *infoString(const bool verbose) {
char *str = NULL;
AF_THROW(af_info_string(&str, verbose));
return (const char *)str;
}
void deviceprop(char *d_name, char *d_platform, char *d_toolkit,
char *d_compute) {
deviceInfo(d_name, d_platform, d_toolkit, d_compute);
}
void deviceInfo(char *d_name, char *d_platform, char *d_toolkit,
char *d_compute) {
AF_THROW(af_device_info(d_name, d_platform, d_toolkit, d_compute));
}
int getDeviceCount() {
int devices = -1;
AF_THROW(af_get_device_count(&devices));
return devices;
}
int devicecount() { return getDeviceCount(); }
void setDevice(const int device) { AF_THROW(af_set_device(device)); }
void deviceset(const int device) { setDevice(device); }
int getDevice() {
int device = 0;
AF_THROW(af_get_device(&device));
return device;
}
bool isDoubleAvailable(const int device) {
bool temp;
AF_THROW(af_get_dbl_support(&temp, device));
return temp;
}
int deviceget() { return getDevice(); }
void sync(int device) { AF_THROW(af_sync(device)); }
///////////////////////////////////////////////////////////////////////////
// Alloc and free host, pinned, zero copy
void *alloc(const size_t elements, const af::dtype type) {
void *ptr;
AF_THROW(af_alloc_device(&ptr, elements * size_of(type)));
// FIXME: Add to map
return ptr;
}
void *pinned(const size_t elements, const af::dtype type) {
void *ptr;
AF_THROW(af_alloc_pinned(&ptr, elements * size_of(type)));
// FIXME: Add to map
return ptr;
}
void free(const void *ptr) {
// FIXME: look up map and call the right free
AF_THROW(af_free_device((void *)ptr));
}
void freePinned(const void *ptr) {
// FIXME: look up map and call the right free
AF_THROW(af_free_pinned((void *)ptr));
}
void *allocHost(const size_t elements, const af::dtype type) {
void *ptr;
AF_THROW(af_alloc_host(&ptr, elements * size_of(type)));
return ptr;
}
void freeHost(const void *ptr) { AF_THROW(af_free_host((void *)ptr)); }
void printMemInfo(const char *msg, const int device_id) {
AF_THROW(af_print_mem_info(msg, device_id));
}
void deviceGC() { AF_THROW(af_device_gc()); }
void deviceMemInfo(size_t *alloc_bytes, size_t *alloc_buffers,
size_t *lock_bytes, size_t *lock_buffers) {
AF_THROW(af_device_mem_info(alloc_bytes, alloc_buffers, lock_bytes,
lock_buffers));
}
void setMemStepSize(const size_t step_bytes) {
AF_THROW(af_set_mem_step_size(step_bytes));
}
size_t getMemStepSize() {
size_t size_bytes = 0;
AF_THROW(af_get_mem_step_size(&size_bytes));
return size_bytes;
}
#define INSTANTIATE(T) \
template<> \
AFAPI T *alloc(const size_t elements) { \
return (T *)alloc(elements, (af::dtype)dtype_traits<T>::af_type); \
} \
template<> \
AFAPI T *pinned(const size_t elements) { \
return (T *)pinned(elements, (af::dtype)dtype_traits<T>::af_type); \
} \
template<> \
AFAPI T *allocHost(const size_t elements) { \
return (T *)allocHost(elements, (af::dtype)dtype_traits<T>::af_type); \
}
INSTANTIATE(float)
INSTANTIATE(double)
INSTANTIATE(cfloat)
INSTANTIATE(cdouble)
INSTANTIATE(int)
INSTANTIATE(unsigned)
INSTANTIATE(unsigned char)
INSTANTIATE(char)
INSTANTIATE(short)
INSTANTIATE(unsigned short)
INSTANTIATE(long long)
INSTANTIATE(unsigned long long)
} // namespace af