"Fossies" - the Fresh Open Source Software Archive

Member "pocl-1.8/examples/accel/accel_example.cpp" (12 Oct 2021, 5200 Bytes) of package /linux/misc/pocl-1.8.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* Tests the generic accelerator device driver.
    2 
    3    Copyright (c) 2019 Pekka Jääskeläinen / Tampere University
    4 
    5    Permission is hereby granted, free of charge, to any person obtaining a copy
    6    of this software and associated documentation files (the "Software"), to deal
    7    in the Software without restriction, including without limitation the rights
    8    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    9    copies of the Software, and to permit persons to whom the Software is
   10    furnished to do so, subject to the following conditions:
   11 
   12    The above copyright notice and this permission notice shall be included in
   13    all copies or substantial portions of the Software.
   14 
   15    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   16    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   17    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   18    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   19    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   20    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   21    THE SOFTWARE.
   22 */
   23 
   24 // Enable OpenCL C++ exceptions
   25 #define CL_HPP_ENABLE_EXCEPTIONS
   26 #define CL_HPP_MINIMUM_OPENCL_VERSION 120
   27 #define CL_HPP_TARGET_OPENCL_VERSION 120
   28 #include <CL/cl2.hpp>
   29 
   30 #include <cstdio>
   31 #include <cstdlib>
   32 #include <iostream>
   33 
   34 #ifndef _MSC_VER
   35 #  include <unistd.h>
   36 #else
   37 #  include "vccompat.hpp"
   38 #endif
   39 
   40 int
   41 main(void)
   42 {
   43     try {
   44         std::vector<cl::Platform> platformList;
   45 
   46         // Pick platform
   47         cl::Platform::get(&platformList);
   48 
   49         // Pick first platform
   50         cl_context_properties cprops[] = {
   51             CL_CONTEXT_PLATFORM, (cl_context_properties)(platformList[0])(), 0};
   52         cl::Context context(CL_DEVICE_TYPE_CUSTOM, cprops);
   53 
   54         // Query the set of devices attched to the context
   55         std::vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();
   56 
   57         cl::Program program(context, devices, "pocl.add32;pocl.mul32");
   58 
   59         // Build program
   60         program.build(devices);
   61 
   62         uint32_t in0[64];
   63         uint32_t in1[64];
   64         uint32_t in2[64];
   65 
   66         uint32_t result[64];
   67         for (size_t i = 0; i < 64; ++i) {
   68             in0[i] = i + 777;
   69             in1[i] = 2020 - i;
   70             in2[i] = i + 9999;
   71         }
   72 
   73         cl::Buffer inBuffer0 = cl::Buffer(
   74             context,
   75             CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR,
   76             (cl::size_type)64*sizeof(uint32_t),
   77             (void*)in0);
   78         cl::Buffer inBuffer1 = cl::Buffer(
   79             context,
   80             CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR,
   81             (cl::size_type)64*sizeof(uint32_t),
   82             (void*)in1);
   83         cl::Buffer inBuffer2 = cl::Buffer(
   84             context,
   85             CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR,
   86             (cl::size_type)64*sizeof(uint32_t),
   87             (void*)in2);
   88 
   89         cl::Buffer outBuffer0 = cl::Buffer(
   90             context,
   91             CL_MEM_READ_WRITE,
   92             (cl::size_type)64*sizeof(uint32_t),
   93             NULL);
   94         cl::Buffer outBuffer1 = cl::Buffer(
   95             context,
   96             CL_MEM_READ_WRITE,
   97             (cl::size_type)64*sizeof(uint32_t),
   98             NULL);
   99 
  100         // Create command queue
  101         cl::CommandQueue queue(context, devices[0], 0);
  102 
  103         // Create kernel object
  104         cl::Kernel mul0(program, "pocl.mul32");
  105         mul0.setArg(0, inBuffer0);
  106         mul0.setArg(1, inBuffer1);
  107         mul0.setArg(2, outBuffer0);
  108 
  109         cl::Kernel mul1(program, "pocl.mul32");
  110         mul1.setArg(0, inBuffer1);
  111         mul1.setArg(1, inBuffer2);
  112         mul1.setArg(2, outBuffer1);
  113 
  114         cl::Kernel add(program, "pocl.add32");
  115         add.setArg(0, outBuffer0);
  116         add.setArg(1, outBuffer1);
  117         add.setArg(2, inBuffer0);
  118 
  119 
  120         //queue.enqueueWriteBuffer(inBuffer, CL_TRUE, 0, 128, &input[0], NULL, NULL);
  121         queue.enqueueNDRangeKernel(mul0, cl::NullRange, cl::NDRange(64),
  122                                    cl::NullRange);
  123         queue.enqueueNDRangeKernel(mul1, cl::NullRange, cl::NDRange(64),
  124                                    cl::NullRange);
  125         queue.enqueueNDRangeKernel(add, cl::NullRange, cl::NDRange(64),
  126                                    cl::NullRange);
  127 
  128         uint32_t* Res = (uint32_t*) queue.enqueueMapBuffer(inBuffer0, CL_FALSE,
  129                                                            CL_MAP_READ, 0, 256);
  130 
  131         queue.finish();
  132 
  133         int correct = 1;
  134         for (size_t i = 0; i < 64; ++i) {
  135             uint32_t value = Res[i];
  136             uint32_t expected = (i + 777)*(2020-i) + (2020-i)*(i+9999);
  137             if (value != expected) {
  138                 printf("at idx %zd expected %i, got %i\n", i, expected, value);
  139                 correct = 0;
  140             }
  141 
  142         }
  143         if (correct == 1) {
  144             printf("OK: Correct result!\n");
  145         }
  146     }
  147     catch (cl::Error &err) {
  148          std::cerr
  149              << "ERROR: "
  150              << err.what()
  151              << "("
  152              << err.err()
  153              << ")"
  154              << std::endl;
  155 
  156          return EXIT_FAILURE;
  157     }
  158 
  159     return EXIT_SUCCESS;
  160 }