Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify basic action implementations #3679

Merged
merged 6 commits into from Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion hpx/lcos/detail/async_implementations.hpp
Expand Up @@ -315,7 +315,7 @@ namespace hpx { namespace detail
naming::address::address_type lva,
naming::address::component_type comptype, Ts&&... vs) const
{
return get_remote_result_type::call(typename Action::invoker()(
return get_remote_result_type::call(Action::invoker(
lva, comptype, std::forward<Ts>(vs)...));
}
};
Expand Down
519 changes: 169 additions & 350 deletions hpx/runtime/actions/basic_action.hpp

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions hpx/runtime/actions/basic_action_fwd.hpp
Expand Up @@ -13,6 +13,8 @@
#include <hpx/util/itt_notify.hpp>
#endif

#include <hpx/runtime/actions/preassigned_action_id.hpp>

namespace hpx { namespace actions
{
///////////////////////////////////////////////////////////////////////////
Expand Down
170 changes: 79 additions & 91 deletions hpx/runtime/actions/component_action.hpp
Expand Up @@ -10,20 +10,17 @@

#include <hpx/config.hpp>
#include <hpx/runtime/actions/basic_action.hpp>
#include <hpx/runtime/actions/continuation.hpp>
#include <hpx/runtime/components/console_error_sink.hpp>
#include <hpx/runtime/components/pinned_ptr.hpp>
#include <hpx/runtime/naming/address.hpp>
#include <hpx/traits/is_future.hpp>
#include <hpx/util/detail/pp/cat.hpp>
#include <hpx/util/detail/pp/expand.hpp>
#include <hpx/util/detail/pp/nargs.hpp>
#include <hpx/util/detail/pp/strip_parens.hpp>
#include <hpx/util/unused.hpp>

#include <boost/utility/string_ref.hpp>

#include <cstdlib>
#include <sstream>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <utility>
Expand All @@ -43,70 +40,85 @@ namespace hpx { namespace actions
components::pinned_ptr p_;
};

template <typename R> struct tag {};
}

///////////////////////////////////////////////////////////////////////////
// Specialized generic non-const component action types allowing to hold
// a different number of arguments
///////////////////////////////////////////////////////////////////////////
template <
typename Component, typename R, typename ...Ps,
typename TF, TF F, typename Derived>
class basic_action_impl<
R (Component::*)(Ps...), TF, F, Derived>
: public basic_action<Component, R(Ps...), Derived>
{
public:
static std::string get_action_name(naming::address::address_type lva)
///////////////////////////////////////////////////////////////////////
inline std::string make_component_action_name(
boost::string_ref action_name, void const* lva)
{
std::stringstream name;
name << "component action("
<< detail::get_action_name<Derived>()
<< ") lva("
<< reinterpret_cast<void const*>(get_lva<Component>::call(lva))
<< ")";
name << "component action(" << action_name << ") lva(" << lva << ")";
return name.str();
}

template <typename R_, typename ...Ts>
static typename std::enable_if<!traits::is_future<R_>::value, R_>::type
invoke_helper(detail::tag<R_>, naming::address::address_type lva,
naming::address::component_type /*comptype*/, Ts&&... vs)
///////////////////////////////////////////////////////////////////////
template <typename Component, typename R, typename F, typename ...Ts>
R component_invoke(std::false_type,
naming::address::address_type lva,
naming::address::component_type /*comptype*/,
F Component::*f, Ts&&... vs)
{
basic_action<Component, R_(Ps...), Derived>::
increment_invocation_count();
return (get_lva<Component>::call(lva)->*F)(std::forward<Ts>(vs)...);
Component* component = get_lva<Component>::call(lva);
return (component->*f)(std::forward<Ts>(vs)...);
}

template <typename R_, typename ...Ts>
static typename std::enable_if<traits::is_future<R_>::value, R_>::type
invoke_helper(detail::tag<R_>, naming::address::address_type lva,
naming::address::component_type comptype, Ts&&... vs)
template <typename Component, typename R, typename F, typename ...Ts>
R component_invoke(std::true_type,
naming::address::address_type lva,
naming::address::component_type comptype,
F Component::*f, Ts&&... vs)
{
basic_action<Component, R_(Ps...), Derived>::
increment_invocation_count();

// additional pinning is required such that the object becomes
// unpinned only after the returned future has become ready
components::pinned_ptr p =
components::pinned_ptr::create<Component>(lva);

auto result =
(get_lva<Component>::call(lva)->*F)(std::forward<Ts>(vs)...);
Component* component = get_lva<Component>::call(lva);
R result = (component->*f)(std::forward<Ts>(vs)...);

traits::detail::get_shared_state(result)->set_on_completed(
detail::keep_object_pinned{std::move(p)});

return result;
}
}

///////////////////////////////////////////////////////////////////////////
// Specialized generic non-const component action types allowing to hold
// a different number of arguments
///////////////////////////////////////////////////////////////////////////
template <
typename Component, typename R, typename ...Ps,
R (Component::*F)(Ps...), typename Derived>
struct action<R (Component::*)(Ps...), F, Derived>
: public basic_action<Component, R(Ps...),
typename detail::action_type<
action<R (Component::*)(Ps...), F, Derived>,
Derived
>::type
>
{
public:
typedef typename detail::action_type<
action, Derived
>::type derived_type;

static std::string get_action_name(naming::address::address_type lva)
{
return detail::make_component_action_name(
detail::get_action_name<derived_type>(),
get_lva<Component>::call(lva));
}

template <typename ...Ts>
static R invoke(naming::address::address_type lva,
static R invoke(
naming::address::address_type lva,
naming::address::component_type comptype, Ts&&... vs)
{
return invoke_helper(
detail::tag<R>{}, lva, comptype, std::forward<Ts>(vs)...);
basic_action<Component, R(Ps...), derived_type>::
increment_invocation_count();

using is_future = typename traits::is_future<R>::type;
return detail::component_invoke<Component, R>(is_future{},
lva, comptype, F, std::forward<Ts>(vs)...);
}
};

Expand All @@ -116,62 +128,38 @@ namespace hpx { namespace actions
///////////////////////////////////////////////////////////////////////////
template <
typename Component, typename R, typename ...Ps,
typename TF, TF F, typename Derived>
class basic_action_impl<
R (Component::*)(Ps...) const, TF, F, Derived>
: public basic_action<Component const, R(Ps...), Derived>
R (Component::*F)(Ps...) const, typename Derived>
struct action<R (Component::*)(Ps...) const, F, Derived>
: public basic_action<Component const, R(Ps...),
typename detail::action_type<
action<R (Component::*)(Ps...) const, F, Derived>,
Derived
>::type
>
{
public:
static std::string get_action_name(naming::address::address_type lva)
{
std::stringstream name;
name << "component action("
<< detail::get_action_name<Derived>()
<< ") lva("
<< reinterpret_cast<void const*>(get_lva<Component>::call(lva))
<< ")";
return name.str();
}
typedef typename detail::action_type<
action, Derived
>::type derived_type;

template <typename R_, typename ...Ts>
static typename std::enable_if<!traits::is_future<R_>::value, R_>::type
invoke_helper(detail::tag<R_>, naming::address::address_type lva,
naming::address::component_type /*comptype*/, Ts&&... vs)
static std::string get_action_name(naming::address::address_type lva)
{
basic_action<Component const, R_(Ps...), Derived>::
increment_invocation_count();
return (get_lva<Component const>::call(lva)->*F)(
std::forward<Ts>(vs)...);
return detail::make_component_action_name(
detail::get_action_name<derived_type>(),
get_lva<Component>::call(lva));
}

template <typename R_, typename ...Ts>
static typename std::enable_if<traits::is_future<R_>::value, R_>::type
invoke_helper(detail::tag<R_>, naming::address::address_type lva,
template <typename ...Ts>
static R invoke(
naming::address::address_type lva,
naming::address::component_type comptype, Ts&&... vs)
{
basic_action<Component const, R_(Ps...), Derived>::
basic_action<Component const, R(Ps...), derived_type>::
increment_invocation_count();

// additional pinning is required such that the object becomes
// unpinned only after the returned future has become ready
components::pinned_ptr p =
components::pinned_ptr::create<Component>(lva);

auto result = (get_lva<Component const>::call(lva)->*F)(
std::forward<Ts>(vs)...);

traits::detail::get_shared_state(result)->set_on_completed(
detail::keep_object_pinned{std::move(p)});

return result;
}

template <typename ...Ts>
static R invoke(naming::address::address_type lva,
naming::address::component_type comptype, Ts&&... vs)
{
return invoke_helper(
detail::tag<R>{}, lva, comptype, std::forward<Ts>(vs)...);
using is_future = typename traits::is_future<R>::type;
return detail::component_invoke<Component const, R>(is_future{},
lva, comptype, F, std::forward<Ts>(vs)...);
}
};

Expand Down
4 changes: 1 addition & 3 deletions hpx/runtime/actions/continuation_fwd.hpp
Expand Up @@ -15,10 +15,8 @@ namespace hpx { namespace actions
template <typename Result, typename RemoteResult = Result>
struct typed_continuation;

template <
typename Result, typename RemoteResult, typename F, typename ...Ts>
template <typename Result, typename RemoteResult, typename F, typename ...Ts>
void trigger(typed_continuation<Result, RemoteResult>&& cont, F&& f, Ts&&... vs);
}}

#endif

2 changes: 0 additions & 2 deletions hpx/runtime/actions/lambda_to_action.hpp
Expand Up @@ -43,8 +43,6 @@ namespace hpx { namespace actions
lambda_action<F, ReturnType, Args...>>
{
typedef lambda_action derived_type;
typedef std::false_type direct_execution;
typedef void is_plain_action;

static std::string get_action_name(naming::address::address_type /*lva*/)
{
Expand Down
41 changes: 27 additions & 14 deletions hpx/runtime/actions/plain_action.hpp
Expand Up @@ -11,16 +11,14 @@

#include <hpx/config.hpp>
#include <hpx/runtime/actions/basic_action.hpp>
#include <hpx/runtime/actions/continuation.hpp>
#include <hpx/runtime/naming/address.hpp>
#include <hpx/traits/component_type_database.hpp>
#include <hpx/util/assert.hpp>
#include <hpx/util/detail/pp/cat.hpp>
#include <hpx/util/detail/pp/expand.hpp>
#include <hpx/util/detail/pp/nargs.hpp>
#include <hpx/util/detail/pack.hpp>
#include <hpx/util/detail/pp/strip_parens.hpp>
#include <hpx/util/unused.hpp>

#include <boost/utility/string_ref.hpp>

#include <cstdlib>
#include <sstream>
Expand Down Expand Up @@ -48,6 +46,15 @@ namespace hpx { namespace actions
return naming::is_locality(id);
}
};

///////////////////////////////////////////////////////////////////////
inline std::string make_plain_action_name(
boost::string_ref action_name)
{
std::stringstream name;
name << "plain action(" << action_name << ")";
return name.str();
}
}

///////////////////////////////////////////////////////////////////////////
Expand All @@ -56,26 +63,32 @@ namespace hpx { namespace actions
///////////////////////////////////////////////////////////////////////////
template <
typename R, typename ...Ps,
typename TF, TF F, typename Derived>
class basic_action_impl<R (*)(Ps...), TF, F, Derived>
: public basic_action<detail::plain_function, R(Ps...), Derived>
R (*F)(Ps...), typename Derived>
struct action<R (*)(Ps...), F, Derived>
: public basic_action<detail::plain_function, R(Ps...),
typename detail::action_type<
action<R (*)(Ps...), F, Derived>,
Derived
>::type
>
{
public:

typedef void is_plain_action;
typedef typename detail::action_type<
action, Derived
>::type derived_type;

static std::string get_action_name(naming::address::address_type /*lva*/)
{
std::stringstream name;
name << "plain action(" << detail::get_action_name<Derived>() << ")";
return name.str();
return detail::make_plain_action_name(
detail::get_action_name<derived_type>());
}

template <typename ...Ts>
static R invoke(naming::address::address_type /*lva*/,
static R invoke(
naming::address::address_type /*lva*/,
naming::address::component_type comptype, Ts&&... vs)
{
basic_action<detail::plain_function, R(Ps...), Derived>::
basic_action<detail::plain_function, R(Ps...), derived_type>::
increment_invocation_count();
return F(std::forward<Ts>(vs)...);
}
Expand Down