88
99#include < sycl/usm.hpp>
1010
11+ #include < string_view>
12+
1113#include " helpers.hpp"
1214
15+ int performResultCheck (size_t NumberOfElements, const int * DataResultPtr, const int ExpectedResultValue, std::string_view ErrorMessage){
16+ int IsSuccessful{0 };
17+ for (size_t i = 0 ; i < NumberOfElements; i++) {
18+ if (DataResultPtr[i] != ExpectedResultValue) {
19+ std::cerr << ErrorMessage
20+ << i << " : " << DataResultPtr
21+ << " != " << ExpectedResultValue << std::endl;
22+ ++IsSuccessful;
23+ }
24+ }
25+ return IsSuccessful;
26+ }
1327int main () {
1428
1529 sycl::queue Queue;
@@ -22,6 +36,10 @@ int main() {
2236 constexpr int ValueSetInMemSetOperationPerByte = 1 ;
2337 constexpr int ValueSetInFillOperation = 444 ;
2438 constexpr size_t NumberOfElements = 1000 ;
39+
40+ int *CopyBack = sycl::malloc_shared<int >(NumberOfElements, Queue);
41+ int *CopyFrom = sycl::malloc_shared<int >(NumberOfElements, Queue);
42+
2543 size_t BytesRequired = NumberOfElements * sizeof (int );
2644
2745 size_t UsedGranularity = GetLCMGranularity (Device, Context);
@@ -36,32 +54,27 @@ int main() {
3654
3755 int *DataPtr = reinterpret_cast <int *>(MappedPtr);
3856
57+ auto copyBackFunc = [&Queue, CopyBack, DataPtr](){
58+ Queue
59+ .parallel_for (NumberOfElements,
60+ [=](sycl::id<1 > Idx) { CopyBack[Idx] = DataPtr[Idx]; })
61+ .wait_and_throw ();
62+
63+ };
64+
3965 Queue
4066 .parallel_for (
4167 NumberOfElements,
4268 [=](sycl::id<1 > Idx) { DataPtr[Idx] = ValueSetInKernelForCopyToUSM; })
4369 .wait_and_throw ();
4470
4571 // Check that one can copy from virtual memory to a USM allocation.
46- int *CopyBack = sycl::malloc_shared<int >(NumberOfElements, Queue);
47-
48- Queue
49- .parallel_for (NumberOfElements,
50- [=](sycl::id<1 > Idx) { CopyBack[Idx] = DataPtr[Idx]; })
51- .wait_and_throw ();
5272
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;
59- ++Failed;
60- }
61- }
73+ copyBackFunc ();
74+ Failed+= performResultCheck (NumberOfElements,CopyBack,ValueSetInKernelForCopyToUSM, " Comparison failed after copy from virtual memory to a USM allocation at index " );
6275
6376 // Check that can copy from a USM allocation to virtual memory
64- int *CopyFrom = sycl::malloc_shared< int >(NumberOfElements, Queue);
77+
6578 for (size_t Idx = 0 ; Idx < NumberOfElements; ++Idx) {
6679 CopyFrom[Idx] = ValueSetForCopyToVirtualMem;
6780 }
@@ -71,61 +84,28 @@ int main() {
7184 [=](sycl::id<1 > Idx) { DataPtr[Idx] = CopyFrom[Idx]; })
7285 .wait_and_throw ();
7386
74- Queue
75- .parallel_for (NumberOfElements,
76- [=](sycl::id<1 > Idx) { CopyBack[Idx] = DataPtr[Idx]; })
77- .wait_and_throw ();
78-
79- for (size_t i = 0 ; i < NumberOfElements; i++) {
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;
85- ++Failed;
86- }
87- }
87+ copyBackFunc ();
8888
89+ Failed+= performResultCheck (NumberOfElements, CopyBack, ValueSetForCopyToVirtualMem, " Comparison failed after copy from a USM allocation to virtual memory at index " );
90+
8991 // Check that can use memset on virtual memory
9092 int ExpectedResultAfterMemSetOperation{0 };
9193 std::memset (&ExpectedResultAfterMemSetOperation,
9294 ValueSetInMemSetOperationPerByte, sizeof (int ));
9395 Queue.memset (MappedPtr, ValueSetInMemSetOperationPerByte, AlignedByteSize)
9496 .wait_and_throw ();
9597
96- Queue
97- .parallel_for (NumberOfElements,
98- [=](sycl::id<1 > Idx) { CopyBack[Idx] = DataPtr[Idx]; })
99- .wait_and_throw ();
98+ copyBackFunc ();
10099
101- for (size_t i = 0 ; i < NumberOfElements; i++) {
102- if (CopyBack[i] != ExpectedResultAfterMemSetOperation) {
103- std::cout << " Comparison failed after memset operation on virtual memory "
104- " at index "
105- << i << " : " << CopyBack[i]
106- << " != " << ExpectedResultAfterMemSetOperation << std::endl;
107- ++Failed;
108- }
109- }
100+ Failed+= performResultCheck (NumberOfElements, CopyBack, ExpectedResultAfterMemSetOperation, " Comparison failed after memset operation on virtual memory at index " );
110101
111102 // Check that can use fill on virtual memory
112103 Queue.fill (DataPtr, ValueSetInFillOperation, NumberOfElements)
113104 .wait_and_throw ();
114105
115- Queue
116- .parallel_for (NumberOfElements,
117- [=](sycl::id<1 > Idx) { CopyBack[Idx] = DataPtr[Idx]; })
118- .wait_and_throw ();
106+ copyBackFunc ();
119107
120- for (size_t i = 0 ; i < NumberOfElements; i++) {
121- if (CopyBack[i] != ValueSetInFillOperation) {
122- std::cout << " Comparison failed after fill operation on virtual memory "
123- " at index "
124- << i << " : " << CopyBack[i] << " != " << ValueSetInFillOperation
125- << std::endl;
126- ++Failed;
127- }
128- }
108+ Failed+= performResultCheck (NumberOfElements, CopyBack, ValueSetInFillOperation, " Comparison failed after fill operation on virtual memory at index " );
129109
130110 sycl::free (CopyFrom, Queue);
131111 sycl::free (CopyBack, Queue);
0 commit comments