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