Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit c1c3b20

Browse files
author
Steffen Larsen
committed
[SYCL] Extend coverage of USM memcpy, fill, and memset
Extends the coverage of the USM operations memcpy, fill, and memset by testing for both regular and aligned allocations for the host, shared, and device allocation types. This also migrates the USM allocator container test. Signed-off-by: Steffen Larsen <[email protected]>
1 parent 8b83ab3 commit c1c3b20

File tree

4 files changed

+614
-77
lines changed

4 files changed

+614
-77
lines changed

SYCL/USM/allocator_container.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t1.out
2+
// RUN: %HOST_RUN_PLACEHOLDER %t1.out
3+
// RUN: %CPU_RUN_PLACEHOLDER %t1.out
4+
// RUN: %GPU_RUN_PLACEHOLDER %t1.out
5+
// RUN: %ACC_RUN_PLACEHOLDER %t1.out
6+
7+
//==------ allocator_container.cpp - USM allocator in containers test ------==//
8+
//
9+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
10+
// See https://llvm.org/LICENSE.txt for license information.
11+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#include <CL/sycl.hpp>
16+
17+
#include <cassert>
18+
19+
using namespace cl::sycl;
20+
21+
constexpr int N = 100;
22+
23+
template <usm::alloc AllocMode, class KernelName>
24+
void runTest(device dev, context ctxt, queue q) {
25+
usm_allocator<int, AllocMode> alloc(ctxt, dev);
26+
27+
std::vector<int, decltype(alloc)> vec(alloc);
28+
vec.resize(N);
29+
30+
for (int i = 0; i < N; i++) {
31+
vec[i] = i;
32+
}
33+
34+
int *vals = &vec[0];
35+
36+
q.submit([=](handler &h) {
37+
h.single_task<KernelName>([=]() {
38+
for (int i = 1; i < N; i++) {
39+
vals[0] += vals[i];
40+
}
41+
});
42+
}).wait();
43+
44+
assert(vals[0] == ((N * (N - 1)) / 2));
45+
}
46+
47+
int main() {
48+
queue q;
49+
auto dev = q.get_device();
50+
auto ctxt = q.get_context();
51+
52+
if (dev.get_info<info::device::usm_shared_allocations>()) {
53+
runTest<usm::alloc::shared, class shared_test>(dev, ctxt, q);
54+
}
55+
56+
if (dev.get_info<info::device::usm_host_allocations>()) {
57+
runTest<usm::alloc::host, class host_test>(dev, ctxt, q);
58+
}
59+
60+
// usm::alloc::device is not supported by usm_allocator
61+
62+
return 0;
63+
}

SYCL/USM/fill.cpp

Lines changed: 137 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,149 @@
1616

1717
using namespace cl::sycl;
1818

19-
constexpr int count = 100;
20-
constexpr int pattern = 42;
19+
template <typename T> class usm_device_transfer;
20+
template <typename T> class usm_aligned_device_transfer;
21+
22+
static constexpr int N = 100;
23+
24+
struct test_struct {
25+
short a;
26+
int b;
27+
long c;
28+
long long d;
29+
sycl::half e;
30+
float f;
31+
double g;
32+
};
33+
34+
bool operator==(const test_struct &lhs, const test_struct &rhs) {
35+
return lhs.a == rhs.a && lhs.b == rhs.b && lhs.c == rhs.c && lhs.d == rhs.d &&
36+
lhs.e == rhs.e && lhs.f == rhs.f && lhs.g == rhs.g;
37+
}
38+
39+
template <typename T>
40+
void runHostTests(device dev, context ctxt, queue q, T val) {
41+
T *array;
42+
43+
array = (T *)malloc_host(N * sizeof(T), q);
44+
q.submit([&](handler &h) { h.fill(array, val, N); }).wait();
45+
for (int i = 0; i < N; ++i) {
46+
assert(array[i] == val);
47+
}
48+
free(array, ctxt);
49+
50+
array = (T *)aligned_alloc_host(alignof(long long), N * sizeof(T), ctxt);
51+
q.submit([&](handler &h) { h.fill(array, val, N); }).wait();
52+
for (int i = 0; i < N; ++i) {
53+
assert(array[i] == val);
54+
}
55+
free(array, ctxt);
56+
}
57+
58+
template <typename T>
59+
void runSharedTests(device dev, context ctxt, queue q, T val) {
60+
T *array;
61+
62+
array = (T *)malloc_shared(N * sizeof(T), q);
63+
q.submit([&](handler &h) { h.fill(array, val, N); }).wait();
64+
for (int i = 0; i < N; ++i) {
65+
assert(array[i] == val);
66+
}
67+
free(array, ctxt);
68+
69+
array =
70+
(T *)aligned_alloc_shared(alignof(long long), N * sizeof(T), dev, ctxt);
71+
q.submit([&](handler &h) { h.fill(array, val, N); }).wait();
72+
for (int i = 0; i < N; ++i) {
73+
assert(array[i] == val);
74+
}
75+
free(array, ctxt);
76+
}
77+
78+
template <typename T>
79+
void runDeviceTests(device dev, context ctxt, queue q, T val) {
80+
T *array;
81+
std::vector<T> out;
82+
out.resize(N);
83+
84+
array = (T *)malloc_device(N * sizeof(T), q);
85+
q.submit([&](handler &h) { h.fill(array, val, N); }).wait();
86+
87+
{
88+
buffer<T, 1> buf{&out[0], range<1>{N}};
89+
q.submit([&](handler &h) {
90+
auto acc = buf.template get_access<access::mode::write>(h);
91+
h.parallel_for<usm_device_transfer<T>>(
92+
range<1>(N), [=](id<1> item) { acc[item] = array[item]; });
93+
}).wait();
94+
}
95+
96+
for (int i = 0; i < N; ++i) {
97+
assert(out[i] == val);
98+
}
99+
free(array, ctxt);
100+
101+
out.clear();
102+
out.resize(N);
103+
104+
array =
105+
(T *)aligned_alloc_device(alignof(long long), N * sizeof(T), dev, ctxt);
106+
q.submit([&](handler &h) { h.fill(array, val, N); }).wait();
107+
108+
{
109+
buffer<T, 1> buf{&out[0], range<1>{N}};
110+
q.submit([&](handler &h) {
111+
auto acc = buf.template get_access<access::mode::write>(h);
112+
h.parallel_for<usm_aligned_device_transfer<T>>(
113+
range<1>(N), [=](id<1> item) { acc[item] = array[item]; });
114+
}).wait();
115+
}
116+
117+
for (int i = 0; i < N; ++i) {
118+
assert(out[i] == val);
119+
}
120+
free(array, ctxt);
121+
}
21122

