-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasiccl.h
More file actions
executable file
·241 lines (198 loc) · 8.75 KB
/
basiccl.h
File metadata and controls
executable file
·241 lines (198 loc) · 8.75 KB
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
#ifndef BASICCL_H
#define BASICCL_H
#ifdef __APPLE__
#include "OpenCL/opencl.h"
#else
#include "CL/opencl.h"
#endif
#define CL_STRING_LENGTH 128
class BasicCL
{
public:
BasicCL();
int getNumPlatform(cl_uint *numPlatforms);
int getPlatformIDs(cl_platform_id *platforms, cl_uint numPlatforms);
int getPlatformInfo(cl_platform_id platform, char *platformVendor, char *platformVersion);
int getNumCpuDevices(cl_platform_id platform, cl_uint *numCpuDevices);
int getNumGpuDevices(cl_platform_id platform, cl_uint *numGpuDevices);
int getCpuDeviceIDs(cl_platform_id platform, cl_uint numCpuDevices, cl_device_id *cpuDevices);
int getGpuDeviceIDs(cl_platform_id platform, cl_uint numGpuDevices, cl_device_id *gpuDevices);
int getDeviceInfo(cl_device_id device, char *deviceName, char *deviceVersion,
int *deviceComputeUnits, cl_ulong *deviceGlobalMem,
cl_ulong *deviceLocalMem, int *maxSubDevices);
int getContext(cl_context *context, cl_device_id *devices, cl_uint numDevices);
int getCommandQueue(cl_command_queue *commandQueue, cl_context context, cl_device_id device);
int getCommandQueueProfilingEnable(cl_command_queue *commandQueue, cl_context context, cl_device_id device);
int getProgram(cl_program *program, cl_context context, const char *kernelSourceCode);
int getProgramFromFile(cl_program *program, cl_context context, const char *sourceFilename);
int getKernel(cl_kernel *kernel, cl_program program, const char *kernelName);
int getEventTimer(cl_event event,
cl_ulong *queuedTime, cl_ulong *submitTime,
cl_ulong *startTime, cl_ulong *endTime);
private:
cl_int _ciErr;
};
BasicCL::BasicCL()
{
}
int BasicCL::getNumPlatform(cl_uint *numPlatforms)
{
_ciErr = clGetPlatformIDs(0, NULL, numPlatforms);
return _ciErr;
}
int BasicCL::getPlatformIDs(cl_platform_id *platforms, cl_uint numPlatforms)
{
_ciErr = clGetPlatformIDs(numPlatforms, platforms, NULL);
return _ciErr;
}
int BasicCL::getPlatformInfo(cl_platform_id platform, char *platformVendor, char *platformVersion)
{
_ciErr = clGetPlatformInfo(platform, CL_PLATFORM_VENDOR, CL_STRING_LENGTH*sizeof(char), platformVendor, NULL);
if(_ciErr != CL_SUCCESS) return _ciErr;
_ciErr = clGetPlatformInfo(platform, CL_PLATFORM_VERSION, CL_STRING_LENGTH*sizeof(char), platformVersion, NULL);
if(_ciErr != CL_SUCCESS) return _ciErr;
return CL_SUCCESS;
}
int BasicCL::getNumCpuDevices(cl_platform_id platform, cl_uint *numCpuDevices)
{
_ciErr = clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, 0, NULL, numCpuDevices);
return _ciErr;
}
int BasicCL::getCpuDeviceIDs(cl_platform_id platform, cl_uint numCpuDevices, cl_device_id *CpuDevices)
{
_ciErr = clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, numCpuDevices, CpuDevices, NULL);
return _ciErr;
}
int BasicCL::getNumGpuDevices(cl_platform_id platform, cl_uint *numGpuDevices)
{
_ciErr = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, numGpuDevices);
return _ciErr;
}
int BasicCL::getGpuDeviceIDs(cl_platform_id platform, cl_uint numGpuDevices, cl_device_id *GpuDevices)
{
_ciErr = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numGpuDevices, GpuDevices, NULL);
return _ciErr;
}
int BasicCL::getDeviceInfo(cl_device_id device, char *deviceName, char *deviceVersion,
int *deviceComputeUnits, cl_ulong *deviceGlobalMem,
cl_ulong *deviceLocalMem, int *maxSubDevices)
{
_ciErr = clGetDeviceInfo(device, CL_DEVICE_NAME, CL_STRING_LENGTH*sizeof(char), deviceName, NULL);
if(_ciErr != CL_SUCCESS) return _ciErr;
_ciErr = clGetDeviceInfo(device, CL_DEVICE_VERSION, CL_STRING_LENGTH*sizeof(char), deviceVersion, NULL);
if(_ciErr != CL_SUCCESS) return _ciErr;
_ciErr = clGetDeviceInfo(device, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(cl_uint), deviceComputeUnits, NULL);
if(_ciErr != CL_SUCCESS) return _ciErr;
_ciErr = clGetDeviceInfo(device, CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(cl_ulong), deviceGlobalMem, NULL);
if(_ciErr != CL_SUCCESS) return _ciErr;
_ciErr = clGetDeviceInfo(device, CL_DEVICE_LOCAL_MEM_SIZE, sizeof(cl_ulong), deviceLocalMem, NULL);
if(_ciErr != CL_SUCCESS) return _ciErr;
//_ciErr = clGetDeviceInfo(device, CL_DEVICE_PARTITION_MAX_SUB_DEVICES, sizeof(cl_uint), maxSubDevices, NULL);
//if(_ciErr != CL_SUCCESS) return _ciErr;
return CL_SUCCESS;
}
int BasicCL::getContext(cl_context *context, cl_device_id *devices, cl_uint numDevices)
{
context[0] = clCreateContext(0, numDevices, devices, NULL, NULL, &_ciErr);
//cout << "------------ _ciErr " << _ciErr << endl;
return _ciErr;
}
int BasicCL::getCommandQueue(cl_command_queue *commandQueue, cl_context context, cl_device_id device)
{
// use clCreateCommandQueueWithProperties for ocl2.0
//commandQueue[0] = clCreateCommandQueue(context, device, CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &_ciErr);
cl_queue_properties *props = NULL;
commandQueue[0] = clCreateCommandQueueWithProperties(context, device, props, &_ciErr);
//CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE
//CL_QUEUE_PROFILING_ENABLE
//cout << "------------ _ciErr " << _ciErr << endl;
return _ciErr;
}
int BasicCL::getCommandQueueProfilingEnable(cl_command_queue *commandQueue, cl_context context, cl_device_id device)
{
// use clCreateCommandQueueWithProperties for ocl2.0
//commandQueue[0] = clCreateCommandQueue(context, device, CL_QUEUE_PROFILING_ENABLE, &_ciErr);
cl_queue_properties props[] = {CL_QUEUE_PROPERTIES, CL_QUEUE_PROFILING_ENABLE, 0};
commandQueue[0] = clCreateCommandQueueWithProperties(context, device, props, &_ciErr);
//CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE
//CL_QUEUE_PROFILING_ENABLE
//cout << "------------ _ciErr " << _ciErr << endl;
return _ciErr;
}
int BasicCL::getProgram(cl_program *program, cl_context context, const char *kernelSourceCode)
{
size_t SourceSize[] = { strlen(kernelSourceCode)};
program[0] = clCreateProgramWithSource(context, 1, &kernelSourceCode, SourceSize, &_ciErr); //cout << "------------ _ciErr " << _ciErr << endl;
if(_ciErr != CL_SUCCESS) return _ciErr;
//_ciErr = clBuildProgram(program[0], 0, NULL, "-cl-opt-disable", NULL, NULL); //cout << "------------ _ciErr " << _ciErr << endl;
_ciErr = clBuildProgram(program[0], 0, NULL, NULL, NULL, NULL); //cout << "------------ _ciErr " << _ciErr << endl;
if(_ciErr != CL_SUCCESS) return _ciErr;
return CL_SUCCESS;
}
char* readSource(const char *sourceFilename) {
FILE *fp;
int err;
int size;
char *source;
fp = fopen(sourceFilename, "rb");
if(fp == NULL) {
printf("Could not open kernel file: %s\n", sourceFilename);
exit(-1);
}
err = fseek(fp, 0, SEEK_END);
if(err != 0) {
printf("Error seeking to end of file\n");
exit(-1);
}
size = ftell(fp);
if(size < 0) {
printf("Error getting file position\n");
exit(-1);
}
err = fseek(fp, 0, SEEK_SET);
if(err != 0) {
printf("Error seeking to start of file\n");
exit(-1);
}
source = (char*)malloc(size+1);
if(source == NULL) {
printf("Error allocating %d bytes for the program source\n", size+1);
exit(-1);
}
err = fread(source, 1, size, fp);
if(err != size) {
printf("only read %d bytes\n", err);
exit(0);
}
source[size] = '\0';
fclose (fp);
return source;
}
int BasicCL::getProgramFromFile(cl_program *program, cl_context context, const char *sourceFilename)
{
char *kernelSourceCode;
kernelSourceCode = readSource(sourceFilename);
//cout << kernelSourceCode << endl;
int err = getProgram(program, context, kernelSourceCode);
return err;
}
int BasicCL::getKernel(cl_kernel *kernel, cl_program program, const char *kernelName)
{
kernel[0] = clCreateKernel(program, kernelName, &_ciErr);
return _ciErr;
}
int BasicCL::getEventTimer(cl_event event,
cl_ulong *queuedTime, cl_ulong *submitTime,
cl_ulong *startTime, cl_ulong *endTime)
{
_ciErr = clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_QUEUED, sizeof(cl_ulong), queuedTime, NULL);
if(_ciErr != CL_SUCCESS) return _ciErr;
_ciErr = clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_SUBMIT, sizeof(cl_ulong), submitTime, NULL);
if(_ciErr != CL_SUCCESS) return _ciErr;
_ciErr = clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_START, sizeof(cl_ulong), startTime, NULL);
if(_ciErr != CL_SUCCESS) return _ciErr;
_ciErr = clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_END, sizeof(cl_ulong), endTime, NULL);
if(_ciErr != CL_SUCCESS) return _ciErr;
return CL_SUCCESS;
}
#endif // BASICCL_H