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

More segmented algorithms #1968

Merged
merged 6 commits into from
Jan 29, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ set(doxygen_dependencies
"${PROJECT_SOURCE_DIR}/hpx/parallel/algorithms/uninitialized_fill.hpp"
"${PROJECT_SOURCE_DIR}/hpx/parallel/container_algorithms/copy.hpp"
"${PROJECT_SOURCE_DIR}/hpx/parallel/container_algorithms/for_each.hpp"
"${PROJECT_SOURCE_DIR}/hpx/parallel/container_algorithms/generate.hpp"
"${PROJECT_SOURCE_DIR}/hpx/parallel/container_algorithms/minmax.hpp"
"${PROJECT_SOURCE_DIR}/hpx/parallel/container_algorithms/remove_copy.hpp"
"${PROJECT_SOURCE_DIR}/hpx/parallel/container_algorithms/replace.hpp"
"${PROJECT_SOURCE_DIR}/hpx/parallel/container_algorithms/reverse.hpp"
Expand Down
25 changes: 25 additions & 0 deletions hpx/components/containers/container_distribution_policy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,31 @@ namespace hpx
return container_distribution_policy(localities.size(), localities);
}

container_distribution_policy operator()(
std::vector<id_type> && localities) const
{
if (num_partitions_ != std::size_t(-1))
{
return container_distribution_policy(
num_partitions_, std::move(localities));
}
return container_distribution_policy(
localities.size(), std::move(localities));
}

container_distribution_policy operator()(std::size_t num_partitions,
std::vector<id_type> const& localities) const
{
return container_distribution_policy(num_partitions, localities);
}

container_distribution_policy operator()(std::size_t num_partitions,
std::vector<id_type> && localities) const
{
return container_distribution_policy(
num_partitions, std::move(localities));
}

///////////////////////////////////////////////////////////////////////
std::size_t get_num_partitions() const
{
Expand Down Expand Up @@ -77,6 +96,12 @@ namespace hpx
num_partitions_(num_partitions)
{}

container_distribution_policy(std::size_t num_partitions,
std::vector<id_type> && localities)
: components::default_distribution_policy(std::move(localities)),
num_partitions_(num_partitions)
{}

private:
std::size_t num_partitions_; // number of chunks to create
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ namespace hpx { namespace server
}}

///////////////////////////////////////////////////////////////////////////////
#define HPX_REGISTER_VECTOR_DECLARATION(...) \
#define HPX_REGISTER_PARTITIONED_VECTOR_DECLARATION(...) \
HPX_REGISTER_VECTOR_DECLARATION_(__VA_ARGS__) \
/**/
#define HPX_REGISTER_VECTOR_DECLARATION_(...) \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ namespace hpx
typedef local_vector_iterator<T> local_iterator;
typedef const_local_vector_iterator<T> local_const_iterator;

local_raw_vector_iterator() {}

local_raw_vector_iterator(base_iterator const& it,
boost::shared_ptr<server::partitioned_vector<T> > const& data)
: base_type(it), data_(data)
Expand Down Expand Up @@ -117,6 +119,8 @@ namespace hpx
typedef const_local_vector_iterator<T> local_iterator;
typedef const_local_vector_iterator<T> local_const_iterator;

const_local_raw_vector_iterator() {}

const_local_raw_vector_iterator(base_iterator const& it,
boost::shared_ptr<server::partitioned_vector<T> > const& data)
: base_type(it), data_(data)
Expand Down Expand Up @@ -502,6 +506,10 @@ namespace hpx
> base_type;

public:
segment_vector_iterator()
: data_(0)
{}

segment_vector_iterator(BaseIter const& it, partitioned_vector<T>* data = 0)
: base_type(it), data_(data)
{}
Expand Down Expand Up @@ -531,6 +539,10 @@ namespace hpx
> base_type;

public:
const_segment_vector_iterator()
: data_(0)
{}

