| 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 <Kokkos_Core.hpp> |
| 18 |
#include <TestHPX_Category.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, independent_instances_synchronization_parallel_for_range_policy) { |
| 29 |
Kokkos::View<int *, Kokkos::Experimental::HPX> a("a", n); |
| 30 |
|
| 31 |
Kokkos::Experimental::HPX instance{ |
| 32 |
Kokkos::Experimental::HPX::instance_mode::independent}; |
| 33 |
Kokkos::RangePolicy<Kokkos::Experimental::HPX> policy(instance, 0, n); |
| 34 |
Kokkos::parallel_for( |
| 35 |
"parallel_for_range_policy", policy, |
| 36 |
KOKKOS_LAMBDA(const auto i) { a[i] = i; }); |
| 37 |
|
| 38 |
instance.fence(); |
| 39 |
|
| 40 |
for (int i = 0; i < n; ++i) { |
| 41 |
ASSERT_EQ(a[i], i); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
TEST(hpx, independent_instances_synchronization_parallel_for_mdrange_policy) { |
| 46 |
Kokkos::View<int *, Kokkos::Experimental::HPX> a("a", n); |
| 47 |
|
| 48 |
Kokkos::Experimental::HPX instance{ |
| 49 |
Kokkos::Experimental::HPX::instance_mode::independent}; |
| 50 |
Kokkos::MDRangePolicy<Kokkos::Experimental::HPX, Kokkos::Rank<2>> policy( |
| 51 |
instance, {{0, 0}}, {{n, 1}}); |
| 52 |
Kokkos::parallel_for( |
| 53 |
"parallel_for_mdrange_policy", policy, |
| 54 |
KOKKOS_LAMBDA(const auto i, const auto) { a[i] = i; }); |
| 55 |
|
| 56 |
instance.fence(); |
| 57 |
|
| 58 |
for (int i = 0; i < n; ++i) { |
| 59 |
ASSERT_EQ(a[i], i); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
TEST(hpx, independent_instances_synchronization_parallel_for_team_policy) { |
| 64 |
Kokkos::View<int *, Kokkos::Experimental::HPX> a("a", n); |
| 65 |
|
| 66 |
Kokkos::Experimental::HPX instance{ |
| 67 |
Kokkos::Experimental::HPX::instance_mode::independent}; |
| 68 |
Kokkos::TeamPolicy<Kokkos::Experimental::HPX> policy(instance, n, 1); |
| 69 |
Kokkos::parallel_for( |
| 70 |
"parallel_for_team_policy", policy, KOKKOS_LAMBDA(const auto &handle) { |
| 71 |
a[handle.league_rank()] = handle.league_rank(); |
| 72 |
}); |
| 73 |
|
| 74 |
instance.fence(); |
| 75 |
|
| 76 |
for (int i = 0; i < n; ++i) { |
| 77 |
ASSERT_EQ(a[i], i); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
TEST(hpx, independent_instances_synchronization_parallel_reduce_range_policy) { |
| 82 |
Kokkos::View<int *, Kokkos::Experimental::HPX> a("a", n); |
| 83 |
Kokkos::View<int, Kokkos::Experimental::HPX> b("b"); |
| 84 |
|
| 85 |
Kokkos::Experimental::HPX instance{ |
| 86 |
Kokkos::Experimental::HPX::instance_mode::independent}; |
| 87 |
Kokkos::RangePolicy<Kokkos::Experimental::HPX> policy(instance, 0, n); |
| 88 |
Kokkos::parallel_reduce( |
| 89 |
"parallel_reduce_range_policy", policy, |
| 90 |
KOKKOS_LAMBDA(const int i, int &) { a[i] = i; }, b); |
| 91 |
|
| 92 |
instance.fence(); |
| 93 |
|
| 94 |
for (int i = 0; i < n; ++i) { |
| 95 |
ASSERT_EQ(a[i], i); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
TEST(hpx, |
| 100 |
independent_instances_synchronization_parallel_reduce_mdrange_policy) { |
| 101 |
Kokkos::View<int *, Kokkos::Experimental::HPX> a("a", n); |
| 102 |
Kokkos::View<int, Kokkos::Experimental::HPX> b("b"); |
| 103 |
|
| 104 |
Kokkos::Experimental::HPX instance{ |
| 105 |
Kokkos::Experimental::HPX::instance_mode::independent}; |
| 106 |
Kokkos::MDRangePolicy<Kokkos::Experimental::HPX, Kokkos::Rank<2>> policy( |
| 107 |
instance, {{0, 0}}, {{n, 1}}); |
| 108 |
Kokkos::parallel_reduce( |
| 109 |
"parallel_reduce_mdrange_policy", policy, |
| 110 |
KOKKOS_LAMBDA(const int i, const int, int &) { a[i] = i; }, b); |
| 111 |
|
| 112 |
instance.fence(); |
| 113 |
|
| 114 |
for (int i = 0; i < n; ++i) { |
| 115 |
ASSERT_EQ(a[i], i); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
TEST(hpx, independent_instances_synchronization_parallel_reduce_team_policy) { |
| 120 |
Kokkos::View<int *, Kokkos::Experimental::HPX> a("a", n); |
| 121 |
Kokkos::View<int, Kokkos::Experimental::HPX> b("b"); |
| 122 |
|
| 123 |
Kokkos::Experimental::HPX instance{ |
| 124 |
Kokkos::Experimental::HPX::instance_mode::independent}; |
| 125 |
Kokkos::TeamPolicy<Kokkos::Experimental::HPX> policy(instance, n, 1); |
| 126 |
Kokkos::parallel_reduce( |
| 127 |
"parallel_reduce_team_policy", policy, |
| 128 |
KOKKOS_LAMBDA(const decltype(policy)::member_type &handle, int &) { |
| 129 |
a[handle.league_rank()] = handle.league_rank(); |
| 130 |
}, |
| 131 |
b); |
| 132 |
|
| 133 |
instance.fence(); |
| 134 |
|
| 135 |
for (int i = 0; i < n; ++i) { |
| 136 |
ASSERT_EQ(a[i], i); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
TEST(hpx, independent_instances_synchronization_parallel_scan_range_policy) { |
| 141 |
Kokkos::View<int *, Kokkos::Experimental::HPX> a("a", n); |
| 142 |
Kokkos::View<int *, Kokkos::Experimental::HPX> b("b", n); |
| 143 |
|
| 144 |
Kokkos::Experimental::HPX instance{ |
| 145 |
Kokkos::Experimental::HPX::instance_mode::independent}; |
| 146 |
Kokkos::RangePolicy<Kokkos::Experimental::HPX> policy(instance, 0, n); |
| 147 |
Kokkos::parallel_scan( |
| 148 |
"parallel_scan_range_policy", policy, |
| 149 |
KOKKOS_LAMBDA(const int i, int &, bool final) { |
| 150 |
if (!final) { |
| 151 |
a[i] = i; |
| 152 |
} |
| 153 |
}, |
| 154 |
b); |
| 155 |
|
| 156 |
instance.fence(); |
| 157 |
|
| 158 |
for (int i = 0; i < n; ++i) { |
| 159 |
ASSERT_EQ(a[i], i); |
| 160 |
} |
| 161 |
} |
| 162 |
} // namespace |