|
| 1 | +// |
| 2 | +// Copyright (c) 2025 The Khronos Group Inc. |
| 3 | +// |
| 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 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +#include "unified_svm_fixture.h" |
| 18 | +#include <cinttypes> |
| 19 | +#include <memory> |
| 20 | + |
| 21 | +struct UnifiedSVMAPISuggestedTypeIndex : UnifiedSVMBase |
| 22 | +{ |
| 23 | + UnifiedSVMAPISuggestedTypeIndex(cl_context context, cl_device_id device, |
| 24 | + cl_command_queue queue, int num_elements) |
| 25 | + : UnifiedSVMBase(context, device, queue, num_elements) |
| 26 | + {} |
| 27 | + |
| 28 | + cl_int run() override |
| 29 | + { |
| 30 | + constexpr size_t size = 16 * 1024; |
| 31 | + |
| 32 | + cl_int err = CL_SUCCESS; |
| 33 | + |
| 34 | + // Get the suggested type index for each set of capabilities supported |
| 35 | + // by the device, and build a set of all capabilities supported by the |
| 36 | + // device. |
| 37 | + cl_svm_capabilities_khr allSupportedDeviceUSVMCaps = 0; |
| 38 | + for (const auto caps : deviceUSVMCaps) |
| 39 | + { |
| 40 | + err = checkSuggestedTypeIndex(caps, size); |
| 41 | + test_error(err, "suggested type index failed"); |
| 42 | + |
| 43 | + allSupportedDeviceUSVMCaps |= caps; |
| 44 | + } |
| 45 | + |
| 46 | + // Get the suggested type index for each supported capability |
| 47 | + // individually. |
| 48 | + for (cl_uint bit = 0; bit < sizeof(cl_svm_capabilities_khr) * 8; bit++) |
| 49 | + { |
| 50 | + cl_svm_capabilities_khr testCap = |
| 51 | + static_cast<cl_svm_capabilities_khr>(1 << bit); |
| 52 | + if (allSupportedDeviceUSVMCaps & testCap) |
| 53 | + { |
| 54 | + err = checkSuggestedTypeIndex(testCap, size); |
| 55 | + test_error(err, "suggested type index failed"); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // Build a set of all capabilities supported by the platform. |
| 60 | + cl_svm_capabilities_khr allSupportedPlatformUSVMCaps = 0; |
| 61 | + for (const auto caps : platformUSVMCaps) |
| 62 | + { |
| 63 | + allSupportedPlatformUSVMCaps |= caps; |
| 64 | + } |
| 65 | + |
| 66 | + // Check that the suggested type index for an unsupported capability is |
| 67 | + // CL_UINT_MAX. |
| 68 | + if (~allSupportedPlatformUSVMCaps != 0) |
| 69 | + { |
| 70 | + cl_uint suggested = ~0; |
| 71 | + err = clGetSVMSuggestedTypeIndexKHR(context, |
| 72 | + ~allSupportedPlatformUSVMCaps, |
| 73 | + 0, nullptr, size, &suggested); |
| 74 | + test_error(err, "suggested type index failed"); |
| 75 | + test_assert_error_ret(suggested == CL_UINT_MAX, |
| 76 | + "suggested type index for unsupported " |
| 77 | + "capability is not CL_UINT_MAX", |
| 78 | + CL_INVALID_VALUE); |
| 79 | + } |
| 80 | + |
| 81 | + return CL_SUCCESS; |
| 82 | + } |
| 83 | + |
| 84 | + cl_int checkSuggestedTypeIndex(cl_svm_capabilities_khr requiredCaps, |
| 85 | + size_t size) |
| 86 | + { |
| 87 | + cl_int err; |
| 88 | + cl_uint suggested = ~0; |
| 89 | + |
| 90 | + // Test without an associated device handle. |
| 91 | + // This must return platform SVM capabilities with the required |
| 92 | + // capability, but it may return unsupported capabilities for the |
| 93 | + // device. |
| 94 | + err = clGetSVMSuggestedTypeIndexKHR(context, requiredCaps, 0, nullptr, |
| 95 | + size, &suggested); |
| 96 | + test_error(err, "clGetSVMSuggestedTypeIndexKHR failed"); |
| 97 | + test_assert_error_ret(suggested < deviceUSVMCaps.size(), |
| 98 | + "suggested type index is out of range", |
| 99 | + CL_INVALID_VALUE); |
| 100 | + if (deviceUSVMCaps[suggested] != 0) |
| 101 | + { |
| 102 | + test_assert_error_ret(deviceUSVMCaps[suggested] & requiredCaps, |
| 103 | + "suggested type index does not have the " |
| 104 | + "required device capability", |
| 105 | + CL_INVALID_VALUE); |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + test_assert_error_ret(platformUSVMCaps[suggested] & requiredCaps, |
| 110 | + "suggested type index does not have the " |
| 111 | + "required platform capability", |
| 112 | + CL_INVALID_VALUE); |
| 113 | + } |
| 114 | + |
| 115 | + // Test with an associated device handle. |
| 116 | + // This must return device SVM capabilities with the required |
| 117 | + // capability. |
| 118 | + std::vector<cl_svm_alloc_properties_khr> props; |
| 119 | + props.push_back(CL_SVM_ALLOC_ASSOCIATED_DEVICE_HANDLE_KHR); |
| 120 | + props.push_back(reinterpret_cast<cl_svm_alloc_properties_khr>(device)); |
| 121 | + props.push_back(0); |
| 122 | + err = clGetSVMSuggestedTypeIndexKHR(context, requiredCaps, 0, |
| 123 | + props.data(), size, &suggested); |
| 124 | + test_error(err, "clGetSVMSuggestedTypeIndexKHR failed"); |
| 125 | + test_assert_error_ret(suggested < deviceUSVMCaps.size(), |
| 126 | + "suggested type index is out of range", |
| 127 | + CL_INVALID_VALUE); |
| 128 | + test_assert_error_ret(deviceUSVMCaps[suggested] & requiredCaps, |
| 129 | + "suggested type index does not have the " |
| 130 | + "required device capability", |
| 131 | + CL_INVALID_VALUE); |
| 132 | + |
| 133 | + // Test with all properties - an associated device handle, an |
| 134 | + // alignment, and access flags. |
| 135 | + props.clear(); |
| 136 | + props.push_back(CL_SVM_ALLOC_ASSOCIATED_DEVICE_HANDLE_KHR); |
| 137 | + props.push_back(reinterpret_cast<cl_svm_alloc_properties_khr>(device)); |
| 138 | + props.push_back(CL_SVM_ALLOC_ACCESS_FLAGS_KHR); |
| 139 | + props.push_back(0); |
| 140 | + props.push_back(CL_SVM_ALLOC_ALIGNMENT_KHR); |
| 141 | + props.push_back(0); |
| 142 | + props.push_back(0); |
| 143 | + err = clGetSVMSuggestedTypeIndexKHR(context, requiredCaps, 0, |
| 144 | + props.data(), size, &suggested); |
| 145 | + test_error(err, "clGetSVMSuggestedTypeIndexKHR failed"); |
| 146 | + test_assert_error_ret(suggested < deviceUSVMCaps.size(), |
| 147 | + "suggested type index is out of range", |
| 148 | + CL_INVALID_VALUE); |
| 149 | + test_assert_error_ret(deviceUSVMCaps[suggested] & requiredCaps, |
| 150 | + "suggested type index does not have the " |
| 151 | + "required device capability", |
| 152 | + CL_INVALID_VALUE); |
| 153 | + |
| 154 | + return CL_SUCCESS; |
| 155 | + } |
| 156 | +}; |
| 157 | + |
| 158 | +REGISTER_TEST(unified_svm_api_suggested_type_index) |
| 159 | +{ |
| 160 | + REQUIRE_EXTENSION("cl_khr_unified_svm"); |
| 161 | + |
| 162 | + cl_int err; |
| 163 | + |
| 164 | + clContextWrapper contextWrapper; |
| 165 | + clCommandQueueWrapper queueWrapper; |
| 166 | + |
| 167 | + // For now: create a new context and queue. |
| 168 | + // If we switch to a new test executable and run the tests without |
| 169 | + // forceNoContextCreation then this can be removed, and we can just use the |
| 170 | + // context and the queue from the harness. |
| 171 | + if (context == nullptr) |
| 172 | + { |
| 173 | + contextWrapper = |
| 174 | + clCreateContext(nullptr, 1, &device, nullptr, nullptr, &err); |
| 175 | + test_error(err, "clCreateContext failed"); |
| 176 | + context = contextWrapper; |
| 177 | + } |
| 178 | + |
| 179 | + if (queue == nullptr) |
| 180 | + { |
| 181 | + queueWrapper = clCreateCommandQueue(context, device, 0, &err); |
| 182 | + test_error(err, "clCreateCommandQueue failed"); |
| 183 | + queue = queueWrapper; |
| 184 | + } |
| 185 | + |
| 186 | + UnifiedSVMAPISuggestedTypeIndex Test(context, device, queue, num_elements); |
| 187 | + err = Test.setup(); |
| 188 | + test_error(err, "test setup failed"); |
| 189 | + |
| 190 | + err = Test.run(); |
| 191 | + test_error(err, "test failed"); |
| 192 | + |
| 193 | + return TEST_PASS; |
| 194 | +} |
0 commit comments