Skip to content

Commit bb8fce9

Browse files
authored
[SYCL][USM] Improve USM allocator test and fix improper behavior. (#1538)
Signed-off-by: James Brodman <james.brodman@intel.com>
1 parent 5e21b71 commit bb8fce9

File tree

2 files changed

+86
-21
lines changed

2 files changed

+86
-21
lines changed

sycl/include/CL/sycl/usm/usm_allocator.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ class usm_allocator {
8787
usm::alloc AllocT = AllocKind,
8888
typename std::enable_if<AllocT == usm::alloc::device, int>::type = 0>
8989
void destroy(pointer Ptr) {
90-
throw feature_not_supported(
91-
"Device pointers do not support destroy on host", PI_INVALID_OPERATION);
90+
// This method must be a NOP for device pointers.
9291
}
9392

9493
/// Note:: AllocKind == alloc::device is not allowed.

sycl/test/usm/allocator_vector.cpp

Lines changed: 85 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,101 @@ int main() {
3030
auto dev = q.get_device();
3131
auto ctxt = q.get_context();
3232

33-
if (!dev.get_info<info::device::usm_host_allocations>())
34-
return 0;
33+
if (dev.get_info<info::device::usm_host_allocations>()) {
34+
usm_allocator<int, usm::alloc::host> alloc(ctxt, dev);
3535

36-
usm_allocator<int, usm::alloc::host> alloc(ctxt, dev);
36+
std::vector<int, decltype(alloc)> vec(alloc);
37+
vec.resize(N);
3738

38-
std::vector<int, decltype(alloc)> vec(alloc);
39-
vec.resize(N);
39+
for (int i = 0; i < N; i++) {
40+
vec[i] = i;
41+
}
4042

41-
for (int i = 0; i < N; i++) {
42-
vec[i] = i;
43+
int *res = &vec[0];
44+
int *vals = &vec[0];
45+
46+
auto e1 = q.submit([=](handler &h) {
47+
h.single_task<class foo>([=]() {
48+
for (int i = 1; i < N; i++) {
49+
res[0] += vals[i];
50+
}
51+
});
52+
});
53+
54+
e1.wait();
55+
56+
int answer = (N * (N - 1)) / 2;
57+
58+
if (vec[0] != answer)
59+
return -1;
60+
}
61+
62+
if (dev.get_info<info::device::usm_shared_allocations>()) {
63+
usm_allocator<int, usm::alloc::shared> alloc(ctxt, dev);
64+
65+
std::vector<int, decltype(alloc)> vec(alloc);
66+
vec.resize(N);
67+
68+
for (int i = 0; i < N; i++) {
69+
vec[i] = i;
70+
}
71+
72+
int *res = &vec[0];
73+
int *vals = &vec[0];
74+
75+
auto e1 = q.submit([=](handler &h) {
76+
h.single_task<class bar>([=]() {
77+
for (int i = 1; i < N; i++) {
78+
res[0] += vals[i];
79+
}
80+
});
81+
});
82+
83+
e1.wait();
84+
85+
int answer = (N * (N - 1)) / 2;
86+
87+
if (vec[0] != answer)
88+
return -1;
4389
}
4490

45-
int *res = &vec[0];
46-
int *vals = &vec[0];
91+
if (dev.get_info<info::device::usm_device_allocations>()) {
92+
usm_allocator<int, usm::alloc::device> alloc(ctxt, dev);
4793

48-
auto e1 = q.submit([=](handler &cgh) {
49-
cgh.single_task<class foo>([=]() {
50-
for (int i = 1; i < N; i++) {
51-
res[0] += vals[i];
52-
}
94+
std::vector<int, decltype(alloc)> vec(alloc);
95+
vec.resize(N);
96+
97+
int *res = &vec[0];
98+
int *vals = &vec[0];
99+
100+
auto e0 = q.submit([=](handler &h) {
101+
h.single_task<class baz_init>([=]() {
102+
res[0] = 0;
103+
for (int i = 0; i < N; i++) {
104+
vals[i] = i;
105+
}
106+
});
53107
});
54-
});
55108

56-
e1.wait();
109+
auto e1 = q.submit([=](handler &h) {
110+
h.depends_on(e0);
111+
h.single_task<class baz>([=]() {
112+
for (int i = 1; i < N; i++) {
113+
res[0] += vals[i];
114+
}
115+
});
116+
});
57117

58-
int answer = (N * (N - 1)) / 2;
118+
e1.wait();
59119

60-
if (vec[0] != answer)
61-
return -1;
120+
int answer = (N * (N - 1)) / 2;
121+
int result;
122+
q.memcpy(&result, res, sizeof(int));
123+
q.wait();
124+
125+
if (result != answer)
126+
return -1;
127+
}
62128

63129
return 0;
64130
}

0 commit comments

Comments
 (0)