|
| 1 | +/* Copyright (c) 2025 Advanced Micro Devices, Inc. |
| 2 | +
|
| 3 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | + of this software and associated documentation files (the "Software"), to deal |
| 5 | + in the Software without restriction, including without limitation the rights |
| 6 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | + copies of the Software, and to permit persons to whom the Software is |
| 8 | + furnished to do so, subject to the following conditions: |
| 9 | +
|
| 10 | + The above copyright notice and this permission notice shall be included in |
| 11 | + all copies or substantial portions of the Software. |
| 12 | +
|
| 13 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | + THE SOFTWARE. */ |
| 20 | + |
| 21 | +#include "OCLCreatePipe.h" |
| 22 | + |
| 23 | +#include <vector> |
| 24 | +#include <array> |
| 25 | + |
| 26 | +#include "CL/cl.h" |
| 27 | + |
| 28 | +OCLCreatePipe::OCLCreatePipe() { _numSubTests = 1; } |
| 29 | + |
| 30 | +OCLCreatePipe::~OCLCreatePipe() {} |
| 31 | + |
| 32 | +void OCLCreatePipe::open(unsigned int test, |
| 33 | + char *units, |
| 34 | + double &conversion, |
| 35 | + unsigned int deviceId) { |
| 36 | + _deviceId = deviceId; |
| 37 | +} |
| 38 | + |
| 39 | +void OCLCreatePipe::run(void) { |
| 40 | + std::vector<cl_device_id> devices(_deviceId + 1, nullptr); |
| 41 | + cl_platform_id platform = nullptr; |
| 42 | + cl_context context = nullptr; |
| 43 | + cl_int err = 0; |
| 44 | + |
| 45 | + err = _wrapper->clGetPlatformIDs(1, &platform, nullptr); |
| 46 | + CHECK_RESULT(err, "clGetPlatformIDs failed"); |
| 47 | + |
| 48 | + err = _wrapper->clGetDeviceIDs(platform, CL_DEVICE_TYPE_DEFAULT, |
| 49 | + devices.size(), devices.data(), nullptr); |
| 50 | + CHECK_RESULT(err, "clGetDeviceIDs failed"); |
| 51 | + |
| 52 | + context = _wrapper->clCreateContext(nullptr, 1, &devices[_deviceId], |
| 53 | + nullptr, nullptr, &err); |
| 54 | + CHECK_RESULT(err, "clCreateContext failed"); |
| 55 | + |
| 56 | + constexpr std::array<cl_mem_flags, 3> valid_flags = { |
| 57 | + CL_MEM_READ_WRITE, |
| 58 | + CL_MEM_HOST_NO_ACCESS, |
| 59 | + |
| 60 | + CL_MEM_READ_WRITE | CL_MEM_HOST_NO_ACCESS, |
| 61 | + }; |
| 62 | + |
| 63 | + constexpr cl_uint pipe_packet_size = sizeof(int); |
| 64 | + constexpr cl_uint pipe_max_packets = 16; |
| 65 | + |
| 66 | + for (cl_mem_flags flags : valid_flags) { |
| 67 | + cl_mem pipe = nullptr; |
| 68 | + |
| 69 | + pipe = _wrapper->clCreatePipe(context, flags, pipe_packet_size, |
| 70 | + pipe_max_packets, nullptr, &err); |
| 71 | + |
| 72 | + CHECK_RESULT(err, "clCreatePipe failed with flag %lu", flags); |
| 73 | + |
| 74 | + if (!pipe) { |
| 75 | + break; |
| 76 | + } |
| 77 | + |
| 78 | + err = _wrapper->clReleaseMemObject(pipe); |
| 79 | + CHECK_RESULT(err, "clReleaseMemObject failed"); |
| 80 | + |
| 81 | + if (err) { |
| 82 | + break; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if (err) { |
| 87 | + err = _wrapper->clReleaseContext(context); |
| 88 | + CHECK_RESULT(err, "clReleaseContext failed"); |
| 89 | + |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + constexpr std::array<cl_mem_flags, 5> invalid_flags = { |
| 94 | + CL_MEM_READ_ONLY, |
| 95 | + CL_MEM_WRITE_ONLY, |
| 96 | + |
| 97 | + CL_MEM_READ_ONLY | CL_MEM_WRITE_ONLY, |
| 98 | + |
| 99 | + 0, |
| 100 | + ~0UL, |
| 101 | + }; |
| 102 | + |
| 103 | + for (cl_mem_flags flags : valid_flags) { |
| 104 | + cl_mem pipe = nullptr; |
| 105 | + |
| 106 | + pipe = _wrapper->clCreatePipe(context, flags, pipe_packet_size, |
| 107 | + pipe_max_packets, nullptr, &err); |
| 108 | + |
| 109 | + CHECK_RESULT(err, "clCreatePipe passed when it shouldn't with flag %lu", |
| 110 | + flags); |
| 111 | + |
| 112 | + if (err) { |
| 113 | + break; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + err = _wrapper->clReleaseContext(context); |
| 118 | + CHECK_RESULT(err, "clReleaseContext failed"); |
| 119 | +} |
0 commit comments