From 5523e177bbea9a871b6417c4e57178e1f3520a77 Mon Sep 17 00:00:00 2001 From: Jacob Domagala Date: Thu, 26 Sep 2024 19:49:34 +0200 Subject: [PATCH] #2281: Update license and add reduce_op.h file for Kokkos reduce operators --- .../reduce/allreduce/allreduce_holder.cc | 2 +- .../reduce/allreduce/allreduce_holder.h | 2 +- .../reduce/allreduce/data_handler.h | 2 +- src/vt/collective/reduce/allreduce/helpers.h | 15 +-- .../reduce/allreduce/rabenseifner.cc | 2 +- .../reduce/allreduce/rabenseifner.h | 2 +- .../reduce/allreduce/rabenseifner.impl.h | 2 +- .../reduce/allreduce/rabenseifner_msg.h | 8 +- .../reduce/allreduce/recursive_doubling.cc | 2 +- .../reduce/allreduce/recursive_doubling.h | 2 +- .../allreduce/recursive_doubling.impl.h | 2 +- .../reduce/allreduce/recursive_doubling_msg.h | 2 +- src/vt/collective/reduce/allreduce/state.h | 2 +- .../reduce/allreduce/state_holder.h | 2 +- src/vt/collective/reduce/allreduce/type.h | 2 +- .../reduce/operators/functors/plus_op.h | 14 ++- src/vt/objgroup/manager.impl.h | 1 - src/vt/utils/kokkos/exec_space.h | 2 +- src/vt/utils/kokkos/reduce_op.h | 93 +++++++++++++++++++ tests/perf/allreduce.cc | 2 - tests/perf/send_cost.cc | 2 - tests/unit/objgroup/test_objgroup_common.h | 13 ++- 22 files changed, 136 insertions(+), 40 deletions(-) create mode 100644 src/vt/utils/kokkos/reduce_op.h diff --git a/src/vt/collective/reduce/allreduce/allreduce_holder.cc b/src/vt/collective/reduce/allreduce/allreduce_holder.cc index 5395f38c67..25c072da3a 100644 --- a/src/vt/collective/reduce/allreduce/allreduce_holder.cc +++ b/src/vt/collective/reduce/allreduce/allreduce_holder.cc @@ -5,7 +5,7 @@ // allreduce_holder.cc // DARMA/vt => Virtual Transport // -// Copyright 2019-2021 National Technology & Engineering Solutions of Sandia, LLC +// Copyright 2019-2024 National Technology & Engineering Solutions of Sandia, LLC // (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S. // Government retains certain rights in this software. // diff --git a/src/vt/collective/reduce/allreduce/allreduce_holder.h b/src/vt/collective/reduce/allreduce/allreduce_holder.h index 3bcb7f4eeb..5aced0328a 100644 --- a/src/vt/collective/reduce/allreduce/allreduce_holder.h +++ b/src/vt/collective/reduce/allreduce/allreduce_holder.h @@ -5,7 +5,7 @@ // allreduce_holder.h // DARMA/vt => Virtual Transport // -// Copyright 2019-2021 National Technology & Engineering Solutions of Sandia, LLC +// Copyright 2019-2024 National Technology & Engineering Solutions of Sandia, LLC // (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S. // Government retains certain rights in this software. // diff --git a/src/vt/collective/reduce/allreduce/data_handler.h b/src/vt/collective/reduce/allreduce/data_handler.h index 1d190b49d2..d6ede93221 100644 --- a/src/vt/collective/reduce/allreduce/data_handler.h +++ b/src/vt/collective/reduce/allreduce/data_handler.h @@ -5,7 +5,7 @@ // data_handler.h // DARMA/vt => Virtual Transport // -// Copyright 2019-2021 National Technology & Engineering Solutions of Sandia, LLC +// Copyright 2019-2024 National Technology & Engineering Solutions of Sandia, LLC // (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S. // Government retains certain rights in this software. // diff --git a/src/vt/collective/reduce/allreduce/helpers.h b/src/vt/collective/reduce/allreduce/helpers.h index 3c418fbc71..def5973998 100644 --- a/src/vt/collective/reduce/allreduce/helpers.h +++ b/src/vt/collective/reduce/allreduce/helpers.h @@ -5,7 +5,7 @@ // helpers.h // DARMA/vt => Virtual Transport // -// Copyright 2019-2021 National Technology & Engineering Solutions of Sandia, LLC +// Copyright 2019-2024 National Technology & Engineering Solutions of Sandia, LLC // (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S. // Government retains certain rights in this software. // @@ -48,6 +48,7 @@ #include "rabenseifner_msg.h" #include "vt/messaging/message/shared_message.h" #include "vt/utils/kokkos/exec_space.h" +#include "vt/utils/kokkos/reduce_op.h" #include @@ -119,10 +120,10 @@ struct DataHelper { #if MAGISTRATE_KOKKOS_ENABLED -template -struct DataHelper> { - using DataT = Kokkos::View; - using ExecSpace = typename utils::kokkos::AssociatedExecSpace::type; +template +struct DataHelper> { + using DataT = Kokkos::View; + using ExecSpace = typename utils::kokkos::AssociatedExecSpace::type; using DataType = DataHandler; template @@ -155,7 +156,7 @@ struct DataHelper> { Kokkos::RangePolicy policy(0, msg->val_.extent(0)); Kokkos::parallel_for( "Rabenseifner::reduce", policy, KOKKOS_LAMBDA(const int i) { - Op()(dest(start_idx + i), msg->val_(i)); + utils::kokkos::KokkosOp>()(dest(start_idx + i), msg->val_(i)); } ); } @@ -168,7 +169,7 @@ struct DataHelper> { Kokkos::RangePolicy policy(0, view_val.extent(0)); Kokkos::parallel_for( "Rabenseifner::reduce", policy, KOKKOS_LAMBDA(const int i) { - Op()(dest(i), view_val(i)); + utils::kokkos::KokkosOp>()(dest(i), view_val(i)); } ); } diff --git a/src/vt/collective/reduce/allreduce/rabenseifner.cc b/src/vt/collective/reduce/allreduce/rabenseifner.cc index 9edd45a7a8..4e6bab4ce8 100644 --- a/src/vt/collective/reduce/allreduce/rabenseifner.cc +++ b/src/vt/collective/reduce/allreduce/rabenseifner.cc @@ -5,7 +5,7 @@ // rabenseifner.cc // DARMA/vt => Virtual Transport // -// Copyright 2019-2021 National Technology & Engineering Solutions of Sandia, LLC +// Copyright 2019-2024 National Technology & Engineering Solutions of Sandia, LLC // (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S. // Government retains certain rights in this software. // diff --git a/src/vt/collective/reduce/allreduce/rabenseifner.h b/src/vt/collective/reduce/allreduce/rabenseifner.h index e49b09725e..74d98ba435 100644 --- a/src/vt/collective/reduce/allreduce/rabenseifner.h +++ b/src/vt/collective/reduce/allreduce/rabenseifner.h @@ -5,7 +5,7 @@ // rabenseifner.h // DARMA/vt => Virtual Transport // -// Copyright 2019-2021 National Technology & Engineering Solutions of Sandia, LLC +// Copyright 2019-2024 National Technology & Engineering Solutions of Sandia, LLC // (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S. // Government retains certain rights in this software. // diff --git a/src/vt/collective/reduce/allreduce/rabenseifner.impl.h b/src/vt/collective/reduce/allreduce/rabenseifner.impl.h index 66e07fc87d..f391a3eb44 100644 --- a/src/vt/collective/reduce/allreduce/rabenseifner.impl.h +++ b/src/vt/collective/reduce/allreduce/rabenseifner.impl.h @@ -5,7 +5,7 @@ // rabenseifner.impl.h // DARMA/vt => Virtual Transport // -// Copyright 2019-2021 National Technology & Engineering Solutions of Sandia, LLC +// Copyright 2019-2024 National Technology & Engineering Solutions of Sandia, LLC // (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S. // Government retains certain rights in this software. // diff --git a/src/vt/collective/reduce/allreduce/rabenseifner_msg.h b/src/vt/collective/reduce/allreduce/rabenseifner_msg.h index 99f58498b5..0571742fce 100644 --- a/src/vt/collective/reduce/allreduce/rabenseifner_msg.h +++ b/src/vt/collective/reduce/allreduce/rabenseifner_msg.h @@ -5,7 +5,7 @@ // rabenseifner_msg.h // DARMA/vt => Virtual Transport // -// Copyright 2019-2021 National Technology & Engineering Solutions of Sandia, LLC +// Copyright 2019-2024 National Technology & Engineering Solutions of Sandia, LLC // (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S. // Government retains certain rights in this software. // @@ -97,9 +97,9 @@ struct RabenseifnerMsg : Message { }; #if MAGISTRATE_KOKKOS_ENABLED -template -struct RabenseifnerMsg> : Message { - using ViewT = Kokkos::View; +template +struct RabenseifnerMsg> : Message { + using ViewT = Kokkos::View; using MessageParentType = vt::Message; vt_msg_serialize_required(); diff --git a/src/vt/collective/reduce/allreduce/recursive_doubling.cc b/src/vt/collective/reduce/allreduce/recursive_doubling.cc index 2a7b2778da..f75803e8ae 100644 --- a/src/vt/collective/reduce/allreduce/recursive_doubling.cc +++ b/src/vt/collective/reduce/allreduce/recursive_doubling.cc @@ -5,7 +5,7 @@ // recursive_doubling.cc // DARMA/vt => Virtual Transport // -// Copyright 2019-2021 National Technology & Engineering Solutions of Sandia, LLC +// Copyright 2019-2024 National Technology & Engineering Solutions of Sandia, LLC // (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S. // Government retains certain rights in this software. // diff --git a/src/vt/collective/reduce/allreduce/recursive_doubling.h b/src/vt/collective/reduce/allreduce/recursive_doubling.h index c0690e2602..c19a8e3cb3 100644 --- a/src/vt/collective/reduce/allreduce/recursive_doubling.h +++ b/src/vt/collective/reduce/allreduce/recursive_doubling.h @@ -5,7 +5,7 @@ // recursive_doubling.h // DARMA/vt => Virtual Transport // -// Copyright 2019-2021 National Technology & Engineering Solutions of Sandia, LLC +// Copyright 2019-2024 National Technology & Engineering Solutions of Sandia, LLC // (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S. // Government retains certain rights in this software. // diff --git a/src/vt/collective/reduce/allreduce/recursive_doubling.impl.h b/src/vt/collective/reduce/allreduce/recursive_doubling.impl.h index 89e4439591..f30a8e825d 100644 --- a/src/vt/collective/reduce/allreduce/recursive_doubling.impl.h +++ b/src/vt/collective/reduce/allreduce/recursive_doubling.impl.h @@ -5,7 +5,7 @@ // recursive_doubling.impl.h // DARMA/vt => Virtual Transport // -// Copyright 2019-2021 National Technology & Engineering Solutions of Sandia, LLC +// Copyright 2019-2024 National Technology & Engineering Solutions of Sandia, LLC // (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S. // Government retains certain rights in this software. // diff --git a/src/vt/collective/reduce/allreduce/recursive_doubling_msg.h b/src/vt/collective/reduce/allreduce/recursive_doubling_msg.h index a1b9dbc319..bb8041e564 100644 --- a/src/vt/collective/reduce/allreduce/recursive_doubling_msg.h +++ b/src/vt/collective/reduce/allreduce/recursive_doubling_msg.h @@ -5,7 +5,7 @@ // recursive_doubling_msg.h // DARMA/vt => Virtual Transport // -// Copyright 2019-2021 National Technology & Engineering Solutions of Sandia, LLC +// Copyright 2019-2024 National Technology & Engineering Solutions of Sandia, LLC // (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S. // Government retains certain rights in this software. // diff --git a/src/vt/collective/reduce/allreduce/state.h b/src/vt/collective/reduce/allreduce/state.h index 7f0376fc04..397c7c5d60 100644 --- a/src/vt/collective/reduce/allreduce/state.h +++ b/src/vt/collective/reduce/allreduce/state.h @@ -5,7 +5,7 @@ // state.h // DARMA/vt => Virtual Transport // -// Copyright 2019-2021 National Technology & Engineering Solutions of Sandia, LLC +// Copyright 2019-2024 National Technology & Engineering Solutions of Sandia, LLC // (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S. // Government retains certain rights in this software. // diff --git a/src/vt/collective/reduce/allreduce/state_holder.h b/src/vt/collective/reduce/allreduce/state_holder.h index 161829935c..ffbae9bf3d 100644 --- a/src/vt/collective/reduce/allreduce/state_holder.h +++ b/src/vt/collective/reduce/allreduce/state_holder.h @@ -5,7 +5,7 @@ // state_holder.h // DARMA/vt => Virtual Transport // -// Copyright 2019-2021 National Technology & Engineering Solutions of Sandia, LLC +// Copyright 2019-2024 National Technology & Engineering Solutions of Sandia, LLC // (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S. // Government retains certain rights in this software. // diff --git a/src/vt/collective/reduce/allreduce/type.h b/src/vt/collective/reduce/allreduce/type.h index c620f98ceb..1e06adc6c2 100644 --- a/src/vt/collective/reduce/allreduce/type.h +++ b/src/vt/collective/reduce/allreduce/type.h @@ -5,7 +5,7 @@ // type.h // DARMA/vt => Virtual Transport // -// Copyright 2019-2021 National Technology & Engineering Solutions of Sandia, LLC +// Copyright 2019-2024 National Technology & Engineering Solutions of Sandia, LLC // (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S. // Government retains certain rights in this software. // diff --git a/src/vt/collective/reduce/operators/functors/plus_op.h b/src/vt/collective/reduce/operators/functors/plus_op.h index 2c7c6057b3..b78b7720ff 100644 --- a/src/vt/collective/reduce/operators/functors/plus_op.h +++ b/src/vt/collective/reduce/operators/functors/plus_op.h @@ -69,23 +69,27 @@ struct PlusOp> { #if MAGISTRATE_KOKKOS_ENABLED -template -struct PlusOp> { +template +struct PlusOp> { void operator()( - Kokkos::View& v1, - Kokkos::View const& v2) const { + Kokkos::View& v1, + const Kokkos::View& v2) const { + // Determine the associated execution space based on the memory space + using MemorySpace = typename Kokkos::View::memory_space; using ExecSpace = typename utils::kokkos::AssociatedExecSpace::type; Kokkos::RangePolicy policy(0, v1.extent(0)); Kokkos::parallel_for( - "PlusOp_Host", + "Kokkos_PlusOp", policy, KOKKOS_LAMBDA(const int i) { v1(i) += v2(i); } ); + + Kokkos::fence(); } }; #endif // MAGISTRATE_KOKKOS_ENABLED diff --git a/src/vt/objgroup/manager.impl.h b/src/vt/objgroup/manager.impl.h index 241fedd198..7d4dc5bbc5 100644 --- a/src/vt/objgroup/manager.impl.h +++ b/src/vt/objgroup/manager.impl.h @@ -41,7 +41,6 @@ //@HEADER */ -#include "vt/collective/reduce/allreduce/allreduce_holder.h" #if !defined INCLUDED_VT_OBJGROUP_MANAGER_IMPL_H #define INCLUDED_VT_OBJGROUP_MANAGER_IMPL_H diff --git a/src/vt/utils/kokkos/exec_space.h b/src/vt/utils/kokkos/exec_space.h index c853cbfe33..7650a9aae9 100644 --- a/src/vt/utils/kokkos/exec_space.h +++ b/src/vt/utils/kokkos/exec_space.h @@ -5,7 +5,7 @@ // exec_space.h // DARMA/vt => Virtual Transport // -// Copyright 2019-2021 National Technology & Engineering Solutions of Sandia, LLC +// Copyright 2019-2024 National Technology & Engineering Solutions of Sandia, LLC // (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S. // Government retains certain rights in this software. // diff --git a/src/vt/utils/kokkos/reduce_op.h b/src/vt/utils/kokkos/reduce_op.h new file mode 100644 index 0000000000..20ede9299c --- /dev/null +++ b/src/vt/utils/kokkos/reduce_op.h @@ -0,0 +1,93 @@ +/* +//@HEADER +// ***************************************************************************** +// +// reduce_op.h +// DARMA/vt => Virtual Transport +// +// Copyright 2019-2024 National Technology & Engineering Solutions of Sandia, LLC +// (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S. +// Government retains certain rights in this software. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * Neither the name of the copyright holder nor the names of its +// contributors may be used to endorse or promote products derived from this +// software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// Questions? Contact darma@sandia.gov +// +// ***************************************************************************** +//@HEADER +*/ + +#if !defined INCLUDED_VT_UTILS_KOKKOS_REDUCE_OP_H +#define INCLUDED_VT_UTILS_KOKKOS_REDUCE_OP_H + +#include "vt/config.h" + +#if MAGISTRATE_KOKKOS_ENABLED + +#include "vt/collective/reduce/operators/default_op.h" + +namespace vt::utils::kokkos { + +template +struct KokkosOp { + template + KOKKOS_INLINE_FUNCTION + void operator()(T& v1, T const& v2) { + if constexpr (std::is_same_v< + Op, vt::collective::reduce::operators::AndOp>) { + v1 = v1 && v2; + } else if constexpr (std::is_same_v< + Op, + vt::collective::reduce::operators::BitAndOp>) { + v1 = v1 & v2; + } else if constexpr (std::is_same_v< + Op, vt::collective::reduce::operators::BitOrOp>) { + v1 = v1 | v2; + } else if constexpr (std::is_same_v< + Op, + vt::collective::reduce::operators::BitXorOp>) { + v1 = v1 ^ v2; + } else if constexpr (std::is_same_v< + Op, vt::collective::reduce::operators::MaxOp>) { + v1 = std::max(v1, v2); + } else if constexpr (std::is_same_v< + Op, vt::collective::reduce::operators::MinOp>) { + v1 = std::min(v1, v2); + } else if constexpr (std::is_same_v< + Op, vt::collective::reduce::operators::OrOp>) { + v1 = v1 || v2; + } else if constexpr (std::is_same_v< + Op, vt::collective::reduce::operators::PlusOp>) { + v1 = v1 + v2; + } + } +}; + +} // namespace vt::utils::kokkos + +#endif // MAGISTRATE_KOKKOS_ENABLED +#endif /*INCLUDED_VT_UTILS_KOKKOS_REDUCE_OP_H*/ diff --git a/tests/perf/allreduce.cc b/tests/perf/allreduce.cc index 86482baac0..d60d4e7124 100644 --- a/tests/perf/allreduce.cc +++ b/tests/perf/allreduce.cc @@ -328,7 +328,6 @@ VT_PERF_TEST(MyTest, test_allreduce_collection_rabenseifner) { .wait(); auto const thisNode = vt::theContext()->getNode(); - auto const nextNode = (thisNode + 1) % num_nodes_; theCollective()->barrier(); @@ -382,7 +381,6 @@ VT_PERF_TEST(MyTest, test_allreduce_collection_racursive_doubling) { .wait(); auto const thisNode = vt::theContext()->getNode(); - auto const nextNode = (thisNode + 1) % num_nodes_; theCollective()->barrier(); diff --git a/tests/perf/send_cost.cc b/tests/perf/send_cost.cc index 6dc5c7f69f..4988d93c05 100644 --- a/tests/perf/send_cost.cc +++ b/tests/perf/send_cost.cc @@ -168,8 +168,6 @@ VT_PERF_TEST(SendTest, test_objgroup_send) { auto const thisNode = vt::theContext()->getNode(); auto const lastNode = theContext()->getNumNodes() - 1; - - auto const prevNode = (thisNode - 1 + num_nodes_) % num_nodes_; auto const nextNode = (thisNode + 1) % num_nodes_; for (auto size : payloadSizes) { diff --git a/tests/unit/objgroup/test_objgroup_common.h b/tests/unit/objgroup/test_objgroup_common.h index 09cead4be4..f6483f0ca9 100644 --- a/tests/unit/objgroup/test_objgroup_common.h +++ b/tests/unit/objgroup/test_objgroup_common.h @@ -159,12 +159,15 @@ struct MyObjA { auto n = vt::theContext()->getNumNodes(); auto const total_sum = n * (n - 1) / 2; - using ExecSpace = typename utils::kokkos::AssociatedExecSpace::type; - Kokkos::RangePolicy policy(0, view.extent(0)); - Kokkos::parallel_for("InitView", policy, KOKKOS_LAMBDA(const int i) { - EXPECT_EQ(view(i), total_sum); - }); + // Just in case it's not Host space + auto view_host = Kokkos::create_mirror_view(view); + Kokkos::deep_copy(view_host, view); + + vtAssert(view_host.extent(0) == 256, "View size is not right"); + for(uint32_t i = 0; i < view_host.extent(0); ++i){ + EXPECT_EQ(view_host(i), total_sum); + } total_verify_expected_++; }