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

Commit 84b230c

Browse files
authored
[SYCL] Add test for atomic_ref operator= (#377)
Signed-off-by: John Pennycook <[email protected]>
1 parent 82a7eec commit 84b230c

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

SYCL/AtomicRef/assignment.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
2+
// RUN: %HOST_RUN_PLACEHOLDER %t.out
3+
// RUN: %CPU_RUN_PLACEHOLDER %t.out
4+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
5+
6+
#include <CL/sycl.hpp>
7+
#include <algorithm>
8+
#include <cassert>
9+
#include <numeric>
10+
#include <vector>
11+
using namespace sycl;
12+
using namespace sycl::ONEAPI;
13+
14+
template <typename T> class assignment_kernel;
15+
16+
template <typename T> void assignment_test(queue q, size_t N) {
17+
T initial = T(N);
18+
T assignment = initial;
19+
{
20+
buffer<T> assignment_buf(&assignment, 1);
21+
q.submit([&](handler &cgh) {
22+
auto st =
23+
assignment_buf.template get_access<access::mode::read_write>(cgh);
24+
cgh.parallel_for<assignment_kernel<T>>(range<1>(N), [=](item<1> it) {
25+
size_t gid = it.get_id(0);
26+
auto atm = atomic_ref<T, ONEAPI::memory_order::relaxed,
27+
ONEAPI::memory_scope::device,
28+
access::address_space::global_space>(st[0]);
29+
atm = T(gid);
30+
});
31+
});
32+
}
33+
34+
// The initial value should have been overwritten by a work-item ID
35+
// Atomicity isn't tested here, but support for assignment() is
36+
assert(assignment != initial);
37+
assert(assignment >= T(0) && assignment <= T(N - 1));
38+
}
39+
40+
int main() {
41+
queue q;
42+
std::string version = q.get_device().get_info<info::device::version>();
43+
44+
constexpr int N = 32;
45+
assignment_test<int>(q, N);
46+
assignment_test<unsigned int>(q, N);
47+
assignment_test<long>(q, N);
48+
assignment_test<unsigned long>(q, N);
49+
assignment_test<long long>(q, N);
50+
assignment_test<unsigned long long>(q, N);
51+
assignment_test<float>(q, N);
52+
assignment_test<double>(q, N);
53+
assignment_test<char *>(q, N);
54+
55+
std::cout << "Test passed." << std::endl;
56+
}

0 commit comments

Comments
 (0)