|
| 1 | +// REQUIRES: aspect-usm_shared_allocations |
| 2 | + |
| 3 | +// This test checks whether a pointer produced by a virtual memory |
| 4 | +// range mapping can indeed be used in various APIs accepting a USM pointer. |
| 5 | + |
| 6 | +// RUN: %{build} -o %t.out |
| 7 | +// RUN: %{run} %t.out |
| 8 | + |
| 9 | +#include <sycl/usm.hpp> |
| 10 | + |
| 11 | +#include <string_view> |
| 12 | + |
| 13 | +#include "helpers.hpp" |
| 14 | + |
| 15 | +int performResultCheck(size_t NumberOfElements, const int *DataResultPtr, |
| 16 | + const int ExpectedResultValue, |
| 17 | + std::string_view ErrorMessage) { |
| 18 | + int IsSuccessful{0}; |
| 19 | + for (size_t i = 0; i < NumberOfElements; i++) { |
| 20 | + if (DataResultPtr[i] != ExpectedResultValue) { |
| 21 | + std::cerr << ErrorMessage << i << ": " << DataResultPtr |
| 22 | + << " != " << ExpectedResultValue << std::endl; |
| 23 | + ++IsSuccessful; |
| 24 | + } |
| 25 | + } |
| 26 | + return IsSuccessful; |
| 27 | +} |
| 28 | +int main() { |
| 29 | + |
| 30 | + sycl::queue Queue; |
| 31 | + sycl::context Context = Queue.get_context(); |
| 32 | + sycl::device Device = Queue.get_device(); |
| 33 | + |
| 34 | + int Failed = 0; |
| 35 | + constexpr int ValueSetInKernelForCopyToUSM = 111; |
| 36 | + constexpr int ValueSetForCopyToVirtualMem = 222; |
| 37 | + constexpr int ValueSetInMemSetOperationPerByte = 1; |
| 38 | + constexpr int ValueSetInFillOperation = 444; |
| 39 | + constexpr size_t NumberOfElements = 1000; |
| 40 | + |
| 41 | + int *CopyBack = sycl::malloc_shared<int>(NumberOfElements, Queue); |
| 42 | + int *CopyFrom = sycl::malloc_shared<int>(NumberOfElements, Queue); |
| 43 | + |
| 44 | + size_t BytesRequired = NumberOfElements * sizeof(int); |
| 45 | + |
| 46 | + size_t UsedGranularity = GetLCMGranularity(Device, Context); |
| 47 | + size_t AlignedByteSize = GetAlignedByteSize(BytesRequired, UsedGranularity); |
| 48 | + |
| 49 | + syclext::physical_mem PhysicalMem{Device, Context, AlignedByteSize}; |
| 50 | + uintptr_t VirtualMemoryPtr = |
| 51 | + syclext::reserve_virtual_mem(0, AlignedByteSize, Context); |
| 52 | + |
| 53 | + void *MappedPtr = PhysicalMem.map(VirtualMemoryPtr, AlignedByteSize, |
| 54 | + syclext::address_access_mode::read_write); |
| 55 | + |
| 56 | + int *DataPtr = reinterpret_cast<int *>(MappedPtr); |
| 57 | + |
| 58 | + auto copyBackFunc = [&Queue, CopyBack, DataPtr]() { |
| 59 | + Queue |
| 60 | + .parallel_for(NumberOfElements, |
| 61 | + [=](sycl::id<1> Idx) { CopyBack[Idx] = DataPtr[Idx]; }) |
| 62 | + .wait_and_throw(); |
| 63 | + }; |
| 64 | + |
| 65 | + Queue |
| 66 | + .parallel_for( |
| 67 | + NumberOfElements, |
| 68 | + [=](sycl::id<1> Idx) { DataPtr[Idx] = ValueSetInKernelForCopyToUSM; }) |
| 69 | + .wait_and_throw(); |
| 70 | + |
| 71 | + // Check that one can copy from virtual memory to a USM allocation. |
| 72 | + |
| 73 | + copyBackFunc(); |
| 74 | + Failed += performResultCheck(NumberOfElements, CopyBack, |
| 75 | + ValueSetInKernelForCopyToUSM, |
| 76 | + "Comparison failed after copy from virtual " |
| 77 | + "memory to a USM allocation at index "); |
| 78 | + |
| 79 | + // Check that can copy from a USM allocation to virtual memory |
| 80 | + |
| 81 | + for (size_t Idx = 0; Idx < NumberOfElements; ++Idx) { |
| 82 | + CopyFrom[Idx] = ValueSetForCopyToVirtualMem; |
| 83 | + } |
| 84 | + |
| 85 | + Queue |
| 86 | + .parallel_for(NumberOfElements, |
| 87 | + [=](sycl::id<1> Idx) { DataPtr[Idx] = CopyFrom[Idx]; }) |
| 88 | + .wait_and_throw(); |
| 89 | + |
| 90 | + copyBackFunc(); |
| 91 | + |
| 92 | + Failed += performResultCheck(NumberOfElements, CopyBack, |
| 93 | + ValueSetForCopyToVirtualMem, |
| 94 | + "Comparison failed after copy from a USM " |
| 95 | + "allocation to virtual memory at index "); |
| 96 | + |
| 97 | + // Check that can use memset on virtual memory |
| 98 | + int ExpectedResultAfterMemSetOperation{0}; |
| 99 | + std::memset(&ExpectedResultAfterMemSetOperation, |
| 100 | + ValueSetInMemSetOperationPerByte, sizeof(int)); |
| 101 | + Queue.memset(MappedPtr, ValueSetInMemSetOperationPerByte, AlignedByteSize) |
| 102 | + .wait_and_throw(); |
| 103 | + |
| 104 | + copyBackFunc(); |
| 105 | + |
| 106 | + Failed += performResultCheck( |
| 107 | + NumberOfElements, CopyBack, ExpectedResultAfterMemSetOperation, |
| 108 | + "Comparison failed after memset operation on virtual memory at index "); |
| 109 | + |
| 110 | + // Check that can use fill on virtual memory |
| 111 | + Queue.fill(DataPtr, ValueSetInFillOperation, NumberOfElements) |
| 112 | + .wait_and_throw(); |
| 113 | + |
| 114 | + copyBackFunc(); |
| 115 | + |
| 116 | + Failed += performResultCheck( |
| 117 | + NumberOfElements, CopyBack, ValueSetInFillOperation, |
| 118 | + "Comparison failed after fill operation on virtual memory at index "); |
| 119 | + |
| 120 | + sycl::free(CopyFrom, Queue); |
| 121 | + sycl::free(CopyBack, Queue); |
| 122 | + syclext::unmap(MappedPtr, AlignedByteSize, Context); |
| 123 | + syclext::free_virtual_mem(VirtualMemoryPtr, AlignedByteSize, Context); |
| 124 | + |
| 125 | + return Failed; |
| 126 | +} |
0 commit comments