forked from intel/llvm-test-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterop-opencl-make-kernel.cpp
71 lines (52 loc) · 2.02 KB
/
interop-opencl-make-kernel.cpp
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
// REQUIRES: opencl, opencl_icd
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out %opencl_lib
// RUN: %CPU_RUN_PLACEHOLDER %t.out
// RUN: %GPU_RUN_PLACEHOLDER %t.out
// RUN: %ACC_RUN_PLACEHOLDER %t.out
// Test for OpenCL make_kernel
#include <CL/opencl.h>
#include <sycl/backend/opencl.hpp>
#include <sycl/sycl.hpp>
using namespace sycl;
class DummyKernel;
constexpr size_t N = 1024;
const char OpenCLIotaKernelCode[] = "__kernel void iota(__global int *a) {\
int i = get_global_id(0);\
a[i] = i;\
}";
const size_t OpenCLIotaKernelCodeSize = sizeof(OpenCLIotaKernelCode);
const char *OpenCLCode[1] = {OpenCLIotaKernelCode};
int main() {
queue Queue{};
auto Context = Queue.get_info<info::queue::context>();
auto Device = Queue.get_info<info::queue::device>();
cl_context OpenCLContext = get_native<backend::opencl>(Context);
cl_device_id OpenCLDevice = get_native<backend::opencl>(Device);
cl_int OpenCLError;
cl_program OpenCLProgram = clCreateProgramWithSource(
OpenCLContext, 1, OpenCLCode, &OpenCLIotaKernelCodeSize, &OpenCLError);
assert(OpenCLError == CL_SUCCESS);
OpenCLError = clBuildProgram(OpenCLProgram, 1, &OpenCLDevice, nullptr,
nullptr, nullptr);
assert(OpenCLError == CL_SUCCESS);
cl_kernel OpenCLKernel = clCreateKernel(OpenCLProgram, "iota", &OpenCLError);
assert(OpenCLError == CL_SUCCESS);
kernel Kernel = make_kernel<backend::opencl>(OpenCLKernel, Context);
// The associated kernel bundle should not contain the dummy-kernel.
assert(!Kernel.get_kernel_bundle().has_kernel(get_kernel_id<DummyKernel>()));
int *IotaResult = malloc_shared<int>(N, Device, Context);
Queue
.submit([&](sycl::handler &CGH) {
CGH.set_arg(0, IotaResult);
CGH.parallel_for(N, Kernel);
})
.wait();
for (int i = 0; i < N; ++i)
assert(IotaResult[i] == i);
free(IotaResult, Context);
Queue
.submit(
[&](sycl::handler &CGH) { CGH.single_task<DummyKernel>([=]() {}); })
.wait();
return 0;
}