Skip to content

Commit 78f8708

Browse files
committed
#276: reduce: add synthesized reduce
1 parent d4f4501 commit 78f8708

File tree

15 files changed

+244
-4
lines changed

15 files changed

+244
-4
lines changed

examples/hello_world/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ set(
1111
hello_world_virtual_context_remote
1212
ring
1313
objgroup
14+
hello_reduce
1415
)
1516

1617
foreach(EXAMPLE_NAME ${HELLO_WORLD_EXAMPLES})
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
//@HEADER
3+
// *****************************************************************************
4+
//
5+
// hello_reduce.cc
6+
// DARMA/vt => Virtual Transport
7+
//
8+
// Copyright 2019-2021 National Technology & Engineering Solutions of Sandia, LLC
9+
// (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
10+
// Government retains certain rights in this software.
11+
//
12+
// Redistribution and use in source and binary forms, with or without
13+
// modification, are permitted provided that the following conditions are met:
14+
//
15+
// * Redistributions of source code must retain the above copyright notice,
16+
// this list of conditions and the following disclaimer.
17+
//
18+
// * Redistributions in binary form must reproduce the above copyright notice,
19+
// this list of conditions and the following disclaimer in the documentation
20+
// and/or other materials provided with the distribution.
21+
//
22+
// * Neither the name of the copyright holder nor the names of its
23+
// contributors may be used to endorse or promote products derived from this
24+
// software without specific prior written permission.
25+
//
26+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29+
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30+
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31+
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32+
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34+
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35+
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36+
// POSSIBILITY OF SUCH DAMAGE.
37+
//
38+
// Questions? Contact darma@sandia.gov
39+
//
40+
// *****************************************************************************
41+
//@HEADER
42+
*/
43+
44+
#include <vt/transport.h>
45+
46+
void reduceResult(int result, double result2) {
47+
auto num_nodes = vt::theContext()->getNumNodes();
48+
fmt::print("reduction value={}, {}\n", result, result2);
49+
vtAssert(num_nodes * 50 == result, "Must be equal");
50+
}
51+
52+
int main(int argc, char** argv) {
53+
vt::initialize(argc, argv);
54+
55+
vt::NodeType const root = 0;
56+
57+
auto r = vt::theCollective()->global();
58+
r->reduce<vt::collective::PlusOp, reduceResult>(vt::Node{root}, 50, 52.334);
59+
60+
vt::finalize();
61+
return 0;
62+
}

src/vt/collective/reduce/operators/default_msg.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ struct ReduceDataMsg : SerializeIfNeeded<
8080
: MessageParentType(), ReduceCombine<void>(), val_(in_val)
8181
{ }
8282

83+
DataType& getTuple() { return val_; }
8384
DataType const& getConstVal() const { return val_; }
8485
DataType& getVal() { return val_; }
8586
DataType&& getMoveVal() { return std::move(val_); }

src/vt/collective/reduce/operators/default_op.impl.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949

