From f0c7ac4c30a69f8f4d3a6e49d9ce5f58fc44dd07 Mon Sep 17 00:00:00 2001 From: Jakub Domagala Date: Tue, 16 Mar 2021 13:52:13 +0100 Subject: [PATCH 1/9] #1260: examples: Use implicit message type deduction for sending message with a functor handler in tutorial code --- tutorial/tutorial_1b.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorial/tutorial_1b.h b/tutorial/tutorial_1b.h index b84ec57458..0596b4f8c3 100644 --- a/tutorial/tutorial_1b.h +++ b/tutorial/tutorial_1b.h @@ -154,7 +154,7 @@ static void msgHandlerA(MyMsg* msg) { NodeType const to_node = 0; auto msg2 = ::vt::makeMessage(10,20); - ::vt::theMsg()->sendMsg(to_node, msg2); + ::vt::theMsg()->sendMsg(to_node, msg2); // Alternate/equivalent form with explicit (and optional) std::move: // From caf0558298ba9f7e4b1b5d948a564859ac43bf2e Mon Sep 17 00:00:00 2001 From: Jakub Domagala Date: Mon, 26 Apr 2021 18:53:02 +0200 Subject: [PATCH 2/9] #1260: utils: Allow FunctorTraits to also work with const functions --- src/vt/utils/static_checks/functor.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/vt/utils/static_checks/functor.h b/src/vt/utils/static_checks/functor.h index 1d125243bd..89b424488c 100644 --- a/src/vt/utils/static_checks/functor.h +++ b/src/vt/utils/static_checks/functor.h @@ -46,6 +46,8 @@ #define INCLUDED_UTILS_STATIC_CHECKS_FUNCTOR_H #include "vt/config.h" +#include + namespace vt { namespace util { @@ -59,6 +61,13 @@ struct FunctorTraits { using ReturnType = ReturnT; }; +template +struct FunctorTraits { + using FunctorType = FunctorT; + using MessageType = MsgT; + using ReturnType = ReturnT; +}; + template < typename FunctorT, typename FunctorFnT = decltype(&FunctorT::operator()), From 42b19890a74f5383c3104aa997a4e2972bb38ffa Mon Sep 17 00:00:00 2001 From: Jakub Domagala Date: Mon, 26 Apr 2021 19:30:13 +0200 Subject: [PATCH 3/9] #1260: examples: Use deduced message type for hello_world_functor example --- examples/hello_world/hello_world_functor.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/hello_world/hello_world_functor.cc b/examples/hello_world/hello_world_functor.cc index 0b4dacdbb9..e28a77c0c4 100644 --- a/examples/hello_world/hello_world_functor.cc +++ b/examples/hello_world/hello_world_functor.cc @@ -70,7 +70,7 @@ int main(int argc, char** argv) { if (this_node == 0) { auto msg = vt::makeMessage(this_node); - vt::theMsg()->broadcastMsg(msg); + vt::theMsg()->broadcastMsg(msg); } vt::finalize(); From 5cd36dd79a8590d09ddfcec14447e50accfcde12 Mon Sep 17 00:00:00 2001 From: Jakub Domagala Date: Mon, 26 Apr 2021 19:36:16 +0200 Subject: [PATCH 4/9] #1260: active: Deduce message type (for send/broadcast with functor handler) by using FunctorExtractor directly with MsgPtrThief --- src/vt/messaging/active.h | 8 ++++---- src/vt/messaging/active.impl.h | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/vt/messaging/active.h b/src/vt/messaging/active.h index 9ef00cde2a..a89aab0952 100644 --- a/src/vt/messaging/active.h +++ b/src/vt/messaging/active.h @@ -904,7 +904,7 @@ struct ActiveMessenger : runtime::component::PollableComponent typename MsgT = typename util::FunctorExtractor::MessageType > PendingSendType broadcastMsg( - MsgPtrThief msg, + MsgPtrThief::MessageType> msg, bool deliver_to_sender = true, TagType tag = no_tag ); @@ -926,7 +926,7 @@ struct ActiveMessenger : runtime::component::PollableComponent typename MsgT = typename util::FunctorExtractor::MessageType > PendingSendType broadcastMsgAuto( - MsgPtrThief msg, + MsgPtrThief::MessageType> msg, TagType tag = no_tag ); @@ -947,7 +947,7 @@ struct ActiveMessenger : runtime::component::PollableComponent > PendingSendType sendMsg( NodeType dest, - MsgPtrThief msg, + MsgPtrThief::MessageType> msg, TagType tag = no_tag ); @@ -970,7 +970,7 @@ struct ActiveMessenger : runtime::component::PollableComponent > PendingSendType sendMsgAuto( NodeType dest, - MsgPtrThief msg, + MsgPtrThief::MessageType> msg, TagType tag = no_tag ); diff --git a/src/vt/messaging/active.impl.h b/src/vt/messaging/active.impl.h index 4207e0eb63..66c566df89 100644 --- a/src/vt/messaging/active.impl.h +++ b/src/vt/messaging/active.impl.h @@ -347,7 +347,7 @@ ActiveMessenger::PendingSendType ActiveMessenger::sendMsg( template ActiveMessenger::PendingSendType ActiveMessenger::broadcastMsg( - MsgPtrThief msg, + MsgPtrThief::MessageType> msg, bool deliver_to_sender, TagType tag ) { @@ -362,7 +362,7 @@ ActiveMessenger::PendingSendType ActiveMessenger::broadcastMsg( template ActiveMessenger::PendingSendType ActiveMessenger::sendMsg( NodeType dest, - MsgPtrThief msg, + MsgPtrThief::MessageType> msg, TagType tag ) { auto const han = auto_registry::makeAutoHandlerFunctor(); @@ -372,7 +372,7 @@ ActiveMessenger::PendingSendType ActiveMessenger::sendMsg( template ActiveMessenger::PendingSendType ActiveMessenger::broadcastMsgAuto( - MsgPtrThief msg, + MsgPtrThief::MessageType> msg, TagType tag ) { auto const han = auto_registry::makeAutoHandlerFunctor(); @@ -385,7 +385,7 @@ ActiveMessenger::PendingSendType ActiveMessenger::broadcastMsgAuto( template ActiveMessenger::PendingSendType ActiveMessenger::sendMsgAuto( NodeType dest, - MsgPtrThief msg, + MsgPtrThief::MessageType> msg, TagType tag ) { auto const han = auto_registry::makeAutoHandlerFunctor(); From 9cb4d8176481f97508b774e023d9529d2863b478 Mon Sep 17 00:00:00 2001 From: Jakub Domagala Date: Mon, 26 Apr 2021 20:07:10 +0200 Subject: [PATCH 5/9] #1260: examples: Use deduced message type for transpose example --- examples/collection/transpose.cc | 2 +- src/vt/utils/static_checks/functor.h | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/collection/transpose.cc b/examples/collection/transpose.cc index 71a89272f7..dd813c926d 100644 --- a/examples/collection/transpose.cc +++ b/examples/collection/transpose.cc @@ -176,7 +176,7 @@ struct Block : vt::Collection { if (getIndex().x() == 0) { auto proxy = this->getCollectionProxy(); auto proxy_msg = vt::makeMessage(proxy.getProxy()); - vt::theMsg()->broadcastMsg(proxy_msg); + vt::theMsg()->broadcastMsg(proxy_msg); } } diff --git a/src/vt/utils/static_checks/functor.h b/src/vt/utils/static_checks/functor.h index 89b424488c..e02ffbf620 100644 --- a/src/vt/utils/static_checks/functor.h +++ b/src/vt/utils/static_checks/functor.h @@ -46,7 +46,6 @@ #define INCLUDED_UTILS_STATIC_CHECKS_FUNCTOR_H #include "vt/config.h" -#include namespace vt { namespace util { From 028479937f9b2f7219c13488b1914390c1bd69af Mon Sep 17 00:00:00 2001 From: Jakub Domagala Date: Mon, 3 May 2021 15:33:07 +0200 Subject: [PATCH 6/9] #1260: active: Add separate version of send/broadcast with MsgT infered by FunctorExtractor --- src/vt/messaging/active.h | 47 +++++++++++++++++++++++++++++----- src/vt/messaging/active.impl.h | 28 +++++++++++++++++--- 2 files changed, 64 insertions(+), 11 deletions(-) diff --git a/src/vt/messaging/active.h b/src/vt/messaging/active.h index a89aab0952..81271632d6 100644 --- a/src/vt/messaging/active.h +++ b/src/vt/messaging/active.h @@ -901,8 +901,26 @@ struct ActiveMessenger : runtime::component::PollableComponent */ template < typename FunctorT, - typename MsgT = typename util::FunctorExtractor::MessageType + typename MsgT > + PendingSendType broadcastMsg( + MsgPtrThief msg, + bool deliver_to_sender = true, + TagType tag = no_tag + ); + + /** + * \brief Broadcast a message. + * + * \note Takes ownership of the supplied message. + * + * \param[in] msg the message to broadcast + * \param[in] deliver_to_sender whether msg should be delivered to sender + * \param[in] tag the optional tag to put on the message + * + * \return the \c PendingSend for the broadcast + */ + template PendingSendType broadcastMsg( MsgPtrThief::MessageType> msg, bool deliver_to_sender = true, @@ -926,7 +944,7 @@ struct ActiveMessenger : runtime::component::PollableComponent typename MsgT = typename util::FunctorExtractor::MessageType > PendingSendType broadcastMsgAuto( - MsgPtrThief::MessageType> msg, + MsgPtrThief msg, TagType tag = no_tag ); @@ -941,10 +959,25 @@ struct ActiveMessenger : runtime::component::PollableComponent * * \return the \c PendingSend for the send */ - template < - typename FunctorT, - typename MsgT = typename util::FunctorExtractor::MessageType - > + template + PendingSendType sendMsg( + NodeType dest, + MsgPtrThief msg, + TagType tag = no_tag + ); + + /** + * \brief Send a message with a type-safe handler. + * + * \note Takes ownership of the supplied message. + * + * \param[in] dest the destination node to send the message to + * \param[in] msg the message to broadcast + * \param[in] tag the tag to put on the message + * + * \return the \c PendingSend for the send + */ + template PendingSendType sendMsg( NodeType dest, MsgPtrThief::MessageType> msg, @@ -970,7 +1003,7 @@ struct ActiveMessenger : runtime::component::PollableComponent > PendingSendType sendMsgAuto( NodeType dest, - MsgPtrThief::MessageType> msg, + MsgPtrThief msg, TagType tag = no_tag ); diff --git a/src/vt/messaging/active.impl.h b/src/vt/messaging/active.impl.h index 66c566df89..d4c8da8a78 100644 --- a/src/vt/messaging/active.impl.h +++ b/src/vt/messaging/active.impl.h @@ -347,7 +347,7 @@ ActiveMessenger::PendingSendType ActiveMessenger::sendMsg( template ActiveMessenger::PendingSendType ActiveMessenger::broadcastMsg( - MsgPtrThief::MessageType> msg, + MsgPtrThief msg, bool deliver_to_sender, TagType tag ) { @@ -359,10 +359,20 @@ ActiveMessenger::PendingSendType ActiveMessenger::broadcastMsg( ); } +template +ActiveMessenger::PendingSendType ActiveMessenger::broadcastMsg( + MsgPtrThief::MessageType> msg, + bool deliver_to_sender, + TagType tag +) { + using MsgT = typename util::FunctorExtractor::MessageType; + return broadcastMsg(msg, deliver_to_sender, tag); +} + template ActiveMessenger::PendingSendType ActiveMessenger::sendMsg( NodeType dest, - MsgPtrThief::MessageType> msg, + MsgPtrThief msg, TagType tag ) { auto const han = auto_registry::makeAutoHandlerFunctor(); @@ -370,9 +380,19 @@ ActiveMessenger::PendingSendType ActiveMessenger::sendMsg( return sendMsgImpl(dest, han, msgptr, msgsize_not_specified, tag); } +template +ActiveMessenger::PendingSendType ActiveMessenger::sendMsg( + NodeType dest, + MsgPtrThief::MessageType> msg, + TagType tag +) { + using MsgT = typename util::FunctorExtractor::MessageType; + return sendMsg(dest, msg, tag); +} + template ActiveMessenger::PendingSendType ActiveMessenger::broadcastMsgAuto( - MsgPtrThief::MessageType> msg, + MsgPtrThief msg, TagType tag ) { auto const han = auto_registry::makeAutoHandlerFunctor(); @@ -385,7 +405,7 @@ ActiveMessenger::PendingSendType ActiveMessenger::broadcastMsgAuto( template ActiveMessenger::PendingSendType ActiveMessenger::sendMsgAuto( NodeType dest, - MsgPtrThief::MessageType> msg, + MsgPtrThief msg, TagType tag ) { auto const han = auto_registry::makeAutoHandlerFunctor(); From 560f6dd3502cf1b8539d72104bd03d8e45b2a1c6 Mon Sep 17 00:00:00 2001 From: Jakub Domagala Date: Mon, 3 May 2021 18:04:04 +0200 Subject: [PATCH 7/9] #1260: examples: Add additional examples that cover the changes made to Active send/broadcast with Functor --- examples/hello_world/hello_world_functor.cc | 28 ++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/examples/hello_world/hello_world_functor.cc b/examples/hello_world/hello_world_functor.cc index e28a77c0c4..025ddbe3ae 100644 --- a/examples/hello_world/hello_world_functor.cc +++ b/examples/hello_world/hello_world_functor.cc @@ -52,9 +52,21 @@ struct HelloMsg : vt::Message { { } }; +struct AnotherMsg : vt::Message{}; + struct HelloWorld { void operator()(HelloMsg* msg) const { - fmt::print("{}: Hello from node {}\n", vt::theContext()->getNode(), msg->from); + fmt::print("{}: HelloWorld -> Hello from node {}\n", vt::theContext()->getNode(), msg->from); + } +}; + +struct MultipleFunctions { + void operator()(HelloMsg* msg) const { + fmt::print("{}: MultipleFunctions -> Hello from node {}\n", vt::theContext()->getNode(), msg->from); + } + + void operator()(AnotherMsg* msg) const { + fmt::print("{}: MultipleFunctions with AnotherMsg\n", vt::theContext()->getNode()); } }; @@ -70,7 +82,21 @@ int main(int argc, char** argv) { if (this_node == 0) { auto msg = vt::makeMessage(this_node); + + // 'HelloWorld' functor has only single 'operator()' declared + // so we can call send/braodcast without specyfing message type vt::theMsg()->broadcastMsg(msg); + + msg = vt::makeMessage(this_node); + vt::theMsg()->sendMsg(1, msg); + + // 'MultipleFunctions' functor declares more than one 'operator()' + // so we have to specify the type of the message, as it can't be deduced + auto new_msg = vt::makeMessage(); + vt::theMsg()->broadcastMsg(new_msg); + + new_msg = vt::makeMessage(); + vt::theMsg()->sendMsg(1, new_msg); } vt::finalize(); From d9269e1f90ed515a74203f5ab947bd893f967bd1 Mon Sep 17 00:00:00 2001 From: Jakub Domagala Date: Fri, 7 May 2021 19:18:01 +0200 Subject: [PATCH 8/9] #1260: examples: Fix typos --- examples/hello_world/hello_world_functor.cc | 2 +- src/vt/utils/static_checks/functor.h | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/hello_world/hello_world_functor.cc b/examples/hello_world/hello_world_functor.cc index 025ddbe3ae..54a873b517 100644 --- a/examples/hello_world/hello_world_functor.cc +++ b/examples/hello_world/hello_world_functor.cc @@ -84,7 +84,7 @@ int main(int argc, char** argv) { auto msg = vt::makeMessage(this_node); // 'HelloWorld' functor has only single 'operator()' declared - // so we can call send/braodcast without specyfing message type + // so we can call send/broadcast without specifying message type vt::theMsg()->broadcastMsg(msg); msg = vt::makeMessage(this_node); diff --git a/src/vt/utils/static_checks/functor.h b/src/vt/utils/static_checks/functor.h index e02ffbf620..1342d5b088 100644 --- a/src/vt/utils/static_checks/functor.h +++ b/src/vt/utils/static_checks/functor.h @@ -47,7 +47,6 @@ #include "vt/config.h" - namespace vt { namespace util { template From b1f37a2a65180954bf29326df4ca2cdc1a14535b Mon Sep 17 00:00:00 2001 From: Jakub Domagala Date: Fri, 7 May 2021 22:29:57 +0200 Subject: [PATCH 9/9] #1260: examples: Updated functor example --- examples/hello_world/hello_world_functor.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/hello_world/hello_world_functor.cc b/examples/hello_world/hello_world_functor.cc index 54a873b517..c579cf63d5 100644 --- a/examples/hello_world/hello_world_functor.cc +++ b/examples/hello_world/hello_world_functor.cc @@ -95,8 +95,8 @@ int main(int argc, char** argv) { auto new_msg = vt::makeMessage(); vt::theMsg()->broadcastMsg(new_msg); - new_msg = vt::makeMessage(); - vt::theMsg()->sendMsg(1, new_msg); + msg = vt::makeMessage(this_node); + vt::theMsg()->sendMsg(1, msg); } vt::finalize();