diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 380c8d4a4b..68350af8de 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -34,6 +34,7 @@ set( collective/scatter/ collective/reduce/ collective/reduce/operators collective/reduce/functors + collective/reduce/allreduce elm group/id group/region group/global group/msg group/collective group/rooted group/base @@ -98,7 +99,7 @@ set( serialization/messaging serialization/traits serialization/auto_dispatch serialization/sizing utils/demangle utils/container utils/bits utils/mutex utils/file_spec - utils/hash utils/atomic utils/static_checks utils/string + utils/hash utils/atomic utils/static_checks utils/string utils/kokkos utils/memory utils/mpi_limits utils/compress utils/json utils/strong registry/auto registry/auto/functor registry/auto/map registry/auto/collection diff --git a/src/vt/collective/reduce/allreduce/allreduce_holder.cc b/src/vt/collective/reduce/allreduce/allreduce_holder.cc new file mode 100644 index 0000000000..a79f90ba34 --- /dev/null +++ b/src/vt/collective/reduce/allreduce/allreduce_holder.cc @@ -0,0 +1,191 @@ +/* +//@HEADER +// ***************************************************************************** +// +// allreduce_holder.cc +// 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 +*/ +#include "allreduce_holder.h" +#include "vt/objgroup/manager.h" +#include "state_holder.h" + +namespace vt::collective::reduce::allreduce { + +template +inline static void removeImpl(MapT& map, uint64_t key){ + auto it = map.find(key); + + if (it != map.end()) { + auto& [rabenseifner, recursive_doubling] = map.at(key); + + if(rabenseifner) { + delete rabenseifner; + } + + if(recursive_doubling) { + delete recursive_doubling; + } + + map.erase(key); + } +} + +Rabenseifner* AllreduceHolder::addRabensifnerAllreducer( + detail::StrongVrtProxy strong_proxy, detail::StrongGroup strong_group, + size_t num_elems) { + auto const coll_proxy = strong_proxy.get(); + + auto obj_proxy = new Rabenseifner(strong_proxy, strong_group, num_elems); + + col_reducers_[coll_proxy].first = obj_proxy; + + vt_debug_print( + verbose, allreduce, "Adding new Rabenseifner reducer for collection={:x}\n", + coll_proxy + ); + + return obj_proxy; +} + +RecursiveDoubling* +AllreduceHolder::addRecursiveDoublingAllreducer( + detail::StrongVrtProxy strong_proxy, detail::StrongGroup strong_group, + size_t num_elems) { + auto const coll_proxy = strong_proxy.get(); + auto obj_proxy = new RecursiveDoubling( + strong_proxy, strong_group, num_elems); + + col_reducers_[coll_proxy].second = obj_proxy; + + vt_debug_print( + verbose, allreduce, + "Adding new RecursiveDoubling reducer for collection={:x}\n", coll_proxy + ); + + return obj_proxy; +} + +Rabenseifner* +AllreduceHolder::addRabensifnerAllreducer(detail::StrongGroup strong_group) { + auto const group = strong_group.get(); + + auto obj_proxy = new Rabenseifner( + strong_group); + + group_reducers_[group].first = obj_proxy; + + vt_debug_print( + verbose, allreduce, + "Adding new Rabenseifner reducer for group={:x}\n", group + ); + + return obj_proxy; +} + +RecursiveDoubling* +AllreduceHolder::addRecursiveDoublingAllreducer( + detail::StrongGroup strong_group) { + auto const group = strong_group.get(); + + auto obj_proxy = new RecursiveDoubling( + strong_group); + + vt_debug_print( + verbose, allreduce, + "Adding new RecursiveDoubling reducer for group={:x}\n", group + ); + + group_reducers_[group].second = obj_proxy; + + return obj_proxy; +} + +Rabenseifner* +AllreduceHolder::addRabensifnerAllreducer(detail::StrongObjGroup strong_objgroup) { + auto const objgroup = strong_objgroup.get(); + + auto obj_proxy = new Rabenseifner( + strong_objgroup); + + objgroup_reducers_[objgroup].first = obj_proxy; + + vt_debug_print( + verbose, allreduce, + "Adding new Rabenseifner reducer for objgroup={:x}\n", objgroup + ); + + return obj_proxy; +} + +RecursiveDoubling* +AllreduceHolder::addRecursiveDoublingAllreducer( + detail::StrongObjGroup strong_objgroup) { + auto const objgroup = strong_objgroup.get(); + + auto obj_proxy = new RecursiveDoubling( + strong_objgroup); + + vt_debug_print( + verbose, allreduce, + "Adding new RecursiveDoubling reducer for objgroup={:x}\n", objgroup + ); + + objgroup_reducers_[objgroup].second = obj_proxy; + + return obj_proxy; +} + +void AllreduceHolder::remove(detail::StrongVrtProxy strong_proxy) { + auto const key = strong_proxy.get(); + StateHolder::clearAll(strong_proxy); + removeImpl(col_reducers_, key); +} + +void AllreduceHolder::remove(detail::StrongGroup strong_group) { + auto const key = strong_group.get(); + StateHolder::clearAll(strong_group); + removeImpl(group_reducers_, key); +} + +void AllreduceHolder::remove(detail::StrongObjGroup strong_objgroup) { + auto const key = strong_objgroup.get(); + StateHolder::clearAll(strong_objgroup); + removeImpl(objgroup_reducers_, key); +} + +} // namespace vt::collective::reduce::allreduce diff --git a/src/vt/collective/reduce/allreduce/allreduce_holder.h b/src/vt/collective/reduce/allreduce/allreduce_holder.h new file mode 100644 index 0000000000..8b9a083227 --- /dev/null +++ b/src/vt/collective/reduce/allreduce/allreduce_holder.h @@ -0,0 +1,142 @@ +/* +//@HEADER +// ***************************************************************************** +// +// allreduce_holder.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_COLLECTIVE_REDUCE_ALLREDUCE_ALLREDUCE_HOLDER_H +#define INCLUDED_VT_COLLECTIVE_REDUCE_ALLREDUCE_ALLREDUCE_HOLDER_H + +#include "vt/configs/types/types_type.h" +#include "vt/collective/reduce/allreduce/type.h" +#include "vt/collective/reduce/scoping/strong_types.h" +#include "vt/objgroup/proxy/proxy_objgroup.h" + +#include + +namespace vt::collective::reduce::allreduce { + +struct Rabenseifner; +struct RecursiveDoubling; + +struct AllreduceHolder { + using RabenseifnerProxy = ObjGroupProxyType; + using RecursiveDoublingProxy = ObjGroupProxyType; + + template + static decltype(auto) getAllreducer(detail::StrongVrtProxy strong_proxy); + + template + static decltype(auto) getAllreducer(detail::StrongGroup strong_group); + + template + static decltype(auto) getAllreducer(detail::StrongObjGroup strong_objgroup); + + template + static decltype(auto) getOrCreateAllreducer( + detail::StrongVrtProxy strong_proxy, detail::StrongGroup strong_group, + size_t num_elems); + + template + static decltype(auto) getOrCreateAllreducer(detail::StrongGroup strong_group); + + template + static decltype(auto) + getOrCreateAllreducer(detail::StrongObjGroup strong_objgroup); + + static void remove(detail::StrongVrtProxy strong_proxy); + static void remove(detail::StrongGroup strong_group); + static void remove(detail::StrongObjGroup strong_group); + +private: + template + static decltype(auto) getAllreducerImpl(MapT& map, uint64_t id); + + template + static decltype(auto) getOrCreateAllreducerImpl(MapT& map, uint64_t id, Args&&... args); + + static Rabenseifner* addRabensifnerAllreducer( + detail::StrongVrtProxy strong_proxy, detail::StrongGroup strong_group, + size_t num_elems); + + static RecursiveDoubling* addRecursiveDoublingAllreducer( + detail::StrongVrtProxy strong_proxy, detail::StrongGroup strong_group, + size_t num_elems); + + static Rabenseifner* + addRabensifnerAllreducer(detail::StrongGroup strong_group); + static RecursiveDoubling* + addRecursiveDoublingAllreducer(detail::StrongGroup strong_group); + + static Rabenseifner* + addRabensifnerAllreducer(detail::StrongObjGroup strong_group); + static RecursiveDoubling* + addRecursiveDoublingAllreducer(detail::StrongObjGroup strong_group); + + static inline std::unordered_map< + VirtualProxyType, std::pair> + col_reducers_ = {}; + static inline std::unordered_map< + GroupType, std::pair> + group_reducers_ = {}; + static inline std::unordered_map< + ObjGroupProxyType, std::pair> + objgroup_reducers_ = {}; +}; + +template +static inline auto* getAllreducer(ComponentInfo type) { + if (type.first == ComponentT::VrtColl) { + return AllreduceHolder::getAllreducer( + detail::StrongVrtProxy{type.second}); + } else if (type.first == ComponentT::ObjGroup) { + return AllreduceHolder::getAllreducer( + detail::StrongObjGroup{type.second}); + } else { + return AllreduceHolder::getAllreducer( + detail::StrongGroup{type.second}); + } +} + +} // namespace vt::collective::reduce::allreduce + +#include "vt/collective/reduce/allreduce/allreduce_holder.impl.h" + +#endif /*INCLUDED_VT_COLLECTIVE_REDUCE_ALLREDUCE_ALLREDUCE_HOLDER_H*/ diff --git a/src/vt/collective/reduce/allreduce/allreduce_holder.impl.h b/src/vt/collective/reduce/allreduce/allreduce_holder.impl.h new file mode 100644 index 0000000000..e9845c8307 --- /dev/null +++ b/src/vt/collective/reduce/allreduce/allreduce_holder.impl.h @@ -0,0 +1,142 @@ +/* +//@HEADER +// ***************************************************************************** +// +// allreduce_holder.impl.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_COLLECTIVE_REDUCE_ALLREDUCE_ALLREDUCE_HOLDER_IMPL_H +#define INCLUDED_VT_COLLECTIVE_REDUCE_ALLREDUCE_ALLREDUCE_HOLDER_IMPL_H + +#include "vt/collective/reduce/allreduce/allreduce_holder.h" + +#include + +namespace vt::collective::reduce::allreduce { + +template +decltype(auto) AllreduceHolder::getAllreducerImpl(MapT& map, uint64_t id) { + auto it = map.find(id); + if (it == map.end()) { + map[id] = {nullptr, nullptr}; + } + + if constexpr (std::is_same_v) { + return map.at(id).first; + } else { + return map.at(id).second; + } +} + +template +decltype(auto) +AllreduceHolder::getAllreducer(detail::StrongVrtProxy strong_proxy) { + auto const coll_proxy = strong_proxy.get(); + + return getAllreducerImpl(col_reducers_, coll_proxy); +} + +template +decltype(auto) AllreduceHolder::getAllreducer(detail::StrongGroup strong_group) { + auto const group = strong_group.get(); + + return getAllreducerImpl(group_reducers_, group); +} + +template +decltype(auto) +AllreduceHolder::getAllreducer(detail::StrongObjGroup strong_objgroup) { + auto const objgroup = strong_objgroup.get(); + + return getAllreducerImpl(objgroup_reducers_, objgroup); +} + +template +decltype(auto) AllreduceHolder::getOrCreateAllreducerImpl( + MapT& map, uint64_t id, Args&&... args) { + if (map.find(id) == col_reducers_.end()) { + map[id] = {nullptr, nullptr}; + } + + if constexpr (std::is_same_v) { + auto reducer = map.at(id).first; + if (reducer == nullptr) { + return addRabensifnerAllreducer(std::forward(args)...); + } else { + return reducer; + } + } else { + auto reducer = map.at(id).second; + if (reducer == nullptr) { + return addRecursiveDoublingAllreducer(std::forward(args)...); + } else { + return reducer; + } + } +} + +template +decltype(auto) AllreduceHolder::getOrCreateAllreducer( + detail::StrongVrtProxy strong_proxy, detail::StrongGroup strong_group, + size_t num_elems) { + auto const coll_proxy = strong_proxy.get(); + + return getOrCreateAllreducerImpl( + col_reducers_, coll_proxy, strong_proxy, strong_group, num_elems); +} + +template +decltype(auto) +AllreduceHolder::getOrCreateAllreducer(detail::StrongGroup strong_group) { + auto const group = strong_group.get(); + return getOrCreateAllreducerImpl( + col_reducers_, group, strong_group); +} + +template +decltype(auto) +AllreduceHolder::getOrCreateAllreducer(detail::StrongObjGroup strong_objgroup) { + auto const objgroup = strong_objgroup.get(); + return getOrCreateAllreducerImpl( + objgroup_reducers_, objgroup, strong_objgroup); +} + +} // namespace vt::collective::reduce::allreduce + +#endif /*INCLUDED_VT_COLLECTIVE_REDUCE_ALLREDUCE_ALLREDUCE_HOLDER_IMPL_H*/ diff --git a/src/vt/collective/reduce/allreduce/data_handler.h b/src/vt/collective/reduce/allreduce/data_handler.h new file mode 100644 index 0000000000..d6ede93221 --- /dev/null +++ b/src/vt/collective/reduce/allreduce/data_handler.h @@ -0,0 +1,140 @@ +/* +//@HEADER +// ***************************************************************************** +// +// data_handler.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_COLLECTIVE_REDUCE_ALLREDUCE_DATA_HANDLER_H +#define INCLUDED_VT_COLLECTIVE_REDUCE_ALLREDUCE_DATA_HANDLER_H + +#include "vt/configs/error/config_assert.h" + +#include + +#ifdef MAGISTRATE_KOKKOS_ENABLED +#include +#endif // MAGISTRATE_KOKKOS_ENABLED + +namespace vt::collective::reduce::allreduce { + +template +class DataHandler { +public: + using Scalar = void; + + static DataType toVec(const DataType&) { + vtAssert( + true, + "Using default DataHandler! This means that you're using custom type for " + "allreduce. Please provide specialization for you data type." + ); + + return {}; + } + static DataType fromVec(const std::vector&) { + vtAssert( + true, + "Using default DataHandler! This means that you're using custom type for " + "allreduce. Please provide specialization for you data type." + ); + + return {}; + } + + static size_t size(void) { + vtAssert( + true, + "Using default DataHandler! This means that you're using custom type for " + "allreduce. Please provide specialization for you data type." + ); + + return {}; + } +}; + +template +class DataHandler::value>::type> { +public: + using Scalar = ScalarType; + + static std::vector toVec(const ScalarType& data) { return std::vector{data}; } + static ScalarType fromVec(const std::vector& data) { return data[0]; } + + static size_t size(const ScalarType&) { return 1; } +}; + +template +class DataHandler> { +public: + using Scalar = T; + + static const std::vector& toVec(const std::vector& data) { return data; } + static std::vector fromVec(const std::vector& data) { return data; } + static size_t size(const std::vector& data) { return data.size(); } +}; + +#if MAGISTRATE_KOKKOS_ENABLED + +template +class DataHandler> { + using ViewType = Kokkos::View; + +public: + using Scalar = T; + + static std::vector toVec(const ViewType& data) { + return std::vector(data.data(), data.data() + data.extent(0)); + } + + static ViewType fromVec(const std::vector& data) { + ViewType view("view", data.size()); + auto data_view = Kokkos::View(data.data(), data.size()); + Kokkos::deep_copy(view, data_view); + return view; + } + + static size_t size(const ViewType& data) { return data.extent(0); } +}; + +#endif // MAGISTRATE_KOKKOS_ENABLED + +} // namespace vt::collective::reduce::allreduce + +#endif /*INCLUDED_VT_COLLECTIVE_REDUCE_ALLREDUCE_DATA_HANDLER_H*/ diff --git a/src/vt/collective/reduce/allreduce/helpers.h b/src/vt/collective/reduce/allreduce/helpers.h new file mode 100644 index 0000000000..d220b24836 --- /dev/null +++ b/src/vt/collective/reduce/allreduce/helpers.h @@ -0,0 +1,195 @@ +/* +//@HEADER +// ***************************************************************************** +// +// helpers.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_COLLECTIVE_REDUCE_ALLREDUCE_HELPERS_H +#define INCLUDED_VT_COLLECTIVE_REDUCE_ALLREDUCE_HELPERS_H + +#include "data_handler.h" +#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 + +namespace vt::collective::reduce::allreduce { + +template +struct ShouldUseView { + static constexpr bool Value = false; +}; + +#if MAGISTRATE_KOKKOS_ENABLED +template +struct ShouldUseView> { + static constexpr bool Value = true; +}; + +#endif // MAGISTRATE_KOKKOS_ENABLED + +template +inline constexpr bool ShouldUseView_v = ShouldUseView::Value; + +template +struct DataHelper { + using DataHan = DataHandler; + + template + static void assignFromMem(std::vector& dest, const Scalar* data, size_t size) { + vtAssert( + dest.size() == size, + fmt::format( + "DataHelper::assignFromMem vector(size={}) and memory(size={}) differ!", + dest.size(), size + ) + ); + + std::memcpy(dest.data(), data, size * sizeof(Scalar)); + } + + template + static void assign(std::vector& dest, Args&&... data) { + dest = DataHan::toVec(std::forward(data)...); + } + + static auto createMessage( + ComponentInfo info, + const std::vector& payload, size_t begin, size_t count, size_t id, + int32_t step = 0) { + return vt::makeMessage>( + info, payload.data() + begin, count, id, step); + } + + static void copy( + std::vector& dest, size_t start_idx, RabenseifnerMsg* msg) { + for (uint32_t i = 0; i < msg->size_; i++) { + dest[start_idx + i] = msg->val_[i]; + } + } + + template