Skip to content

Commit

Permalink
Improved exception handling for parallel::sort
Browse files Browse the repository at this point in the history
  • Loading branch information
hkaiser committed Nov 8, 2015
1 parent 8b66cca commit 9f4f918
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 34 deletions.
67 changes: 63 additions & 4 deletions hpx/parallel/algorithms/sort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,50 @@ namespace hpx { namespace parallel { HPX_INLINE_NAMESPACE(v1)
Proj proj_;
};

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

static hpx::future<void> 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>(
boost::current_exception());
}
catch (...) {
// package up everything else as an exception_list
return hpx::make_exceptional_future<void>(
exception_list(e));
}
}
};

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

HPX_ATTRIBUTE_NORETURN
static hpx::future<void> call(boost::exception_ptr const&)
{
hpx::terminate();
}
};

///////////////////////////////////////////////////////////////////////
// std::is_sorted is not available on all supported platforms yet
template <typename Iter, typename Compare>
Expand Down Expand Up @@ -169,7 +213,22 @@ namespace hpx { namespace parallel { HPX_INLINE_NAMESPACE(v1)
policy, c_first, last, comp
));

return hpx::when_all(std::move(left), std::move(right));
return hpx::lcos::local::dataflow(
[](hpx::future<void> && left, hpx::future<void> && right)
{
if (left.has_exception() || right.has_exception())
{
std::list<boost::exception_ptr> errors;
if (left.has_exception())
errors.push_back(left.get_exception_ptr());
if (right.has_exception())
errors.push_back(right.get_exception_ptr());

boost::throw_exception(
exception_list(std::move(errors)));
}
},
std::move(left), std::move(right));
}

//------------------------------------------------------------------------
Expand Down Expand Up @@ -213,14 +272,14 @@ namespace hpx { namespace parallel { HPX_INLINE_NAMESPACE(v1)
));
}
catch (...) {
return util::detail::handle_local_exception<ExPolicy>::call(
return detail::handle_sort_exception<ExPolicy>::call(
boost::current_exception());
}

if (result.has_exception())
{
return util::detail::handle_local_exception<ExPolicy>::call(
result.get_exception_ptr());
return detail::handle_sort_exception<ExPolicy>::call(
std::move(result));
}

return result;
Expand Down
30 changes: 0 additions & 30 deletions hpx/parallel/util/detail/handle_local_exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,6 @@
///////////////////////////////////////////////////////////////////////////////
namespace hpx { namespace parallel { namespace util { namespace detail
{
///////////////////////////////////////////////////////////////////////////
template <typename ExPolicy>
struct handle_local_exception
{
static hpx::future<void> call(boost::exception_ptr const& e)
{
try {
boost::rethrow_exception(e);
}
catch (std::bad_alloc const&) {
return hpx::make_exceptional_future<void>(
boost::current_exception());
}
catch (...) {
return hpx::make_exceptional_future<exception_list>(
boost::current_exception());
}
}
};

template <>
struct handle_local_exception<parallel_vector_execution_policy>
{
HPX_ATTRIBUTE_NORETURN
static hpx::future<void> call(boost::exception_ptr const&)
{
hpx::terminate();
}
};

///////////////////////////////////////////////////////////////////////////
template <typename ExPolicy>
struct handle_local_exceptions
Expand Down

0 comments on commit 9f4f918

Please sign in to comment.