-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCLDeviceManager.h
340 lines (316 loc) · 10.9 KB
/
CLDeviceManager.h
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
#pragma once
#include <CL/cl.h>
#include <vector>
#include <map>
#include <fstream>
#include <iostream>
typedef std::map<cl_platform_id, std::vector<cl_device_id> > mpd;
class CLContext {
public:
cl_context context;
CLContext(cl_platform_id pid) {
cl_context_properties prop[] = {
CL_CONTEXT_PLATFORM,
(cl_context_properties)pid,
0
};
context = clCreateContextFromType(
prop,
CL_DEVICE_TYPE_ALL,
NULL,
NULL,
NULL);
}
void Release() {
if (context) {
clReleaseContext(context);
}
context = NULL;
}
~CLContext() {
Release();
}
};
class CLDeviceManager {
mpd devices;
public:
CLDeviceManager() {
cl_uint n;
clGetPlatformIDs(0, NULL, &n);
auto pf = std::vector<cl_platform_id>(n);
clGetPlatformIDs(n, &pf[0], NULL);
for (auto i: pf) {
auto ret = devices.insert(
std::make_pair(
i,
std::vector<cl_device_id>())
);
auto& r = ret.first->second;
clGetDeviceIDs(i, CL_DEVICE_TYPE_ALL, 0, NULL, &n);
r = std::vector<cl_device_id>(n);
clGetDeviceIDs(i, CL_DEVICE_TYPE_ALL, n, &r[0], NULL);
}
}
const mpd& GetDevices() {
return devices;
}
cl_device_type GetDeviceType(cl_device_id did) {
cl_device_type ret;
clGetDeviceInfo(did,
CL_DEVICE_TYPE,
sizeof(cl_device_type),
&ret,
NULL);
return ret;
}
bool IsGPU(cl_device_id did) {
return GetDeviceType(did) == CL_DEVICE_TYPE_GPU;
}
bool IsCPU(cl_device_id did) {
return GetDeviceType(did) == CL_DEVICE_TYPE_CPU;
}
cl_platform_id GetPlatformWithType(cl_device_type type=CL_DEVICE_TYPE_ALL) {
cl_platform_id pid = 0;
for (auto& p: devices) {
for (auto& dev: p.second) {
cl_device_type dev_type;
clGetDeviceInfo(
dev,
CL_DEVICE_TYPE,
sizeof(cl_device_type),
&dev_type,
NULL);
if (dev_type == type) {
pid = p.first;
break;
}
}
}
return pid;
}
CLContext CreateSpecificContext(cl_device_type type=CL_DEVICE_TYPE_ALL) {
cl_platform_id pid = GetPlatformWithType(type);
return CLContext(pid);
}
};
class CLProgram {
void CreateProgramFromString(CLContext& context,
const char src[],
const char *options) {
cl_int err;
const char *pa[] = {src};
program = clCreateProgramWithSource(
context.context,
1,
pa,
NULL,
&err);
assert(program);
err = clBuildProgram(program, 0, NULL, options, NULL, NULL);
if (err != CL_SUCCESS) {
char buildlog[1<<20];
buildlog[0] = 0;
size_t size;
clGetContextInfo(context.context,
CL_CONTEXT_DEVICES,
0, NULL, &size);
int cnt = size/sizeof(cl_device_id);
std::vector<cl_device_id> v(cnt);
clGetContextInfo(context.context,
CL_CONTEXT_DEVICES,
cnt, &v[0], NULL);
size_t copied;
for (auto& dev: v) {
clGetProgramBuildInfo(
program,
dev,
CL_PROGRAM_BUILD_LOG,
sizeof(buildlog),
buildlog,
&copied);
assert(copied);
std::cerr << "build error" << std::endl;
std::cerr << buildlog << std::endl;
}
abort();
}
}
public:
cl_program program;
CLProgram(CLContext& context,
const char src[],
bool is_file,
const char *options=NULL) {
const char *src_str;
char *buf = NULL;
if (is_file) {
std::ifstream file(
src,
std::ios::in | std::ios::binary | std::ios::ate);
if (!file.is_open()) {
std::cout << "file can not be opened" << std::endl;
abort();
}
std::streampos pos = file.tellg();
size_t sz = pos;
buf = new char[sz+1];
file.seekg(0, std::ios::beg);
file.read(buf, sz);
buf[sz] = 0;
file.close();
src_str = buf;
} else {
src_str = src;
}
CreateProgramFromString(context, src_str, options);
delete buf;
}
void Release() {
if (program) {
clReleaseProgram(program);
}
program = NULL;
}
~CLProgram() {
Release();
}
};
class CLQueue {
public:
cl_command_queue q;
CLQueue(CLContext c,
cl_device_id d,
cl_command_queue_properties props=0) {
cl_int err;
q = clCreateCommandQueue(c.context, d, props, &err);
assert(err == CL_SUCCESS);
}
void Release() {
if (q) {
clReleaseCommandQueue(q);
}
q = NULL;
}
~CLQueue() {
Release();
}
};
class CLEvent {
public:
cl_event e;
CLEvent() = default;
cl_ulong GetTime(cl_profiling_info i) {
size_t sz;
cl_ulong v;
clGetEventProfilingInfo(e, i, sizeof(cl_ulong), &v, &sz);
assert(sz);
return v;
}
void Wait() {
clWaitForEvents(1, &e);
}
};
class CLKernel {
public:
cl_kernel kernel;
CLKernel(CLProgram& prog, const char name[]) {
cl_int err;
kernel = clCreateKernel(prog.program, name, &err);
assert(err == CL_SUCCESS);
}
void Release() {
if (kernel) {
clReleaseKernel(kernel);
}
kernel = NULL;
}
~CLKernel() {
Release();
}
void SetArg(int pos, size_t size, void *arg) {
auto ret = clSetKernelArg(kernel, pos, size, arg);
assert(ret == CL_SUCCESS);
}
void Run(
CLQueue& q,
cl_uint dim,
const size_t *gsize,
const size_t *lsize,
CLEvent *this_ev=NULL,
const size_t *goffset=0,
cl_uint nev=0,
CLEvent *evl=NULL) {
clEnqueueNDRangeKernel(
q.q,
kernel,
dim,
goffset, gsize, lsize,
nev, (cl_event *)evl, (cl_event *)this_ev);
}
};
class CLBuffer {
public:
cl_mem buff;
CLBuffer(CLContext& context,
size_t size,
void *host_ptr=NULL,
cl_mem_flags flags=CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR) {
cl_int err;
buff = clCreateBuffer(
context.context,
flags,
size,
host_ptr,
&err);
assert(err == CL_SUCCESS);
}
void Write(CLQueue& cq,
cl_bool should_block,
size_t offset,
size_t cnt,
void *src,
CLEvent *this_ev=NULL,
cl_uint nevents=0,
const CLEvent *evs=NULL) {
auto r = clEnqueueWriteBuffer(
cq.q,
buff,
should_block,
offset,
cnt,
src,
nevents,
(cl_event *)evs,
(cl_event *)this_ev);
assert(r == CL_SUCCESS);
}
void Read(CLQueue& cq,
cl_bool should_block,
size_t offset,
size_t cnt,
void *dst,
CLEvent *this_ev=NULL,
cl_uint nevents=0,
const CLEvent *evs=NULL) {
auto r = clEnqueueReadBuffer(
cq.q,
buff,
should_block,
offset,
cnt,
dst,
nevents,
(cl_event *)evs,
(cl_event *)this_ev);
assert(r == CL_SUCCESS);
}
void Release() {
if (buff) {
clReleaseMemObject(buff);
}
buff = NULL;
}
~CLBuffer() {
Release();
}
};