|
| 1 | +// This test checks whether virtual memory range can correctly be accessed |
| 2 | +// even if it was re-mapped to a different physical range. |
| 3 | + |
| 4 | +// RUN: %{build} -o %t.out |
| 5 | +// RUN: %{run} %t.out |
| 6 | + |
| 7 | +#include <sycl/detail/core.hpp> |
| 8 | + |
| 9 | +#include <cassert> |
| 10 | + |
| 11 | +#include "helpers.hpp" |
| 12 | + |
| 13 | +namespace syclext = sycl::ext::oneapi::experimental; |
| 14 | + |
| 15 | +int main() { |
| 16 | + |
| 17 | + sycl::queue Q; |
| 18 | + sycl::context Context = Q.get_context(); |
| 19 | + sycl::device Device = Q.get_device(); |
| 20 | + |
| 21 | + int Failed = 0; |
| 22 | + |
| 23 | + constexpr size_t NumberOfElements = 1000; |
| 24 | + constexpr int ValueSetInFirstKernel = 555; |
| 25 | + constexpr int ValueSetInSecondKernel = 999; |
| 26 | + |
| 27 | + size_t BytesRequired = NumberOfElements * sizeof(int); |
| 28 | + |
| 29 | + size_t UsedGranularity = GetLCMGranularity(Device, Context); |
| 30 | + size_t AlignedByteSize = GetAlignedByteSize(BytesRequired, UsedGranularity); |
| 31 | + |
| 32 | + syclext::physical_mem FirstPhysicalMemory{Device, Context, AlignedByteSize}; |
| 33 | + uintptr_t VirtualMemoryPtr = |
| 34 | + syclext::reserve_virtual_mem(0, AlignedByteSize, Context); |
| 35 | + |
| 36 | + void *MappedPtr = |
| 37 | + FirstPhysicalMemory.map(VirtualMemoryPtr, AlignedByteSize, |
| 38 | + syclext::address_access_mode::read_write); |
| 39 | + |
| 40 | + int *DataPtr = reinterpret_cast<int *>(MappedPtr); |
| 41 | + |
| 42 | + std::vector<int> ResultHostData(NumberOfElements); |
| 43 | + |
| 44 | + Q.parallel_for(NumberOfElements, [=](sycl::id<1> Idx) { |
| 45 | + DataPtr[Idx] = ValueSetInFirstKernel; |
| 46 | + }).wait_and_throw(); |
| 47 | + |
| 48 | + syclext::unmap(MappedPtr, AlignedByteSize, Context); |
| 49 | + |
| 50 | + syclext::physical_mem SecondPhysicalMemory{Device, Context, AlignedByteSize}; |
| 51 | + MappedPtr = |
| 52 | + SecondPhysicalMemory.map(VirtualMemoryPtr, AlignedByteSize, |
| 53 | + syclext::address_access_mode::read_write); |
| 54 | + |
| 55 | + Q.parallel_for(NumberOfElements, [=](sycl::id<1> Idx) { |
| 56 | + DataPtr[Idx] = ValueSetInSecondKernel; |
| 57 | + }).wait_and_throw(); |
| 58 | + |
| 59 | + { |
| 60 | + sycl::buffer<int> ResultBuffer(ResultHostData); |
| 61 | + |
| 62 | + Q.submit([&](sycl::handler &Handle) { |
| 63 | + sycl::accessor A(ResultBuffer, Handle, sycl::write_only); |
| 64 | + Handle.parallel_for(NumberOfElements, |
| 65 | + [=](sycl::id<1> Idx) { A[Idx] = DataPtr[Idx]; }); |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + for (size_t i = 0; i < NumberOfElements; i++) { |
| 70 | + if (ResultHostData[i] != ValueSetInSecondKernel) { |
| 71 | + std::cout << "Comparison failed at index " << i << ": " |
| 72 | + << ResultHostData[i] << " != " << ValueSetInSecondKernel |
| 73 | + << std::endl; |
| 74 | + ++Failed; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + syclext::unmap(MappedPtr, AlignedByteSize, Context); |
| 79 | + syclext::free_virtual_mem(VirtualMemoryPtr, AlignedByteSize, Context); |
| 80 | + |
| 81 | + return Failed; |
| 82 | +} |
0 commit comments