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..8afcb69e3d --- /dev/null +++ b/src/vt/collective/reduce/allreduce/data_handler.h @@ -0,0 +1,126 @@ +/* +//@HEADER +// ***************************************************************************** +// +// data_handler.h +// DARMA/vt => Virtual Transport +// +// Copyright 2019-2021 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 + +#ifdef MAGISTRATE_KOKKOS_ENABLED +#include +#endif // MAGISTRATE_KOKKOS_ENABLED + +namespace vt::collective::reduce::allreduce { + +template +class DataHandler { +public: + using Scalar = void; + static size_t size(void) { return 0; } +}; + +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 ScalarType fromMemory(const ScalarType* data, size_t) { + return *data; + } + + 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 std::vector fromMemory(const T* data, size_t count) { + return std::vector(data, data + count); + } + + 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) { + std::vector vec; + vec.resize(data.extent(0)); + std::memcpy(vec.data(), data.data(), data.extent(0) * sizeof(T)); + return vec; + } + + static ViewType fromMemory(T* data, size_t size) { + return ViewType(data, size); + } + + static ViewType fromVec(const std::vector& data) { + ViewType view("", data.size()); + Kokkos::parallel_for( + "InitView", view.extent(0), + KOKKOS_LAMBDA(const int i) { view(i) = static_cast(data[i]); }); + + 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..1083e32823 --- /dev/null +++ b/src/vt/collective/reduce/allreduce/helpers.h @@ -0,0 +1,203 @@ +/* +//@HEADER +// ***************************************************************************** +// +// helpers.h +// DARMA/vt => Virtual Transport +// +// Copyright 2019-2021 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 +#include + +namespace vt { +template +using remove_cvref = std::remove_cv_t>; +} + +namespace vt::collective::reduce::allreduce { + +template +struct DataHelper { + using DataType = DataHandler; + + template + static void assign(std::vector& dest, Args&&... data) { + dest = DataHandler::toVec(std::forward(data)...); + } + + static MsgPtr> createMessage( + const std::vector& payload, size_t begin, size_t count, size_t id, + int32_t step = 0) { + return vt::makeMessage>( + 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