Skip to content

Commit ab20212

Browse files
committed
added new e2e test for virtual mem extension
1 parent 2c98fd8 commit ab20212

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
#include <sycl/detail/core.hpp>
4+
#include <sycl/ext/oneapi/virtual_mem/physical_mem.hpp>
5+
#include <sycl/ext/oneapi/virtual_mem/virtual_mem.hpp>
6+
7+
namespace syclext = sycl::ext::oneapi::experimental;
8+
9+
// Find the least common multiple of the context and device granularities. This
10+
// value can be used for aligning both physical memory allocations and for
11+
// reserving virtual memory ranges.
12+
size_t GetLCMGranularity(
13+
const sycl::device &Dev, const sycl::context &Ctx,
14+
syclext::granularity_mode Gm = syclext::granularity_mode::recommended) {
15+
size_t CtxGranularity = syclext::get_mem_granularity(Ctx, Gm);
16+
size_t DevGranularity = syclext::get_mem_granularity(Dev, Ctx, Gm);
17+
18+
size_t GCD = CtxGranularity;
19+
size_t Rem = DevGranularity % GCD;
20+
while (Rem != 0) {
21+
std::swap(GCD, Rem);
22+
Rem %= GCD;
23+
}
24+
return (DevGranularity / GCD) * CtxGranularity;
25+
}
26+
27+
size_t GetAlignedByteSize(const size_t UnalignedBytes, const size_t AligmentGranularity){
28+
return ((UnalignedBytes + AligmentGranularity - 1) / AligmentGranularity) * AligmentGranularity;
29+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// This test checks whether virtual memory range can correctly be accessed
2+
// even if it was re-mapped to a different physical range.
3+
4+
// RUN: %{build} -o %t.out
5+
// RUN: %{run} %t.out
6+
7+
#include <sycl/detail/core.hpp>
8+
9+
#include <cassert>
10+
11+
#include "helpers.hpp"
12+
13+
namespace syclext = sycl::ext::oneapi::experimental;
14+
15+
int main(){
16+
17+
sycl::queue Q;
18+
sycl::context Context = Q.get_context();
19+
sycl::device Device = Q.get_device();
20+
21+
int Failed = 0;
22+
23+
constexpr size_t NumberOfElements = 1000;
24+
constexpr int ValueSetInFirstKernel = 555;
25+
constexpr int ValueSetInSecondKernel = 999;
26+
27+
size_t BytesRequired = NumberOfElements * sizeof(int);
28+
29+
size_t UsedGranularity = GetLCMGranularity(Device, Context);
30+
size_t AlignedByteSize = GetAlignedByteSize(BytesRequired, UsedGranularity);
31+
32+
syclext::physical_mem FirstPhysicalMemory{Device, Context, AlignedByteSize};
33+
uintptr_t VirtualMemoryPtr =
34+
syclext::reserve_virtual_mem(0, AlignedByteSize, Context);
35+
36+
void *MappedPtr =
37+
FirstPhysicalMemory.map(VirtualMemoryPtr, AlignedByteSize,
38+
syclext::address_access_mode::read_write);
39+
40+
int *DataPtr = reinterpret_cast<int *>(MappedPtr);
41+
42+
std::vector<int> ResultHostData(NumberOfElements);
43+
44+
Q.parallel_for(NumberOfElements, [=](sycl::id<1> Idx) {
45+
DataPtr[Idx] = ValueSetInFirstKernel;
46+
}).wait_and_throw();
47+
48+
syclext::unmap(MappedPtr, AlignedByteSize, Context);
49+
50+
syclext::physical_mem SecondPhysicalMemory{Device, Context, AlignedByteSize};
51+
MappedPtr = SecondPhysicalMemory.map(VirtualMemoryPtr, AlignedByteSize, syclext::address_access_mode::read_write);
52+
53+
Q.parallel_for(NumberOfElements, [=](sycl::id<1> Idx) {
54+
DataPtr[Idx] = ValueSetInSecondKernel;
55+
}).wait_and_throw();
56+
57+
58+
{
59+
sycl::buffer<int> ResultBuffer(ResultHostData);
60+
61+
Q.submit([&](sycl::handler &Handle) {
62+
sycl::accessor A(ResultBuffer, Handle, sycl::write_only);
63+
Handle.parallel_for(NumberOfElements,
64+
[=](sycl::id<1> Idx) { A[Idx] = DataPtr[Idx]; });
65+
});
66+
}
67+
68+
for (size_t i = 0; i < NumberOfElements; i++) {
69+
if (ResultHostData[i] != ValueSetInSecondKernel) {
70+
std::cout << "Comparison failed at index " << i << ": "
71+
<< ResultHostData[i] << " != " << ValueSetInSecondKernel<< std::endl;
72+
++Failed;
73+
}
74+
}
75+
76+
syclext::unmap(MappedPtr, AlignedByteSize, Context);
77+
syclext::free_virtual_mem(VirtualMemoryPtr, AlignedByteSize, Context);
78+
79+
return 0;
80+
}

0 commit comments

Comments
 (0)