1 /*
2 //@HEADER
3 // ************************************************************************
4 //
5 // Kokkos v. 3.0
6 // Copyright (2020) National Technology & Engineering
7 // Solutions of Sandia, LLC (NTESS).
8 //
9 // Under the terms of Contract DE-NA0003525 with NTESS,
10 // the U.S. Government retains certain rights in this software.
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions are
14 // met:
15 //
16 // 1. Redistributions of source code must retain the above copyright
17 // notice, this list of conditions and the following disclaimer.
18 //
19 // 2. Redistributions in binary form must reproduce the above copyright
20 // notice, this list of conditions and the following disclaimer in the
21 // documentation and/or other materials provided with the distribution.
22 //
23 // 3. Neither the name of the Corporation nor the names of the
24 // contributors may be used to endorse or promote products derived from
25 // this software without specific prior written permission.
26 //
27 // THIS SOFTWARE IS PROVIDED BY NTESS "AS IS" AND ANY
28 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NTESS OR THE
31 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 //
39 // Questions? Contact Christian R. Trott (crtrott@sandia.gov)
40 //
41 // ************************************************************************
42 //@HEADER
43 */
44
45 #include <TestHIP_Category.hpp>
46 #include <Kokkos_Core.hpp>
47
48 namespace Test {
49
50 namespace Impl {
51
52 struct HIPStreamScratchTestFunctor {
53 using team_t = Kokkos::TeamPolicy<Kokkos::Experimental::HIP>::member_type;
54 using scratch_t =
55 Kokkos::View<int64_t*, Kokkos::Experimental::HIP::scratch_memory_space>;
56
57 Kokkos::View<int64_t, Kokkos::Experimental::HIPSpace,
58 Kokkos::MemoryTraits<Kokkos::Atomic>>
59 counter;
60 int N, M;
HIPStreamScratchTestFunctorTest::Impl::HIPStreamScratchTestFunctor61 HIPStreamScratchTestFunctor(
62 Kokkos::View<int64_t, Kokkos::Experimental::HIPSpace> counter_, int N_,
63 int M_)
64 : counter(counter_), N(N_), M(M_) {}
65
66 KOKKOS_FUNCTION
operator ()Test::Impl::HIPStreamScratchTestFunctor67 void operator()(const team_t& team) const {
68 scratch_t scr(team.team_scratch(1), M);
69 Kokkos::parallel_for(Kokkos::TeamThreadRange(team, 0, M),
70 [&](int i) { scr[i] = 0; });
71 team.team_barrier();
72 for (int i = 0; i < N; i++) {
73 Kokkos::parallel_for(Kokkos::TeamThreadRange(team, 0, M),
74 [&](int j) { scr[j] += 1; });
75 }
76 team.team_barrier();
77 Kokkos::parallel_for(Kokkos::TeamThreadRange(team, 0, M), [&](int i) {
78 if (scr[i] != N) counter()++;
79 });
80 }
81 };
82
hip_stream_scratch_test_one(int N,int T,int M_base,Kokkos::View<int64_t,Kokkos::Experimental::HIPSpace> counter,Kokkos::Experimental::HIP hip,int tid)83 void hip_stream_scratch_test_one(
84 int N, int T, int M_base,
85 Kokkos::View<int64_t, Kokkos::Experimental::HIPSpace> counter,
86 Kokkos::Experimental::HIP hip, int tid) {
87 int M = M_base + tid * 5;
88 Kokkos::TeamPolicy<Kokkos::Experimental::HIP> p(hip, T, 64);
89 using scratch_t =
90 Kokkos::View<int64_t*, Kokkos::Experimental::HIP::scratch_memory_space>;
91
92 int bytes = scratch_t::shmem_size(M);
93
94 for (int r = 0; r < 15; r++) {
95 Kokkos::parallel_for("Run", p.set_scratch_size(1, Kokkos::PerTeam(bytes)),
96 HIPStreamScratchTestFunctor(counter, N, M));
97 }
98 }
99
hip_stream_scratch_test(int N,int T,int M_base,Kokkos::View<int64_t,Kokkos::Experimental::HIPSpace> counter)100 void hip_stream_scratch_test(
101 int N, int T, int M_base,
102 Kokkos::View<int64_t, Kokkos::Experimental::HIPSpace> counter) {
103 int K = 4;
104 hipStream_t stream[4];
105 Kokkos::Experimental::HIP hip[4];
106 for (int i = 0; i < K; i++) {
107 HIP_SAFE_CALL(hipStreamCreate(&stream[i]));
108 hip[i] = Kokkos::Experimental::HIP(stream[i]);
109 }
110 // Test that growing scratch size in subsequent calls doesn't crash things
111 #if defined(KOKKOS_ENABLE_OPENMP)
112 #pragma omp parallel
113 {
114 int tid = omp_get_thread_num();
115 // Limit how many threads submit
116 if (tid < 4) {
117 hip_stream_scratch_test_one(N, T, M_base, counter, hip[tid], tid);
118 }
119 }
120 #else
121 for (int tid = 0; tid < K; tid++) {
122 hip_stream_scratch_test_one(N, T, M_base, counter, hip[tid], tid);
123 }
124 #endif
125 // Test that if everything is large enough, multiple launches with different
126 // scratch sizes don't step on each other
127 for (int tid = K - 1; tid >= 0; tid--) {
128 hip_stream_scratch_test_one(N, T, M_base, counter, hip[tid], tid);
129 }
130
131 Kokkos::fence();
132 for (int i = 0; i < K; i++) {
133 hip[i] = Kokkos::Experimental::HIP();
134 HIP_SAFE_CALL(hipStreamDestroy(stream[i]));
135 }
136 }
137 } // namespace Impl
138
TEST(hip,team_scratch_1_streams)139 TEST(hip, team_scratch_1_streams) {
140 int N = 1000000;
141 int T = 10;
142 int M_base = 150;
143
144 Kokkos::View<int64_t, Kokkos::Experimental::HIPSpace> counter("C");
145
146 Impl::hip_stream_scratch_test(N, T, M_base, counter);
147
148 int64_t result;
149 Kokkos::deep_copy(result, counter);
150 ASSERT_EQ(0, result);
151 }
152 } // namespace Test
153