Skip to content

[libc++] Overhaul the PSTL dispatching mechanism #88131

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

Merged
merged 11 commits into from
Jun 12, 2024
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
1 change: 1 addition & 0 deletions libcxx/benchmarks/algorithms/pstl.stable_sort.bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include <algorithm>
#include <execution>

#include "common.h"

Expand Down
8 changes: 5 additions & 3 deletions libcxx/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ set(files
__algorithm/pop_heap.h
__algorithm/prev_permutation.h
__algorithm/pstl.h
__algorithm/pstl_frontend_dispatch.h
__algorithm/push_heap.h
__algorithm/ranges_adjacent_find.h
__algorithm/ranges_all_of.h
Expand Down Expand Up @@ -570,11 +569,12 @@ set(files
__numeric/transform_reduce.h
__ostream/basic_ostream.h
__ostream/print.h
__pstl/backend.h
__pstl/backend_fwd.h
__pstl/backends/default.h
__pstl/backends/libdispatch.h
__pstl/backends/serial.h
__pstl/backends/std_thread.h
__pstl/configuration.h
__pstl/configuration_fwd.h
__pstl/cpu_algos/any_of.h
__pstl/cpu_algos/cpu_traits.h
__pstl/cpu_algos/fill.h
Expand All @@ -584,6 +584,8 @@ set(files
__pstl/cpu_algos/stable_sort.h
__pstl/cpu_algos/transform.h
__pstl/cpu_algos/transform_reduce.h
__pstl/dispatch.h
__pstl/handle_exception.h
__random/bernoulli_distribution.h
__random/binomial_distribution.h
__random/cauchy_distribution.h
Expand Down
1,142 changes: 219 additions & 923 deletions libcxx/include/__algorithm/pstl.h

Large diffs are not rendered by default.

44 changes: 0 additions & 44 deletions libcxx/include/__algorithm/pstl_frontend_dispatch.h

This file was deleted.

198 changes: 66 additions & 132 deletions libcxx/include/__numeric/pstl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@
#ifndef _LIBCPP___NUMERIC_PSTL_H
#define _LIBCPP___NUMERIC_PSTL_H

#include <__algorithm/pstl_frontend_dispatch.h>
#include <__config>
#include <__functional/identity.h>
#include <__functional/operations.h>
#include <__iterator/cpp17_iterator_concepts.h>
#include <__iterator/iterator_traits.h>
#include <__numeric/transform_reduce.h>
#include <__pstl/configuration.h>
#include <__pstl/backend.h>
#include <__pstl/dispatch.h>
#include <__pstl/handle_exception.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/is_execution_policy.h>
#include <__type_traits/remove_cvref.h>
#include <__utility/forward.h>
#include <__utility/move.h>
#include <optional>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
Expand All @@ -33,30 +35,50 @@ _LIBCPP_PUSH_MACROS
_LIBCPP_BEGIN_NAMESPACE_STD

template <class _ExecutionPolicy,
class _ForwardIterator1,
class _ForwardIterator2,
class _ForwardIterator,
class _Tp,
class _BinaryOperation1,
class _BinaryOperation2,
class _BinaryOperation,
class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
_LIBCPP_HIDE_FROM_ABI optional<_Tp> __transform_reduce(
_ExecutionPolicy&&,
_ForwardIterator1&& __first1,
_ForwardIterator1&& __last1,
_ForwardIterator2&& __first2,
_Tp&& __init,
_BinaryOperation1&& __reduce,
_BinaryOperation2&& __transform) noexcept {
using _Backend = typename __select_backend<_RawPolicy>::type;
return std::__pstl_transform_reduce<_RawPolicy>(
_Backend{},
std::move(__first1),
std::move(__last1),
std::move(__first2),
_LIBCPP_HIDE_FROM_ABI _Tp reduce(
_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Tp __init, _BinaryOperation __op) {
_LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator, "reduce requires ForwardIterators");
using _Implementation = __pstl::__dispatch<__pstl::__reduce, __pstl::__current_configuration, _RawPolicy>;
return __pstl::__handle_exception<_Implementation>(
std::forward<_ExecutionPolicy>(__policy),
std::move(__first),
std::move(__last),
std::move(__init),
std::move(__reduce),
std::move(__transform));
std::move(__op));
}

template <class _ExecutionPolicy,
class _ForwardIterator,
class _Tp,
class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
_LIBCPP_HIDE_FROM_ABI _Tp
reduce(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Tp __init) {
_LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator, "reduce requires ForwardIterators");
using _Implementation = __pstl::__dispatch<__pstl::__reduce, __pstl::__current_configuration, _RawPolicy>;
return __pstl::__handle_exception<_Implementation>(
std::forward<_ExecutionPolicy>(__policy), std::move(__first), std::move(__last), std::move(__init), plus{});
}

template <class _ExecutionPolicy,
class _ForwardIterator,
class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
_LIBCPP_HIDE_FROM_ABI __iter_value_type<_ForwardIterator>
reduce(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last) {
_LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator, "reduce requires ForwardIterators");
using _Implementation = __pstl::__dispatch<__pstl::__reduce, __pstl::__current_configuration, _RawPolicy>;
return __pstl::__handle_exception<_Implementation>(
std::forward<_ExecutionPolicy>(__policy),
std::move(__first),
std::move(__last),
__iter_value_type<_ForwardIterator>(),
plus{});
}

template <class _ExecutionPolicy,
Expand All @@ -77,18 +99,16 @@ _LIBCPP_HIDE_FROM_ABI _Tp transform_reduce(
_BinaryOperation2 __transform) {
_LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator1, "transform_reduce requires ForwardIterators");
_LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator2, "transform_reduce requires ForwardIterators");
auto __res = std::__transform_reduce(
__policy,
using _Implementation =
__pstl::__dispatch<__pstl::__transform_reduce_binary, __pstl::__current_configuration, _RawPolicy>;
return __pstl::__handle_exception<_Implementation>(
std::forward<_ExecutionPolicy>(__policy),
std::move(__first1),
std::move(__last1),
std::move(__first2),
std::move(__init),
std::move(__reduce),
std::move(__transform));

if (!__res)
std::__throw_bad_alloc();
return *std::move(__res);
}

