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
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright Contributors to the Kokkos project
#include <Kokkos_Macros.hpp>
#ifdef KOKKOS_ENABLE_EXPERIMENTAL_CXX20_MODULES
import kokkos.core;
#else
#include <Kokkos_Core.hpp>
#endif
#include <TestHPX_Category.hpp>
// 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, independent_instances_synchronization_parallel_for_range_policy) {
Kokkos::View<int *, Kokkos::Experimental::HPX> a("a", n);
Kokkos::Experimental::HPX instance{
Kokkos::Experimental::HPX::instance_mode::independent};
Kokkos::RangePolicy<Kokkos::Experimental::HPX> policy(instance, 0, n);
Kokkos::parallel_for(
"parallel_for_range_policy", policy,
KOKKOS_LAMBDA(const auto i) { a[i] = i; });
instance.fence();
for (int i = 0; i < n; ++i) {
ASSERT_EQ(a[i], i);
}
}
TEST(hpx, independent_instances_synchronization_parallel_for_mdrange_policy) {
Kokkos::View<int *, Kokkos::Experimental::HPX> a("a", n);
Kokkos::Experimental::HPX instance{
Kokkos::Experimental::HPX::instance_mode::independent};
Kokkos::MDRangePolicy<Kokkos::Experimental::HPX, Kokkos::Rank<2>> policy(
instance, {{0, 0}}, {{n, 1}});
Kokkos::parallel_for(
"parallel_for_mdrange_policy", policy,
KOKKOS_LAMBDA(const auto i, const auto) { a[i] = i; });
instance.fence();
for (int i = 0; i < n; ++i) {
ASSERT_EQ(a[i], i);
}
}
TEST(hpx, independent_instances_synchronization_parallel_for_team_policy) {
Kokkos::View<int *, Kokkos::Experimental::HPX> a("a", n);
Kokkos::Experimental::HPX instance{
Kokkos::Experimental::HPX::instance_mode::independent};
Kokkos::TeamPolicy<Kokkos::Experimental::HPX> policy(instance, n, 1);
Kokkos::parallel_for(
"parallel_for_team_policy", policy, KOKKOS_LAMBDA(const auto &handle) {
a[handle.league_rank()] = handle.league_rank();
});
instance.fence();
for (int i = 0; i < n; ++i) {
ASSERT_EQ(a[i], i);
}
}
TEST(hpx, independent_instances_synchronization_parallel_reduce_range_policy) {
Kokkos::View<int *, Kokkos::Experimental::HPX> a("a", n);
Kokkos::View<int, Kokkos::Experimental::HPX> b("b");
Kokkos::Experimental::HPX instance{
Kokkos::Experimental::HPX::instance_mode::independent};
Kokkos::RangePolicy<Kokkos::Experimental::HPX> policy(instance, 0, n);
Kokkos::parallel_reduce(
"parallel_reduce_range_policy", policy,
KOKKOS_LAMBDA(const int i, int &) { a[i] = i; }, b);
instance.fence();
for (int i = 0; i < n; ++i) {
ASSERT_EQ(a[i], i);
}
}
TEST(hpx,
independent_instances_synchronization_parallel_reduce_mdrange_policy) {
Kokkos::View<int *, Kokkos::Experimental::HPX> a("a", n);
Kokkos::View<int, Kokkos::Experimental::HPX> b("b");
Kokkos::Experimental::HPX instance{
Kokkos::Experimental::HPX::instance_mode::independent};
Kokkos::MDRangePolicy<Kokkos::Experimental::HPX, Kokkos::Rank<2>> policy(
instance, {{0, 0}}, {{n, 1}});
Kokkos::parallel_reduce(
"parallel_reduce_mdrange_policy", policy,
KOKKOS_LAMBDA(const int i, const int, int &) { a[i] = i; }, b);
instance.fence();
for (int i = 0; i < n; ++i) {
ASSERT_EQ(a[i], i);
}
}
TEST(hpx, independent_instances_synchronization_parallel_reduce_team_policy) {
Kokkos::View<int *, Kokkos::Experimental::HPX> a("a", n);
Kokkos::View<int, Kokkos::Experimental::HPX> b("b");
Kokkos::Experimental::HPX instance{
Kokkos::Experimental::HPX::instance_mode::independent};
Kokkos::TeamPolicy<Kokkos::Experimental::HPX> policy(instance, n, 1);
Kokkos::parallel_reduce(
"parallel_reduce_team_policy", policy,
KOKKOS_LAMBDA(const decltype(policy)::member_type &handle, int &) {
a[handle.league_rank()] = handle.league_rank();
},
b);
instance.fence();
for (int i = 0; i < n; ++i) {
ASSERT_EQ(a[i], i);
}
}
TEST(hpx, independent_instances_synchronization_parallel_scan_range_policy) {
Kokkos::View<int *, Kokkos::Experimental::HPX> a("a", n);
Kokkos::View<int *, Kokkos::Experimental::HPX> b("b", n);
Kokkos::Experimental::HPX instance{
Kokkos::Experimental::HPX::instance_mode::independent};
Kokkos::RangePolicy<Kokkos::Experimental::HPX> policy(instance, 0, n);
Kokkos::parallel_scan(
"parallel_scan_range_policy", policy,
KOKKOS_LAMBDA(const int i, int &, bool final) {
if (!final) {
a[i] = i;
}
},
b);
instance.fence();
for (int i = 0; i < n; ++i) {
ASSERT_EQ(a[i], i);
}
}
} // namespace
|