|
| 1 | +// REQUIRES: opencl |
| 2 | + |
| 3 | +// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out -L %opencl_libs_dir -lOpenCL |
| 4 | +// RUN: %CPU_RUN_PLACEHOLDER %t.out |
| 5 | +// RUN: %GPU_RUN_PLACEHOLDER %t.out |
| 6 | +// RUN: %ACC_RUN_PLACEHOLDER %t.out |
| 7 | + |
| 8 | +#include <CL/sycl.hpp> |
| 9 | +#include <cassert> |
| 10 | +#include <exception> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +#define CL_CHECK_ERRORS(ERR) \ |
| 14 | + if (ERR != CL_SUCCESS) { \ |
| 15 | + throw std::runtime_error("Error in OpenCL object creation."); \ |
| 16 | + } |
| 17 | + |
| 18 | +cl_platform_id selectOpenCLPlatform() { |
| 19 | + cl_int err = 0; |
| 20 | + cl_uint num_of_platforms = 0; |
| 21 | + |
| 22 | + err = clGetPlatformIDs(0, NULL, &num_of_platforms); |
| 23 | + CL_CHECK_ERRORS(err); |
| 24 | + |
| 25 | + std::vector<cl_platform_id> platforms(num_of_platforms); |
| 26 | + |
| 27 | + err = clGetPlatformIDs(num_of_platforms, &platforms[0], 0); |
| 28 | + CL_CHECK_ERRORS(err); |
| 29 | + |
| 30 | + return platforms[0]; |
| 31 | +} |
| 32 | + |
| 33 | +cl_device_id selectOpenCLDevice(cl_platform_id platform) { |
| 34 | + cl_int err = 0; |
| 35 | + cl_uint num_of_devices = 0; |
| 36 | + |
| 37 | + err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 0, 0, &num_of_devices); |
| 38 | + CL_CHECK_ERRORS(err); |
| 39 | + |
| 40 | + std::vector<cl_device_id> devices(num_of_devices); |
| 41 | + |
| 42 | + err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, num_of_devices, |
| 43 | + &devices[0], 0); |
| 44 | + CL_CHECK_ERRORS(err); |
| 45 | + |
| 46 | + return devices[0]; |
| 47 | +} |
| 48 | + |
| 49 | +cl_context createOpenCLContext(cl_platform_id platform, cl_device_id device) { |
| 50 | + cl_int err = 0; |
| 51 | + cl_context context; |
| 52 | + std::vector<cl_context_properties> context_props(3); |
| 53 | + |
| 54 | + context_props[0] = CL_CONTEXT_PLATFORM; |
| 55 | + context_props[1] = cl_context_properties(platform); |
| 56 | + context_props.back() = 0; |
| 57 | + |
| 58 | + context = clCreateContext(&context_props[0], 1, &device, 0, 0, &err); |
| 59 | + CL_CHECK_ERRORS(err); |
| 60 | + |
| 61 | + return context; |
| 62 | +} |
| 63 | + |
| 64 | +int main() { |
| 65 | + cl_platform_id ocl_platform = selectOpenCLPlatform(); |
| 66 | + cl::sycl::platform syclPlt(ocl_platform); |
| 67 | + assert(ocl_platform == syclPlt.get() && |
| 68 | + "SYCL returns invalid OpenCL platform id"); |
| 69 | + |
| 70 | + cl_device_id ocl_device = selectOpenCLDevice(ocl_platform); |
| 71 | + cl::sycl::device syclDev(ocl_device); |
| 72 | + assert(ocl_device == syclDev.get() && |
| 73 | + "SYCL returns invalid OpenCL device id"); |
| 74 | + |
| 75 | + cl_context ocl_context = createOpenCLContext(ocl_platform, ocl_device); |
| 76 | + cl::sycl::context syclContext(ocl_context); |
| 77 | + assert(ocl_context == syclContext.get() && |
| 78 | + "SYCL returns invalid OpenCL context id"); |
| 79 | + |
| 80 | + return 0; |
| 81 | +} |
0 commit comments