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