-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.hpp
322 lines (273 loc) · 10.8 KB
/
Utils.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
314
315
316
317
318
319
320
321
322
//Raymond Kirk - 14474219@students.lincoln.ac.uk
#pragma clang diagnostic push
#pragma ide diagnostic ignored "TemplateArgumentsIssues"
#ifndef _UTILS_H_
#define _UTILS_H_
#include <fstream>
#include <vector>
#include <iostream>
#include <sstream>
#ifdef __APPLE__
#include <OpenCL/cl.hpp>
#else
#include <CL/cl.hpp>
#endif
using namespace std;
//Print All vector values that arent zero
template<typename T>
void PrintNonZeros(std::vector<T>& v){
int c = 0;
for(auto const& val : v) {
if(val != 0)
std::cout << val << ", ";
c++;
}
std::cout << "COUNT: "<< c << std::endl;
}
//Print X rows of a vector, use ful when debugging
template<typename T>
void PrintSortedVector(std::vector<T>& v, int local_size, int rows){
int c = 0;
int r = 0;
for(auto const& val : v) {
if(c == local_size) {
std::cout << std::endl;
c = 0;
if(rows > 0)
if(++r == rows)
break;
}
if(val != 0) {
std::cout << val << ", ";
c++;
}
}
std::cout << std::endl;
}
template<typename T>
void PrintVal(std::vector<T>& v, int i){
for(auto const& val : v) {
if(val == i)
std::cout << val << ", ";
}
std::cout << std::endl;
}
void print_help() {
std::cerr << "Application usage:" << std::endl;
std::cerr << " -p : select platform " << std::endl;
std::cerr << " -d : select device" << std::endl;
std::cerr << " -l : list all platforms and devices" << std::endl;
std::cerr << " -h : print this message" << std::endl;
}
//Print preffered kernel options.
void PrintPreferredWorkGroupSize(cl::Context& context, const cl::Kernel& k, unsigned int data_size, unsigned int local_size) {
cl::Device device = context.getInfo<CL_CONTEXT_DEVICES>()[0];
size_t preferred = k.getWorkGroupInfo<CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE>(device);
size_t max_work_group_size = k.getWorkGroupInfo<CL_KERNEL_WORK_GROUP_SIZE>(device);
std::cout << "Preferred Kernel Options: " << "\n\t";
std::cout << "Preferred Work Group Size: " << preferred << "\n\t";
std::cout << "Max Work Group Size: " << max_work_group_size << "\n\t";
std::cout << "Global Size: " << data_size << "\n\t";
std::cout << "Local Size: " << local_size << '\n' << std::endl;
}
unsigned int GetPreferredWorkGroupSize(cl::Context& context, const cl::Kernel& k) {
cl::Device device = context.getInfo<CL_CONTEXT_DEVICES>()[0];
return k.getWorkGroupInfo<CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE>(device);
}
void PrintBuildErrors(cl::Context& context, cl::Program& program){
std::cerr << "Build Status: "
<< program.getBuildInfo<CL_PROGRAM_BUILD_STATUS>(context.getInfo<CL_CONTEXT_DEVICES>()[0])
<< '\n';
std::cerr << "Build Options:\t" << program.getBuildInfo<CL_PROGRAM_BUILD_OPTIONS>(
context.getInfo<CL_CONTEXT_DEVICES>()[0]) << '\n';
std::cerr << "Build Log:\t "
<< program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(context.getInfo<CL_CONTEXT_DEVICES>()[0])
<< std::endl;
}
enum ProfilingResolution {
PROF_NS = 1,
PROF_US = 1000,
PROF_MS = 1000000,
PROF_S = 1000000000
};
string GetFullProfilingInfo(const cl::Event& evnt, ProfilingResolution resolution) {
stringstream sstream;
sstream << "Queued: " << (evnt.getProfilingInfo<CL_PROFILING_COMMAND_SUBMIT>() - evnt.getProfilingInfo<CL_PROFILING_COMMAND_QUEUED>()) / resolution;
sstream << "\n\tSubmitted: " << (evnt.getProfilingInfo<CL_PROFILING_COMMAND_START>() - evnt.getProfilingInfo<CL_PROFILING_COMMAND_SUBMIT>()) / resolution;
sstream << "\n\tExecuted: " << (evnt.getProfilingInfo<CL_PROFILING_COMMAND_END>() - evnt.getProfilingInfo<CL_PROFILING_COMMAND_START>()) / resolution;
sstream << "\n\tTotal: " << (evnt.getProfilingInfo<CL_PROFILING_COMMAND_END>() - evnt.getProfilingInfo<CL_PROFILING_COMMAND_QUEUED>()) / resolution;
switch (resolution) {
case PROF_NS: sstream << "ns"; break;
case PROF_US: sstream << "us"; break;
case PROF_MS: sstream << "ms"; break;
case PROF_S: sstream << "s"; break;
default: break;
}
return sstream.str();
}
void ProfilingInfo(cl::Event& prof_event) {
std::cout << "Execution time: "
<< prof_event.getProfilingInfo<CL_PROFILING_COMMAND_END>() -
prof_event.getProfilingInfo<CL_PROFILING_COMMAND_START>() << "ns" << "\n\t"
<< GetFullProfilingInfo(prof_event, ProfilingResolution::PROF_US) << '\n' << std::endl;
};
template <typename T>
ostream& operator<< (ostream& out, const vector<T>& v) {
if (!v.empty()) {
out << '[';
copy(v.begin(), v.end(), ostream_iterator<T>(out, ", "));
out << "\b\b]";
}
return out;
}
string GetPlatformName(int platform_id) {
vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
return platforms[platform_id].getInfo<CL_PLATFORM_NAME>();
}
string GetDeviceName(int platform_id, int device_id) {
vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
vector<cl::Device> devices;
platforms[platform_id].getDevices((cl_device_type)CL_DEVICE_TYPE_ALL, &devices);
return devices[device_id].getInfo<CL_DEVICE_NAME>();
}
const char *getErrorString(cl_int error) {
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";
}
}
void CheckError(cl_int error) {
if (error != CL_SUCCESS) {
cerr << "OpenCL call failed with error " << getErrorString(error) << endl;
exit(1);
}
}
void AddSources(cl::Program::Sources& sources, const string& file_name) {
//TODO: add file existence check
ifstream file(file_name);
string* source_code = new string(istreambuf_iterator<char>(file), (istreambuf_iterator<char>()));
sources.push_back(make_pair((*source_code).c_str(), source_code->length() + 1));
}
string ListPlatformsDevices() {
stringstream sstream;
vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
sstream << "Found " << platforms.size() << " platform(s):" << endl;
for (unsigned int i = 0; i < platforms.size(); i++)
{
sstream << "\nPlatform " << i << ", " << platforms[i].getInfo<CL_PLATFORM_NAME>() << ", version: " << platforms[i].getInfo<CL_PLATFORM_VERSION>();
sstream << ", vendor: " << platforms[i].getInfo<CL_PLATFORM_VENDOR>() << endl;
// sstream << ", extensions: " << platforms[i].getInfo<CL_PLATFORM_EXTENSIONS>() << endl;
vector<cl::Device> devices;
platforms[i].getDevices((cl_device_type)CL_DEVICE_TYPE_ALL, &devices);
sstream << "\n Found " << devices.size() << " device(s):" << endl;
for (unsigned int j = 0; j < devices.size(); j++)
{
sstream << "\n Device " << j << ", " << devices[j].getInfo<CL_DEVICE_NAME>() << ", version: " << devices[j].getInfo<CL_DEVICE_VERSION>();
sstream << ", vendor: " << devices[j].getInfo<CL_DEVICE_VENDOR>();
cl_device_type device_type = devices[j].getInfo<CL_DEVICE_TYPE>();
sstream << ", type: ";
if (device_type & CL_DEVICE_TYPE_DEFAULT)
sstream << "DEFAULT ";
if (device_type & CL_DEVICE_TYPE_CPU)
sstream << "CPU ";
if (device_type & CL_DEVICE_TYPE_GPU)
sstream << "GPU ";
if (device_type & CL_DEVICE_TYPE_ACCELERATOR)
sstream << "ACCELERATOR ";
sstream << ", compute units: " << devices[j].getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>();
sstream << ", clock freq [MHz]: " << devices[j].getInfo<CL_DEVICE_MAX_CLOCK_FREQUENCY>();
sstream << ", max memory size [B]: " << devices[j].getInfo<CL_DEVICE_GLOBAL_MEM_SIZE>();
sstream << ", max allocatable memory [B]: " << devices[j].getInfo<CL_DEVICE_MAX_MEM_ALLOC_SIZE>();
sstream << endl;
}
}
sstream << "----------------------------------------------------------------" << endl;
return sstream.str();
}
cl::Context GetContext(int platform_id, int device_id) {
vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
for (unsigned int i = 0; i < platforms.size(); i++)
{
vector<cl::Device> devices;
platforms[i].getDevices((cl_device_type)CL_DEVICE_TYPE_ALL, &devices);
for (unsigned int j = 0; j < devices.size(); j++)
{
if ((i == platform_id) && (j == device_id))
return cl::Context({ devices[j] });
}
}
return cl::Context();
}
#endif
#pragma clang diagnostic pop