1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
/* Test the cl_ext_buffer_device_address extension.
Copyright (c) 2023-2024 Pekka Jääskeläinen / Intel Finland Oy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// Enable OpenCL C++ exceptions
#define CL_HPP_ENABLE_EXCEPTIONS
#include "pocl_opencl.h"
#include "../../include/CL/cl_ext_pocl.h"
#include <CL/opencl.hpp>
#include <cassert>
#include <cstdlib>
#include <iostream>
#define BUF_SIZE 16
// A kernel that gets the device-seen address of the buffer.
static char GetAddrSourceCode[] = R"raw(
__kernel void get_addr (__global int *buffer,
__global ulong* addr) {
for (int i = 0; i < BUF_SIZE; ++i)
buffer[i] += 1;
*addr = (ulong)buffer;
}
)raw";
// A kernel that accesses another buffer indirectly.
static char IndirectAccess[] = R"raw(
__kernel void indirect_access (__global long* in_addr,
__global int* out) {
*out = **(int __global* __global*)in_addr;
}
)raw";
// A kernel that gets passed a pointer to a middle of a buffer,
// with the data _before_ the passed pointer. Tests the property
// of sub-buffers to synchronize the whole parent buffer when
// using the CL_MEM_BUFFER_DEVICE_ADDRESS flag.
static char PtrArith[] = R"raw(
__kernel void ptr_arith (__global int* in_addr,
__global int* out) {
*out = *(in_addr - 1);
}
)raw";
void *getDeviceAddressFromHost(cl::Buffer &Buf) {
cl_mem_device_address_EXT Addr;
cl_int Err = Buf.getInfo(CL_MEM_DEVICE_PTR_EXT, &Addr);
if (Err != CL_SUCCESS) {
std::cerr << "Got error " << Err
<< " when asking for CL_MEM_DEVICE_PTR_EXT\n";
return nullptr;
}
return (void *)Addr;
}
int main(void) {
bool AllOK = true;
std::vector<cl::Platform> PlatformList;
cl::Platform SelectedPlatform;
try {
cl::Platform::get(&PlatformList);
if (!PlatformList.size()) {
std::cerr << "Error: no platforms found!\n";
return EXIT_FAILURE;
}
bool PlatformFound = false;
for (cl::Platform &Platform : PlatformList) {
if (Platform.getInfo<CL_PLATFORM_EXTENSIONS>().find(
"cl_ext_buffer_device_address") == std::string::npos)
continue;
SelectedPlatform = Platform;
PlatformFound = true;
break;
}
if (!PlatformFound) {
std::cerr << "No platforms with cl_ext_buffer_device_address found. Not "
"testing PoCL?\n";
return 77;
}
cl_context_properties cprops[] = {
CL_CONTEXT_PLATFORM, (cl_context_properties)(SelectedPlatform)(), 0};
cl::Context Context(CL_DEVICE_TYPE_CPU | CL_DEVICE_TYPE_GPU, cprops);
std::vector<cl::Device> Devices = Context.getInfo<CL_CONTEXT_DEVICES>();
std::vector<cl::Device> SuitableDevices;
for (cl::Device &Dev : Devices) {
std::string Exts = Dev.getInfo<CL_DEVICE_EXTENSIONS>();
std::cout << Dev.getInfo<CL_DEVICE_NAME>() << " "
<< Dev.getInfo<CL_DEVICE_VERSION>() << ": ";
if (Exts.find("cl_ext_buffer_device_address") != std::string::npos) {
std::cout << "suitable" << std::endl;
SuitableDevices.push_back(Dev);
break;
}
}
if (SuitableDevices.empty()) {
std::cout << "No devices with cl_ext_buffer_device_address found.\n";
return 77;
}
int PinnedBufferHost[BUF_SIZE];
int PinnedBufferHost2[BUF_SIZE];
for (int i = 0; i < BUF_SIZE; ++i) {
PinnedBufferHost[i] = i;
PinnedBufferHost2[i] = i + 1;
}
cl_ulong DeviceAddrFromKernel = 1;
cl::CommandQueue Queue(Context, SuitableDevices[0], 0);
cl::Program::Sources Sources({GetAddrSourceCode, IndirectAccess, PtrArith});
cl::Program Program(Context, Sources);
#define STRINGIFY(X, Y) X #Y
#define SET_BUF_SIZE(NUM) STRINGIFY("-DBUF_SIZE=", NUM)
Program.build(SuitableDevices, SET_BUF_SIZE(BUF_SIZE));
cl::Kernel GetAddrKernel(Program, "get_addr");
cl::Buffer PinnedCLBuffer = cl::Buffer(
Context,
(cl_mem_flags)(CL_MEM_READ_WRITE | CL_MEM_DEVICE_ADDRESS_EXT |
CL_MEM_COPY_HOST_PTR),
(size_t)BUF_SIZE * sizeof(cl_int), (void *)&PinnedBufferHost[0]);
if (getDeviceAddressFromHost(PinnedCLBuffer) == nullptr) {
std::cerr << "Pinned buffers should get allocated immediately to get the "
"address assigned."
<< std::endl;
return EXIT_FAILURE;
}
cl::Buffer AddrCLBuffer =
cl::Buffer(Context, CL_MEM_WRITE_ONLY, sizeof(cl_ulong), nullptr);
GetAddrKernel.setArg(0, PinnedCLBuffer);
GetAddrKernel.setArg(1, AddrCLBuffer);
Queue.enqueueNDRangeKernel(GetAddrKernel, cl::NullRange, cl::NDRange(1),
cl::NullRange);
Queue.enqueueReadBuffer(PinnedCLBuffer,
CL_TRUE, // block
0, BUF_SIZE * sizeof(cl_int),
(void *)&PinnedBufferHost[0]);
Queue.enqueueReadBuffer(AddrCLBuffer,
CL_TRUE, // block
0, sizeof(cl_ulong), (void *)&DeviceAddrFromKernel);
AllOK = true;
for (int i = 0; i < BUF_SIZE; ++i) {
if (PinnedBufferHost[i] != i + 1) {
AllOK = false;
std::cerr << "PinnedBufferHost[" << i << "] expected to be " << i + 1
<< " but got " << PinnedBufferHost[i] << std::endl;
}
}
if (getDeviceAddressFromHost(PinnedCLBuffer) !=
(void *)DeviceAddrFromKernel) {
std::cerr << "Pinned buffer's device address on the kernel side and "
<< "the host side do not match" << std::endl;
return EXIT_FAILURE;
}
// Test a buffer which doesn't have any hostptr associated with it.
cl::Buffer PinnedCLBufferNoHostCopy = cl::Buffer(
Context, CL_MEM_DEVICE_ADDRESS_EXT, BUF_SIZE * sizeof(cl_int));
GetAddrKernel.setArg(0, PinnedCLBufferNoHostCopy);
Queue.enqueueWriteBuffer(PinnedCLBufferNoHostCopy,
CL_TRUE, // block
0, BUF_SIZE * sizeof(cl_int),
(void *)&PinnedBufferHost[0]);
Queue.enqueueNDRangeKernel(GetAddrKernel, cl::NullRange, cl::NDRange(1),
cl::NullRange);
Queue.enqueueReadBuffer(PinnedCLBufferNoHostCopy,
CL_TRUE, // block
0, BUF_SIZE * sizeof(cl_int),
(void *)&PinnedBufferHost2[0]);
Queue.enqueueReadBuffer(AddrCLBuffer,
CL_TRUE, // block
0, sizeof(cl_ulong), (void *)&DeviceAddrFromKernel);
for (int i = 0; i < BUF_SIZE; ++i) {
if (PinnedBufferHost2[i] != i + 2) {
AllOK = false;
std::cerr << "PinnedBufferHost2[" << i << "] expected to be " << i + 2
<< " but got " << PinnedBufferHost2[i] << std::endl;
}
}
if (getDeviceAddressFromHost(PinnedCLBufferNoHostCopy) !=
(void *)DeviceAddrFromKernel) {
std::cerr << "Pinned buffer's device address on kernel side and host "
"side do not match"
<< std::endl;
return EXIT_FAILURE;
}
// Test a buffer which is passed to the kernel indirectly.
cl::Kernel IndirectAccessKernel(Program, "indirect_access");
int DataIn = 1234;
// A devaddr buffer with the payload data.
cl::Buffer DevAddrCLBuffer = cl::Buffer(
Context,
(cl_mem_flags)(CL_MEM_READ_WRITE | CL_MEM_DEVICE_ADDRESS_EXT |
CL_MEM_COPY_HOST_PTR),
sizeof(int), (void *)&DataIn);
void *DevAddr = getDeviceAddressFromHost(DevAddrCLBuffer);
// A basic buffer used to pass the other buffer's address.
cl::Buffer NormalCLBufferIn = cl::Buffer(
Context, (cl_mem_flags)(CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR),
sizeof(cl_long), (void *)&DevAddr);
cl::Buffer NormalCLBufferOut = cl::Buffer(
Context, (cl_mem_flags)(CL_MEM_WRITE_ONLY), sizeof(cl_int), nullptr);
IndirectAccessKernel.setArg(0, NormalCLBufferIn);
IndirectAccessKernel.setArg(1, NormalCLBufferOut);
if (::clSetKernelExecInfo(IndirectAccessKernel.get(),
CL_KERNEL_EXEC_INFO_DEVICE_PTRS_EXT,
sizeof(void *), &DevAddr) != CL_SUCCESS) {
std::cerr << "Setting indirect access for device ptrs failed!\n";
return EXIT_FAILURE;
}
/// The Level 0 doesn't get the buffer initialized with
/// CL_MEM_COPY_HOST_PTR. This is a workaround until that is fixed.
Queue.enqueueWriteBuffer(DevAddrCLBuffer,
CL_TRUE, // block
0, sizeof(cl_int), (void *)&DataIn);
Queue.enqueueNDRangeKernel(IndirectAccessKernel, cl::NullRange,
cl::NDRange(1), cl::NullRange);
int DataOut = -1;
Queue.enqueueReadBuffer(NormalCLBufferOut,
CL_TRUE, // block
0, sizeof(cl_int), (void *)&DataOut);
if (DataIn != DataOut) {
AllOK = false;
std::cerr << "Passing data via indirect buffers failed. Got: " << DataOut
<< " expected: " << DataIn << "\n";
return EXIT_FAILURE;
}
// Test using clSetKernelArgDevicePointerEXT to pass pointers to
// inside a buffer.
cl::Kernel PtrArithKernel(Program, "ptr_arith");
clSetKernelArgDevicePointerEXT_fn clSetKernelArgDevicePointer =
(clSetKernelArgDevicePointerEXT_fn)
clGetExtensionFunctionAddressForPlatform(
SelectedPlatform(), "clSetKernelArgDevicePointerEXT");
assert(clSetKernelArgDevicePointer != nullptr);
clSetKernelArgDevicePointer(
PtrArithKernel.get(), 0,
(cl_mem_device_address_EXT)((cl_uint *)getDeviceAddressFromHost(
PinnedCLBuffer) +
2));
PtrArithKernel.setArg(1, NormalCLBufferOut);
DataOut = -1;
Queue.enqueueNDRangeKernel(PtrArithKernel, cl::NullRange, cl::NDRange(1),
cl::NullRange);
Queue.enqueueReadBuffer(NormalCLBufferOut,
CL_TRUE, // block
0, sizeof(cl_int), (void *)&DataOut);
if (DataOut != PinnedBufferHost[1]) {
AllOK = false;
std::cerr << "Negative offsetting from passed in pointer failed: "
<< "Expected: " << PinnedBufferHost[1] << " got: " << DataOut
<< "\n";
return EXIT_FAILURE;
}
} catch (cl::Error &err) {
std::cerr << "ERROR: " << err.what() << "(" << err.err() << ")"
<< std::endl;
return EXIT_FAILURE;
}
SelectedPlatform.unloadCompiler();
if (AllOK) {
std::cout << "OK" << std::endl;
return EXIT_SUCCESS;
} else {
std::cout << "FAIL" << std::endl;
return EXIT_FAILURE;
}
}
|