Skip to content

Commit 7c44a8f

Browse files
dyniolsAlexeySachkovsteffenlarsen
authored
[SYCL][E2E] Add test for sycl_ext_oneapi_virtual_mem extension (#15807)
Based on the test plan #15509, this PR adds an e2e test described as `Basic access from a kernel`. It introduces the `helpers.hpp` common header file, which includes the `GetLCMGranularity` function, to be used in upcoming e2e tests for `sycl_ext_oneapi_virtual_mem` extension. --------- Co-authored-by: Alexey Sachkov <[email protected]> Co-authored-by: Steffen Larsen <[email protected]>
1 parent 362063b commit 7c44a8f

File tree

3 files changed

+88
-23
lines changed

3 files changed

+88
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}

sycl/test-e2e/VirtualMem/remapping_virtual_memory_range.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
// RUN: %{build} -o %t.out
55
// RUN: %{run} %t.out
66

7-
#include <sycl/detail/core.hpp>
8-
97
#include <cassert>
108

119
#include "helpers.hpp"

sycl/test-e2e/VirtualMem/vector_with_virtual_mem.cpp

+2-21
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,11 @@
66
// RUN: %{build} -o %t.out
77
// RUN: %{run} %t.out
88

9-
#include <sycl/detail/core.hpp>
109
#include <sycl/usm.hpp>
1110

1211
#include <sycl/ext/oneapi/experimental/device_architecture.hpp>
13-
#include <sycl/ext/oneapi/virtual_mem/physical_mem.hpp>
14-
#include <sycl/ext/oneapi/virtual_mem/virtual_mem.hpp>
15-
16-
namespace syclext = sycl::ext::oneapi::experimental;
17-
18-
// Find the least common multiple of the context and device granularities. This
19-
// value can be used for aligning both physical memory allocations and for
20-
// reserving virtual memory ranges.
21-
size_t GetLCMGranularity(const sycl::device &Dev, const sycl::context &Ctx) {
22-
size_t CtxGranularity = syclext::get_mem_granularity(Ctx);
23-
size_t DevGranularity = syclext::get_mem_granularity(Dev, Ctx);
24-
25-
size_t GCD = CtxGranularity;
26-
size_t Rem = DevGranularity % GCD;
27-
while (Rem != 0) {
28-
std::swap(GCD, Rem);
29-
Rem %= GCD;
30-
}
31-
return (DevGranularity / GCD) * CtxGranularity;
32-
}
12+
13+
#include "helpers.hpp"
3314

3415
template <typename T> class VirtualVector {
3516
public:

0 commit comments

Comments
 (0)