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