Skip to content

Commit

Permalink
Adding locality parameter to python execution interface
Browse files Browse the repository at this point in the history
- flyby: adding sleep primitive
  • Loading branch information
hkaiser committed May 16, 2020
1 parent 11ce5dc commit a7a2b67
Show file tree
Hide file tree
Showing 18 changed files with 678 additions and 185 deletions.
90 changes: 90 additions & 0 deletions phylanx/execution_tree/compiler/actors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,49 @@ namespace phylanx { namespace execution_tree { namespace compiler
return extract_copy_value(arg_, name_);
}

// evaluate object itself and use the returned value to
// asynchronously evaluate the arguments
hpx::shared_future<result_type> async(
arguments_type const& args, eval_context ctx) const
{
if (is_primitive_operand(arg_))
{
arguments_type params;
params.reserve(args.size());
for (auto const& arg : args)
{
params.emplace_back(extract_ref_value(arg, name_));
}

return value_operand(arg_, std::move(params), name_).share();
}
return hpx::make_ready_future(extract_copy_value(arg_, name_))
.share();
}

hpx::shared_future<result_type> async(
arguments_type&& args, eval_context ctx) const
{
if (is_primitive_operand(arg_))
{
arguments_type keep_alive(std::move(args));

// construct argument-pack to use for actual call
arguments_type params;
params.reserve(keep_alive.size());
for (auto const& arg : keep_alive)
{
params.emplace_back(extract_ref_value(arg, name_));
}

return value_operand(
arg_, std::move(params), name_, "<unknown>", std::move(ctx))
.share();
}
return hpx::make_ready_future(extract_copy_value(arg_, name_))
.share();
}

using kwarguments_type = std::map<std::string, primitive_argument_type>;

result_type operator()(arguments_type&& args, kwarguments_type&& kwargs,
Expand Down Expand Up @@ -222,6 +265,53 @@ namespace phylanx { namespace execution_tree { namespace compiler
return extract_copy_value(arg_, name_);
}

hpx::shared_future<result_type> async(arguments_type&& args,
kwarguments_type&& kwargs, eval_context ctx) const
{
if (is_primitive_operand(arg_))
{
arguments_type keep_alive(std::move(args));
kwarguments_type kw_keep_alive(std::move(kwargs));

// construct argument-pack to use for actual call
arguments_type params;
params.reserve(keep_alive.size() + num_named_args_);
for (auto const& arg : keep_alive)
{
params.emplace_back(extract_ref_value(arg, name_));
}

// fill in the given named arguments at their correct positions
for (auto const& kwarg : kw_keep_alive)
{
auto it = std::find(named_args_.get(),
named_args_.get() + num_named_args_, kwarg.first);
if (it == named_args_.get() + num_named_args_)
{
HPX_THROW_EXCEPTION(hpx::bad_parameter,
"function::operator()",
hpx::util::format("cannot locate requested "
"named argument '{}'", kwarg.first));
}

std::ptrdiff_t kwarg_pos =
std::distance(named_args_.get(), it);
if (kwarg_pos >= std::ptrdiff_t(params.size()))
{
params.resize(kwarg_pos + 1);
}

params[kwarg_pos] = extract_ref_value(kwarg.second, name_);
}

return value_operand(
arg_, std::move(params), name_, "<unknown>", std::move(ctx))
.share();
}
return hpx::make_ready_future(extract_copy_value(arg_, name_))
.share();
}

template <typename T1, typename ... Ts>
typename std::enable_if<
!std::is_same<eval_context, typename std::decay<T1>::type>::value,
Expand Down
1 change: 1 addition & 0 deletions phylanx/plugins/controls/controls.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <phylanx/plugins/controls/parallel_block_operation.hpp>
#include <phylanx/plugins/controls/parallel_map_operation.hpp>
#include <phylanx/plugins/controls/range_operation.hpp>
#include <phylanx/plugins/controls/sleep_operation.hpp>
#include <phylanx/plugins/controls/while_operation.hpp>

#endif
Expand Down
46 changes: 46 additions & 0 deletions phylanx/plugins/controls/sleep_operation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2020 Hartmut Kaiser
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#if !defined(PHYLANX_SLEEP_OPERATION_APR_24_2020_0840AM)
#define PHYLANX_SLEEP_OPERATION_APR_24_2020_0840AM

#include <phylanx/config.hpp>
#include <phylanx/execution_tree/primitives/base_primitive.hpp>
#include <phylanx/execution_tree/primitives/primitive_component_base.hpp>

#include <hpx/lcos/future.hpp>

#include <string>
#include <utility>

namespace phylanx { namespace execution_tree { namespace primitives {

class sleep_operation : public primitive_component_base
{
public:
static match_pattern_type const match_data;

sleep_operation() = default;

sleep_operation(primitive_arguments_type&& operands,
std::string const& name, std::string const& codename);

protected:
hpx::future<primitive_argument_type> eval(
primitive_arguments_type const& operands,
primitive_arguments_type const& args,
eval_context ctx) const override;
};

inline primitive create_sleep_operation(hpx::id_type const& locality,
primitive_arguments_type&& operands, std::string const& name = "",
std::string const& codename = "")
{
return create_primitive_component(
locality, "sleep", std::move(operands), name, codename);
}
}}} // namespace phylanx::execution_tree::primitives

#endif
Loading

0 comments on commit a7a2b67

Please sign in to comment.