|
| 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 UnifiedSVMAPIQueryDefaults : UnifiedSVMBase |
| 22 | +{ |
| 23 | + UnifiedSVMAPIQueryDefaults(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 test_query_defaults(cl_device_id queryDevice) |
| 29 | + { |
| 30 | + cl_int err = CL_SUCCESS; |
| 31 | + const void* query_ptr = &err; // a random non-USVM pointer |
| 32 | + |
| 33 | + cl_uint typeIndexQuery = 0; |
| 34 | + err = clGetSVMPointerInfoKHR( |
| 35 | + context, queryDevice, query_ptr, CL_SVM_INFO_TYPE_INDEX_KHR, |
| 36 | + sizeof(typeIndexQuery), &typeIndexQuery, nullptr); |
| 37 | + test_error(err, |
| 38 | + "clGetSVMPointerInfoKHR for CL_SVM_INFO_TYPE_INDEX_KHR"); |
| 39 | + test_assert_error_ret(typeIndexQuery == CL_UINT_MAX, |
| 40 | + "type index is not the default", |
| 41 | + CL_INVALID_VALUE); |
| 42 | + |
| 43 | + cl_svm_capabilities_khr capabilitiesQuery = ~0; |
| 44 | + err = clGetSVMPointerInfoKHR( |
| 45 | + context, queryDevice, query_ptr, CL_SVM_INFO_CAPABILITIES_KHR, |
| 46 | + sizeof(capabilitiesQuery), &capabilitiesQuery, nullptr); |
| 47 | + test_error(err, |
| 48 | + "clGetSVMPointerInfoKHR for CL_SVM_INFO_CAPABILITIES_KHR"); |
| 49 | + test_assert_error_ret(capabilitiesQuery == 0, |
| 50 | + "capabilities are not the default", |
| 51 | + CL_INVALID_VALUE); |
| 52 | + |
| 53 | + cl_svm_alloc_access_flags_khr accessFlagsQuery = ~0; |
| 54 | + err = clGetSVMPointerInfoKHR( |
| 55 | + context, queryDevice, query_ptr, CL_SVM_INFO_ACCESS_FLAGS_KHR, |
| 56 | + sizeof(accessFlagsQuery), &accessFlagsQuery, nullptr); |
| 57 | + test_error(err, |
| 58 | + "clGetSVMPointerInfoKHR for CL_SVM_INFO_ACCESS_FLAGS_KHR"); |
| 59 | + test_assert_error_ret(accessFlagsQuery == 0, |
| 60 | + "access flags are not the default", |
| 61 | + CL_INVALID_VALUE); |
| 62 | + |
| 63 | + void* basePtrQuery = &err; |
| 64 | + err = clGetSVMPointerInfoKHR( |
| 65 | + context, queryDevice, query_ptr, CL_SVM_INFO_BASE_PTR_KHR, |
| 66 | + sizeof(basePtrQuery), &basePtrQuery, nullptr); |
| 67 | + test_error(err, "clGetSVMPointerInfoKHR for CL_SVM_INFO_BASE_PTR_KHR"); |
| 68 | + test_assert_error_ret(basePtrQuery == nullptr, |
| 69 | + "base pointer is not the default", |
| 70 | + CL_INVALID_VALUE); |
| 71 | + |
| 72 | + size_t sizeQuery = ~0; |
| 73 | + err = clGetSVMPointerInfoKHR(context, queryDevice, query_ptr, |
| 74 | + CL_SVM_INFO_SIZE_KHR, sizeof(sizeQuery), |
| 75 | + &sizeQuery, nullptr); |
| 76 | + test_error(err, "clGetSVMPointerInfoKHR for CL_SVM_INFO_SIZE_KHR"); |
| 77 | + test_assert_error_ret(sizeQuery == 0, "size is not the default", |
| 78 | + CL_INVALID_VALUE); |
| 79 | + |
| 80 | + cl_device_id associatedDeviceQuery = device; |
| 81 | + err = clGetSVMPointerInfoKHR(context, queryDevice, query_ptr, |
| 82 | + CL_SVM_INFO_ASSOCIATED_DEVICE_HANDLE_KHR, |
| 83 | + sizeof(associatedDeviceQuery), |
| 84 | + &associatedDeviceQuery, nullptr); |
| 85 | + test_error(err, |
| 86 | + "clGetSVMPointerInfoKHR for " |
| 87 | + "CL_SVM_INFO_ASSOCIATED_DEVICE_HANDLE_KHR"); |
| 88 | + test_assert_error_ret(associatedDeviceQuery == nullptr, |
| 89 | + "associated device handle is not the default", |
| 90 | + CL_INVALID_VALUE); |
| 91 | + |
| 92 | + return CL_SUCCESS; |
| 93 | + } |
| 94 | + |
| 95 | + cl_int run() override |
| 96 | + { |
| 97 | + cl_int err; |
| 98 | + |
| 99 | + log_info(" testing query defaults with no device\n"); |
| 100 | + err = test_query_defaults(nullptr); |
| 101 | + test_error(err, "query defaults failed"); |
| 102 | + |
| 103 | + log_info(" testing query defaults with a device\n"); |
| 104 | + err = test_query_defaults(device); |
| 105 | + test_error(err, "query defaults failed"); |
| 106 | + |
| 107 | + return CL_SUCCESS; |
| 108 | + } |
| 109 | +}; |
| 110 | + |
| 111 | +REGISTER_TEST(unified_svm_api_query_defaults) |
| 112 | +{ |
| 113 | + REQUIRE_EXTENSION("cl_khr_unified_svm"); |
| 114 | + |
| 115 | + cl_int err; |
| 116 | + |
| 117 | + clContextWrapper contextWrapper; |
| 118 | + clCommandQueueWrapper queueWrapper; |
| 119 | + |
| 120 | + // For now: create a new context and queue. |
| 121 | + // If we switch to a new test executable and run the tests without |
| 122 | + // forceNoContextCreation then this can be removed, and we can just use the |
| 123 | + // context and the queue from the harness. |
| 124 | + if (context == nullptr) |
| 125 | + { |
| 126 | + contextWrapper = |
| 127 | + clCreateContext(nullptr, 1, &device, nullptr, nullptr, &err); |
| 128 | + test_error(err, "clCreateContext failed"); |
| 129 | + context = contextWrapper; |
| 130 | + } |
| 131 | + |
| 132 | + if (queue == nullptr) |
| 133 | + { |
| 134 | + queueWrapper = clCreateCommandQueue(context, device, 0, &err); |
| 135 | + test_error(err, "clCreateCommandQueue failed"); |
| 136 | + queue = queueWrapper; |
| 137 | + } |
| 138 | + |
| 139 | + UnifiedSVMAPIQueryDefaults Test(context, device, queue, num_elements); |
| 140 | + err = Test.setup(); |
| 141 | + test_error(err, "test setup failed"); |
| 142 | + |
| 143 | + err = Test.run(); |
| 144 | + test_error(err, "test failed"); |
| 145 | + |
| 146 | + return TEST_PASS; |
| 147 | +} |
0 commit comments