|
| 1 | +// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple -lOpenCL %s -o %t1.out |
| 2 | +// RUN: %CPU_RUN_PLACEHOLDER %t1.out |
| 3 | +// RUN: %GPU_RUN_PLACEHOLDER %t1.out |
| 4 | +// REQUIRES: opencl |
| 5 | + |
| 6 | +#include <CL/cl.h> |
| 7 | +#include <CL/sycl.hpp> |
| 8 | + |
| 9 | +using namespace sycl; |
| 10 | + |
| 11 | +static const char *Src = R"( |
| 12 | +kernel void test(global ulong *PSrc, global ulong *PDst) { |
| 13 | + global int *Src = (global int *) *PSrc; |
| 14 | + global int *Dst = (global int *) *PDst; |
| 15 | + int Old = *Src, New = Old + 1; |
| 16 | + printf("Read %d from %p; write %d to %p\n", Old, Src, New, Dst); |
| 17 | + *Dst = New; |
| 18 | +} |
| 19 | +)"; |
| 20 | + |
| 21 | +int main() { |
| 22 | + queue Q{}; |
| 23 | + |
| 24 | + cl_context Ctx = Q.get_context().get(); |
| 25 | + cl_program Prog = clCreateProgramWithSource(Ctx, 1, &Src, NULL, NULL); |
| 26 | + clBuildProgram(Prog, 0, NULL, NULL, NULL, NULL); |
| 27 | + |
| 28 | + cl_kernel OclKernel = clCreateKernel(Prog, "test", NULL); |
| 29 | + |
| 30 | + cl::sycl::kernel SyclKernel(OclKernel, Q.get_context()); |
| 31 | + |
| 32 | + auto POuter = malloc_shared<int *>(1, Q); |
| 33 | + auto PInner = malloc_shared<int>(1, Q); |
| 34 | + auto QOuter = malloc_shared<int *>(1, Q); |
| 35 | + auto QInner = malloc_shared<int>(1, Q); |
| 36 | + |
| 37 | + *PInner = 4; |
| 38 | + *POuter = PInner; |
| 39 | + *QInner = 0; |
| 40 | + *QOuter = QInner; |
| 41 | + |
| 42 | + Q.submit([&](handler &CGH) { |
| 43 | + CGH.set_arg(0, POuter); |
| 44 | + CGH.set_arg(1, QOuter); |
| 45 | + CGH.parallel_for(cl::sycl::range<1>(1), SyclKernel); |
| 46 | + }).wait(); |
| 47 | + |
| 48 | + assert(*PInner == 4 && "Read value is corrupted"); |
| 49 | + assert(*QInner == 5 && "Value value is incorrect"); |
| 50 | + |
| 51 | + std::cout << "Increment: " << *PInner << " -> " << *QInner << std::endl; |
| 52 | + |
| 53 | + clReleaseKernel(OclKernel); |
| 54 | + clReleaseProgram(Prog); |
| 55 | + clReleaseContext(Ctx); |
| 56 | +} |
0 commit comments