Skip to content

Commit 8d3c5ad

Browse files
committed
#276: active: implement parameterization
1 parent 3020c27 commit 8d3c5ad

File tree

6 files changed

+114
-10
lines changed

6 files changed

+114
-10
lines changed

examples/hello_world/hello_world.cc

+3-6
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,21 @@ struct HelloMsg : vt::Message {
4949
vt::NodeType from = 0;
5050
};
5151

52-
static void hello_world(HelloMsg* msg) {
52+
void helloWorld(int a, int b, float c) {
5353
vt::NodeType this_node = vt::theContext()->getNode();
54-
fmt::print("{}: Hello from node {}\n", this_node, msg->from);
54+
fmt::print("{}: Hello from node vals = {} {} {}\n", this_node, a, b, c);
5555
}
5656

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

60-
vt::NodeType this_node = vt::theContext()->getNode();
6160
vt::NodeType num_nodes = vt::theContext()->getNumNodes();
62-
6361
if (num_nodes == 1) {
6462
return vt::rerror("requires at least 2 nodes");
6563
}
6664

6765
if (this_node == 0) {
68-
auto msg = vt::makeMessage<HelloMsg>(this_node);
69-
vt::theMsg()->broadcastMsg<hello_world>(msg);
66+
vt::theMsg()->send<helloWorld>(1, 10, 20, 11.3f);
7067
}
7168

7269
vt::finalize();

src/vt/messaging/active.h

+18
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#include "vt/messaging/request_holder.h"
5757
#include "vt/messaging/send_info.h"
5858
#include "vt/messaging/async_op_wrapper.h"
59+
#include "vt/messaging/param_msg.h"
5960
#include "vt/event/event.h"
6061
#include "vt/registry/auto/auto_registry_interface.h"
6162
#include "vt/trace/trace_common.h"
@@ -756,6 +757,23 @@ struct ActiveMessenger : runtime::component::PollableComponent<ActiveMessenger>
756757
return sendMsg<MsgT, f>(dest, msg, tag);
757758
}
758759

760+
/**
761+
* \brief Send parameters to a handler in a message
762+
*
763+
* \param[in] dest the destination node to send the message to
764+
* \param[in] params the parameters
765+
*
766+
* \return the \c PendingSend for the sent message
767+
*/
768+
template <auto f, typename... Params>
769+
PendingSendType send(NodeType dest, Params&&... params) {
770+
using Tuple = typename std::decay<std::tuple<Params...>>::type;
771+
using MsgT = ParamMsg<Tuple>;
772+
auto msg = vt::makeMessage<MsgT>(std::forward<Params>(params)...);
773+
auto han = auto_registry::makeAutoHandlerParam<decltype(f),f,Params...>();
774+
return sendMsg<MsgT>(dest, han, msg, no_tag);
775+
}
776+
759777
/**
760778
* \brief Send a message with explicit size.
761779
*

src/vt/messaging/param_msg.h

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
//@HEADER
3+
// *****************************************************************************
4+
//
5+
// param_msg.h
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+
#if !defined INCLUDED_VT_MESSAGING_PARAM_MSG_H
45+
#define INCLUDED_VT_MESSAGING_PARAM_MSG_H
46+
47+
#include "vt/messaging/message/message_serialize.h"
48+
49+
namespace vt { namespace messaging {
50+
51+
template <typename Tuple>
52+
struct ParamMsg : vt::Message {
53+
using MessageParentType = vt::Message;
54+
vt_msg_serialize_if_needed_by_parent_or_type1(Tuple); // by tup
55+
56+
template <typename... Params>
57+
explicit ParamMsg(Params&&... in_params)
58+
: params(std::forward<Params>(in_params)...)
59+
{ }
60+
61+
Tuple params;
62+
63+
template <typename SerializerT>
64+
void serialize(SerializerT& s) {
65+
MessageParentType::serialize(s);
66+
s | params;
67+
}
68+
};
69+
70+
}} /* end namespace vt::messaging */
71+
72+
#endif /*INCLUDED_VT_MESSAGING_PARAM_MSG_H*/

src/vt/registry/auto/auto_registry_common.h

+15
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,21 @@ struct HandlersDispatcher final : BaseHandlersDispatcher {
133133
}
134134
};
135135

136+
template <typename T>
137+
struct DispatchImpl<
138+
T,
139+
std::enable_if_t<
140+
std::is_same<ObjT, SentinelObject>::value and
141+
not std::is_same<T, ActiveVoidFnType*>::value and
142+
not std::is_same<T, ActiveTypedFnType<MsgT>*>::value
143+
>
144+
> {
145+
static void run(MsgT* msg, void*, HandlerT han) {
146+
std::apply(han, msg->params);
147+
}
148+
};
149+
150+
136151
public:
137152
void dispatch(messaging::BaseMsg* msg, void* object) const override {
138153
DispatchImpl<HandlerT>::run(static_cast<MsgT*>(msg), object, fn_ptr_);

src/vt/registry/auto/auto_registry_general.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
#include "vt/config.h"
4747
#include "vt/registry/auto/auto_registry_common.h"
48+
#include "vt/messaging/param_msg.h"
4849

4950
#include "vt/utils/demangle/demangle.h"
5051

@@ -154,10 +155,11 @@ struct FunctorAdapterArgs<ObjTypeT, MsgT> {
154155

155156
// Need to provide a non-pointer overload for parameterization auto-registered
156157
// functions for GCC
157-
template <typename F, F f>
158+
template <typename F, F f, typename... Params>
158159
struct FunctorAdapterParam {
159160
using FunctionPtrType = F;
160-
using ObjType = void;
161+
using ObjType = SentinelObject;
162+
using MsgType = messaging::ParamMsg<std::tuple<Params...>>;
161163

162164
static constexpr FunctionPtrType getFunction() { return f; }
163165

src/vt/registry/auto/auto_registry_impl.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ inline BaseScatterDispatcherPtr const& getScatterAutoHandler(HandlerType const h
144144
return getAutoRegistryGen<ScatterContainerType>().at(han_id).getFun();
145145
}
146146

147-
template <typename T, T value>
147+
template <typename T, T value, typename... Params>
148148
inline HandlerType makeAutoHandlerParam() {
149-
using AdapterT = FunctorAdapterParam<T, value>;
149+
using AdapterT = FunctorAdapterParam<T, value, Params...>;
150150
using ContainerType = AutoActiveContainerType;
151151
using RegInfoType = AutoRegInfoType<AutoActiveType>;
152152
using FuncType = ActiveFnPtrType;

0 commit comments

Comments
 (0)