Debian Codegraph @

packages/kokkos/core/unit_test/sycl/TestSYCL_TeamScratchStreams.cpp

raw
1 //@HEADER
2 // ************************************************************************
3 //
4 // Kokkos v. 4.0
5 // Copyright (2022) National Technology & Engineering
6 // Solutions of Sandia, LLC (NTESS).
7 //
8 // Under the terms of Contract DE-NA0003525 with NTESS,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12 // See https://kokkos.org/LICENSE for license information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //@HEADER
16
17 #include <TestSYCL_Category.hpp>
18 #include <Kokkos_Core.hpp>
19
20 namespace Test {
21
22 namespace Impl {
23
24 struct SYCLQueueScratchTestFunctor {
25 using team_t = Kokkos::TeamPolicy<Kokkos::SYCL>::member_type;
26 using scratch_t = Kokkos::View<int64_t*, Kokkos::SYCL::scratch_memory_space>;
27
28 Kokkos::View<int64_t, Kokkos::SYCLDeviceUSMSpace,
29 Kokkos::MemoryTraits<Kokkos::Atomic>>
30 counter;
31 int N, M;
32 SYCLQueueScratchTestFunctor(
33 Kokkos::View<int64_t, Kokkos::SYCLDeviceUSMSpace> counter_, int N_,
34 int M_)
35 : counter(counter_), N(N_), M(M_) {}
36
37 KOKKOS_FUNCTION
38 void operator()(const team_t& team) const {
39 scratch_t scr(team.team_scratch(1), M);
40 Kokkos::parallel_for(Kokkos::TeamThreadRange(team, 0, M),
41 [&](int i) { scr[i] = 0; });
42 team.team_barrier();
43 for (int i = 0; i < N; i++) {
44 Kokkos::parallel_for(Kokkos::TeamThreadRange(team, 0, M),
45 [&](int j) { scr[j] += 1; });
46 }
47 team.team_barrier();
48 Kokkos::parallel_for(Kokkos::TeamThreadRange(team, 0, M), [&](int i) {
49 if (scr[i] != N) counter()++;
50 });
51 }
52 };
53
54 void sycl_queue_scratch_test_one(
55 int N, int T, int M_base,
56 Kokkos::View<int64_t, Kokkos::SYCLDeviceUSMSpace> counter,
57 Kokkos::SYCL sycl, int tid) {
58 int M = M_base + tid * 5;
59 Kokkos::TeamPolicy<Kokkos::SYCL> p(sycl, T, 64);
60 using scratch_t = Kokkos::View<int64_t*, Kokkos::SYCL::scratch_memory_space>;
61
62 int bytes = scratch_t::shmem_size(M);
63
64 for (int r = 0; r < 15; r++) {
65 Kokkos::parallel_for("Run", p.set_scratch_size(1, Kokkos::PerTeam(bytes)),
66 SYCLQueueScratchTestFunctor(counter, N, M));
67 }
68 }
69
70 void sycl_queue_scratch_test(
71 int N, int T, int M_base,
72 Kokkos::View<int64_t, Kokkos::SYCLDeviceUSMSpace> counter) {
73 constexpr int K = 4;
74 Kokkos::SYCL default_space;
75 sycl::context default_context = default_space.sycl_queue().get_context();
76
77 sycl::queue queue(default_context, sycl::default_selector_v,
78 sycl::property::queue::in_order());
79
80 std::array<Kokkos::SYCL, K> sycl;
81 for (int i = 0; i < K; i++) {
82 sycl[i] =
83 Kokkos::SYCL(sycl::queue(default_context, sycl::default_selector_v,
84 sycl::property::queue::in_order()));
85 }
86
87 // Test that growing scratch size in subsequent calls doesn't crash things
88 #if defined(KOKKOS_ENABLE_OPENMP)
89 #pragma omp parallel
90 {
91 int tid = omp_get_thread_num();
92 // Limit how many threads submit
93 if (tid < 4) {
94 sycl_queue_scratch_test_one(N, T, M_base, counter, sycl[tid], tid);
95 }
96 }
97 #else
98 for (int tid = 0; tid < K; tid++) {
99 sycl_queue_scratch_test_one(N, T, M_base, counter, sycl[tid], tid);
100 }
101 #endif
102 // Test that if everything is large enough, multiple launches with different
103 // scratch sizes don't step on each other
104 for (int tid = K - 1; tid >= 0; tid--) {
105 sycl_queue_scratch_test_one(N, T, M_base, counter, sycl[tid], tid);
106 }
107
108 Kokkos::fence();
109 }
110 } // namespace Impl
111
112 TEST(sycl, team_scratch_1_queues) {
113 int N = 1000000;
114 int T = 10;
115 int M_base = 150;
116
117 Kokkos::View<int64_t, Kokkos::SYCLDeviceUSMSpace> counter("C");
118
119 Impl::sycl_queue_scratch_test(N, T, M_base, counter);
120
121 int64_t result;
122 Kokkos::deep_copy(result, counter);
123 ASSERT_EQ(0, result);
124 }
125 } // namespace Test