Skip to content

Commit

Permalink
#276: reduce: add synthesized reduce
Browse files Browse the repository at this point in the history
  • Loading branch information
lifflander committed Apr 18, 2023
1 parent d4f4501 commit 78f8708
Show file tree
Hide file tree
Showing 15 changed files with 244 additions and 4 deletions.
1 change: 1 addition & 0 deletions examples/hello_world/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ set(
hello_world_virtual_context_remote
ring
objgroup
hello_reduce
)

foreach(EXAMPLE_NAME ${HELLO_WORLD_EXAMPLES})
Expand Down
62 changes: 62 additions & 0 deletions examples/hello_world/hello_reduce.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
//@HEADER
// *****************************************************************************
//
// hello_reduce.cc
// 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 [email protected]
//
// *****************************************************************************
//@HEADER
*/

#include <vt/transport.h>

void reduceResult(int result, double result2) {
auto num_nodes = vt::theContext()->getNumNodes();
fmt::print("reduction value={}, {}\n", result, result2);
vtAssert(num_nodes * 50 == result, "Must be equal");
}

int main(int argc, char** argv) {
vt::initialize(argc, argv);

vt::NodeType const root = 0;

auto r = vt::theCollective()->global();
r->reduce<vt::collective::PlusOp, reduceResult>(vt::Node{root}, 50, 52.334);

vt::finalize();
return 0;
}
1 change: 1 addition & 0 deletions src/vt/collective/reduce/operators/default_msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ struct ReduceDataMsg : SerializeIfNeeded<
: MessageParentType(), ReduceCombine<void>(), val_(in_val)
{ }

DataType& getTuple() { return val_; }
DataType const& getConstVal() const { return val_; }
DataType& getVal() { return val_; }
DataType&& getMoveVal() { return std::move(val_); }
Expand Down
8 changes: 7 additions & 1 deletion src/vt/collective/reduce/operators/default_op.impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@

