|
| 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 | +#include <CL/cl.h> |
| 9 | + |
| 10 | +#include <cassert> |
| 11 | +#include <iostream> |
| 12 | +#include <string> |
| 13 | +#include <sycl/sycl.hpp> |
| 14 | +#include <vector> |
| 15 | + |
| 16 | +char const *source = R"OCL(kernel void do_nothing() {})OCL"; |
| 17 | + |
| 18 | +using namespace sycl; |
| 19 | + |
| 20 | +int select_opencl(const sycl::device &dev) { |
| 21 | + if (dev.get_platform().get_backend() == backend::opencl) { |
| 22 | + return 1; |
| 23 | + } |
| 24 | + return -1; |
| 25 | +} |
| 26 | + |
| 27 | +int main() { |
| 28 | + auto q = queue{select_opencl}; |
| 29 | + |
| 30 | + std::cerr << "Get native context." << std::endl; |
| 31 | + auto native_context = get_native<backend::opencl, context>(q.get_context()); |
| 32 | + std::cerr << "Create native program." << std::endl; |
| 33 | + cl_program p = |
| 34 | + clCreateProgramWithSource(native_context, 1, &source, nullptr, nullptr); |
| 35 | + std::cerr << "Build native program." << std::endl; |
| 36 | + clBuildProgram(p, 0, nullptr, nullptr, nullptr, nullptr); |
| 37 | + std::cerr << "Release native context." << std::endl; |
| 38 | + clReleaseContext(native_context); |
| 39 | + |
| 40 | + std::cerr << "Make kernel bundle." << std::endl; |
| 41 | + auto bundle = make_kernel_bundle<backend::opencl, bundle_state::executable>( |
| 42 | + p, q.get_context()); |
| 43 | + std::cerr << "Release native program." << std::endl; |
| 44 | + // cl_program must have been retained by the above call. |
| 45 | + clReleaseProgram(p); |
| 46 | + |
| 47 | + std::cerr << "Get native program." << std::endl; |
| 48 | + std::vector<cl_program> device_image = |
| 49 | + get_native<backend::opencl, bundle_state::executable>(bundle); |
| 50 | + assert(device_image.size() == 1); |
| 51 | + std::cerr << "Create native kernel." << std::endl; |
| 52 | + cl_kernel k = clCreateKernel(device_image.front(), "do_nothing", nullptr); |
| 53 | + // get_native must have retained cl_program as well. |
| 54 | + clReleaseProgram(device_image.front()); |
| 55 | + |
| 56 | + std::cerr << "Make kernel." << std::endl; |
| 57 | + make_kernel<backend::opencl>(k, q.get_context()); |
| 58 | + std::cerr << "Release native kernel." << std::endl; |
| 59 | + clReleaseKernel(k); |
| 60 | + |
| 61 | + return 0; |
| 62 | +} |
0 commit comments