|
| 1 | +// This test checks whether certain operations in virtual memory extension work |
| 2 | +// as expectd. |
| 3 | + |
| 4 | +// RUN: %{build} -o %t.out |
| 5 | +// RUN: %{run} %t.out |
| 6 | + |
| 7 | +#include "helpers.hpp" |
| 8 | + |
| 9 | +int main() { |
| 10 | + |
| 11 | + constexpr size_t NumberOfIterations = 3; |
| 12 | + std::array<size_t, NumberOfIterations> NumberOfElementsPerIteration{10, 100, |
| 13 | + 1000}; |
| 14 | + |
| 15 | + sycl::queue Q; |
| 16 | + sycl::context Context = Q.get_context(); |
| 17 | + sycl::device Device = Q.get_device(); |
| 18 | + |
| 19 | + // A check should be performed that we can successfully perform and |
| 20 | + // immediately release a valid reservation. |
| 21 | + for (const size_t RequiredNumElements : NumberOfElementsPerIteration) { |
| 22 | + size_t BytesRequired = RequiredNumElements * sizeof(int); |
| 23 | + size_t UsedGranularity = GetLCMGranularity(Device, Context); |
| 24 | + size_t AlignedByteSize = GetAlignedByteSize(BytesRequired, UsedGranularity); |
| 25 | + uintptr_t VirtualMemoryPtr = |
| 26 | + syclext::reserve_virtual_mem(0, AlignedByteSize, Context); |
| 27 | + syclext::free_virtual_mem(VirtualMemoryPtr, AlignedByteSize, Context); |
| 28 | + } |
| 29 | + |
| 30 | + // A check should be performed that we can successfully map and immediately |
| 31 | + // unmap a virtual memory range. |
| 32 | + for (const size_t RequiredNumElements : NumberOfElementsPerIteration) { |
| 33 | + size_t BytesRequired = RequiredNumElements * sizeof(int); |
| 34 | + size_t UsedGranularity = GetLCMGranularity(Device, Context); |
| 35 | + size_t AlignedByteSize = GetAlignedByteSize(BytesRequired, UsedGranularity); |
| 36 | + uintptr_t VirtualMemoryPtr = |
| 37 | + syclext::reserve_virtual_mem(0, AlignedByteSize, Context); |
| 38 | + syclext::physical_mem PhysicalMem{Device, Context, AlignedByteSize}; |
| 39 | + void *MappedPtr = PhysicalMem.map(VirtualMemoryPtr, AlignedByteSize, |
| 40 | + syclext::address_access_mode::read_write); |
| 41 | + syclext::unmap(MappedPtr, AlignedByteSize, Context); |
| 42 | + syclext::free_virtual_mem(VirtualMemoryPtr, AlignedByteSize, Context); |
| 43 | + } |
| 44 | + |
| 45 | + { |
| 46 | + // Check should be performed that methods get_context(), get_device() and |
| 47 | + // size() return correct values (i.e. ones which were passed to physical_mem |
| 48 | + // constructor). |
| 49 | + size_t BytesRequired = NumberOfElementsPerIteration[2] * sizeof(int); |
| 50 | + size_t UsedGranularity = GetLCMGranularity(Device, Context); |
| 51 | + size_t AlignedByteSize = GetAlignedByteSize(BytesRequired, UsedGranularity); |
| 52 | + |
| 53 | + syclext::physical_mem PhysicalMem{Device, Context, AlignedByteSize}; |
| 54 | + |
| 55 | + assert(PhysicalMem.get_device() == Device && |
| 56 | + "device passed to physical_mem must be the same as returned from " |
| 57 | + "get_device()"); |
| 58 | + |
| 59 | + assert(PhysicalMem.get_context() == Context && |
| 60 | + "context passed to physical_mem must be the same as returned from " |
| 61 | + "get_context()"); |
| 62 | + |
| 63 | + assert(PhysicalMem.size() == AlignedByteSize && |
| 64 | + "size in bytes passed to physical_mem must be the same as returned " |
| 65 | + "from size()"); |
| 66 | + } |
| 67 | + |
| 68 | + { |
| 69 | + // Check to see if value returned from a valid call to map() is the same as |
| 70 | + // reinterpret_cast<void *>(ptr). |
| 71 | + size_t BytesRequired = NumberOfElementsPerIteration[2] * sizeof(int); |
| 72 | + size_t UsedGranularity = GetLCMGranularity(Device, Context); |
| 73 | + size_t AlignedByteSize = GetAlignedByteSize(BytesRequired, UsedGranularity); |
| 74 | + |
| 75 | + uintptr_t VirtualMemoryPtr = |
| 76 | + syclext::reserve_virtual_mem(0, AlignedByteSize, Context); |
| 77 | + |
| 78 | + syclext::physical_mem PhysicalMem{Device, Context, AlignedByteSize}; |
| 79 | + |
| 80 | + void *MappedPtr = PhysicalMem.map(VirtualMemoryPtr, AlignedByteSize, |
| 81 | + syclext::address_access_mode::read_write); |
| 82 | + |
| 83 | + assert(MappedPtr == reinterpret_cast<void *>(VirtualMemoryPtr) && |
| 84 | + "value returned from a valid call to map() must be equal " |
| 85 | + "reinterpret_cast<void *>(ptr)"); |
| 86 | + |
| 87 | + syclext::unmap(MappedPtr, AlignedByteSize, Context); |
| 88 | + syclext::free_virtual_mem(VirtualMemoryPtr, AlignedByteSize, Context); |
| 89 | + } |
| 90 | + |
| 91 | + // Check to see if can change access mode of a virtual memory range and |
| 92 | + // immediately see it changed. |
| 93 | + for (const size_t RequiredNumElements : NumberOfElementsPerIteration) { |
| 94 | + size_t BytesRequired = RequiredNumElements * sizeof(int); |
| 95 | + size_t UsedGranularity = GetLCMGranularity(Device, Context); |
| 96 | + size_t AlignedByteSize = GetAlignedByteSize(BytesRequired, UsedGranularity); |
| 97 | + uintptr_t VirtualMemoryPtr = |
| 98 | + syclext::reserve_virtual_mem(0, AlignedByteSize, Context); |
| 99 | + syclext::physical_mem PhysicalMem{Device, Context, AlignedByteSize}; |
| 100 | + void *MappedPtr = PhysicalMem.map(VirtualMemoryPtr, AlignedByteSize, |
| 101 | + syclext::address_access_mode::read_write); |
| 102 | + |
| 103 | + syclext::address_access_mode CurrentAccessMode = |
| 104 | + syclext::get_access_mode(MappedPtr, AlignedByteSize, Context); |
| 105 | + |
| 106 | + assert(CurrentAccessMode == syclext::address_access_mode::read_write && |
| 107 | + "access mode must be address_access_mode::read_write before change " |
| 108 | + "with " |
| 109 | + "set_access_mode()"); |
| 110 | + |
| 111 | + syclext::set_access_mode(MappedPtr, AlignedByteSize, |
| 112 | + syclext::address_access_mode::read, Context); |
| 113 | + |
| 114 | + CurrentAccessMode = |
| 115 | + syclext::get_access_mode(MappedPtr, AlignedByteSize, Context); |
| 116 | + |
| 117 | + assert(CurrentAccessMode == syclext::address_access_mode::read && |
| 118 | + "access mode must be address_access_mode::read after change with " |
| 119 | + "set_access_mode()"); |
| 120 | + |
| 121 | + syclext::unmap(MappedPtr, AlignedByteSize, Context); |
| 122 | + syclext::free_virtual_mem(VirtualMemoryPtr, AlignedByteSize, Context); |
| 123 | + } |
| 124 | + |
| 125 | + return 0; |
| 126 | +} |
0 commit comments