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     class MockProgram : public Backend::Program {
 30         class MockKernel : public Backend::Program::Kernel {
 31         public:
 32             MockKernel(Backend::Program *program, char *name)
 33                     : Backend::Program::Kernel(program, name) {
 34             }
 35             ~MockKernel() {}
 36             long ndrange(void *argArray) {
 37                 std::cout << "mock ndrange() " << std::endl;
 38                 return 0;
 39             }
 40         };
 41 
 42     public:
 43         MockProgram(Backend *backend, BuildInfo *buildInfo)
 44                 : Backend::Program(backend, buildInfo) {
 45         }
 46 
 47         ~MockProgram() {
 48         }
 49 
 50         long getKernel(int nameLen, char *name) {
 51             return (long) new MockKernel(this, name);
 52         }
 53 
 54         bool programOK() {
 55             return true;
 56         }
 57     };
 58 
 59 public:
 60 
 61     MockBackend(int mode): Backend(mode) {
 62     }
 63 
 64     ~MockBackend() {
 65     }
 66 
 67     bool getBufferFromDeviceIfDirty(void *memorySegment, long memorySegmentLength) {
 68         std::cout << "attempting  to get buffer from Mockbackend "<<std::endl;
 69         return false;
 70     }
 71 
 72     int getMaxComputeUnits() {
 73         std::cout << "mock getMaxComputeUnits()" << std::endl;
 74         return 0;
 75     }
 76 
 77     void info() {
 78         std::cout << "mock info()" << std::endl;
 79     }
 80      void computeStart(){
 81            std::cout << "mock compute start()" << std::endl;
 82          }
 83             void computeEnd(){
 84               std::cout << "mock compute start()" << std::endl;
 85             }
 86 
 87     long compileProgram(int len, char *source) {
 88         std::cout << "mock compileProgram()" << std::endl;
 89         size_t srcLen = ::strlen(source);
 90         char *src = new char[srcLen + 1];
 91         ::strncpy(src, source, srcLen);
 92         src[srcLen] = '\0';
 93         std::cout << "native compiling " << src << std::endl;
 94         return (long) new MockProgram(this, new BuildInfo(src, nullptr, false));
 95     }
 96 };
 97 
 98 long getMockBackend(int mode) {
 99     return (long) new MockBackend(mode);
100 }