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 #define CL_TARGET_OPENCL_VERSION 120
 27 
 28 #ifdef __APPLE__
 29 #include <opencl/opencl.h>
 30 #define LongUnsignedNewline "%llu\n"
 31 #define Size_tNewline "%lu\n"
 32 #define LongHexNewline "(0x%llx)\n"
 33 #define alignedMalloc(size, alignment) memalign(alignment, size)
 34 #define SNPRINTF snprintf
 35 #else
 36 
 37 #include <CL/cl.h>
 38 #include <malloc.h>
 39 
 40 #define LongHexNewline "(0x%lx)\n"
 41 #define LongUnsignedNewline "%lu\n"
 42 #define Size_tNewline "%lu\n"
 43 #if defined (_WIN32)
 44 #include "windows.h"
 45 #define alignedMalloc(size, alignment) _aligned_malloc(size, alignment)
 46 #define SNPRINTF _snprintf
 47 #else
 48 #define alignedMalloc(size, alignment) memalign(alignment, size)
 49 #define SNPRINTF  snprintf
 50 #endif
 51 #endif
 52 
 53 #include "shared.h"
 54 
 55 extern void __checkOpenclErrors(cl_int status, const char *file, const int line);
 56 
 57 #define checkOpenCLErrors(err)  __checkOpenclErrors (err, __FILE__, __LINE__)
 58 
 59 class OpenCLBackend : public Backend {
 60 public:
 61     class OpenCLConfig : public Backend::Config {
 62     public:
 63         boolean gpu;
 64     };
 65 
 66     class OpenCLProgram : public Backend::Program {
 67         class OpenCLKernel : public Backend::Program::Kernel {
 68 
 69             class OpenCLBuffer : public Backend::Program::Kernel::Buffer {
 70             public:
 71                 cl_mem clMem;
 72 
 73                 void copyToDevice();
 74 
 75                 void copyFromDevice();
 76 
 77                 OpenCLBuffer(Backend::Program::Kernel *kernel, Arg_s *arg);
 78 
 79                 virtual ~OpenCLBuffer();
 80             };
 81 
 82         private:
 83             cl_kernel kernel;
 84             size_t eventMax;
 85             cl_event *events;
 86             size_t eventc;
 87         protected:
 88             void showEvents(int width);
 89         public:
 90             OpenCLKernel(Backend::Program *program, char* name,cl_kernel kernel);
 91 
 92             ~OpenCLKernel();
 93 
 94             long ndrange( void *argArray);
 95         };
 96 
 97     private:
 98         cl_program program;
 99 
100     public:
101         OpenCLProgram(Backend *backend, BuildInfo *buildInfo, cl_program program);
102 
103         ~OpenCLProgram();
104 
105         long getKernel(int nameLen, char *name);
106 
107         bool programOK();
108     };
109 
110 public:
111     cl_platform_id platform_id;
112     cl_context context;
113     cl_command_queue command_queue;
114     cl_device_id device_id;
115 
116 
117     OpenCLBackend();
118 
119     OpenCLBackend(OpenCLConfig *config, int configSchemaLen, char *configSchema);
120 
121     ~OpenCLBackend();
122 
123     int getMaxComputeUnits();
124 
125     void info();
126     void dumpSled(std::ostream &out,void *argArray);
127     char *dumpSchema(std::ostream &out,int depth, char *ptr, void *data);
128     long compileProgram(int len, char *source);
129 
130 public:
131     static const char *errorMsg(cl_int status);
132 };
133