1 /*
2 * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 #include "opencl_backend.h"
27
28 OpenCLBackend::OpenCLBuffer *OpenCLBackend::getOrCreateBuffer(BufferState *bufferState) {
29 OpenCLBuffer *openclBuffer = nullptr;
30 if (bufferState->vendorPtr == nullptr || bufferState->state == BufferState::NEW_STATE) {
31 openclBuffer = new OpenCLBuffer(this, bufferState);
32 if (config->trace) {
33 std::cout << "We allocated arg buffer " << std::endl;
34 }
35 } else {
36 if (config->trace) {
37 std::cout << "Were reusing buffer buffer " << std::endl;
38 }
39 openclBuffer = static_cast<OpenCLBuffer *>(bufferState->vendorPtr);
40 }
41 return openclBuffer;
42 }
43
44 bool OpenCLBackend::getBufferFromDeviceIfDirty(void *memorySegment, long memorySegmentLength) {
45 if (config->traceCalls) {
46 std::cout << "getBufferFromDeviceIfDirty(" << std::hex << (long) memorySegment << "," << std::dec <<
47 memorySegmentLength << "){" << std::endl;
48 }
49 if (config->minimizeCopies) {
50 const BufferState *bufferState = BufferState::of(memorySegment, memorySegmentLength);
51 if (bufferState->state == BufferState::DEVICE_OWNED) {
52 queue->copyFromDevice(static_cast<Buffer *>(bufferState->vendorPtr));
53 if (config->traceEnqueues | config->traceCopies) {
54 std::cout << "copying buffer from device (from java access) " << std::endl;
55 }
56 queue->wait();
57 queue->release();
58 } else {
59 std::cout << "HOW DID WE GET HERE 1 attempting to get buffer but buffer is not device dirty" << std::endl;
60 std::exit(1);
61 }
62 } else {
63 std::cerr <<
64 "HOW DID WE GET HERE ? java side should avoid calling getBufferFromDeviceIfDirty as we are not minimising buffers!"
65 << std::endl;
66 std::exit(1);
67 }
68 if (config->traceCalls) {
69 std::cout << "}getBufferFromDeviceIfDirty()" << std::endl;
70 }
71 return true;
72 }
73
74 OpenCLBackend::OpenCLBackend(int configBits)
75 : Backend(new Config(configBits), new OpenCLQueue(this)) {
76
77 if (config->info) {
78 std::cerr << "[INFO] Config Bits = " << std::hex << configBits << std::dec << std::endl;
79 }
80
81 cl_int status;
82 cl_uint platformc = 0;
83 OPENCL_CHECK(clGetPlatformIDs(0, nullptr, &platformc), "clGetPlatformIDs");
84
85 if (config->platform >= platformc) {
86 std::cerr << "We only have " << platformc << " platform" << ((platformc > 1) ? "s" : "") <<
87 " (platform[0]-platform[" << (platformc - 1) << "] inclusive) you requested platform[" << config->
88 platform << "]" << std::endl;
89 std::exit(1);
90 }
91 auto *platforms = new cl_platform_id[platformc];
92 OPENCL_CHECK(clGetPlatformIDs(platformc, platforms, nullptr), "clGetPlatformIDs");
93
94 cl_uint numDevices = 0;
95 platform_id = platforms[config->platform];
96 if ((status = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_ALL, 0, nullptr, &numDevices)) != CL_SUCCESS) {
97 if (status != CL_SUCCESS) {
98 std::cerr << "clGetDeviceIDs (to get count) failed " << errorMsg(status) << std::endl;
99 }
100 delete[] platforms;
101 return;
102 }
103 if (config->device >= numDevices) {
104 std::cerr << "Platform[" << config->platform << "] only has " << numDevices << " device" << (
105 (numDevices > 1) ? "s" : "") << " (device[0]-device[" << (numDevices - 1) <<
106 "] inclusive) and you requested device[" << config->device << "]" << std::endl;
107 std::cerr << "No device available " << errorMsg(CL_DEVICE_NOT_AVAILABLE) << std::endl;
108 delete[] platforms;
109 std::exit(1);
110 }
111
112 if (numDevices == 0) {
113 status = CL_DEVICE_NOT_AVAILABLE;
114 std::cerr << "No device available " << errorMsg(status) << std::endl;
115 delete[] platforms;
116 return;
117 }
118 auto *device_ids = new cl_device_id[numDevices]; // compute device id
119 if ((status = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_ALL, numDevices, device_ids, nullptr)) != CL_SUCCESS) {
120 std::cerr << "clGetDeviceIDs failed " << errorMsg(status) << std::endl;
121 delete[] platforms;
122 delete[] device_ids;
123 return;
124 }
125 if ((context = clCreateContext(nullptr, 1, &device_ids[config->device], nullptr, nullptr, &status)) == nullptr ||
126 status != CL_SUCCESS) {
127 std::cerr << "clCreateContext failed " << errorMsg(status) << std::endl;
128 delete[] platforms;
129 delete[] device_ids;
130 return;
131 }
132
133 cl_command_queue_properties queue_props = CL_QUEUE_PROFILING_ENABLE;
134 const auto openCLQueue = dynamic_cast<OpenCLQueue *>(queue);
135 if ((openCLQueue->command_queue = clCreateCommandQueue(context, device_ids[config->device], queue_props, &status))
136 == nullptr || status != CL_SUCCESS) {
137 std::cerr << "clCreateCommandQueue failed " << errorMsg(status) << std::endl;
138 clReleaseContext(context);
139 delete[] platforms;
140 delete[] device_ids;
141 return;
142 }
143
144 device_id = device_ids[config->device];
145 delete[] device_ids;
146 delete[] platforms;
147 }
148
149 OpenCLBackend::~OpenCLBackend() {
150 clReleaseContext(context);
151 }
152
153 void OpenCLBackend::computeStart() {
154 if (config->trace) {
155 std::cout << "compute start" << std::endl;
156 }
157 queue->computeStart();
158 }
159
160 void OpenCLBackend::computeEnd() {
161 queue->computeEnd();
162 queue->wait();
163
164 if (config->profile) {
165 const auto openCLQueue = dynamic_cast<OpenCLQueue *>(queue);
166 openCLQueue->showEvents(100);
167 }
168 queue->release();
169 if (config->trace) {
170 std::cout << "compute end" << std::endl;
171 }
172 }
173
174 OpenCLBackend::OpenCLProgram *OpenCLBackend::compileProgram(OpenCLSource &openclSource) {
175 return compileProgram(&openclSource);
176 }
177
178 OpenCLBackend::OpenCLProgram *OpenCLBackend::compileProgram(const OpenCLSource *openclSource) {
179 return compileProgram(openclSource->len, openclSource->text);
180 }
181
182 OpenCLBackend::OpenCLProgram *OpenCLBackend::compileProgram(int len, char *text) {
183 return dynamic_cast<OpenCLProgram *>(compile(len, text));
184 }
185
186 Backend::CompilationUnit *OpenCLBackend::compile(int len, char *source) {
187 const size_t srcLen = ::strlen(source);
188 auto src = new char[srcLen + 1];
189 strncpy(src, source, srcLen);
190 src[srcLen] = '\0';
191 if (config->trace) {
192 std::cout << "native compiling " << src << std::endl;
193 }
194 cl_int status;
195 cl_program program;
196 if ((program = clCreateProgramWithSource(context, 1, (const char **) &src, nullptr, &status)) == nullptr ||
197 status != CL_SUCCESS) {
198 std::cerr << "clCreateProgramWithSource failed" << std::endl;
199 delete[] src;
200 return nullptr;
201 }
202
203 cl_int buildStatus = clBuildProgram(program, 0, nullptr, nullptr, nullptr, nullptr);
204 if (buildStatus != CL_SUCCESS) {
205 std::cerr << "buildStatus =failed" << std::endl;
206 }
207 size_t logLen = 0;
208 OpenCLProgram *openclProgram = nullptr;
209 if ((status = clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, 0, nullptr, &logLen)) != CL_SUCCESS) {
210 std::cerr << "clGetBuildInfo (getting log size) failed" << std::endl;
211 //openclProgram->buildInfo = new Backend::CompilationUnit::BuildInfo(openclProgram, src, nullptr, false);
212 openclProgram = new OpenCLProgram(this, src, nullptr, buildStatus == CL_SUCCESS, program);
213 } else {
214 // cl_build_status buildStatus;
215 clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_STATUS, sizeof(buildStatus), &buildStatus, nullptr);
216 if (logLen > 0) {
217 char *log = new char[logLen + 1];
218 if ((status = clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, logLen + 1, (void *) log,
219 nullptr)) != CL_SUCCESS) {
220 std::cerr << "clGetBuildInfo (getting log) failed" << std::endl;
221 delete[] log;
222 log = nullptr;
223 } else {
224 log[logLen] = '\0';
225 if (logLen > 2) {
226 std::cerr << "logLen = " << logLen << " log = " << log << std::endl;
227 }
228 }
229 openclProgram = new OpenCLProgram(this, src, log, buildStatus == CL_SUCCESS, program);
230 } else {
231 openclProgram = new OpenCLProgram(this, src, nullptr, buildStatus == CL_SUCCESS, program);
232 }
233 }
234 return openclProgram;
235 }
236
237 const char *OpenCLBackend::errorMsg(cl_int status) {
238 static struct {
239 cl_int code;
240 const char *msg;
241 } error_table[] = {
242 // @formatter:off
243 {CL_SUCCESS, "success"},
244 {CL_DEVICE_NOT_FOUND, "device not found",},
245 {CL_DEVICE_NOT_AVAILABLE, "device not available",},
246 {CL_COMPILER_NOT_AVAILABLE, "compiler not available",},
247 {CL_MEM_OBJECT_ALLOCATION_FAILURE, "mem object allocation failure",},
248 {CL_OUT_OF_RESOURCES, "out of resources",},
249 {CL_OUT_OF_HOST_MEMORY, "out of host memory",},
250 {CL_PROFILING_INFO_NOT_AVAILABLE, "profiling not available",},
251 {CL_MEM_COPY_OVERLAP, "memcopy overlaps",},
252 {CL_IMAGE_FORMAT_MISMATCH, "image format mismatch",},
253 {CL_IMAGE_FORMAT_NOT_SUPPORTED, "image format not supported",},
254 {CL_BUILD_PROGRAM_FAILURE, "build program failed",},
255 {CL_MAP_FAILURE, "map failed",},
256 {CL_INVALID_VALUE, "invalid value",},
257 {CL_INVALID_DEVICE_TYPE, "invalid device type",},
258 {CL_INVALID_PLATFORM, "invlaid platform",},
259 {CL_INVALID_DEVICE, "invalid device",},
260 {CL_INVALID_CONTEXT, "invalid context",},
261 {CL_INVALID_QUEUE_PROPERTIES, "invalid queue properties",},
262 {CL_INVALID_COMMAND_QUEUE, "invalid command queue",},
263 {CL_INVALID_HOST_PTR, "invalid host ptr",},
264 {CL_INVALID_MEM_OBJECT, "invalid mem object",},
265 {CL_INVALID_IMAGE_FORMAT_DESCRIPTOR, "invalid image format descriptor ",},
266 {CL_INVALID_IMAGE_SIZE, "invalid image size",},
267 {CL_INVALID_SAMPLER, "invalid sampler",},
268 {CL_INVALID_BINARY, "invalid binary",},
269 {CL_INVALID_BUILD_OPTIONS, "invalid build options",},
270 {CL_INVALID_PROGRAM, "invalid program ",},
271 {CL_INVALID_PROGRAM_EXECUTABLE, "invalid program executable",},
272 {CL_INVALID_KERNEL_NAME, "invalid kernel name",},
273 {CL_INVALID_KERNEL_DEFINITION, "invalid definition",},
274 {CL_INVALID_KERNEL, "invalid kernel",},
275 {CL_INVALID_ARG_INDEX, "invalid arg index",},
276 {CL_INVALID_ARG_VALUE, "invalid arg value",},
277 {CL_INVALID_ARG_SIZE, "invalid arg size",},
278 {CL_INVALID_KERNEL_ARGS, "invalid kernel args",},
279 {CL_INVALID_WORK_DIMENSION, "invalid work dimension",},
280 {CL_INVALID_WORK_GROUP_SIZE, "invalid work group size",},
281 {CL_INVALID_WORK_ITEM_SIZE, "invalid work item size",},
282 {CL_INVALID_GLOBAL_OFFSET, "invalid global offset",},
283 {CL_INVALID_EVENT_WAIT_LIST, "invalid event wait list",},
284 {CL_INVALID_EVENT, "invalid event",},
285 {CL_INVALID_OPERATION, "invalid operation",},
286 {CL_INVALID_GL_OBJECT, "invalid gl object",},
287 {CL_INVALID_BUFFER_SIZE, "invalid buffer size",},
288 {CL_INVALID_MIP_LEVEL, "invalid mip level",},
289 {CL_INVALID_GLOBAL_WORK_SIZE, "invalid global work size",},
290 {-9999, "enqueueNdRangeKernel Illegal read or write to a buffer",},
291 {0, nullptr},
292 // @formatter:on
293 };
294 for (int i = 0; error_table[i].msg != nullptr; i++) {
295 if (error_table[i].code == status) {
296 //std::cerr << " clerror '" << error_table[i].msg << "'" << std::endl;
297 return error_table[i].msg;
298 }
299 }
300 static char unknown[256];
301 #if defined (_WIN32)
302 _snprintf
303 #else
304 snprintf
305 #endif
306 (unknown, sizeof(unknown), "unmapped string for error %d", status);
307 return unknown;
308 }
309
310 extern "C" long getBackend(int configBits) {
311 return reinterpret_cast<long>(new OpenCLBackend(configBits));
312 }
313
314 void __checkOpenclErrors(cl_int status, const char *functionName, const char *file, const int line) {
315 if (CL_SUCCESS != status) {
316 std::cerr << "Opencl Error ( " << functionName << ") with error code: " << status << " from file " << file <<
317 " line " << line << std::endl;
318 exit(-1);
319 }
320 }
321
322 OpenCLSource::OpenCLSource()
323 : Text(0L) {
324 }
325
326 OpenCLSource::OpenCLSource(const size_t len)
327 : Text(len) {
328 }
329
330 OpenCLSource::OpenCLSource(char *text)
331 : Text(text, false) {
332 }