const_segment_vector_iterator(BaseIter const& it,
partitioned_vector<T> const* data = 0)
: base_type(it), data_(data)
Expand Down Expand Up @@ -588,6 +600,9 @@ namespace hpx
typedef detail::is_requested_locality<BaseIter> predicate;

public:
local_segment_vector_iterator()
{}

local_segment_vector_iterator(BaseIter const& end)
: base_type(end), predicate_(), end_(end)
{}
Expand Down
4 changes: 3 additions & 1 deletion hpx/include/parallel_generate.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2007-2014 Hartmut Kaiser
// Copyright (c) 2007-2016 Hartmut Kaiser
// Copyright (c) 2014 Grant Mercer
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
Expand All @@ -8,6 +8,8 @@
#define HPX_PARALLEL_GENERATE_OCT_06_2014_1007AM

#include <hpx/parallel/algorithms/generate.hpp>
#include <hpx/parallel/container_algorithms/generate.hpp>
#include <hpx/parallel/segmented_algorithms/generate.hpp>

#endif

4 changes: 3 additions & 1 deletion hpx/include/parallel_minmax.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2007-2014 Hartmut Kaiser
// Copyright (c) 2007-2016 Hartmut Kaiser
// Copyright (c) 2014 Grant Mercer
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
Expand All @@ -8,6 +8,8 @@
#define HPX_PARALLEL_MINMAX_AUG_20_2014_0454PM

#include <hpx/parallel/algorithms/minmax.hpp>
#include <hpx/parallel/container_algorithms/minmax.hpp>
#include <hpx/parallel/segmented_algorithms/minmax.hpp>

#endif

