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