// This overload doesn't get a customization point because it's trivial to detect (through e.g.
Expand All @@ -97,7 +117,8 @@ template <class _ExecutionPolicy,
class _ForwardIterator1,
class _ForwardIterator2,
class _Tp,
enable_if_t<is_execution_policy_v<__remove_cvref_t<_ExecutionPolicy>>, int> = 0>
class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
_LIBCPP_HIDE_FROM_ABI _Tp transform_reduce(
_ExecutionPolicy&& __policy,
_ForwardIterator1 __first1,
Expand All @@ -106,31 +127,16 @@ _LIBCPP_HIDE_FROM_ABI _Tp transform_reduce(
_Tp __init) {
_LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator1, "transform_reduce requires ForwardIterators");
_LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator2, "transform_reduce requires ForwardIterators");
return std::transform_reduce(__policy, __first1, __last1, __first2, __init, plus{}, multiplies{});
}

template <class _ExecutionPolicy,
class _ForwardIterator,
class _Tp,
class _BinaryOperation,
class _UnaryOperation,
class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__remove_cvref_t<_Tp>> __transform_reduce(
_ExecutionPolicy&&,
_ForwardIterator&& __first,
_ForwardIterator&& __last,
_Tp&& __init,
_BinaryOperation&& __reduce,
_UnaryOperation&& __transform) noexcept {
using _Backend = typename __select_backend<_RawPolicy>::type;
return std::__pstl_transform_reduce<_RawPolicy>(
_Backend{},
std::move(__first),
std::move(__last),
using _Implementation =
__pstl::__dispatch<__pstl::__transform_reduce_binary, __pstl::__current_configuration, _RawPolicy>;
return __pstl::__handle_exception<_Implementation>(
std::forward<_ExecutionPolicy>(__policy),
std::move(__first1),
std::move(__last1),
std::move(__first2),
std::move(__init),
std::move(__reduce),
std::move(__transform));
plus{},
multiplies{});
}

template <class _ExecutionPolicy,
Expand All @@ -148,86 +154,14 @@ _LIBCPP_HIDE_FROM_ABI _Tp transform_reduce(
_BinaryOperation __reduce,
_UnaryOperation __transform) {
_LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator, "transform_reduce requires ForwardIterators");
auto __res = std::__transform_reduce(
__policy, std::move(__first), std::move(__last), std::move(__init), std::move(__reduce), std::move(__transform));
if (!__res)
std::__throw_bad_alloc();
return *std::move(__res);
}

