|
| 1 | +#include <sycl/sycl.hpp> |
| 2 | + |
| 3 | +#include <sycl/ext/oneapi/virtual_mem/physical_mem.hpp> |
| 4 | +#include <sycl/ext/oneapi/virtual_mem/virtual_mem.hpp> |
| 5 | + |
| 6 | +#include "ur_api.h" |
| 7 | + |
| 8 | +#include <helpers/UrMock.hpp> |
| 9 | + |
| 10 | +#include <gtest/gtest.h> |
| 11 | + |
| 12 | +#include <array> |
| 13 | + |
| 14 | +namespace syclext = sycl::ext::oneapi::experimental; |
| 15 | + |
| 16 | +constexpr size_t NumberOfDevices = 3; |
| 17 | + |
| 18 | +std::array<ur_device_handle_t, NumberOfDevices> GlobalDevicesHandle{ |
| 19 | + mock::createDummyHandle<ur_device_handle_t>(), |
| 20 | + mock::createDummyHandle<ur_device_handle_t>(), |
| 21 | + mock::createDummyHandle<ur_device_handle_t>(), |
| 22 | +}; |
| 23 | + |
| 24 | +ur_result_t setup_urDeviceGet(void *pParams) { |
| 25 | + auto params = *static_cast<ur_device_get_params_t *>(pParams); |
| 26 | + if (*params.ppNumDevices) { |
| 27 | + **params.ppNumDevices = NumberOfDevices; |
| 28 | + } |
| 29 | + if (*params.pphDevices) { |
| 30 | + for (size_t i = 0; i < NumberOfDevices; ++i) |
| 31 | + (*params.pphDevices)[i] = GlobalDevicesHandle[i]; |
| 32 | + } |
| 33 | + return UR_RESULT_SUCCESS; |
| 34 | +} |
| 35 | + |
| 36 | +template <bool VirtualMemSupported> |
| 37 | +ur_result_t after_urDeviceGetInfo_AllDevices(void *pParams) { |
| 38 | + auto params = reinterpret_cast<ur_device_get_info_params_t *>(pParams); |
| 39 | + if (*params->ppropName == UR_DEVICE_INFO_VIRTUAL_MEMORY_SUPPORT) { |
| 40 | + if (*params->ppPropValue) |
| 41 | + *static_cast<ur_bool_t *>(*params->ppPropValue) = VirtualMemSupported; |
| 42 | + if (*params->ppPropSizeRet) |
| 43 | + **params->ppPropSizeRet = sizeof(ur_bool_t); |
| 44 | + return UR_RESULT_SUCCESS; |
| 45 | + } |
| 46 | + return UR_RESULT_SUCCESS; |
| 47 | +} |
| 48 | + |
| 49 | +template <bool VirtualMemSupported> |
| 50 | +ur_result_t after_urDeviceGetInfo_SingleDevice(void *pParams) { |
| 51 | + auto params = reinterpret_cast<ur_device_get_info_params_t *>(pParams); |
| 52 | + if (*params->ppropName == UR_DEVICE_INFO_VIRTUAL_MEMORY_SUPPORT) { |
| 53 | + if (*params->ppPropValue && *params->phDevice == GlobalDevicesHandle[0]) |
| 54 | + *static_cast<ur_bool_t *>(*params->ppPropValue) = VirtualMemSupported; |
| 55 | + if (*params->ppPropSizeRet) |
| 56 | + **params->ppPropSizeRet = sizeof(ur_bool_t); |
| 57 | + return UR_RESULT_SUCCESS; |
| 58 | + } |
| 59 | + return UR_RESULT_SUCCESS; |
| 60 | +} |
| 61 | + |
| 62 | +TEST(VirtualMemoryMultipleDevices, ThrowExceptionForGetMemGranularityContext) { |
| 63 | + |
| 64 | + sycl::unittest::UrMock<> Mock; |
| 65 | + mock::getCallbacks().set_after_callback("urDeviceGet", &setup_urDeviceGet); |
| 66 | + mock::getCallbacks().set_after_callback( |
| 67 | + "urDeviceGetInfo", &after_urDeviceGetInfo_SingleDevice<false>); |
| 68 | + sycl::platform Platform = sycl::platform(); |
| 69 | + sycl::context Context{Platform}; |
| 70 | + |
| 71 | + try { |
| 72 | + syclext::get_mem_granularity(Context, |
| 73 | + syclext::granularity_mode::recommended); |
| 74 | + FAIL() << "No exception thrown."; |
| 75 | + } catch (sycl::exception &e) { |
| 76 | + EXPECT_EQ(e.code(), sycl::errc::feature_not_supported); |
| 77 | + EXPECT_STREQ(e.what(), "One or more devices in the context does not " |
| 78 | + "support aspect::ext_oneapi_virtual_mem."); |
| 79 | + } |
| 80 | + |
| 81 | + try { |
| 82 | + syclext::get_mem_granularity(Context, syclext::granularity_mode::minimum); |
| 83 | + FAIL() << "No exception thrown."; |
| 84 | + } catch (sycl::exception &e) { |
| 85 | + EXPECT_EQ(e.code(), sycl::errc::feature_not_supported); |
| 86 | + EXPECT_STREQ(e.what(), "One or more devices in the context does not " |
| 87 | + "support aspect::ext_oneapi_virtual_mem."); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +TEST(VirtualMemoryMultipleDevices, ThrowExceptionForGetMemGranularityDevice) { |
| 92 | + |
| 93 | + sycl::unittest::UrMock<> Mock; |
| 94 | + mock::getCallbacks().set_after_callback("urDeviceGet", &setup_urDeviceGet); |
| 95 | + mock::getCallbacks().set_after_callback( |
| 96 | + "urDeviceGetInfo", &after_urDeviceGetInfo_AllDevices<false>); |
| 97 | + |
| 98 | + sycl::platform Platform = sycl::platform(); |
| 99 | + sycl::context Context{Platform}; |
| 100 | + |
| 101 | + try { |
| 102 | + syclext::get_mem_granularity(Context.get_devices()[0], Context, |
| 103 | + syclext::granularity_mode::recommended); |
| 104 | + FAIL() << "No exception thrown."; |
| 105 | + } catch (sycl::exception &e) { |
| 106 | + EXPECT_EQ(e.code(), sycl::errc::feature_not_supported); |
| 107 | + EXPECT_STREQ(e.what(), |
| 108 | + "Device does not support aspect::ext_oneapi_virtual_mem."); |
| 109 | + } |
| 110 | + |
| 111 | + try { |
| 112 | + syclext::get_mem_granularity(Context.get_devices()[0], Context, |
| 113 | + syclext::granularity_mode::minimum); |
| 114 | + FAIL() << "No exception thrown."; |
| 115 | + } catch (sycl::exception &e) { |
| 116 | + EXPECT_EQ(e.code(), sycl::errc::feature_not_supported); |
| 117 | + EXPECT_STREQ(e.what(), |
| 118 | + "Device does not support aspect::ext_oneapi_virtual_mem."); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +TEST(VirtualMemoryMultipleDevices, ReserveVirtualMemoryRange) { |
| 123 | + |
| 124 | + sycl::unittest::UrMock<> Mock; |
| 125 | + mock::getCallbacks().set_after_callback("urDeviceGet", &setup_urDeviceGet); |
| 126 | + mock::getCallbacks().set_after_callback( |
| 127 | + "urDeviceGetInfo", &after_urDeviceGetInfo_SingleDevice<false>); |
| 128 | + sycl::platform Platform = sycl::platform(); |
| 129 | + sycl::context Context{Platform}; |
| 130 | + |
| 131 | + try { |
| 132 | + syclext::reserve_virtual_mem(0, sizeof(int), Context); |
| 133 | + FAIL() << "No exception thrown."; |
| 134 | + } catch (sycl::exception &e) { |
| 135 | + EXPECT_EQ(e.code(), sycl::errc::feature_not_supported); |
| 136 | + EXPECT_STREQ(e.what(), |
| 137 | + "One or more devices in the supplied context does not support " |
| 138 | + "aspect::ext_oneapi_virtual_mem."); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +TEST(VirtualMemoryMultipleDevices, ReservePhysicalMemory) { |
| 143 | + |
| 144 | + sycl::unittest::UrMock<> Mock; |
| 145 | + mock::getCallbacks().set_after_callback("urDeviceGet", &setup_urDeviceGet); |
| 146 | + mock::getCallbacks().set_after_callback( |
| 147 | + "urDeviceGetInfo", &after_urDeviceGetInfo_AllDevices<false>); |
| 148 | + sycl::platform Platform = sycl::platform(); |
| 149 | + sycl::context Context{Platform}; |
| 150 | + |
| 151 | + try { |
| 152 | + syclext::physical_mem PhysicalMem{Context.get_devices()[0], Context, |
| 153 | + sizeof(int)}; |
| 154 | + FAIL() << "No exception thrown."; |
| 155 | + } catch (sycl::exception &e) { |
| 156 | + EXPECT_EQ(e.code(), sycl::errc::feature_not_supported); |
| 157 | + EXPECT_STREQ(e.what(), |
| 158 | + "Device does not support aspect::ext_oneapi_virtual_mem."); |
| 159 | + } |
| 160 | +} |
0 commit comments