|
| 1 | +/* |
| 2 | +// Copyright (c) 2025 Ben Ashbaugh |
| 3 | +// |
| 4 | +// SPDX-License-Identifier: MIT |
| 5 | +*/ |
| 6 | + |
| 7 | +#include <popl/popl.hpp> |
| 8 | + |
| 9 | +#include <CL/opencl.hpp> |
| 10 | + |
| 11 | +#include <memory> |
| 12 | +#include <numeric> |
| 13 | + |
| 14 | +const size_t sz = 1024; |
| 15 | + |
| 16 | +static const char kernelString[] = R"CLC( |
| 17 | +struct s { const global uint* ptr; }; |
| 18 | +kernel void test_IndirectAccessRead( |
| 19 | + const global struct s* src, |
| 20 | + global uint* dst, |
| 21 | + global uint* dummy) |
| 22 | +{ |
| 23 | + dst[get_global_id(0)] = src->ptr[get_global_id(0)]; |
| 24 | +} |
| 25 | +)CLC"; |
| 26 | + |
| 27 | +int main( |
| 28 | + int argc, |
| 29 | + char** argv ) |
| 30 | +{ |
| 31 | + int platformIndex = 0; |
| 32 | + int deviceIndex = 0; |
| 33 | + |
| 34 | + bool enableIndirectAccess = false; |
| 35 | + bool setAsKernelArg = false; |
| 36 | + bool useHostUSM = false; |
| 37 | + |
| 38 | + { |
| 39 | + popl::OptionParser op("Supported Options"); |
| 40 | + op.add<popl::Value<int>>("p", "platform", "Platform Index", platformIndex, &platformIndex); |
| 41 | + op.add<popl::Value<int>>("d", "device", "Device Index", deviceIndex, &deviceIndex); |
| 42 | + op.add<popl::Switch>("i", "indirect", "Enable Indirect Access", &enableIndirectAccess); |
| 43 | + op.add<popl::Switch>("a", "argument", "Set as Kernel Argument", &setAsKernelArg); |
| 44 | + op.add<popl::Switch>("h", "host", "Use Host USM", &useHostUSM); |
| 45 | + bool printUsage = false; |
| 46 | + try { |
| 47 | + op.parse(argc, argv); |
| 48 | + } catch (std::exception& e) { |
| 49 | + fprintf(stderr, "Error: %s\n\n", e.what()); |
| 50 | + printUsage = true; |
| 51 | + } |
| 52 | + |
| 53 | + if (printUsage || !op.unknown_options().empty() || !op.non_option_args().empty()) { |
| 54 | + fprintf(stderr, |
| 55 | + "Usage: dmemindirect [options]\n" |
| 56 | + "%s", op.help().c_str()); |
| 57 | + return -1; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + std::vector<cl::Platform> platforms; |
| 62 | + cl::Platform::get(&platforms); |
| 63 | + |
| 64 | + printf("Running on platform: %s\n", |
| 65 | + platforms[platformIndex].getInfo<CL_PLATFORM_NAME>().c_str() ); |
| 66 | + |
| 67 | + std::vector<cl::Device> devices; |
| 68 | + platforms[platformIndex].getDevices(CL_DEVICE_TYPE_ALL, &devices); |
| 69 | + |
| 70 | + printf("Running on device: %s\n", |
| 71 | + devices[deviceIndex].getInfo<CL_DEVICE_NAME>().c_str() ); |
| 72 | + |
| 73 | + cl::Context context{devices[deviceIndex]}; |
| 74 | + cl::CommandQueue commandQueue{context, devices[deviceIndex]}; |
| 75 | + |
| 76 | + cl::Program program{ context, kernelString }; |
| 77 | + program.build(); |
| 78 | + cl::Kernel kernel = cl::Kernel{ program, "test_IndirectAccessRead" }; |
| 79 | + |
| 80 | + cl_uint* d_src = useHostUSM ? |
| 81 | + (cl_uint*)clHostMemAllocINTEL( |
| 82 | + context(), |
| 83 | + nullptr, |
| 84 | + sz * sizeof(cl_uint), |
| 85 | + 0, |
| 86 | + nullptr) : |
| 87 | + (cl_uint*)clDeviceMemAllocINTEL( |
| 88 | + context(), |
| 89 | + devices[deviceIndex](), |
| 90 | + nullptr, |
| 91 | + sz * sizeof(cl_uint), |
| 92 | + 0, |
| 93 | + nullptr); |
| 94 | + |
| 95 | + // destination buffer |
| 96 | + cl::Buffer dst = cl::Buffer{ |
| 97 | + context, |
| 98 | + CL_MEM_READ_WRITE, |
| 99 | + sz * sizeof(cl_uint)}; |
| 100 | + |
| 101 | + // indirect source buffer |
| 102 | + cl::Buffer src = cl::Buffer{ |
| 103 | + context, |
| 104 | + CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, |
| 105 | + sizeof(d_src), |
| 106 | + &d_src}; |
| 107 | + |
| 108 | + if (d_src) { |
| 109 | + if (enableIndirectAccess) { |
| 110 | + cl_bool enable = CL_TRUE; |
| 111 | + clSetKernelExecInfo( |
| 112 | + kernel(), |
| 113 | + CL_KERNEL_EXEC_INFO_INDIRECT_DEVICE_ACCESS_INTEL, |
| 114 | + sizeof(cl_bool), |
| 115 | + &enable); |
| 116 | + } |
| 117 | + |
| 118 | + clSetKernelExecInfo( |
| 119 | + kernel(), |
| 120 | + CL_KERNEL_EXEC_INFO_USM_PTRS_INTEL, |
| 121 | + sizeof(d_src), |
| 122 | + &d_src); |
| 123 | + |
| 124 | + kernel.setArg(0, src); |
| 125 | + kernel.setArg(1, dst); |
| 126 | + if (setAsKernelArg) { |
| 127 | + clSetKernelArgMemPointerINTEL( |
| 128 | + kernel(), |
| 129 | + 2, |
| 130 | + d_src); |
| 131 | + } else { |
| 132 | + kernel.setArg(2, nullptr); |
| 133 | + } |
| 134 | + |
| 135 | + cl_int errorCode = commandQueue.enqueueNDRangeKernel( |
| 136 | + kernel, |
| 137 | + cl::NullRange, |
| 138 | + cl::NDRange(sz), |
| 139 | + cl::NullRange); |
| 140 | + printf("clEnqueueNDRangeKernel returned: %d\n", errorCode); |
| 141 | + |
| 142 | + commandQueue.finish(); |
| 143 | + } |
| 144 | + else |
| 145 | + { |
| 146 | + printf("Allocation failed - does this device support Unified Shared Memory?\n"); |
| 147 | + } |
| 148 | + |
| 149 | + printf("Cleaning up...\n"); |
| 150 | + |
| 151 | + clMemFreeINTEL(context(), d_src); |
| 152 | + |
| 153 | + return 0; |
| 154 | +} |
0 commit comments