Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit 6bc37ad

Browse files
[SYCL] Adds test for make_kernel with OpenCL (#547)
Signed-off-by: Steffen Larsen <[email protected]>
1 parent 18c2f11 commit 6bc37ad

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// REQUIRES: opencl, opencl_icd
2+
3+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out %opencl_lib
4+
// RUN: %CPU_RUN_PLACEHOLDER %t.out
5+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
6+
// RUN: %ACC_RUN_PLACEHOLDER %t.out
7+
8+
// Test for OpenCL make_kernel
9+
10+
#include <CL/opencl.h>
11+
#include <CL/sycl.hpp>
12+
#include <CL/sycl/backend/opencl.hpp>
13+
14+
using namespace cl::sycl;
15+
16+
class DummyKernel;
17+
18+
constexpr size_t N = 1024;
19+
20+
const char OpenCLIotaKernelCode[] = "__kernel void iota(__global int *a) {\
21+
int i = get_global_id(0);\
22+
a[i] = i;\
23+
}";
24+
const size_t OpenCLIotaKernelCodeSize = sizeof(OpenCLIotaKernelCode);
25+
const char *OpenCLCode[1] = {OpenCLIotaKernelCode};
26+
27+
int main() {
28+
queue Queue{};
29+
auto Context = Queue.get_info<info::queue::context>();
30+
auto Device = Queue.get_info<info::queue::device>();
31+
32+
cl_context OpenCLContext = get_native<backend::opencl>(Context);
33+
cl_device_id OpenCLDevice = get_native<backend::opencl>(Device);
34+
35+
cl_int OpenCLError;
36+
cl_program OpenCLProgram = clCreateProgramWithSource(
37+
OpenCLContext, 1, OpenCLCode, &OpenCLIotaKernelCodeSize, &OpenCLError);
38+
assert(OpenCLError == CL_SUCCESS);
39+
40+
OpenCLError = clBuildProgram(OpenCLProgram, 1, &OpenCLDevice, nullptr,
41+
nullptr, nullptr);
42+
assert(OpenCLError == CL_SUCCESS);
43+
44+
cl_kernel OpenCLKernel = clCreateKernel(OpenCLProgram, "iota", &OpenCLError);
45+
assert(OpenCLError == CL_SUCCESS);
46+
47+
kernel Kernel = make_kernel<backend::opencl>(OpenCLKernel, Context);
48+
49+
// The associated kernel bundle should not contain the dummy-kernel.
50+
assert(!Kernel.get_kernel_bundle().has_kernel(get_kernel_id<DummyKernel>()));
51+
52+
int *IotaResult = malloc_shared<int>(N, Device, Context);
53+
Queue
54+
.submit([&](sycl::handler &CGH) {
55+
CGH.set_arg(0, IotaResult);
56+
CGH.parallel_for(N, Kernel);
57+
})
58+
.wait();
59+
60+
for (int i = 0; i < N; ++i)
61+
assert(IotaResult[i] == i);
62+
63+
free(IotaResult, Context);
64+
65+
Queue
66+
.submit(
67+
[&](sycl::handler &CGH) { CGH.single_task<DummyKernel>([=]() {}); })
68+
.wait();
69+
70+
return 0;
71+
}

0 commit comments

Comments
 (0)