-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenClKernel.cpp
333 lines (295 loc) · 12.1 KB
/
OpenClKernel.cpp
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
// Copyright (c) 2014-2015 Josh Blum
// SPDX-License-Identifier: BSL-1.0
#include "OpenClKernel.hpp"
#include <Pothos/Framework.hpp>
#include <Poco/NumberParser.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <algorithm> //min/max
/***********************************************************************
* |PothosDoc OpenCL Kernel
*
* The OpenCL Kernel block executes a kernel on supported devices.
* The kernel source is just in time (JIT) compiled by the OpenCL API.
* This block exclusively computes kernels of one dimensional arrays.
* Two and three dimensional kernels and others others are not handled by this block
*
* |category /Kernels
* |category /OpenCL
* |keywords kernel jit opencl
*
* |param deviceId[Device ID] A markup to specify OpenCL platform and device.
* The markup takes the format [platform index]:[device index]
* The platform index represents a platform ID found in clGetPlatformIDs().
* The device index represents a device ID found in clGetDeviceIDs().
* |default "0:0"
*
* |param inputTypes[Input Types] An array of input port sizes.
* |unit bytes
* |default ["float32"]
*
* |param outputTypes[Output Types] An array of output port sizes.
* |unit bytes
* |default ["float32", "float32"]
*
* |param kernelName[Kernel Name] The name of a kernel in the source.
* |default ""
* |widget StringEntry()
*
* |param kernelSource[Kernel Source] Source code for an OpenCL kernel.
* The source can either be a string representing the cl source,
* or a path to a .cl file containing the cl source code.
* |default ""
* |widget FileEntry(mode=open)
*
* |param localSize[Local Size] The number of work units/resources to allocate.
* This controls the parallelism of the kernel execution.
* |default 2
*
* |param globalFactor[Global Factor] This factor controls the global size.
* The global size is the number of kernel iterarions per call.
* Global size = number of input elements * global factor.
* |default 1.0
*
* |param productionFactor[Production Factor] This factor controls the elements produced.
* For each call to work, elements produced = number of input elements * production factor.
* |default 1.0
*
* |factory /blocks/opencl_kernel(deviceId, inputTypes, outputTypes)
* |setter setSource(kernelName, kernelSource)
* |setter setLocalSize(localSize)
* |setter setGlobalFactor(globalFactor)
* |setter setProductionFactor(productionFactor)
**********************************************************************/
class OpenClKernel : public Pothos::Block
{
public:
static Pothos::Block *make(const std::string &deviceId, const std::vector<std::string> &inputTypes, const std::vector<std::string> &outputTypes)
{
return new OpenClKernel(deviceId, inputTypes, outputTypes);
}
OpenClKernel(const std::string &deviceId, const std::vector<std::string> &inputTypes, const std::vector<std::string> &outputTypes);
~OpenClKernel(void)
{
//reset in order of creation
_kernel.reset();
_queue.reset();
_program.reset();
_context.reset();
}
void setSource(const std::string &name, const std::string &source);
void setLocalSize(const size_t size)
{
_localSize = size;
}
size_t getLocalSize(void) const
{
return _localSize;
}
void setGlobalFactor(const double factor)
{
_globalFactor = factor;
}
double getGlobalFactor(void) const
{
return _globalFactor;
}
void setProductionFactor(const double factor)
{
_productionFactor = factor;
}
double getProductionFactor(void) const
{
return _productionFactor;
}
Pothos::BufferManager::Sptr getInputBufferManager(const std::string &, const std::string &domain)
{
if (domain.empty())
{
OpenClBufferContainerArgs args;
args.mem_flags = CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR;
args.map_flags = CL_MAP_WRITE;
args.context = _context;
args.queue = _queue;
return makeOpenClBufferManager(args);
}
if (domain == _myDomain)
{
return Pothos::BufferManager::Sptr();
}
throw Pothos::PortDomainError();
}
Pothos::BufferManager::Sptr getOutputBufferManager(const std::string &, const std::string &domain)
{
if (domain.empty() or domain == _myDomain)
{
OpenClBufferContainerArgs args;
args.mem_flags = CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR;
args.map_flags = 0;
args.context = _context;
args.queue = _queue;
return makeOpenClBufferManager(args);
}
throw Pothos::PortDomainError();
}
void work(void);
void propagateLabels(const Pothos::InputPort *port)
{
for (auto outputPort : this->outputs())
{
for (const auto &label : port->labels())
{
outputPort->postLabel(label.toAdjusted(_productionFactor, 1.0));
}
}
}
private:
std::string _myDomain;
cl_platform_id _platform;
cl_device_id _device;
std::shared_ptr<cl_context> _context;
std::shared_ptr<cl_program> _program;
std::shared_ptr<cl_kernel> _kernel;
std::shared_ptr<cl_command_queue> _queue;
size_t _localSize;
double _globalFactor;
double _productionFactor;
};
OpenClKernel::OpenClKernel(const std::string &deviceId, const std::vector<std::string> &inputTypes, const std::vector<std::string> &outputTypes):
_localSize(1),
_globalFactor(1.0),
_productionFactor(1.0)
{
const auto colon = deviceId.find(":");
const auto platformIndex = Poco::NumberParser::parseUnsigned(deviceId.substr(0, colon));
const auto deviceIndex = Poco::NumberParser::parseUnsigned(deviceId.substr(colon+1));
/* Identify a platform */
cl_int err = 0;
cl_uint num_platforms = 0;
cl_platform_id platforms[64];
err = clGetPlatformIDs(64, platforms, &num_platforms);
if (err < 0) throw Pothos::Exception("OpenClKernel::clGetPlatformIDs()", clErrToStr(err));
if (platformIndex >= num_platforms) throw Pothos::Exception("OpenClKernel()", "platform index does not exist");
_platform = platforms[platformIndex];
/* Access a device */
cl_uint num_devices = 0;
cl_device_id devices[64];
err = clGetDeviceIDs(_platform, CL_DEVICE_TYPE_ALL, 64, devices, &num_devices);
if (err < 0) throw Pothos::Exception("OpenClKernel::clGetDeviceIDs()", clErrToStr(err));
if (deviceIndex >= num_devices) throw Pothos::Exception("OpenClKernel()", "device index does not exist");
_device = devices[deviceIndex];
/* Create context */
_context = lookupContextCache(_device);
/* Create ports */
_myDomain = "OpenCl_"+std::to_string(size_t(_device));
for (size_t i = 0; i < inputTypes.size(); i++)
{
this->setupInput(i, Pothos::DType(inputTypes[i]), _myDomain);
}
for (size_t i = 0; i < outputTypes.size(); i++)
{
this->setupOutput(i, Pothos::DType(outputTypes[i]), _myDomain);
}
this->registerCall(this, POTHOS_FCN_TUPLE(OpenClKernel, setSource));
this->registerCall(this, POTHOS_FCN_TUPLE(OpenClKernel, setLocalSize));
this->registerCall(this, POTHOS_FCN_TUPLE(OpenClKernel, getLocalSize));
this->registerCall(this, POTHOS_FCN_TUPLE(OpenClKernel, setGlobalFactor));
this->registerCall(this, POTHOS_FCN_TUPLE(OpenClKernel, getGlobalFactor));
this->registerCall(this, POTHOS_FCN_TUPLE(OpenClKernel, setProductionFactor));
this->registerCall(this, POTHOS_FCN_TUPLE(OpenClKernel, getProductionFactor));
}
void OpenClKernel::setSource(const std::string &kernelName, const std::string &kernelSource_)
{
cl_int err = 0;
//load kernel source from file if it ends in .cl
auto kernelSource = kernelSource_;
if (kernelSource.size() > 3 and kernelSource.substr(kernelSource.size()-3) == ".cl")
{
std::cout << "OpenClKernel block loading " << kernelSource << "..." << std::endl;
std::ifstream t(kernelSource);
if (not t.good()) throw Pothos::Exception("OpenClKernel::setSource("+kernelSource+")", "cant read file");
kernelSource = std::string((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
}
/* Create program from source */
if (kernelSource.empty()) throw Pothos::Exception("OpenClKernel::setSource()", "no source specified");
const char *sourcePtr = kernelSource.data();
const size_t sourceSize = kernelSource.size();
auto program = clCreateProgramWithSource(*_context, 1, &sourcePtr, &sourceSize, &err);
if(err < 0) throw Pothos::Exception("OpenClKernel::clCreateProgramWithSource()", clErrToStr(err));
_program.reset(new cl_program(program), clReleaseProgramPtr);
/* Build program */
err = clBuildProgram(*_program, 0, nullptr, nullptr, nullptr, nullptr);
if (err < 0)
{
/* Find size of log and print to std output */
size_t logSize = 0;
clGetProgramBuildInfo(*_program, _device, CL_PROGRAM_BUILD_LOG, 0, nullptr, &logSize);
std::vector<char> errorLog(logSize);
clGetProgramBuildInfo(*_program, _device, CL_PROGRAM_BUILD_LOG, logSize, errorLog.data(), nullptr);
std::string errorString(errorLog.begin(), errorLog.end());
throw Pothos::Exception("OpenClKernel::clBuildProgram()", errorString);
}
/* Create a command queue */
auto queue = clCreateCommandQueue(*_context, _device, 0, &err);
if (err < 0) throw Pothos::Exception("OpenClKernel::clCreateCommandQueue()", clErrToStr(err));
_queue.reset(new cl_command_queue(queue), clReleaseCommandQueuePtr);
/* Create a kernel */
auto kernel = clCreateKernel(*_program, kernelName.c_str(), &err);
if (err < 0) throw Pothos::Exception("OpenClKernel::clCreateKernel()", clErrToStr(err));
_kernel.reset(new cl_kernel(kernel), clReleaseKernelPtr);
}
void OpenClKernel::work(void)
{
const auto &inputs = this->inputs();
const auto &outputs = this->outputs();
std::vector<cl_mem> inputBuffs(inputs.size());
std::vector<cl_mem> outputBuffs(outputs.size());
cl_int err = 0;
if (this->workInfo().minElements == 0) return;
//calculate number of elements
size_t inputElems = this->workInfo().minInElements;
size_t outputElems = this->workInfo().minOutElements;
if (_productionFactor > 1.0)
{
outputElems = std::min<size_t>(inputElems*_productionFactor, outputElems);
inputElems = outputElems/_productionFactor;
}
else
{
inputElems = std::min<size_t>(outputElems/_productionFactor, inputElems);
outputElems = inputElems*_productionFactor;
}
size_t globalSize = inputElems*_globalFactor;
/* Create data buffer */
size_t argNo = 0;
for (size_t i = 0; i < inputs.size(); i++)
{
inputBuffs[i] = getClBufferFromManaged(inputs[i]->buffer().getManagedBuffer());
err = clSetKernelArg(*_kernel, argNo++, sizeof(cl_mem), &inputBuffs[i]);
if (err < 0) throw Pothos::Exception("OpenClKernel::work::clSetKernelArg()", clErrToStr(err));
}
for (size_t i = 0; i < outputs.size(); i++)
{
outputBuffs[i] = getClBufferFromManaged(outputs[i]->buffer().getManagedBuffer());
err = clSetKernelArg(*_kernel, argNo++, sizeof(cl_mem), &outputBuffs[i]);
if (err < 0) throw Pothos::Exception("OpenClKernel::work::clSetKernelArg()", clErrToStr(err));
}
/* Enqueue kernel */
err = clEnqueueNDRangeKernel(*_queue, *_kernel, 1, nullptr, &globalSize, &_localSize, 0, nullptr, nullptr);
if (err < 0) throw Pothos::Exception("OpenClKernel::work::enqueueKernel()", clErrToStr(err));
/* Read the kernel's output */
for (size_t i = 0; i < inputs.size(); i++)
{
inputs[i]->consume(inputElems);
}
for (size_t i = 0; i < outputs.size(); i++)
{
err = clEnqueueReadBuffer(*_queue, outputBuffs[i], CL_TRUE, 0,
outputElems*outputs[i]->dtype().size(), outputs[i]->buffer().as<void *>(), 0, nullptr, nullptr);
if (err < 0) throw Pothos::Exception("OpenClKernel::work::clEnqueueReadBuffer()", clErrToStr(err));
outputs[i]->produce(outputElems);
}
}
static Pothos::BlockRegistry registerOpenClKernel(
"/blocks/opencl_kernel", &OpenClKernel::make);