namespace vt { namespace collective { namespace reduce { namespace operators {

struct NoCombine {};

template <typename T>
template <typename MsgT, typename Op, typename ActOp>
/*static*/ void ReduceCombine<T>::msgHandler(MsgT* msg) {
Expand All @@ -61,8 +63,12 @@ template <typename MsgT, typename Op, typename ActOp>
if (cb.valid()) {
envelopeUnlockForForwarding(msg->env);
cb.template send<MsgT>(msg);
} else if (msg->root_handler_ != uninitialized_handler) {
auto_registry::getAutoHandler(msg->root_handler_)->dispatch(msg, nullptr);
} else {
ActOp()(msg);
if constexpr (not std::is_same_v<ActOp, NoCombine>) {
ActOp()(msg);
}
}
} else {
MsgT* fst_msg = msg;
Expand Down
8 changes: 8 additions & 0 deletions src/vt/collective/reduce/operators/functors/and_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#define INCLUDED_VT_COLLECTIVE_REDUCE_OPERATORS_FUNCTORS_AND_OP_H

#include "vt/config.h"
#include "vt/collective/reduce/operators/functors/tuple_op_helper.h"

namespace vt { namespace collective { namespace reduce { namespace operators {

Expand All @@ -55,6 +56,13 @@ struct AndOp {
}
};

template <typename... Params>
struct AndOp<std::tuple<Params...>> {
void operator()(std::tuple<Params...>& v1, std::tuple<Params...> const& v2) {
opTuple<AndOp>(v1, v2);
}
};

template <typename T>
struct AndOp< std::vector<T> > {
void operator()(std::vector<T>& v1, std::vector<T> const& v2) {
Expand Down
8 changes: 8 additions & 0 deletions src/vt/collective/reduce/operators/functors/bit_and_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#define INCLUDED_VT_COLLECTIVE_REDUCE_OPERATORS_FUNCTORS_BIT_AND_OP_H

#include "vt/config.h"
#include "vt/collective/reduce/operators/functors/tuple_op_helper.h"

namespace vt { namespace collective { namespace reduce { namespace operators {

Expand All @@ -55,6 +56,13 @@ struct BitAndOp {
}
};

template <typename... Params>
struct BitAndOp<std::tuple<Params...>> {
void operator()(std::tuple<Params...>& v1, std::tuple<Params...> const& v2) {
opTuple<BitAndOp>(v1, v2);
}
};

}}}} /* end namespace vt::collective::reduce::operators */

namespace vt { namespace collective {
Expand Down
8 changes: 8 additions & 0 deletions src/vt/collective/reduce/operators/functors/bit_or_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#define INCLUDED_VT_COLLECTIVE_REDUCE_OPERATORS_FUNCTORS_BIT_OR_OP_H

#include "vt/config.h"
#include "vt/collective/reduce/operators/functors/tuple_op_helper.h"

namespace vt { namespace collective { namespace reduce { namespace operators {

Expand All @@ -55,6 +56,13 @@ struct BitOrOp {
}
};

template <typename... Params>
struct BitOrOp<std::tuple<Params...>> {
void operator()(std::tuple<Params...>& v1, std::tuple<Params...> const& v2) {
opTuple<BitOrOp>(v1, v2);
}
};

}}}} /* end namespace vt::collective::reduce::operators */

namespace vt { namespace collective {
Expand Down
8 changes: 8 additions & 0 deletions src/vt/collective/reduce/operators/functors/bit_xor_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#define INCLUDED_VT_COLLECTIVE_REDUCE_OPERATORS_FUNCTORS_BIT_XOR_OP_H

#include "vt/config.h"
#include "vt/collective/reduce/operators/functors/tuple_op_helper.h"

namespace vt { namespace collective { namespace reduce { namespace operators {

Expand All @@ -55,6 +56,13 @@ struct BitXorOp {
}
};

template <typename... Params>
struct BitXorOp<std::tuple<Params...>> {
void operator()(std::tuple<Params...>& v1, std::tuple<Params...> const& v2) {
opTuple<BitXorOp>(v1, v2);
}
};

}}}} /* end namespace vt::collective::reduce::operators */

namespace vt { namespace collective {
Expand Down
8 changes: 8 additions & 0 deletions src/vt/collective/reduce/operators/functors/max_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#define INCLUDED_VT_COLLECTIVE_REDUCE_OPERATORS_FUNCTORS_MAX_OP_H

#include "vt/config.h"
#include "vt/collective/reduce/operators/functors/tuple_op_helper.h"

#include <algorithm>

Expand All @@ -57,6 +58,13 @@ struct MaxOp {
}
};

template <typename... Params>
struct MaxOp<std::tuple<Params...>> {
void operator()(std::tuple<Params...>& v1, std::tuple<Params...> const& v2) {
opTuple<MaxOp>(v1, v2);
}
};

template <typename T>
struct MaxOp< std::vector<T> > {
void operator()(std::vector<T>& v1, std::vector<T> const& v2) {
Expand Down
8 changes: 8 additions & 0 deletions src/vt/collective/reduce/operators/functors/min_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#define INCLUDED_VT_COLLECTIVE_REDUCE_OPERATORS_FUNCTORS_MIN_OP_H

#include "vt/config.h"
#include "vt/collective/reduce/operators/functors/tuple_op_helper.h"

#include <algorithm>

Expand All @@ -57,6 +58,13 @@ struct MinOp {
}
};

template <typename... Params>
struct MinOp<std::tuple<Params...>> {
void operator()(std::tuple<Params...>& v1, std::tuple<Params...> const& v2) {
opTuple<MinOp>(v1, v2);
}
};

template <typename T>
struct MinOp< std::vector<T> > {
void operator()(std::vector<T>& v1, std::vector<T> const& v2) {
Expand Down
8 changes: 8 additions & 0 deletions src/vt/collective/reduce/operators/functors/or_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#define INCLUDED_VT_COLLECTIVE_REDUCE_OPERATORS_FUNCTORS_OR_OP_H

#include "vt/config.h"
#include "vt/collective/reduce/operators/functors/tuple_op_helper.h"

namespace vt { namespace collective { namespace reduce { namespace operators {

Expand All @@ -55,6 +56,13 @@ struct OrOp {
}
};

template <typename... Params>
struct OrOp<std::tuple<Params...>> {
void operator()(std::tuple<Params...>& v1, std::tuple<Params...> const& v2) {
opTuple<OrOp>(v1, v2);
}
};

template <typename T>
struct OrOp<std::vector<T>> {
void operator()(std::vector<T>& v1, std::vector<T> const& v2) {
Expand Down
8 changes: 8 additions & 0 deletions src/vt/collective/reduce/operators/functors/plus_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#define INCLUDED_VT_COLLECTIVE_REDUCE_OPERATORS_FUNCTORS_PLUS_OP_H

#include "vt/config.h"
#include "vt/collective/reduce/operators/functors/tuple_op_helper.h"

namespace vt { namespace collective { namespace reduce { namespace operators {

Expand All @@ -55,6 +56,13 @@ struct PlusOp {
}
};

template <typename... Params>
struct PlusOp<std::tuple<Params...>> {
void operator()(std::tuple<Params...>& v1, std::tuple<Params...> const& v2) {
opTuple<PlusOp>(v1, v2);
}
};

template <typename T>
struct PlusOp< std::vector<T> > {
void operator()(std::vector<T>& v1, std::vector<T> const& v2) {
Expand Down
70 changes: 70 additions & 0 deletions src/vt/collective/reduce/operators/functors/tuple_op_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
//@HEADER
// *****************************************************************************
//
// tuple_op_helper.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 [email protected]
//
// *****************************************************************************
//@HEADER
*/

#if !defined INCLUDED_VT_COLLECTIVE_REDUCE_OPERATORS_FUNCTORS_TUPLE_OP_HELPER_H
#define INCLUDED_VT_COLLECTIVE_REDUCE_OPERATORS_FUNCTORS_TUPLE_OP_HELPER_H

#include <tuple>

namespace vt { namespace collective { namespace reduce { namespace operators {

template <
template <typename X> class Op,
typename... Ts, typename... Us, std::size_t... I
>
void opTuple(
std::tuple<Ts...>& t1, std::tuple<Us...> const& t2, std::index_sequence<I...>
) {
std::forward_as_tuple(
(Op<std::decay_t<decltype(std::get<I>(t1))>>()(std::get<I>(t1),std::get<I>(t2)),0)...
);
}

template <template <typename X> class Op, typename... Ts, typename... Us>
void opTuple(std::tuple<Ts...>& t1, std::tuple<Us...> const& t2) {
opTuple<Op>(t1, t2, std::make_index_sequence<sizeof...(Ts)>{});
}

}}}} /* end namespace vt::collective::reduce::operators */

#endif /*INCLUDED_VT_COLLECTIVE_REDUCE_OPERATORS_FUNCTORS_TUPLE_OP_HELPER_H*/
Loading

0 comments on commit 78f8708

Please sign in to comment.