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 #include "shared.h"
 26 
 27 class MockBackend : public Backend {
 28 public:
 29 
 30 
 31     class MockProgram : public Backend::CompilationUnit {
 32         class MockKernel : public Backend::CompilationUnit::Kernel {
 33         public:
 34             MockKernel(Backend::CompilationUnit *compilationUnit, char *name)
 35                     : Backend::CompilationUnit::Kernel(compilationUnit, name) {
 36             }
 37             ~MockKernel() override = default;
 38             bool setArg(KernelArg *arg, Buffer *buffer) override{
 39                 return false ;
 40             }
 41             bool setArg(KernelArg *arg) override{
 42                 return false ;
 43             }
 44         };
 45 
 46     public:
 47         MockProgram(Backend *backend, char *src, char *log, bool ok )
 48                 : Backend::CompilationUnit(backend, src,log, ok) {
 49         }
 50 
 51         ~MockProgram() {
 52         }
 53 
 54         Kernel* getKernel(int nameLen, char *name) {
 55             return new MockKernel(this, name);
 56         }
 57     };
 58     class MockQueue: public Backend::Queue{
 59     public:
 60         void wait()override{};
 61         void release()override{};
 62         void computeStart()override{};
 63         void computeEnd()override{};
 64         void dispatch(KernelContext *kernelContext, Backend::CompilationUnit::Kernel *kernel) override{
 65             std::cout << "mock dispatch() " << std::endl;
 66             size_t dims = 1;
 67             if (backend->config->trace | backend->config->traceEnqueues){
 68                 std::cout << "enqueued kernel dispatch \""<< kernel->name <<"\" globalSize=" << kernelContext->maxX << std::endl;
 69             }
 70 
 71         }
 72         void copyToDevice(Buffer *buffer) override{}
 73         void copyFromDevice(Buffer *buffer) override{};
 74         explicit MockQueue(Backend *backend):Queue(backend){}
 75         ~MockQueue() override =default;
 76     };
 77 public:
 78 
 79     MockBackend(int configBits): Backend(new Config(configBits), new MockQueue(this)) {
 80     }
 81 
 82     ~MockBackend() {
 83     }
 84     Buffer * getOrCreateBuffer(BufferState *bufferState) override{
 85         Buffer *buffer = nullptr;
 86 
 87         /* if (bufferState->vendorPtr == 0L || bufferState->state == BufferState::NEW_STATE){
 88               openclBuffer = new OpenCLBuffer(this,  bufferState);
 89               if (openclConfig.trace){
 90                   std::cout << "We allocated arg buffer "<<std::endl;
 91               }
 92           }else{
 93               if (openclConfig.trace){
 94                   std::cout << "Were reusing  buffer  buffer "<<std::endl;
 95               }
 96               openclBuffer=  static_cast<OpenCLBuffer*>(bufferState->vendorPtr);
 97           }*/
 98         return buffer;
 99     }
100     bool getBufferFromDeviceIfDirty(void *memorySegment, long memorySegmentLength) override {
101         std::cout << "attempting  to get buffer from Mockbackend "<<std::endl;
102         return false;
103     }
104 
105 
106     void info() override {
107         std::cout << "mock info()" << std::endl;
108     }
109 
110     void computeStart() override{
111         std::cout << "mock compute start()" << std::endl;
112     }
113 
114     void computeEnd() override{
115         std::cout << "mock compute start()" << std::endl;
116     }
117 
118     CompilationUnit *compile(int len, char *source) override{
119         std::cout << "mock compileProgram()" << std::endl;
120         size_t srcLen = ::strlen(source);
121         char *src = new char[srcLen + 1];
122         ::strncpy(src, source, srcLen);
123         src[srcLen] = '\0';
124         std::cout << "native compiling " << src << std::endl;
125         MockProgram *mockProgram = new MockProgram(this,src, nullptr, false);
126         return dynamic_cast<CompilationUnit*>(mockProgram);
127     }
128 };
129 
130 long getBackend(int configBits) {
131     return reinterpret_cast<long>(new MockBackend(configBits));
132 }