Skip to content

Commit

Permalink
Merge pull request #1897 from STEllAR-GROUP/fix_sort
Browse files Browse the repository at this point in the history
Changing parallel::sort to return the last iterator as proposed by N4560
  • Loading branch information
hkaiser committed Dec 8, 2015
2 parents 8417f14 + 86f7d30 commit 4f8925a
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions hpx/parallel/algorithms/sort.hpp
Expand Up @@ -61,44 +61,44 @@ namespace hpx { namespace parallel { HPX_INLINE_NAMESPACE(v1)
};

///////////////////////////////////////////////////////////////////////
template <typename ExPolicy>
template <typename ExPolicy, typename R>
struct handle_sort_exception
{
static hpx::future<void> call(hpx::future<void> f)
static hpx::future<R> call(hpx::future<R> f)
{
HPX_ASSERT(f.has_exception());
return f;
}

static hpx::future<void> call(boost::exception_ptr const& e)
static hpx::future<R> call(boost::exception_ptr const& e)
{
try {
boost::rethrow_exception(e);
}
catch (std::bad_alloc const&) {
// rethrow bad_alloc
return hpx::make_exceptional_future<void>(
return hpx::make_exceptional_future<R>(
boost::current_exception());
}
catch (...) {
// package up everything else as an exception_list
return hpx::make_exceptional_future<void>(
return hpx::make_exceptional_future<R>(
exception_list(e));
}
}
};

template <>
struct handle_sort_exception<parallel_vector_execution_policy>
template <typename R>
struct handle_sort_exception<parallel_vector_execution_policy, R>
{
HPX_ATTRIBUTE_NORETURN
static hpx::future<void> call(hpx::future<void> &&)
static hpx::future<R> call(hpx::future<void> &&)
{
hpx::terminate();
}

HPX_ATTRIBUTE_NORETURN
static hpx::future<void> call(boost::exception_ptr const&)
static hpx::future<R> call(boost::exception_ptr const&)
{
hpx::terminate();
}
Expand Down Expand Up @@ -131,7 +131,7 @@ namespace hpx { namespace parallel { HPX_INLINE_NAMESPACE(v1)
/// \remarks
//------------------------------------------------------------------------
template <typename ExPolicy, typename RandomIt, typename Compare>
hpx::future<void> sort_thread(ExPolicy policy,
hpx::future<RandomIt> sort_thread(ExPolicy policy,
RandomIt first, RandomIt last, Compare comp)
{
typedef typename ExPolicy::executor_type executor_type;
Expand All @@ -147,6 +147,7 @@ namespace hpx { namespace parallel { HPX_INLINE_NAMESPACE(v1)
[first, last, comp]()
{
std::sort(first, last, comp);
return last;
});
}

Expand All @@ -156,7 +157,7 @@ namespace hpx { namespace parallel { HPX_INLINE_NAMESPACE(v1)

//------------------- check if sorted ----------------------------
if (detail::is_sorted_sequential(first, last, comp))
return hpx::make_ready_future();
return hpx::make_ready_future(last);

//---------------------- pivot select ----------------------------
std::size_t nx = std::size_t(N) >> 1;
Expand Down Expand Up @@ -214,7 +215,7 @@ namespace hpx { namespace parallel { HPX_INLINE_NAMESPACE(v1)
));

return hpx::lcos::local::dataflow(
[](hpx::future<void> && left, hpx::future<void> && right)
[last](hpx::future<void> && left, hpx::future<void> && right)
{
if (left.has_exception() || right.has_exception())
{
Expand All @@ -227,6 +228,7 @@ namespace hpx { namespace parallel { HPX_INLINE_NAMESPACE(v1)
boost::throw_exception(
exception_list(std::move(errors)));
}
return last;
},
std::move(left), std::move(right));
}
Expand All @@ -241,24 +243,24 @@ namespace hpx { namespace parallel { HPX_INLINE_NAMESPACE(v1)
/// @return
/// @remarks
template <typename ExPolicy, typename RandomIt, typename Compare>
hpx::future<void>
hpx::future<RandomIt>
parallel_sort_async(ExPolicy policy, RandomIt first, RandomIt last,
Compare comp)
{
hpx::future<void> result;
hpx::future<RandomIt> result;
try {
std::ptrdiff_t N = last - first;
HPX_ASSERT(N >= 0);

if (std::size_t(N) < sort_limit_per_task)
{
std::sort(first, last, comp);
return hpx::make_ready_future();
return hpx::make_ready_future(last);
}

// check if already sorted
if (detail::is_sorted_sequential(first, last, comp))
return hpx::make_ready_future();
return hpx::make_ready_future(last);

typedef typename ExPolicy::executor_type executor_type;
typedef typename hpx::parallel::executor_traits<executor_type>
Expand All @@ -272,13 +274,13 @@ namespace hpx { namespace parallel { HPX_INLINE_NAMESPACE(v1)
));
}
catch (...) {
return detail::handle_sort_exception<ExPolicy>::call(
return detail::handle_sort_exception<ExPolicy, RandomIt>::call(
boost::current_exception());
}

if (result.has_exception())
{
return detail::handle_sort_exception<ExPolicy>::call(
return detail::handle_sort_exception<ExPolicy, RandomIt>::call(
std::move(result));
}

Expand All @@ -288,14 +290,14 @@ namespace hpx { namespace parallel { HPX_INLINE_NAMESPACE(v1)
///////////////////////////////////////////////////////////////////////
// sort
template <typename RandomIt>
struct sort : public detail::algorithm<sort<RandomIt>, void>
struct sort : public detail::algorithm<sort<RandomIt>, RandomIt>
{
sort()
: sort::algorithm("sort")
{}

template <typename ExPolicy, typename Compare, typename Proj>
static hpx::util::unused_type
static RandomIt
sequential(ExPolicy, RandomIt first, RandomIt last,
Compare && comp, Proj && proj)
{
Expand All @@ -304,17 +306,19 @@ namespace hpx { namespace parallel { HPX_INLINE_NAMESPACE(v1)
std::forward<Compare>(comp),
std::forward<Proj>(proj)
));
return hpx::util::unused;
return last;
}

template <typename ExPolicy, typename Compare, typename Proj>
static typename util::detail::algorithm_result<ExPolicy>::type
static typename util::detail::algorithm_result<
ExPolicy, RandomIt
>::type
parallel(ExPolicy policy, RandomIt first, RandomIt last,
Compare && comp, Proj && proj)
{
// call the sort routine and return the right type,
// depending on execution policy
return util::detail::algorithm_result<ExPolicy>::get(
return util::detail::algorithm_result<ExPolicy, RandomIt>::get(
parallel_sort_async(policy, first, last,
compare_projected<Compare, Proj>(
std::forward<Compare>(comp),
Expand Down Expand Up @@ -407,7 +411,7 @@ namespace hpx { namespace parallel { HPX_INLINE_NAMESPACE(v1)
traits::projected<Proj, RandomIt>,
traits::projected<Proj, RandomIt>
>::value)>
typename util::detail::algorithm_result<ExPolicy, void>::type
typename util::detail::algorithm_result<ExPolicy, RandomIt>::type
sort(ExPolicy && policy, RandomIt first, RandomIt last,
Compare && comp = Compare(), Proj && proj = Proj())
{
Expand Down

0 comments on commit 4f8925a

Please sign in to comment.