12 changes: 6 additions & 6 deletions hpx/parallel/algorithms/copy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ namespace hpx { namespace parallel { HPX_INLINE_NAMESPACE(v1)
template <typename ExPolicy, typename InIter, typename OutIter,
HPX_CONCEPT_REQUIRES_(
is_execution_policy<ExPolicy>::value &&
traits::detail::is_iterator<InIter>::value &&
traits::detail::is_iterator<OutIter>::value)>
traits::is_iterator<InIter>::value &&
traits::is_iterator<OutIter>::value)>
typename util::detail::algorithm_result<
ExPolicy, hpx::util::tagged_pair<tag::in(InIter), tag::out(OutIter)>
>::type
Expand Down Expand Up @@ -336,8 +336,8 @@ namespace hpx { namespace parallel { HPX_INLINE_NAMESPACE(v1)
typename OutIter,
HPX_CONCEPT_REQUIRES_(
is_execution_policy<ExPolicy>::value &&
traits::detail::is_iterator<InIter>::value &&
traits::detail::is_iterator<OutIter>::value)>
traits::is_iterator<InIter>::value &&
traits::is_iterator<OutIter>::value)>
typename util::detail::algorithm_result<
ExPolicy, hpx::util::tagged_pair<tag::in(InIter), tag::out(OutIter)>
>::type
Expand Down Expand Up @@ -583,8 +583,8 @@ namespace hpx { namespace parallel { HPX_INLINE_NAMESPACE(v1)
typename Proj = util::projection_identity,
HPX_CONCEPT_REQUIRES_(
is_execution_policy<ExPolicy>::value &&
traits::detail::is_iterator<InIter>::value &&
traits::detail::is_iterator<OutIter>::value &&
traits::is_iterator<InIter>::value &&
traits::is_iterator<OutIter>::value &&
traits::is_projected<Proj, InIter>::value &&
traits::is_indirect_callable<
F, traits::projected<Proj, InIter>
Expand Down
6 changes: 3 additions & 3 deletions hpx/parallel/algorithms/for_each.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ namespace hpx { namespace parallel { HPX_INLINE_NAMESPACE(v1)
// ref
util::loop_n(
part_begin, part_size,
[=](Iter const& curr)
[=](Iter const& curr) mutable
{
hpx::util::invoke(
f, hpx::util::invoke(proj, *curr));
Expand Down Expand Up @@ -171,7 +171,7 @@ namespace hpx { namespace parallel { HPX_INLINE_NAMESPACE(v1)
typename Proj = util::projection_identity,
HPX_CONCEPT_REQUIRES_(
is_execution_policy<ExPolicy>::value &&
traits::detail::is_iterator<InIter>::value &&
traits::is_iterator<InIter>::value &&
traits::is_projected<Proj, InIter>::value &&
traits::is_indirect_callable<
F, traits::projected<Proj, InIter>
Expand Down Expand Up @@ -365,7 +365,7 @@ namespace hpx { namespace parallel { HPX_INLINE_NAMESPACE(v1)
typename Proj = util::projection_identity,
HPX_CONCEPT_REQUIRES_(
is_execution_policy<ExPolicy>::value &&
traits::detail::is_iterator<InIter>::value &&
traits::is_iterator<InIter>::value &&
traits::is_projected<Proj, InIter>::value &&
traits::is_indirect_callable<
F, traits::projected<Proj, InIter>
Expand Down
90 changes: 61 additions & 29 deletions hpx/parallel/algorithms/generate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
#if !defined(HPX_PARALLEL_DETAIL_GENERATE_JULY_15_2014_0224PM)
#define HPX_PARALLEL_DETAIL_GENERATE_JULY_15_2014_0224PM

#include <hpx/hpx_fwd.hpp>
#include <hpx/config.hpp>
#include <hpx/traits/concepts.hpp>

#include <hpx/parallel/execution_policy.hpp>
#include <hpx/parallel/algorithms/detail/dispatch.hpp>
#include <hpx/parallel/algorithms/for_each.hpp>
#include <hpx/parallel/util/detail/algorithm_result.hpp>
#include <hpx/util/void_guard.hpp>

#include <algorithm>
#include <iterator>
Expand All @@ -28,37 +29,62 @@ namespace hpx { namespace parallel { HPX_INLINE_NAMESPACE(v1)
namespace detail
{
/// \cond NOINTERNAL
struct generate : public detail::algorithm<generate>
template <typename Iter>
struct generate : public detail::algorithm<generate<Iter>, Iter>
{
generate()
: generate::algorithm("generate")
: generate::algorithm("generate")
{}

template <typename ExPolicy, typename FwdIter, typename F>
static hpx::util::unused_type
static FwdIter
sequential(ExPolicy, FwdIter first, FwdIter last, F && f)
{
std::generate(first, last, std::forward<F>(f));
return hpx::util::unused;
return last;
}

template <typename ExPolicy, typename FwdIter, typename F>
static typename util::detail::algorithm_result<ExPolicy>::type
static typename util::detail::algorithm_result<ExPolicy, FwdIter>::type
parallel(ExPolicy policy, FwdIter first, FwdIter last, F && f)
{
typedef typename util::detail::algorithm_result<ExPolicy>::type
result_type;
typedef typename std::iterator_traits<FwdIter>::value_type type;

return hpx::util::void_guard<result_type>(),
for_each_n<FwdIter>().call(
return for_each_n<FwdIter>().call(
policy, boost::mpl::false_(),
first, std::distance(first, last),
[f](type& v) {
[f](type& v) mutable
{
v = f();
});
}
};

///////////////////////////////////////////////////////////////////////
// non-segmented implementation
template <typename ExPolicy, typename FwdIter, typename F>
inline typename util::detail::algorithm_result<ExPolicy, FwdIter>::type
generate_(ExPolicy && policy, FwdIter first, FwdIter last, F && f,
std::false_type)
{
typedef typename parallel::is_sequential_execution_policy<
ExPolicy
>::type is_seq;

return detail::generate<FwdIter>().call(
std::forward<ExPolicy>(policy), is_seq(),
first, last, std::forward<F>(f));
}

///////////////////////////////////////////////////////////////////////
// segmented implementation
template <typename ExPolicy, typename FwdIter, typename F>
inline typename util::detail::algorithm_result<ExPolicy, FwdIter>::type
generate_(ExPolicy && policy, FwdIter first, FwdIter last, F && f,
std::true_type);

/// \endcond
}

Expand Down Expand Up @@ -105,16 +131,18 @@ namespace hpx { namespace parallel { HPX_INLINE_NAMESPACE(v1)
/// fashion in unspecified threads, and indeterminately sequenced
/// within each thread.
///
/// \returns The \a replace_if algorithm returns a \a hpx::future<void>
/// \returns The \a replace_if algorithm returns a \a hpx::future<FwdIter>
/// if the execution policy is of type
/// \a sequential_task_execution_policy or
/// \a parallel_task_execution_policy
/// and returns \a void otherwise.
template <typename ExPolicy, typename FwdIter, typename F>
inline typename boost::enable_if<
is_execution_policy<ExPolicy>,
typename util::detail::algorithm_result<ExPolicy, void>::type
>::type
/// and returns \a FwdIter otherwise.
/// It returns \a last.
///
template <typename ExPolicy, typename FwdIter, typename F,
HPX_CONCEPT_REQUIRES_(
is_execution_policy<ExPolicy>::value &&
traits::is_iterator<FwdIter>::value)>
typename util::detail::algorithm_result<ExPolicy, FwdIter>::type
generate(ExPolicy && policy, FwdIter first, FwdIter last, F && f)
{
typedef typename std::iterator_traits<FwdIter>::iterator_category
Expand All @@ -125,11 +153,12 @@ namespace hpx { namespace parallel { HPX_INLINE_NAMESPACE(v1)
std::forward_iterator_tag, iterator_category>::value),
"Required at least forward iterator.");

typedef typename is_sequential_execution_policy<ExPolicy>::type is_seq;
typedef hpx::traits::segmented_iterator_traits<FwdIter> iterator_traits;
typedef typename iterator_traits::is_segmented_iterator is_segmented;

return detail::generate().call(
std::forward<ExPolicy>(policy), is_seq(),
first, last, std::forward<F>(f));
return detail::generate_(
std::forward<ExPolicy>(policy), first, last,
std::forward<F>(f), is_segmented());
}

///////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -163,7 +192,8 @@ namespace hpx { namespace parallel { HPX_INLINE_NAMESPACE(v1)
return
for_each_n<OutIter>().call(
policy, boost::mpl::false_(), first, count,
[f](type& v) {
[f](type& v) mutable
{
v = f();
});
}
Expand Down Expand Up @@ -215,16 +245,18 @@ namespace hpx { namespace parallel { HPX_INLINE_NAMESPACE(v1)
/// fashion in unspecified threads, and indeterminately sequenced
/// within each thread.
///
/// \returns The \a replace_if algorithm returns a \a hpx::future<void>
/// \returns The \a replace_if algorithm returns a \a hpx::future<OutIter>
/// if the execution policy is of type
/// \a sequential_task_execution_policy or
/// \a parallel_task_execution_policy
/// and returns \a void otherwise.
template <typename ExPolicy, typename OutIter, typename Size, typename F>
inline typename boost::enable_if<
is_execution_policy<ExPolicy>,
typename util::detail::algorithm_result<ExPolicy, OutIter>::type
>::type
/// and returns \a OutIter otherwise.
/// It returns \a last.
///
template <typename ExPolicy, typename OutIter, typename Size, typename F,
HPX_CONCEPT_REQUIRES_(
is_execution_policy<ExPolicy>::value &&
traits::is_iterator<OutIter>::value)>
typename util::detail::algorithm_result<ExPolicy, OutIter>::type
generate_n(ExPolicy && policy, OutIter first, Size count, F && f)
{
typedef typename std::iterator_traits<OutIter>::iterator_category
Expand Down
Loading