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 " helpers.hpp"
12+
13+ int main (){
14+
15+ sycl::queue Queue;
16+ sycl::context Context = Queue.get_context ();
17+ sycl::device Device = Queue.get_device ();
18+
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 );
26+
27+ size_t UsedGranularity = GetLCMGranularity (Device, Context);
28+ size_t AlignedByteSize = GetAlignedByteSize (BytesRequired, UsedGranularity);
29+
30+ syclext::physical_mem PhysicalMem{Device, Context, AlignedByteSize};
31+ uintptr_t VirtualMemoryPtr = syclext::reserve_virtual_mem (0 , AlignedByteSize, Context);
32+
33+ void *MappedPtr =
34+ PhysicalMem.map (VirtualMemoryPtr, AlignedByteSize,
35+ syclext::address_access_mode::read_write);
36+
37+ int *DataPtr = reinterpret_cast <int *>(MappedPtr);
38+
39+ Queue.parallel_for (NumberOfElements, [=](sycl::id<1 > Idx) {
40+ DataPtr[Idx] = ValueSetInKernelForCopyToUSM;
41+ }).wait_and_throw ();
42+
43+ // Check that can copy from virtual memory to a USM allocation
44+ int *CopyBack = sycl::malloc_shared<int >(NumberOfElements, Queue);
45+
46+ Queue.parallel_for (NumberOfElements, [=](sycl::id<1 > Idx) {
47+ CopyBack[Idx] = DataPtr[Idx];
48+ }).wait_and_throw ();
49+
50+
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;
56+ ++Failed;
57+ }
58+ }
59+
60+ // Check that can copy from a USM allocation to virtual memory
61+ int *CopyFrom = sycl::malloc_shared<int >(NumberOfElements, Queue);
62+ for (size_t Idx =0 ; Idx<NumberOfElements; ++Idx){
63+ CopyFrom[Idx] = ValueSetForCopyToVirtualMem;
64+ }
65+
66+ Queue.parallel_for (NumberOfElements, [=](sycl::id<1 > Idx) {
67+ DataPtr[Idx] = CopyFrom[Idx];
68+ }).wait_and_throw ();
69+
70+ Queue.parallel_for (NumberOfElements, [=](sycl::id<1 > Idx) {
71+ CopyBack[Idx] = DataPtr[Idx];
72+ }).wait_and_throw ();
73+
74+ 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;
79+ ++Failed;
80+ }
81+ }
82+
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+
90+ 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;
95+ ++Failed;
96+ }
97+ }
98+
99+ // Check that can use fill on virtual memory
100+
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 ();
106+
107+ 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
111+ << std::endl;
112+ ++Failed;
113+ }
114+ }
115+
116+ sycl::free (CopyFrom, Queue);
117+ sycl::free (CopyBack, Queue);
118+ syclext::unmap (MappedPtr, AlignedByteSize, Context);
119+ syclext::free_virtual_mem (VirtualMemoryPtr, AlignedByteSize, Context);
120+
121+ return Failed;
122+ }
0 commit comments