Debian Codegraph @

packages/kokkos/core/unit_test/hpx/TestHPX_InParallel.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 <gtest/gtest.h>
18 #include <Kokkos_Core.hpp>
19
20 // These tests specifically check that work dispatched to independent instances
21 // is synchronized correctly on fences. A previous bug that this protects
22 // against is work being mistakenly dispatched to the default instance, but the
23 // fence fencing the independent instance. In that case these tests will fail.
24
25 namespace {
26 inline constexpr int n = 1 << 10;
27
28 TEST(hpx, in_parallel_for_range_policy) {
29 Kokkos::View<bool *, Kokkos::Experimental::HPX> a("a", n);
30
31 ASSERT_FALSE(Kokkos::Experimental::HPX::in_parallel());
32
33 Kokkos::RangePolicy<Kokkos::Experimental::HPX> policy(0, n);
34 Kokkos::parallel_for(
35 "parallel_for_range_policy", policy, KOKKOS_LAMBDA(const int i) {
36 a(i) = Kokkos::Experimental::HPX::in_parallel();
37 });
38
39 ASSERT_FALSE(Kokkos::Experimental::HPX::in_parallel());
40 Kokkos::fence();
41 ASSERT_FALSE(Kokkos::Experimental::HPX::in_parallel());
42
43 for (int i = 0; i < n; ++i) {
44 ASSERT_TRUE(a(i));
45 }
46 }
47
48 TEST(hpx, in_parallel_for_mdrange_policy) {
49 Kokkos::View<bool *, Kokkos::Experimental::HPX> a("a", n);
50
51 ASSERT_FALSE(Kokkos::Experimental::HPX::in_parallel());
52
53 Kokkos::MDRangePolicy<Kokkos::Experimental::HPX, Kokkos::Rank<2>> policy(
54 {0, 0}, {n, 1});
55 Kokkos::parallel_for(
56 "parallel_for_mdrange_policy", policy,
57 KOKKOS_LAMBDA(const int i, const int) {
58 a(i) = Kokkos::Experimental::HPX::in_parallel();
59 });
60
61 ASSERT_FALSE(Kokkos::Experimental::HPX::in_parallel());
62 Kokkos::fence();
63 ASSERT_FALSE(Kokkos::Experimental::HPX::in_parallel());
64
65 for (int i = 0; i < n; ++i) {
66 ASSERT_TRUE(a(i));
67 }
68 }
69
70 TEST(hpx, in_parallel_for_team_policy) {
71 Kokkos::View<bool *, Kokkos::Experimental::HPX> a("a", n);
72
73 ASSERT_FALSE(Kokkos::Experimental::HPX::in_parallel());
74
75 Kokkos::TeamPolicy<Kokkos::Experimental::HPX> policy(n, 1);
76 using member_type = decltype(policy)::member_type;
77 Kokkos::parallel_for(
78 "parallel_for_team_policy", policy,
79 KOKKOS_LAMBDA(const member_type &handle) {
80 a(handle.league_rank()) = Kokkos::Experimental::HPX::in_parallel();
81 });
82
83 ASSERT_FALSE(Kokkos::Experimental::HPX::in_parallel());
84 Kokkos::fence();
85 ASSERT_FALSE(Kokkos::Experimental::HPX::in_parallel());
86
87 for (int i = 0; i < n; ++i) {
88 ASSERT_TRUE(a(i));
89 }
90 }
91
92 TEST(hpx, in_parallel_reduce_range_policy) {
93 Kokkos::View<int, Kokkos::Experimental::HPX> a("a");
94
95 ASSERT_FALSE(Kokkos::Experimental::HPX::in_parallel());
96
97 Kokkos::RangePolicy<Kokkos::Experimental::HPX> policy(0, n);
98 Kokkos::parallel_reduce(
99 "parallel_reduce_range_policy", policy,
100 KOKKOS_LAMBDA(const int, int &x) {
101 if (!Kokkos::Experimental::HPX::in_parallel()) {
102 ++x;
103 }
104 },
105 a);
106
107 ASSERT_FALSE(Kokkos::Experimental::HPX::in_parallel());
108 Kokkos::fence();
109 ASSERT_FALSE(Kokkos::Experimental::HPX::in_parallel());
110
111 ASSERT_EQ(a(), 0);
112 }
113
114 TEST(hpx, in_parallel_reduce_mdrange_policy) {
115 Kokkos::View<int, Kokkos::Experimental::HPX> a("a");
116
117 ASSERT_FALSE(Kokkos::Experimental::HPX::in_parallel());
118
119 Kokkos::MDRangePolicy<Kokkos::Experimental::HPX, Kokkos::Rank<2>> policy(
120 {0, 0}, {n, 1});
121 Kokkos::parallel_reduce(
122 "parallel_reduce_mdrange_policy", policy,
123 KOKKOS_LAMBDA(const int, const int, int &x) {
124 if (!Kokkos::Experimental::HPX::in_parallel()) {
125 ++x;
126 }
127 },
128 a);
129
130 ASSERT_FALSE(Kokkos::Experimental::HPX::in_parallel());
131 Kokkos::fence();
132 ASSERT_FALSE(Kokkos::Experimental::HPX::in_parallel());
133
134 ASSERT_EQ(a(), 0);
135 }
136
137 TEST(hpx, in_parallel_reduce_team_policy) {
138 Kokkos::View<int, Kokkos::Experimental::HPX> a("a");
139
140 ASSERT_FALSE(Kokkos::Experimental::HPX::in_parallel());
141
142 Kokkos::TeamPolicy<Kokkos::Experimental::HPX> policy(n, 1);
143 using member_type = decltype(policy)::member_type;
144 Kokkos::parallel_reduce(
145 "parallel_reduce_team_policy", policy,
146 KOKKOS_LAMBDA(const member_type &, int &x) {
147 if (!Kokkos::Experimental::HPX::in_parallel()) {
148 ++x;
149 }
150 },
151 a);
152
153 ASSERT_FALSE(Kokkos::Experimental::HPX::in_parallel());
154 Kokkos::fence();
155 ASSERT_FALSE(Kokkos::Experimental::HPX::in_parallel());
156
157 ASSERT_EQ(a(), 0);
158 }
159
160 TEST(hpx, in_parallel_scan_range_policy) {
161 Kokkos::View<int *, Kokkos::Experimental::HPX> a("a", n);
162
163 ASSERT_FALSE(Kokkos::Experimental::HPX::in_parallel());
164
165 Kokkos::RangePolicy<Kokkos::Experimental::HPX> policy(0, n);
166 Kokkos::parallel_scan(
167 "parallel_scan_range_policy", policy,
168 KOKKOS_LAMBDA(const int, int &x, bool) {
169 if (!Kokkos::Experimental::HPX::in_parallel()) {
170 ++x;
171 }
172 },
173 a);
174
175 ASSERT_FALSE(Kokkos::Experimental::HPX::in_parallel());
176 Kokkos::fence();
177 ASSERT_FALSE(Kokkos::Experimental::HPX::in_parallel());
178
179 for (int i = 0; i < n; ++i) {
180 ASSERT_EQ(a(i), 0);
181 }
182 }
183 } // namespace