From e0e93544acf067ede87dbc949d0365b63805977d Mon Sep 17 00:00:00 2001 From: Matthieu Dorier Date: Thu, 24 Jun 2021 10:19:28 +0000 Subject: [PATCH] preventing copies from happening --- .../thallium/callable_remote_procedure.hpp | 8 +++---- include/thallium/engine.hpp | 2 +- include/thallium/packed_data.hpp | 6 ++--- include/thallium/proc_object.hpp | 22 ++++++++++++++++--- include/thallium/request.hpp | 4 ++-- 5 files changed, 29 insertions(+), 13 deletions(-) diff --git a/include/thallium/callable_remote_procedure.hpp b/include/thallium/callable_remote_procedure.hpp index 724b130..71b4e44 100644 --- a/include/thallium/callable_remote_procedure.hpp +++ b/include/thallium/callable_remote_procedure.hpp @@ -94,7 +94,7 @@ class callable_remote_procedure_with_context { double timeout_ms = -1.0) { hg_return_t ret; meta_proc_fn mproc = [this, &args](hg_proc_t proc) { - return proc_object(proc, const_cast&>(args), + return proc_object_encode(proc, const_cast&>(args), m_engine_impl, m_context); }; if(timeout_ms > 0.0) { @@ -160,8 +160,8 @@ class callable_remote_procedure_with_context { hg_return_t ret; margo_request req; meta_proc_fn mproc = [this, &args](hg_proc_t proc) { - return proc_object(proc, const_cast&>(args), - m_engine_impl, m_context); + return proc_object_encode(proc, const_cast&>(args), + m_engine_impl, m_context); }; if(timeout_ms > 0.0) { ret = margo_provider_iforward_timed( @@ -311,7 +311,7 @@ class callable_remote_procedure_with_context { * @return a packed_data object containing the returned value. */ template packed_data<> operator()(const T&... args) { - return forward(std::make_tuple(args...)); + return forward(std::make_tuple(std::cref(args)...)); } /** diff --git a/include/thallium/engine.hpp b/include/thallium/engine.hpp index 6ad11bd..3468f0e 100644 --- a/include/thallium/engine.hpp +++ b/include/thallium/engine.hpp @@ -768,7 +768,7 @@ engine::define(const std::string& name, typename std::decay::type...> iargs; meta_proc_fn mproc = [w_impl, &iargs](hg_proc_t proc) { auto ctx = std::tuple<>(); // TODO make this context available as argument - return proc_object(proc, iargs, w_impl, ctx); + return proc_object_decode(proc, iargs, w_impl, ctx); }; hg_return_t ret = margo_get_input(r.m_handle, &mproc); if(ret != HG_SUCCESS) diff --git a/include/thallium/packed_data.hpp b/include/thallium/packed_data.hpp index ed0b7b4..9e6e76e 100644 --- a/include/thallium/packed_data.hpp +++ b/include/thallium/packed_data.hpp @@ -100,13 +100,13 @@ class packed_data { } std::tuple t; meta_proc_fn mproc = [this, &t](hg_proc_t proc) { - return proc_object(proc, t, m_engine_impl, m_context); + return proc_object_decode(proc, t, m_engine_impl, m_context); }; hg_return_t ret = m_unpack_fn(m_handle, &mproc); MARGO_ASSERT(ret, m_unpack_fn); ret = margo_free_output(m_handle, &mproc); MARGO_ASSERT(ret, m_free_fn); - return std::get<0>(t); + return std::get<0>(std::move(t)); } /** @@ -134,7 +134,7 @@ class packed_data { typename std::decay::type...> t; meta_proc_fn mproc = [this, &t](hg_proc_t proc) { - return proc_object(proc, t, m_engine_impl, m_context); + return proc_object_decode(proc, t, m_engine_impl, m_context); }; hg_return_t ret = m_unpack_fn(m_handle, &mproc); MARGO_ASSERT(ret, m_unpack_fn); diff --git a/include/thallium/proc_object.hpp b/include/thallium/proc_object.hpp index 58103dc..580a7de 100644 --- a/include/thallium/proc_object.hpp +++ b/include/thallium/proc_object.hpp @@ -57,9 +57,9 @@ inline hg_return_t hg_proc_meta_serialization(hg_proc_t proc, void* data) { } template -hg_return_t proc_object(hg_proc_t proc, T& data, - const std::weak_ptr& e, - std::tuple& ctx) { +hg_return_t proc_object_encode(hg_proc_t proc, T& data, + const std::weak_ptr& e, + std::tuple& ctx) { switch(hg_proc_get_op(proc)) { case HG_ENCODE: { proc_output_archive ar(proc, ctx, e); @@ -69,6 +69,22 @@ hg_return_t proc_object(hg_proc_t proc, T& data, #endif ar << data; } break; + case HG_DECODE: + return HG_INVALID_ARG; // not supposed to happen + case HG_FREE: + default: + break; + } + return HG_SUCCESS; +} + +template +hg_return_t proc_object_decode(hg_proc_t proc, T& data, + const std::weak_ptr& e, + std::tuple& ctx) { + switch(hg_proc_get_op(proc)) { + case HG_ENCODE: + return HG_INVALID_ARG; // not supposed to happen case HG_DECODE: { proc_input_archive ar(proc, ctx, e); #ifdef THALLIUM_DEBUG_RPC_TYPES diff --git a/include/thallium/request.hpp b/include/thallium/request.hpp index fdbb70e..099c48a 100644 --- a/include/thallium/request.hpp +++ b/include/thallium/request.hpp @@ -166,9 +166,9 @@ class request_with_context { "Calling respond from an RPC that has disabled responses"); } if(m_handle != HG_HANDLE_NULL) { - auto args = std::make_tuple(t1, t...); + auto args = std::make_tuple(std::cref(t1), std::cref(t)...); meta_proc_fn mproc = [this, &args](hg_proc_t proc) { - return proc_object(proc, args, m_engine_impl, m_context); + return proc_object_encode(proc, args, m_engine_impl, m_context); }; hg_return_t ret = margo_respond(m_handle, &mproc); MARGO_ASSERT(ret, margo_respond);