1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
/*
Issue #701
*/
#include "pocl_opencl.h"
#define CL_HPP_ENABLE_EXCEPTIONS
#define CL_HPP_MINIMUM_OPENCL_VERSION 120
#define CL_HPP_TARGET_OPENCL_VERSION 120
#include <CL/opencl.hpp>
#include <cassert>
#include <iostream>
using namespace std;
const char *SOURCE = R"RAW(
__kernel void evaluate(global const float *in, global float *out)
{
/* Variable declarations */
size_t elementIndex = get_global_id(1);
size_t i, j;
float testValue[3];
float trialValue[1];
float shapeIntegral[3][1][1];
for (i = 0; i < 3; ++i)
for (j = 0; j < 1; ++j)
shapeIntegral[i][j][j] = 0.0f;
trialValue[0] = 1.5f;
for (i = 0; i < 3; ++i)
for (j = 0; j < 1; ++j)
{
shapeIntegral[i][j][j] += in[i] * trialValue[j];
}
if (elementIndex == 0)
{
out[0] = shapeIntegral[0][0][0];
out[1] = shapeIntegral[1][0][0];
out[2] = shapeIntegral[2][0][0];
}
}
)RAW";
#define ARRAY_SIZE 4
int main(int argc, char *argv[]) {
cl::Platform platform = cl::Platform::getDefault();
cl::Device device = cl::Device::getDefault();
float in1[ARRAY_SIZE] = { 0.0f };
float out[ARRAY_SIZE] = { 0.0f };
try {
cl::CommandQueue queue = cl::CommandQueue::getDefault();
cl::Program program(SOURCE);
program.build("-cl-std=CL1.2");
cl::Buffer inbuf((cl_mem_flags)(CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR),
(ARRAY_SIZE * sizeof(float)), in1);
cl::Buffer outbuf((cl_mem_flags)(CL_MEM_WRITE_ONLY),
(ARRAY_SIZE * sizeof(float)), NULL);
// This triggers compilation of dynamic WG binaries.
cl::Program::Binaries binaries{};
int err = program.getInfo<>(CL_PROGRAM_BINARIES, &binaries);
assert(err == CL_SUCCESS);
auto kernel =
cl::KernelFunctor<cl::Buffer, cl::Buffer>(program, "evaluate");
kernel(cl::EnqueueArgs(queue, cl::NDRange(1, 2), cl::NDRange(1, 1)), inbuf,
outbuf);
queue.enqueueReadBuffer(outbuf, 1, 0, (ARRAY_SIZE * sizeof(float)), out);
queue.finish();
} catch (cl::Error &err) {
std::cerr << "ERROR: " << err.what() << "(" << err.err() << ")"
<< std::endl;
return EXIT_FAILURE;
}
platform.unloadCompiler();
printf("Value: %le \n", out[0]);
printf("Value: %le \n", out[1]);
printf("Value: %le \n", out[2]);
printf("OK\n");
return EXIT_SUCCESS;
}
|