|
| 1 | +/*************************************************************************** |
| 2 | + * |
| 3 | + * Copyright (C) 2018 Codeplay Software Limited |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * For your convenience, a copy of the License has been included in this |
| 11 | + * repository. |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * |
| 19 | + * Codeplay's ComputeCpp SDK |
| 20 | + * |
| 21 | + * builtin_kernel_example.cpp |
| 22 | + * |
| 23 | + * Description: |
| 24 | + * Example of using an OpenCL builtin kernel with SYCL via the codeplay |
| 25 | + * extension |
| 26 | + * |
| 27 | + **************************************************************************/ |
| 28 | + |
| 29 | +#include <CL/sycl.hpp> |
| 30 | + |
| 31 | +#include <array> |
| 32 | +#include <iostream> |
| 33 | + |
| 34 | +using namespace cl::sycl; |
| 35 | + |
| 36 | +int main() { |
| 37 | + |
| 38 | + queue testQueue; |
| 39 | + context testContext = testQueue.get_context(); |
| 40 | + |
| 41 | + device dev = testContext.get_devices()[0]; |
| 42 | + |
| 43 | + /* |
| 44 | + * Using the device properties, we can query which built in kernels |
| 45 | + * are supported |
| 46 | + */ |
| 47 | + auto builtinKernels = dev.get_info<info::device::built_in_kernels>(); |
| 48 | + |
| 49 | + if (builtinKernels.size() == 0) { |
| 50 | + std::cout << "[EXIT] No built-in kernels available for testing " |
| 51 | + << std::endl; |
| 52 | + return 0; |
| 53 | + } |
| 54 | + |
| 55 | + /* |
| 56 | + * The only builtin kernel supported by this example is the |
| 57 | + * ComputeAorta copy buffer, defined as: |
| 58 | + * copy_buffer(__global * in, __global * out) |
| 59 | + * And copies input into output. |
| 60 | + */ |
| 61 | + const std::string kAortaTestKernelName{"copy_buffer"}; |
| 62 | + |
| 63 | + auto kernelNamePos = |
| 64 | + std::find(std::begin(builtinKernels), std::end(builtinKernels), |
| 65 | + kAortaTestKernelName); |
| 66 | + if (kernelNamePos == std::end(builtinKernels)) { |
| 67 | + std::cout << "[EXIT] Only ComputeAorta test built-in kernel is supported " |
| 68 | + << " on this example " << std::endl; |
| 69 | + return 0; |
| 70 | + } |
| 71 | + |
| 72 | + const float goldenValue = 1234.0f; |
| 73 | + float input = goldenValue; |
| 74 | + float output = 0; |
| 75 | + |
| 76 | + { |
| 77 | + buffer<float, 1> buf(&input, range<1>(1)); |
| 78 | + buffer<float, 1> bufOut(range<1>(1)); |
| 79 | + bufOut.set_final_data(&output); |
| 80 | + |
| 81 | + program syclProgram(testContext); |
| 82 | + |
| 83 | + /* |
| 84 | + * The "create_from_built_in_kernel" method from the |
| 85 | + * ComputeCpp program class uses the clCreateProgramWithBultinKernels |
| 86 | + * to load the OpenCL builtin kernels into the SYCL program object. |
| 87 | + */ |
| 88 | + syclProgram.create_from_built_in_kernel(kAortaTestKernelName); |
| 89 | + |
| 90 | + kernel kernelC(syclProgram.get_kernel(kAortaTestKernelName)); |
| 91 | + |
| 92 | + testQueue.submit([&](handler& cgh) { |
| 93 | + auto accIn = buf.get_access<access::mode::read>(cgh); |
| 94 | + auto accOut = bufOut.get_access<access::mode::write>(cgh); |
| 95 | + |
| 96 | + /* Using the OpenCL interoperability to set the kernel |
| 97 | + * arguments to match the accessors to the builtin kernel |
| 98 | + * arguments. |
| 99 | + */ |
| 100 | + cgh.set_arg(0, accIn); |
| 101 | + cgh.set_arg(1, accOut); |
| 102 | + |
| 103 | + auto myRange = range<1>{1}; |
| 104 | + /* The kernel can be dispatched to the device using the |
| 105 | + * parallel_for dispatch function. |
| 106 | + * In this case, since the range is 1, the single_task could |
| 107 | + * also have been used. |
| 108 | + */ |
| 109 | + cgh.parallel_for(myRange, kernelC); |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + int retVal = 0; |
| 114 | + if (input != output) { |
| 115 | + std::cout << " The result of the builtin kernel is not expected!" |
| 116 | + << std::endl; |
| 117 | + retVal = 1; |
| 118 | + } |
| 119 | + |
| 120 | + return retVal; |
| 121 | +} |
0 commit comments