22123
int main() {
23124
queue q;
24-
if (q.get_device().get_info<info::device::usm_shared_allocations>()) {
25-
int *mem = malloc_shared<int>(count, q);
125+
auto dev = q.get_device();
126+
auto ctxt = q.get_context();
26127

27-
for (int i = 0; i < count; i++)
28-
mem[i] = 0;
128+
test_struct test_obj{4, 42, 424, 4242, 4.2f, 4.242, 4.24242};
29129

30-
q.fill(mem, pattern, count);
31-
q.wait();
130+
if (dev.get_info<info::device::usm_host_allocations>()) {
131+
runHostTests<short>(dev, ctxt, q, 4);
132+
runHostTests<int>(dev, ctxt, q, 42);
133+
runHostTests<long>(dev, ctxt, q, 424);
134+
runHostTests<long long>(dev, ctxt, q, 4242);
135+
runHostTests<sycl::half>(dev, ctxt, q, sycl::half(4.2f));
136+
runHostTests<float>(dev, ctxt, q, 4.242f);
137+
runHostTests<double>(dev, ctxt, q, 4.24242);
138+
runHostTests<test_struct>(dev, ctxt, q, test_obj);
139+
}
140+
141+
if (dev.get_info<info::device::usm_shared_allocations>()) {
142+
runSharedTests<short>(dev, ctxt, q, 4);
143+
runSharedTests<int>(dev, ctxt, q, 42);
144+
runSharedTests<long>(dev, ctxt, q, 424);
145+
runSharedTests<long long>(dev, ctxt, q, 4242);
146+
runSharedTests<sycl::half>(dev, ctxt, q, sycl::half(4.2f));
147+
runSharedTests<float>(dev, ctxt, q, 4.242f);
148+
runSharedTests<double>(dev, ctxt, q, 4.24242);
149+
runSharedTests<test_struct>(dev, ctxt, q, test_obj);
150+
}
32151

33-
for (int i = 0; i < count; i++) {
34-
assert(mem[i] == pattern);
35-
}
152+
if (dev.get_info<info::device::usm_device_allocations>()) {
153+
runDeviceTests<short>(dev, ctxt, q, 4);
154+
runDeviceTests<int>(dev, ctxt, q, 42);
155+
runDeviceTests<long>(dev, ctxt, q, 420);
156+
runDeviceTests<long long>(dev, ctxt, q, 4242);
157+
runDeviceTests<sycl::half>(dev, ctxt, q, sycl::half(4.2f));
158+
runDeviceTests<float>(dev, ctxt, q, 4.242f);
159+
runDeviceTests<double>(dev, ctxt, q, 4.24242);
160+
runDeviceTests<test_struct>(dev, ctxt, q, test_obj);
36161
}
37-
std::cout << "Passed\n";
162+
38163
return 0;
39164
}

0 commit comments

Comments
 (0)