template <class>
void __pstl_reduce();

template <class _ExecutionPolicy,
class _ForwardIterator,
class _Tp,
class _BinaryOperation = plus<>,
class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<_Tp>
__reduce(_ExecutionPolicy&& __policy,
_ForwardIterator&& __first,
_ForwardIterator&& __last,
_Tp&& __init,
_BinaryOperation&& __op = {}) noexcept {
return std::__pstl_frontend_dispatch(
_LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_reduce, _RawPolicy),
[&__policy](_ForwardIterator __g_first, _ForwardIterator __g_last, _Tp __g_init, _BinaryOperation __g_op) {
return std::__transform_reduce(
__policy, std::move(__g_first), std::move(__g_last), std::move(__g_init), std::move(__g_op), __identity{});
},
using _Implementation = __pstl::__dispatch<__pstl::__transform_reduce, __pstl::__current_configuration, _RawPolicy>;
return __pstl::__handle_exception<_Implementation>(
std::forward<_ExecutionPolicy>(__policy),
std::move(__first),
std::move(__last),
std::move(__init),
std::move(__op));
}

template <class _ExecutionPolicy,
class _ForwardIterator,
class _Tp,
class _BinaryOperation = plus<>,
class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
_LIBCPP_HIDE_FROM_ABI _Tp
reduce(_ExecutionPolicy&& __policy,
_ForwardIterator __first,
_ForwardIterator __last,
_Tp __init,
_BinaryOperation __op = {}) {
_LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator, "reduce requires ForwardIterators");
auto __res = std::__reduce(__policy, std::move(__first), std::move(__last), std::move(__init), std::move(__op));
if (!__res)
std::__throw_bad_alloc();
return *std::move(__res);
}

template <class _ExecutionPolicy,
class _ForwardIterator,
class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__iter_value_type<_ForwardIterator>>
__reduce(_ExecutionPolicy&& __policy, _ForwardIterator&& __first, _ForwardIterator&& __last) noexcept {
return std::__pstl_frontend_dispatch(
_LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_reduce, _RawPolicy),
[&__policy](_ForwardIterator __g_first, _ForwardIterator __g_last) {
return std::__reduce(
__policy, std::move(__g_first), std::move(__g_last), __iter_value_type<_ForwardIterator>());
},
std::move(__first),
std::move(__last));
}

template <class _ExecutionPolicy,
class _ForwardIterator,
class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
_LIBCPP_HIDE_FROM_ABI __iter_value_type<_ForwardIterator>
reduce(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last) {
_LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator, "reduce requires ForwardIterators");
auto __res = std::__reduce(__policy, std::move(__first), std::move(__last));
if (!__res)
std::__throw_bad_alloc();
return *std::move(__res);
std::move(__reduce),
std::move(__transform));
}

_LIBCPP_END_NAMESPACE_STD
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,30 @@
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCPP___PSTL_CONFIGURATION_H
#define _LIBCPP___PSTL_CONFIGURATION_H
#ifndef _LIBCPP___PSTL_BACKEND_H
#define _LIBCPP___PSTL_BACKEND_H

#include <__config>
#include <__pstl/configuration_fwd.h>
#include <__pstl/backend_fwd.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif

_LIBCPP_PUSH_MACROS
#include <__undef_macros>

#if defined(_LIBCPP_PSTL_BACKEND_SERIAL)
# include <__pstl/backends/default.h>
# include <__pstl/backends/serial.h>
#elif defined(_LIBCPP_PSTL_BACKEND_STD_THREAD)
# include <__pstl/backends/default.h>
# include <__pstl/backends/std_thread.h>
#elif defined(_LIBCPP_PSTL_BACKEND_LIBDISPATCH)
# include <__pstl/backends/default.h>
# include <__pstl/backends/libdispatch.h>
#endif

#endif // _LIBCPP___PSTL_CONFIGURATION_H
_LIBCPP_POP_MACROS

#endif // _LIBCPP___PSTL_BACKEND_H
Loading
Loading