-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOpenCL.hpp
313 lines (252 loc) · 9.03 KB
/
OpenCL.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
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
#ifndef OPENCL_HPP
#define OPENCL_HPP
#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#define __CL_ENABLE_EXCEPTIONS
#include <CL/cl.h>
#endif
#include <array>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <memory>
#include "Backend.hpp"
#include "utils/Util.hpp"
using std::cout;
using std::cerr;
using std::endl;
const char *openCLErrorToString(const cl_int code);
#define OPENCL_CHK(ans) openCLAssert((ans), __FILE__, __LINE__)
inline void openCLAssert(const cl_int code, const char *file, const int line)
{
if (code != CL_SUCCESS) {
cout.flush();
cerr << "OpenCL error #"
<< code
<< " ("
<< file
<< ":"
<< line
<< "):"
<< endl
<< openCLErrorToString(code)
<< endl
<< endl
<< "Callstack:"
<< endl;
dump_stack_trace(code);
}
}
inline void __attribute__((noreturn))
opencl_error_callback(const char *errinfo, const void *, size_t , void *)
{
cerr << "OpenCL Error: " << errinfo << endl;
dump_stack_trace(EXIT_FAILURE);
}
class OpenCLBackend;
extern OpenCLBackend &OpenCL;
class OpenCLBackend : public Backend {
friend class Backend;
class opencl_alloc_t : public base_alloc_t
{
using mem = typename std::remove_pointer<cl_mem>::type;
static std::shared_ptr<void>
allocHostPtr(size_t sz)
{
void *hostPtr = new char[sz];
return std::shared_ptr<void>(hostPtr, [] (void* p) {
delete[] static_cast<char*>(p);
});
}
static std::shared_ptr<mem>
allocDevPtr(cl_context ctx, size_t size, bool readonly)
{
cl_int ret;
cl_mem_flags type = readonly ? CL_MEM_READ_ONLY : CL_MEM_READ_WRITE;
cl_mem device = clCreateBuffer(ctx, type, size, nullptr, &ret);
OPENCL_CHK(ret);
return {device, clReleaseMemObject};
}
protected:
std::shared_ptr<mem> devPtr;
std::vector<opencl_alloc_t> localAllocs;
public:
opencl_alloc_t() {}
opencl_alloc_t(cl_context ctx, size_t size, bool readonly)
: base_alloc_t(allocHostPtr(size), size, readonly)
, devPtr(allocDevPtr(ctx, size, readonly))
{}
opencl_alloc_t(const opencl_alloc_t& o)
: base_alloc_t(o), devPtr(o.devPtr)
, localAllocs(o.localAllocs)
{}
opencl_alloc_t(opencl_alloc_t&& o)
: base_alloc_t(std::move(o)), devPtr(std::move(o.devPtr))
, localAllocs(std::move(o.localAllocs))
{}
~opencl_alloc_t() override;
opencl_alloc_t& operator=(opencl_alloc_t&& other)
{
base_alloc_t::operator=(std::move(other));
devPtr = std::move(other.devPtr);
localAllocs = std::move(other.localAllocs);
return *this;
}
void copyHostToDevImpl() final override
{
for (auto alloc : localAllocs) alloc.copyHostToDev();
/*
OPENCL_CHK(clEnqueueWriteBuffer(queue, devPtr.get(), CL_FALSE, 0,
byteSize, hostPtr.get(), 0,
nullptr, nullptr));
*/
//if (associatedPtr) *associatedPtr = devPtr.get();
}
void copyDevToHostImpl() final override
{
/*
OPENCL_CHK(clEnqueueReadBuffer(queue, devPtr.get(), CL_TRUE, 0,
byteSize, hostPtr.get(), 0,
nullptr, nullptr));
*/
for (auto alloc : localAllocs) alloc.copyDevToHost();
}
void freeImpl() final override
{
devPtr.reset();
localAllocs.clear();
}
void registerAlloc(const opencl_alloc_t& val, void** ptr)
{
localAllocs.emplace_back(val);
localAllocs.back().associatedPtr = ptr;
*ptr = val.hostPtr.get();
}
void registerAlloc(const opencl_alloc_t& val, void* ptr)
{ registerAlloc(val, static_cast<void**>(ptr)); }
};
public:
template<typename V>
class alloc_t : public typed_alloc_t<V, opencl_alloc_t>
{
friend OpenCLBackend;
public:
alloc_t() {}
alloc_t(alloc_t&& o) : typed_alloc_t<V, opencl_alloc_t>(std::move(o))
{}
alloc_t(size_t N, bool ro)
: typed_alloc_t<V, opencl_alloc_t>(N, sizeof(V) * N, ro)
{}
alloc_t& operator=(alloc_t&& o)
{
typed_alloc_t<V, opencl_alloc_t>::operator=(std::move(o));
return *this;
}
template<typename T>
void allocLocal(T * __restrict__ *loc, size_t N)
{ allocLocal(const_cast<T**>(loc), N); }
template<typename T>
void allocLocal(T **ptr, size_t N)
{ this->registerAlloc(alloc_t<T>(N, false), static_cast<void*>(ptr)); }
};
void initKernel(cl_uint) {}
template<typename V, typename... Args>
void initKernel(cl_kernel kernel, cl_uint offset,
alloc_t<V> val, Args... args)
{
OPENCL_CHK(clSetKernelArg(kernel, offset, sizeof val.device, val.device));
initKernel(kernel, offset + 1, args...);
}
template<typename T, typename... Args>
void initKernel(cl_kernel kernel, cl_uint offset, T val, Args... args)
{
OPENCL_CHK(clSetKernelArg(kernel, offset, sizeof val, val));
initKernel(kernel, offset + 1, args...);
}
OpenCLBackend()
{
cl_int ret;
cl_uint platformCount;
cl_uint deviceCount;
OPENCL_CHK(clGetPlatformIDs(0, nullptr, &platformCount));
platforms.resize(platformCount);
OPENCL_CHK(clGetPlatformIDs(platformCount, platforms.data(), nullptr));
for (size_t i = 0; i < platformCount; i++) {
OPENCL_CHK(
clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 0,
nullptr, &deviceCount));
devicesPerPlatform_.push_back(static_cast<int>(deviceCount));
devices.emplace_back(deviceCount);
OPENCL_CHK(
clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL,
deviceCount, devices.back().data(), nullptr));
}
ctxt = clCreateContext(nullptr, static_cast<cl_uint>(devices[0].size()),
devices[0].data(), opencl_error_callback,
nullptr, &ret);
OPENCL_CHK(ret);
queue = clCreateCommandQueue(ctxt, devices[0][0], 0, &ret);
OPENCL_CHK(ret);
activePlatform = nullptr;
activeDevice = nullptr;
}
~OpenCLBackend() override
{
clFinish(queue);
clReleaseCommandQueue(queue);
clReleaseContext(ctxt);
}
public:
static OpenCLBackend &get();
void queryPlatform(size_t platform, bool verbose) override;
void queryDevice(size_t platform, int dev, bool verbose) override;
void setDevice(size_t platform, int device) override;
void setWorkSizes(size_t dims, std::vector<size_t> blockSizes,
std::vector<size_t> gridSizes,
size_t sharedMem = 0) override;
template<typename V>
alloc_t<V> alloc(size_t count)
{ return alloc_t<V>(count, false); }
template<typename V>
alloc_t<V> allocConstant(int count)
{ return alloc_t<V>(count, true); }
template<size_t N>
cl_kernel createKernel(const char *kernelName, std::array<const char*, N> files, std::array<size_t, N> sizes)
{
cl_int ret;
cl_kernel kernel;
cl_program program;
program = clCreateProgramWithSource(ctxt, N, files.data(), sizes.data(), &ret);
OPENCL_CHK(ret);
ret = clBuildProgram(program, 1, &activeDevice, nullptr, nullptr, nullptr);
if (ret != CL_SUCCESS) {
char *buildLog;
size_t logSize;
OPENCL_CHK(clGetProgramBuildInfo(program, activeDevice,
CL_PROGRAM_BUILD_LOG, 0, nullptr, &logSize));
buildLog = new char[logSize+1];
OPENCL_CHK(clGetProgramBuildInfo(program, activeDevice,
CL_PROGRAM_BUILD_LOG, logSize, buildLog, nullptr));
buildLog[logSize] = '\0';
cerr << "OpenCL Build Error:" << endl << buildLog << endl;
OPENCL_CHK(ret);
delete[] buildLog;
}
kernel = clCreateKernel(program, kernelName, &ret);
OPENCL_CHK(ret);
return kernel;
}
void runKernel(cl_kernel kernel) const;
private:
std::vector<cl_platform_id> platforms;
std::vector<std::vector<cl_device_id>> devices;
cl_platform_id activePlatform;
cl_device_id activeDevice;
cl_context ctxt;
cl_command_queue queue;
};
template<typename T>
struct isBackendAllocTrait<OpenCLBackend::alloc_t<T>> : public std::true_type
{};
#endif