5050
namespace vt { namespace collective { namespace reduce { namespace operators {
5151

52+
struct NoCombine {};
53+
5254
template <typename T>
5355
template <typename MsgT, typename Op, typename ActOp>
5456
/*static*/ void ReduceCombine<T>::msgHandler(MsgT* msg) {
@@ -61,8 +63,12 @@ template <typename MsgT, typename Op, typename ActOp>
6163
if (cb.valid()) {
6264
envelopeUnlockForForwarding(msg->env);
6365
cb.template send<MsgT>(msg);
66+
} else if (msg->root_handler_ != uninitialized_handler) {
67+
auto_registry::getAutoHandler(msg->root_handler_)->dispatch(msg, nullptr);
6468
} else {
65-
ActOp()(msg);
69+
if constexpr (not std::is_same_v<ActOp, NoCombine>) {
70+
ActOp()(msg);
71+
}
6672
}
6773
} else {
6874
MsgT* fst_msg = msg;

src/vt/collective/reduce/operators/functors/and_op.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#define INCLUDED_VT_COLLECTIVE_REDUCE_OPERATORS_FUNCTORS_AND_OP_H
4646

4747
#include "vt/config.h"
48+
#include "vt/collective/reduce/operators/functors/tuple_op_helper.h"
4849

4950
namespace vt { namespace collective { namespace reduce { namespace operators {
5051

@@ -55,6 +56,13 @@ struct AndOp {
5556
}
5657
};
5758

59+
template <typename... Params>
60+
struct AndOp<std::tuple<Params...>> {
61+
void operator()(std::tuple<Params...>& v1, std::tuple<Params...> const& v2) {
62+
opTuple<AndOp>(v1, v2);
63+
}
64+
};
65+
5866
template <typename T>
5967
struct AndOp< std::vector<T> > {
6068
void operator()(std::vector<T>& v1, std::vector<T> const& v2) {

src/vt/collective/reduce/operators/functors/bit_and_op.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#define INCLUDED_VT_COLLECTIVE_REDUCE_OPERATORS_FUNCTORS_BIT_AND_OP_H
4646

4747
#include "vt/config.h"
48+
#include "vt/collective/reduce/operators/functors/tuple_op_helper.h"
4849

4950
namespace vt { namespace collective { namespace reduce { namespace operators {
5051

@@ -55,6 +56,13 @@ struct BitAndOp {
5556
}
5657
};
5758

59+
template <typename... Params>
60+
struct BitAndOp<std::tuple<Params...>> {
61+
void operator()(std::tuple<Params...>& v1, std::tuple<Params...> const& v2) {
62+
opTuple<BitAndOp>(v1, v2);
63+
}
64+
};
65+
5866
}}}} /* end namespace vt::collective::reduce::operators */
5967

6068
namespace vt { namespace collective {

src/vt/collective/reduce/operators/functors/bit_or_op.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#define INCLUDED_VT_COLLECTIVE_REDUCE_OPERATORS_FUNCTORS_BIT_OR_OP_H
4646

4747
#include "vt/config.h"
48+
#include "vt/collective/reduce/operators/functors/tuple_op_helper.h"
4849

4950
namespace vt { namespace collective { namespace reduce { namespace operators {
5051

@@ -55,6 +56,13 @@ struct BitOrOp {
5556
}
5657
};
5758

59+
template <typename... Params>
60+
struct BitOrOp<std::tuple<Params...>> {
61+
void operator()(std::tuple<Params...>& v1, std::tuple<Params...> const& v2) {
62+
opTuple<BitOrOp>(v1, v2);
63+
}
64+
};
65+
5866
}}}} /* end namespace vt::collective::reduce::operators */
5967

6068
namespace vt { namespace collective {

src/vt/collective/reduce/operators/functors/bit_xor_op.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#define INCLUDED_VT_COLLECTIVE_REDUCE_OPERATORS_FUNCTORS_BIT_XOR_OP_H
4646

4747
#include "vt/config.h"
48+
#include "vt/collective/reduce/operators/functors/tuple_op_helper.h"
4849

4950
namespace vt { namespace collective { namespace reduce { namespace operators {
5051

@@ -55,6 +56,13 @@ struct BitXorOp {
5556
}
5657
};
5758

59+
template <typename... Params>
60+
struct BitXorOp<std::tuple<Params...>> {
61+
void operator()(std::tuple<Params...>& v1, std::tuple<Params...> const& v2) {
62+
opTuple<BitXorOp>(v1, v2);
63+
}
64+
};
65+
5866
}}}} /* end namespace vt::collective::reduce::operators */
5967

6068
namespace vt { namespace collective {

src/vt/collective/reduce/operators/functors/max_op.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#define INCLUDED_VT_COLLECTIVE_REDUCE_OPERATORS_FUNCTORS_MAX_OP_H
4646

4747
#include "vt/config.h"
48+
#include "vt/collective/reduce/operators/functors/tuple_op_helper.h"
4849

4950
#include <algorithm>
5051

@@ -57,6 +58,13 @@ struct MaxOp {
5758
}
5859
};
5960

61+
template <typename... Params>
62+
struct MaxOp<std::tuple<Params...>> {
63+
void operator()(std::tuple<Params...>& v1, std::tuple<Params...> const& v2) {
64+
opTuple<MaxOp>(v1, v2);
65+
}
66+
};
67+
6068
template <typename T>
6169
struct MaxOp< std::vector<T> > {
6270
void operator()(std::vector<T>& v1, std::vector<T> const& v2) {

src/vt/collective/reduce/operators/functors/min_op.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#define INCLUDED_VT_COLLECTIVE_REDUCE_OPERATORS_FUNCTORS_MIN_OP_H
4646

4747
#include "vt/config.h"
48+
#include "vt/collective/reduce/operators/functors/tuple_op_helper.h"
4849

4950
#include <algorithm>
5051

@@ -57,6 +58,13 @@ struct MinOp {
5758
}
5859
};
5960

61+
template <typename... Params>
62+
struct MinOp<std::tuple<Params...>> {
63+
void operator()(std::tuple<Params...>& v1, std::tuple<Params...> const& v2) {
64+
opTuple<MinOp>(v1, v2);
65+
}
66+
};
67+
6068
template <typename T>
6169
struct MinOp< std::vector<T> > {
6270
void operator()(std::vector<T>& v1, std::vector<T> const& v2) {

0 commit comments

Comments
 (0)