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 #pragma once
 26 // The following looks like it is not used (at least to CLION) but it is. ;) don't remove
 27 #define CL_TARGET_OPENCL_VERSION 120
 28 #ifdef __APPLE__
 29    #include <opencl/opencl.h>
 30 #else
 31    #include <CL/cl.h>
 32    #include <malloc.h>
 33    #if defined (_WIN32)
 34        #include "windows.h"
 35    #endif
 36 #endif
 37 #include "shared.h"
 38 
 39 class OpenCLSource:public Text  {
 40 public:
 41     OpenCLSource(size_t len, char *text, bool isCopy);
 42     OpenCLSource(size_t len);
 43     OpenCLSource(char* text);
 44     OpenCLSource();
 45     ~OpenCLSource() = default;
 46 };
 47 
 48 extern void __checkOpenclErrors(cl_int status, const char *file, const int line);
 49 
 50 #define checkOpenCLErrors(err)  __checkOpenclErrors (err, __FILE__, __LINE__)
 51 
 52 class OpenCLBackend : public Backend {
 53 public:
 54     class OpenCLBuffer : public Backend::Buffer {
 55     public:
 56         cl_mem clMem;
 57         OpenCLBuffer(Backend *backend, BufferState *bufferState);
 58         virtual ~OpenCLBuffer();
 59     };
 60 
 61     class OpenCLProgram : public Backend::CompilationUnit {
 62         public:
 63         class OpenCLKernel : public Backend::CompilationUnit::Kernel {
 64         public:
 65             cl_kernel kernel;
 66             OpenCLKernel(Backend::CompilationUnit *compilationUnit, char* name,cl_kernel kernel);
 67             bool setArg(KernelArg *arg) override;
 68             bool setArg(KernelArg *arg, Buffer *buffer) override;
 69             ~OpenCLKernel() override;
 70         };
 71     private:
 72         cl_program program;
 73     public:
 74         OpenCLProgram(Backend *backend, char *src, char *log, bool ok, cl_program program);
 75         ~OpenCLProgram() override;
 76         OpenCLKernel *getOpenCLKernel(char *name);
 77         OpenCLKernel *getOpenCLKernel(int nameLen, char *name);
 78         CompilationUnit::Kernel *getKernel(int nameLen, char *name) override;
 79     };
 80     class OpenCLQueue : public Backend::ProfilableQueue {
 81     public:
 82         cl_command_queue command_queue;
 83         cl_event *events;
 84 
 85         cl_event *eventListPtr();
 86         cl_event *nextEventPtr();
 87 
 88         explicit OpenCLQueue(Backend *backend);
 89 
 90          void wait() override;
 91          void release() override;
 92          void computeStart() override;
 93          void computeEnd() override;
 94          void showEvents(int width) override;
 95          void inc(int bits) override;
 96          void inc(int bits, const char *arg) override;
 97          void marker(int bits) override;
 98          void marker(int bits, const char *arg) override;
 99          void markAsStartComputeAndInc() override;
100          void markAsEndComputeAndInc() override;
101          void markAsEnterKernelDispatchAndInc() override;
102          void markAsLeaveKernelDispatchAndInc() override;
103 
104          void copyToDevice(Buffer *buffer) override;
105          void copyFromDevice(Buffer *buffer) override;
106 
107 
108         void dispatch(KernelContext *kernelContext, CompilationUnit::Kernel *kernel) override;
109          ~OpenCLQueue() override;
110     };
111 public:
112     cl_platform_id platform_id;
113     cl_context context;
114     cl_device_id device_id;
115     explicit OpenCLBackend(int configBits);
116     ~OpenCLBackend() override;
117 
118     OpenCLBuffer *getOrCreateBuffer(BufferState *bufferState) override;
119     OpenCLProgram *compileProgram(OpenCLSource &openclSource) ;
120     OpenCLProgram *compileProgram(OpenCLSource *openclSource);
121     OpenCLProgram *compileProgram(int len, char *source);
122 
123     CompilationUnit *compile(int len, char *source) override;
124     void computeStart() override;
125     void computeEnd() override;
126     bool getBufferFromDeviceIfDirty(void *memorySegment, long memorySegmentLength) override;
127     void info() override;
128 
129 public:
130     static const char *errorMsg(cl_int status);
131 };
132 
133 
134 struct PlatformInfo{
135     struct DeviceInfo{
136       OpenCLBackend *openclBackend;
137       cl_int maxComputeUnits;
138       cl_int maxWorkItemDimensions;
139       cl_device_type deviceType;
140       size_t maxWorkGroupSize;
141       cl_ulong globalMemSize;
142       cl_ulong localMemSize;
143       cl_ulong maxMemAllocSize;
144       char *profile;
145       char *deviceVersion;
146       size_t *maxWorkItemSizes ;
147       char *driverVersion;
148       char *cVersion;
149       char *name;
150       char *extensions;
151       char *builtInKernels;
152       char *deviceTypeStr;
153       explicit DeviceInfo(OpenCLBackend *openclBackend);
154       ~DeviceInfo();
155     };
156   OpenCLBackend *openclBackend;
157   char *versionName;
158   char *vendorName;
159   char *name;
160   DeviceInfo deviceInfo;
161 
162   explicit PlatformInfo(OpenCLBackend *openclBackend);
163   ~PlatformInfo();
164 };
165 
166