-
Notifications
You must be signed in to change notification settings - Fork 0
/
oclHelper.cpp
executable file
·238 lines (209 loc) · 7 KB
/
oclHelper.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
// Copyright 2014, Xilinx Inc.
// All rights reserved.
#include "oclHelper.h"
#include <cstring>
#include <fstream>
#include <iostream>
#include <vector>
static int loadFile2Memory(const char *filename, char **result)
{
int size = 0;
std::ifstream stream(filename, std::ifstream::binary);
if (!stream) {
return -1;
}
stream.seekg(0, stream.end);
size = stream.tellg();
stream.seekg(0, stream.beg);
*result = new char[size + 1];
stream.read(*result, size);
if (!stream) {
return -2;
}
stream.close();
(*result)[size] = 0;
return size;
}
static void getDeviceVersion(oclHardware& hardware)
{
char versionString[512];
size_t size = 0;
cl_int err = clGetDeviceInfo (hardware.mDevice, CL_DEVICE_VERSION, 511, versionString, &size);
if ( err != CL_SUCCESS) {
std::cout << oclErrorCode(err) << "\n";
return;
}
unsigned major = 0;
unsigned minor = 0;
unsigned state = 0;
for (size_t i = 0; i < size; i++) {
if (versionString[i] == ' ') {
state++;
continue;
}
if (versionString[i] == '.') {
state++;
continue;
}
if (state == 0) {
continue;
}
if (state == 1) {
major *= 10;
major += (versionString[i] - '0');
continue;
}
if (state == 2) {
minor *= 10;
minor += (versionString[i] - '0');
continue;
}
break;
}
hardware.mMajorVersion = major;
hardware.mMinorVersion = minor;
}
static int compileProgram(const oclHardware &hardware, oclSoftware &software)
{
cl_int err = clBuildProgram(software.mProgram, 1, &hardware.mDevice, software.mCompileOptions, 0, 0);
if (err != CL_SUCCESS)
{
std::cout << oclErrorCode(err) << "\n";
size_t size = 0;
err = clGetProgramBuildInfo(software.mProgram, hardware.mDevice, CL_PROGRAM_BUILD_LOG, 0, 0, &size);
if (err != CL_SUCCESS) {
std::cout << oclErrorCode(err) << "\n";
return -1;
}
std::vector<char> log(size + 1);
err = clGetProgramBuildInfo(software.mProgram, hardware.mDevice, CL_PROGRAM_BUILD_LOG, size, &log[0], 0);
if (err != CL_SUCCESS) {
std::cout << oclErrorCode(err) << "\n";
return -1;
}
std::cout << &log[0] << "\n";
return -1;
}
software.mKernel = clCreateKernel(software.mProgram, software.mKernelName, NULL);
if (software.mKernel == 0)
{
std::cout << oclErrorCode(err) << "\n";
return -2;
}
return 0;
}
oclHardware getOclHardware(cl_device_type type, const char *target_device)
{
oclHardware hardware = {0, 0, 0, 0, 0, 0};
cl_platform_id platforms[16] = { 0 };
cl_device_id devices[16];
cl_device_id device_id;
char platformName[256];
char deviceName[256];
cl_uint platformCount = 0;
cl_int err = clGetPlatformIDs(0, 0, &platformCount);
err = clGetPlatformIDs(16, platforms, &platformCount);
if (err != CL_SUCCESS) {
std::cout << oclErrorCode(err) << "\n";
return hardware;
}
for (cl_uint i = 0; i < platformCount; i++) {
err = clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME, 256, platformName, 0);
if (err != CL_SUCCESS) {
std::cout << oclErrorCode(err) << "\n";
return hardware;
}
if (strcmp(platformName, "Xilinx") != 0) {
//skip non-Xilinx platform
continue;
}
//std::cout << "INFO: Selected platform " << platformName << std::endl;
cl_uint deviceCount = 0;
err = clGetDeviceIDs(platforms[i], type, 16, devices, &deviceCount);
if ((err != CL_SUCCESS) || (deviceCount == 0)) {
continue;
}
//iterate all devices to find the target device
cl_uint idev;
for (idev=0; idev < deviceCount; idev++) {
err = clGetDeviceInfo(devices[idev], CL_DEVICE_NAME, 256, deviceName, 0);
if (err != CL_SUCCESS) {
std::cout << oclErrorCode(err) << "\n";
return hardware;
}
if (strcmp(deviceName, target_device) == 0) {
device_id = devices[i];
break;
}
}
if (idev == deviceCount) {
std::cout << "ERROR: target device " << target_device << " not found " << std::endl;
return hardware;
}
cl_context_properties contextData[3] = {CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[i], 0};
cl_context context = clCreateContextFromType(contextData, type, 0, 0, &err);
if (err != CL_SUCCESS) {
continue;
}
cl_command_queue queue = clCreateCommandQueue(context, device_id, 0, &err);
if (err != CL_SUCCESS) {
std::cout << oclErrorCode(err) << "\n";
return hardware;
}
hardware.mPlatform = platforms[i];
hardware.mContext = context;
hardware.mDevice = device_id;
hardware.mQueue = queue;
getDeviceVersion(hardware);
std::cout << "Platform = " << platformName << "\n";
std::cout << "Device = " << deviceName << "\n";
std::cout << "OpenCL Version = " << hardware.mMajorVersion << '.' << hardware.mMinorVersion << "\n";
return hardware;
}
return hardware;
}
int getOclSoftware(oclSoftware &soft, const oclHardware &hardware)
{
cl_device_type deviceType = CL_DEVICE_TYPE_DEFAULT;
cl_int err = clGetDeviceInfo(hardware.mDevice, CL_DEVICE_TYPE, sizeof(deviceType), &deviceType, 0);
if ( err != CL_SUCCESS) {
std::cout << oclErrorCode(err) << "\n";
return -1;
}
unsigned char *kernelCode = 0;
std::cout << "Loading " << soft.mFileName << "\n";
int size = loadFile2Memory(soft.mFileName, (char **) &kernelCode);
if (size < 0) {
std::cout << "Failed to load kernel\n";
return -2;
}
if (deviceType == CL_DEVICE_TYPE_ACCELERATOR) {
size_t n = size;
soft.mProgram = clCreateProgramWithBinary(hardware.mContext, 1, &hardware.mDevice, &n,
(const unsigned char **) &kernelCode, 0, &err);
}
else {
soft.mProgram = clCreateProgramWithSource(hardware.mContext, 1, (const char **)&kernelCode, 0, &err);
}
if (!soft.mProgram || (err != CL_SUCCESS)) {
std::cout << oclErrorCode(err) << "\n";
return -3;
}
int status = compileProgram(hardware, soft);
delete [] kernelCode;
return status;
}
void release(oclSoftware& software)
{
clReleaseKernel(software.mKernel);
clReleaseProgram(software.mProgram);
}
void release(oclHardware& hardware)
{
clReleaseCommandQueue(hardware.mQueue);
clReleaseContext(hardware.mContext);
if ((hardware.mMajorVersion >= 1) && (hardware.mMinorVersion > 1)) {
// Only available in OpenCL >= 1.2
clReleaseDevice(hardware.mDevice);
}
}