-
Notifications
You must be signed in to change notification settings - Fork 0
/
oclhelpers.cpp
269 lines (246 loc) · 7.38 KB
/
oclhelpers.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
#include "oclhelpers.hpp"
#include <fstream>
#include <iostream>
#include <sstream>
#include <tuple>
namespace oclhelpers {
std::vector<cl::Platform> get_platforms() {
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
if (!platforms.size()) {
throw OCLHelpersException("No platform found!");
}
return platforms;
}
cl::Platform get_platform_matching(std::string_view s) {
auto platforms = get_platforms();
for (auto &p : platforms) {
auto pname = p.getInfo<CL_PLATFORM_NAME>();
if (pname.find(s) != std::string::npos)
return p;
}
}
cl::Platform get_default_platform() { return get_platforms()[0]; }
std::vector<cl::Device> get_devices(const cl::Platform &p, int type) {
std::vector<cl::Device> devices;
p.getDevices(type, &devices);
return devices;
}
std::vector<cl::Device> get_gpus(const cl::Platform &p) {
return get_devices(p, CL_DEVICE_TYPE_GPU);
}
std::vector<cl::Device> get_cpus(const cl::Platform &p) {
return get_devices(p, CL_DEVICE_TYPE_CPU);
}
cl::Device get_default_device(const cl::Platform &p) { return get_gpus(p)[0]; }
// fixme
cl::Device get_default_gpu(const cl::Platform &p) { return get_gpus(p)[0]; }
cl::Device get_default_cpu(const cl::Platform &p) { return get_cpus(p)[0]; }
std::string read_kernel_from_file(const std::string &filename) {
std::ifstream is(filename);
return std::string(std::istreambuf_iterator<char>(is),
std::istreambuf_iterator<char>());
}
cl::Program make_program_from_file(cl::Context &ctx,
const std::string &filename) {
cl::Program::Sources sources;
auto kernel_code = read_kernel_from_file(filename);
if (kernel_code.empty()) {
throw OCLHelpersException("Kernel code is empty!");
}
return {ctx, kernel_code};
}
void build(cl::Program &program, const cl::Device &device) {
if (program.build({device}) != CL_SUCCESS) {
std::stringstream ss;
ss << "Building failed: "
<< program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device) << std::endl;
throw OCLHelpersException(ss.str());
}
}
std::tuple<cl::Platform, cl::Device, cl::Context, cl::Program>
compile_file_with(const std::string &filename, const cl::Platform &platform,
const cl::Device &device) {
cl::Context ctx({device});
auto program = make_program_from_file(ctx, filename);
build(program, device);
return std::make_tuple(platform, device, ctx, program);
}
std::tuple<cl::Platform, cl::Device, cl::Context, cl::Program>
compile_file_with_defaults(const std::string &filename) {
auto platform = get_default_platform();
auto device = get_default_device(platform);
return compile_file_with(filename, platform, device);
}
std::tuple<cl::Platform, cl::Device, cl::Context, cl::Program>
compile_file_with_default_cpu(const std::string &filename) {
for (auto &p : get_platforms()) {
if (get_cpus(p).size()) {
return compile_file_with(filename, p, get_default_cpu(p));
}
}
throw OCLHelpersException("No cpu found!");
}
std::tuple<cl::Platform, cl::Device, cl::Context, cl::Program>
compile_file_with_default_cpu(cl::Platform &platform,
const std::string &filename) {
auto device = get_default_cpu(platform);
return compile_file_with(filename, platform, device);
}
std::tuple<cl::Platform, cl::Device, cl::Context, cl::Program>
compile_file_with_default_gpu(const std::string &filename) {
for (auto &p : get_platforms()) {
if (get_gpus(p).size()) {
return compile_file_with(filename, p, get_default_gpu(p));
}
}
throw OCLHelpersException("No gpu found!");
}
std::tuple<cl::Platform, cl::Device, cl::Context, cl::Program>
compile_file_with_default_gpu(cl::Platform &platform,
const std::string &filename) {
auto device = get_default_gpu(platform);
return compile_file_with(filename, platform, device);
}
std::string get_error_string(int error) {
// source:
// https://stackoverflow.com/questions/24326432/convenient-way-to-show-opencl-error-codes
switch (error) {
// run-time and JIT compiler errors
case 0:
return "CL_SUCCESS";
case -1:
return "CL_DEVICE_NOT_FOUND";
case -2:
return "CL_DEVICE_NOT_AVAILABLE";
case -3:
return "CL_COMPILER_NOT_AVAILABLE";
case -4:
return "CL_MEM_OBJECT_ALLOCATION_FAILURE";
case -5:
return "CL_OUT_OF_RESOURCES";
case -6:
return "CL_OUT_OF_HOST_MEMORY";
case -7:
return "CL_PROFILING_INFO_NOT_AVAILABLE";
case -8:
return "CL_MEM_COPY_OVERLAP";
case -9:
return "CL_IMAGE_FORMAT_MISMATCH";
case -10:
return "CL_IMAGE_FORMAT_NOT_SUPPORTED";
case -11:
return "CL_BUILD_PROGRAM_FAILURE";
case -12:
return "CL_MAP_FAILURE";
case -13:
return "CL_MISALIGNED_SUB_BUFFER_OFFSET";
case -14:
return "CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST";
case -15:
return "CL_COMPILE_PROGRAM_FAILURE";
case -16:
return "CL_LINKER_NOT_AVAILABLE";
case -17:
return "CL_LINK_PROGRAM_FAILURE";
case -18:
return "CL_DEVICE_PARTITION_FAILED";
case -19:
return "CL_KERNEL_ARG_INFO_NOT_AVAILABLE";
// compile-time errors
case -30:
return "CL_INVALID_VALUE";
case -31:
return "CL_INVALID_DEVICE_TYPE";
case -32:
return "CL_INVALID_PLATFORM";
case -33:
return "CL_INVALID_DEVICE";
case -34:
return "CL_INVALID_CONTEXT";
case -35:
return "CL_INVALID_QUEUE_PROPERTIES";
case -36:
return "CL_INVALID_COMMAND_QUEUE";
case -37:
return "CL_INVALID_HOST_PTR";
case -38:
return "CL_INVALID_MEM_OBJECT";
case -39:
return "CL_INVALID_IMAGE_FORMAT_DESCRIPTOR";
case -40:
return "CL_INVALID_IMAGE_SIZE";
case -41:
return "CL_INVALID_SAMPLER";
case -42:
return "CL_INVALID_BINARY";
case -43:
return "CL_INVALID_BUILD_OPTIONS";
case -44:
return "CL_INVALID_PROGRAM";
case -45:
return "CL_INVALID_PROGRAM_EXECUTABLE";
case -46:
return "CL_INVALID_KERNEL_NAME";
case -47:
return "CL_INVALID_KERNEL_DEFINITION";
case -48:
return "CL_INVALID_KERNEL";
case -49:
return "CL_INVALID_ARG_INDEX";
case -50:
return "CL_INVALID_ARG_VALUE";
case -51:
return "CL_INVALID_ARG_SIZE";
case -52:
return "CL_INVALID_KERNEL_ARGS";
case -53:
return "CL_INVALID_WORK_DIMENSION";
case -54:
return "CL_INVALID_WORK_GROUP_SIZE";
case -55:
return "CL_INVALID_WORK_ITEM_SIZE";
case -56:
return "CL_INVALID_GLOBAL_OFFSET";
case -57:
return "CL_INVALID_EVENT_WAIT_LIST";
case -58:
return "CL_INVALID_EVENT";
case -59:
return "CL_INVALID_OPERATION";
case -60:
return "CL_INVALID_GL_OBJECT";
case -61:
return "CL_INVALID_BUFFER_SIZE";
case -62:
return "CL_INVALID_MIP_LEVEL";
case -63:
return "CL_INVALID_GLOBAL_WORK_SIZE";
case -64:
return "CL_INVALID_PROPERTY";
case -65:
return "CL_INVALID_IMAGE_DESCRIPTOR";
case -66:
return "CL_INVALID_COMPILER_OPTIONS";
case -67:
return "CL_INVALID_LINKER_OPTIONS";
case -68:
return "CL_INVALID_DEVICE_PARTITION_COUNT";
// extension errors
case -1000:
return "CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR";
case -1001:
return "CL_PLATFORM_NOT_FOUND_KHR";
case -1002:
return "CL_INVALID_D3D10_DEVICE_KHR";
case -1003:
return "CL_INVALID_D3D10_RESOURCE_KHR";
case -1004:
return "CL_D3D10_RESOURCE_ALREADY_ACQUIRED_KHR";
case -1005:
return "CL_D3D10_RESOURCE_NOT_ACQUIRED_KHR";
default:
return "Unknown OpenCL error";
}
}
} // namespace oclhelpers