-
Notifications
You must be signed in to change notification settings - Fork 130
[SYCL] Added tests for atomics with various memory orders and scopes #534
Changes from 1 commit
6260333
a900c8f
d7f7e34
0e272ec
0375249
c215e68
8a185e1
d18ca34
f3e6079
94b90b7
0ff5fe0
5351b6d
a8fb5f8
db06775
840c89d
138a98a
996581a
d4af22d
ccd5690
81abc0d
8ba8f1a
ed4ecdb
220b722
94e763f
e8c2553
d1e7591
9708521
55795d3
9a23a34
ad77010
8bae70f
19c3856
6a4fa77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // RUN: %clangxx -fsycl -fsycl-unnamed-lambda -fsycl-targets=%sycl_triple %s -o %t.out \ | ||
| // RUN: -Xsycl-target-backend --cuda-gpu-arch=sm_70 | ||
| // RUN: %HOST_RUN_PLACEHOLDER %t.out | ||
| // RUN: %GPU_RUN_PLACEHOLDER %t.out | ||
| // RUN: %CPU_RUN_PLACEHOLDER %t.out | ||
| // RUN: %ACC_RUN_PLACEHOLDER %t.out | ||
|
|
||
| #define SYCL_USE_NATIVE_FP_ATOMICS | ||
|
|
||
| #include "add.h" | ||
| #include <iostream> | ||
| using namespace sycl; | ||
|
|
||
| template <typename T, typename Difference = T, | ||
| memory_order order = memory_order::relaxed> | ||
| void add_test_scopes(queue q, size_t N) { | ||
| add_test<T, Difference, order, memory_scope::system>(q, N); | ||
| add_test<T, Difference, order, memory_scope::device>(q, N); | ||
| add_test<T, Difference, order, memory_scope::work_group>(q, N); | ||
| add_test<T, Difference, order, memory_scope::sub_group>(q, N); | ||
| } | ||
|
|
||
| template <typename T, typename Difference = T> | ||
| void add_test_orders_scopes(queue q, size_t N) { | ||
| add_test_scopes<T, Difference, memory_order::relaxed>(q, N); | ||
| add_test_scopes<T, Difference, memory_order::acquire>(q, N); | ||
| add_test_scopes<T, Difference, memory_order::release>(q, N); | ||
| add_test_scopes<T, Difference, memory_order::acq_rel>(q, N); | ||
| } | ||
|
|
||
| int main() { | ||
| queue q; | ||
|
|
||
| constexpr int N = 32; | ||
| add_test_orders_scopes<int>(q, N); | ||
| add_test_orders_scopes<float>(q, N); | ||
| add_test_orders_scopes<unsigned int>(q, N); | ||
| add_test_orders_scopes<double>(q, N); | ||
| add_test_orders_scopes<long>(q, N); | ||
| add_test_orders_scopes<unsigned long>(q, N); | ||
|
|
||
| // Include long long tests if they are 64 bits wide | ||
| if constexpr (sizeof(long long) == 8) { | ||
| add_test_orders_scopes<long long>(q, N); | ||
| add_test_orders_scopes<unsigned long long>(q, N); | ||
| } | ||
|
||
|
|
||
| std::cout << "Test passed." << std::endl; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| #pragma once | ||
|
|
||
| #include <CL/sycl.hpp> | ||
| #include <algorithm> | ||
| #include <cassert> | ||
| #include <numeric> | ||
| #include <vector> | ||
|
|
||
| using namespace sycl; | ||
| using namespace sycl::ext::oneapi; | ||
|
|
||
| template <typename T, memory_order order = memory_order::relaxed, | ||
| memory_scope scope = memory_scope::device> | ||
| void and_local_test(queue q) { | ||
| const size_t N = 32; | ||
| T cum = 0; | ||
| std::vector<T> output(N); | ||
| std::fill(output.begin(), output.end(), T(123456)); | ||
| { | ||
| buffer<T> cum_buf(&cum, 1); | ||
| buffer<T> output_buf(output.data(), output.size()); | ||
| q.submit([&](handler &cgh) { | ||
| auto cum = cum_buf.template get_access<access::mode::read_write>(cgh); | ||
| auto out = | ||
| output_buf.template get_access<access::mode::discard_write>(cgh); | ||
| accessor<T, 1, access::mode::read_write, access::target::local> loc(1, | ||
| cgh); | ||
|
|
||
| cgh.parallel_for(nd_range<1>(N, N), [=](nd_item<1> it) { | ||
| int gid = it.get_global_id(0); | ||
| if (gid == 0) | ||
| loc[0] = T((1ll << N) - 1); | ||
| it.barrier(access::fence_space::local_space); | ||
| auto atm = atomic_ref < T, | ||
| (order == memory_order::acquire || order == memory_order::release) | ||
| ? memory_order::relaxed | ||
| : order, | ||
| scope, access::address_space::local_space > (loc[0]); | ||
| out[gid] = atm.fetch_and(~T(1ll << gid), order); | ||
| it.barrier(access::fence_space::local_space); | ||
| if (gid == 0) | ||
| cum[0] = loc[0]; | ||
| }); | ||
| }).wait_and_throw(); | ||
| } | ||
|
|
||
| // Final value should be equal to 0 | ||
| assert(cum == 0); | ||
|
|
||
| // All other values should be unique; each work-item sets one bit to 0 | ||
| std::sort(output.begin(), output.end()); | ||
| assert(std::unique(output.begin(), output.end()) == output.end()); | ||
| } | ||
|
|
||
| template <typename T, memory_order order = memory_order::relaxed, | ||
| memory_scope scope = memory_scope::device> | ||
| void and_global_test(queue q) { | ||
| const size_t N = 32; | ||
| const T initial = T((1ll << N) - 1); | ||
| T cum = initial; | ||
| std::vector<T> output(N); | ||
| std::fill(output.begin(), output.end(), T(0)); | ||
| { | ||
| buffer<T> cum_buf(&cum, 1); | ||
| buffer<T> output_buf(output.data(), output.size()); | ||
|
|
||
| q.submit([&](handler &cgh) { | ||
| auto cum = cum_buf.template get_access<access::mode::read_write>(cgh); | ||
| auto out = | ||
| output_buf.template get_access<access::mode::discard_write>(cgh); | ||
| cgh.parallel_for(range<1>(N), [=](item<1> it) { | ||
| size_t gid = it.get_id(0); | ||
| auto atm = atomic_ref < T, | ||
| (order == memory_order::acquire || order == memory_order::release) | ||
| ? memory_order::relaxed | ||
| : order, | ||
| scope, access::address_space::global_space > (cum[0]); | ||
| out[gid] = atm.fetch_and(~T(1ll << gid), order); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| // Final value should be equal to 0 | ||
| assert(cum == 0); | ||
|
|
||
| // All other values should be unique; each work-item sets one bit to 0 | ||
| std::sort(output.begin(), output.end()); | ||
| assert(std::unique(output.begin(), output.end()) == output.end()); | ||
| } | ||
|
|
||
| template <typename T, memory_order order = memory_order::relaxed, | ||
| memory_scope scope = memory_scope::device> | ||
| void and_test(queue q) { | ||
| and_local_test<T, order, scope>(q); | ||
| and_global_test<T, order, scope>(q); | ||
| } | ||
t4c1 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Uh oh!
There was an error while loading. Please reload this page.