-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.c
228 lines (185 loc) · 7.04 KB
/
init.c
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
#include "hetker_lib.h"
#include "kernels.h"
void hetkerInit() {
deviceList.length = 0;
for (int i = 0; i < HETKER_KERNELS_COUNT; i++) {
KERNEL_LENGTHS[i] = strlen(KERNELS[i]);
}
}
void hetkerFree() {
for (size_t i = 0; i < deviceList.length; i++) {
Device* device = deviceList.data[i];
clReleaseCommandQueue(device->queue);
clReleaseContext(device->context);
clReleaseDevice(device->id);
}
free(deviceList.data);
}
void hetkerEnableLogging() {
loggingEnabled = 1;
}
Result* hetkerInitDevice(cl_device_id deviceId) {
if (deviceList.length == CL_MAX_DEVICES) {
return loggingResult(1, "Can't init new deviceId: maximum number of initialized devices exceed");
}
cl_int errCode;
cl_context context = clCreateContext( NULL,
1,
&deviceId,
NULL, NULL, &errCode);
if (errCode != CL_SUCCESS) {
return loggingResult(errCode, "Error on create context");
}
cl_command_queue queue = clCreateCommandQueue(context,
deviceId,
CL_QUEUE_PROFILING_ENABLE, &errCode);
if (errCode != CL_SUCCESS) {
clReleaseContext(context);
return loggingResult(errCode, "Error on create command queue");
}
// cl_program program = clCreateProgramWithSource(context,
// 4,
// &KERNELS[0],
// &KERNEL_LENGTHS[0], &errCode);
//
// if (errCode != CL_SUCCESS) {
// clReleaseCommandQueue(queue);
// clReleaseContext(context);
// return loggingResult(errCode, "Error on create program");
// }
//
// char options[100];
//// sprintf(options, "-D BATCH_SIZE=%i", batchSize);
//
// if ((errCode = clBuildProgram(program, 1, &deviceId, options, NULL, NULL)) != 0) {
//
// size_t log_size;
// clGetProgramBuildInfo(program, deviceId, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
//
// char *log = malloc(log_size);
// clGetProgramBuildInfo(program, deviceId, CL_PROGRAM_BUILD_LOG, log_size, log, NULL);
//
// char* text = malloc(log_size + 50 * sizeof(char));
// sprintf(text, "Error on build program: %i\n%s\n", errCode, log);
// free(log);
// Result* res = loggingResult(errCode, text);
// free(text);
//
// clReleaseProgram(program);
// clReleaseCommandQueue(queue);
// clReleaseContext(context);
// return res;
// }
Device* device = malloc(sizeof(Device));
device->id = deviceId;
device->context = context;
device->queue = queue;
deviceList.data[deviceList.length] = device;
return resultCode(0);
}
int deviceComparator(const cl_device_id *d1, const cl_device_id *d2) {
cl_device_type type1, type2;
cl_bool mem1, mem2;
clGetDeviceInfo(*d1, CL_DEVICE_TYPE, sizeof(cl_device_type), &type1, NULL);
clGetDeviceInfo(*d2, CL_DEVICE_TYPE, sizeof(cl_device_type), &type2, NULL);
if (type1 == CL_DEVICE_TYPE_CPU) {
return type2 != CL_DEVICE_TYPE_CPU;
} else if (type2 == CL_DEVICE_TYPE_CPU) {
return -1;
}
clGetDeviceInfo(*d1, CL_DEVICE_HOST_UNIFIED_MEMORY, sizeof(cl_bool), &mem1, NULL);
clGetDeviceInfo(*d2, CL_DEVICE_HOST_UNIFIED_MEMORY, sizeof(cl_bool), &mem2, NULL);
if (mem1 == CL_FALSE) {
return -(mem2 != CL_FALSE);
} else {
return mem2 != CL_TRUE;
}
}
cl_device_id getPreferredDevice(int index) {
cl_int err;
cl_uint numPlatforms;
err = clGetPlatformIDs(0, NULL, &numPlatforms);
if (checkErr(err, "Unable to get platform count")) {
return 0;
}
//printf("Platforms: %i\n", numPlatforms);
cl_platform_id platform[numPlatforms];
err = clGetPlatformIDs(numPlatforms, platform, NULL);
if (checkErr(err, "Unable to get platforms")) {
return 0;
}
// printf("Detected %d OpenCL platforms:\n", numPlatforms);
// printf("----------------------------------------\n");
// calculate general number of devices
cl_uint deviceCount = 0;
for (cl_uint i = 0; i < numPlatforms; i++) {
cl_uint numDevices;
err = clGetDeviceIDs(platform[i], CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
if (err != CL_SUCCESS && err != CL_DEVICE_NOT_FOUND) {
printf("Unable to get GPU count: %d", err);
return 0;
}
deviceCount += numDevices;
err = clGetDeviceIDs(platform[i], CL_DEVICE_TYPE_CPU, 0, NULL, &numDevices);
if (err != CL_SUCCESS && err != CL_DEVICE_NOT_FOUND) {
printf("Unable to get CPU count: %d", err);
return 0;
}
deviceCount += numDevices;
}
if (deviceCount == 0) {
printf("There are no OpenCL devices available\n");
return 0;
}
cl_device_id deviceIds[deviceCount];
cl_uint deviceIndex = 0;
for (cl_uint i = 0; i < numPlatforms; i++) {
cl_uint numDevices;
err = clGetDeviceIDs(platform[i], CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
if (err == CL_SUCCESS) {
cl_device_id gpus[numDevices];
err = clGetDeviceIDs(platform[i], CL_DEVICE_TYPE_GPU, numDevices, gpus, 0);
if (err == CL_SUCCESS) {
for (cl_uint j = 0; j < numDevices; j++) {
// add to main device list
deviceIds[deviceIndex++] = gpus[j];
}
}
}
err = clGetDeviceIDs(platform[i], CL_DEVICE_TYPE_CPU, 0, NULL, &numDevices);
if (err == CL_SUCCESS) {
cl_device_id cpus[numDevices];
err = clGetDeviceIDs(platform[i], CL_DEVICE_TYPE_CPU, numDevices, cpus, 0);
if (err == CL_SUCCESS) {
for (cl_uint j = 0; j < numDevices; j++) {
// add to main device list
deviceIds[deviceIndex++] = cpus[j];
}
}
}
}
qsort(deviceIds, deviceCount, sizeof(cl_device_id), (int (*)(const void *, const void *)) deviceComparator);
//printf("deviceCount: %i\n", deviceCount);
if (index < 0 || index >= deviceCount) {
index = 0;
}
cl_device_id device = deviceIds[index];
printf("----------------------------------------\n");
// Device name
size_t deviceNameSize;
err = clGetDeviceInfo(device, CL_DEVICE_NAME, 0, NULL, &deviceNameSize);
if (err != CL_SUCCESS) {
printf("Can't get device name for device %d. Error code: %d", device, err);
return 0;
}
cl_uchar deviceName[deviceNameSize];
err = clGetDeviceInfo(device, CL_DEVICE_NAME, deviceNameSize, deviceName, 0);
if (err == CL_SUCCESS) {
printf("Running on device: %s\n", deviceName);
} else {
printf("Can't get device name for device %d. Error code: %d", device, err);
return 0;
}
printf("----------------------------------------\n");
return deviceIds[index];
}