diff --git a/libcxx/include/__algorithm/pstl_backend.h b/libcxx/include/__algorithm/pstl_backend.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/pstl_backend.h @@ -0,0 +1,91 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___ALGORITHM_PSTL_BACKEND_H +#define _LIBCPP___ALGORITHM_PSTL_BACKEND_H + +#include <__algorithm/pstl_backends/serial.h> +#include <__algorithm/pstl_backends/simd.h> +#include <__config> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17 + +_LIBCPP_BEGIN_NAMESPACE_STD + +/* +TODO: Documentation of how backends work + +A PSTL parallel backend is a tag type to which the following functions are associated, at minimum: + + template + void __for_each(_Backend, _ExecutionPolicy&&, _Iterator __first, _Iterator __last, _Func __f); + + template + _Tp __reduce(_Backend, _ExecutionPolicy&&, _Iterator __first, _Iterator __last, _Tp const& __value, _BinOp __op); + + etc... + +The following functions are optional but can be provided. If provided, they are used by the corresponding +algorithms, otherwise they are implemented in terms of the basis operations mentioned above: + + template + void __for_each_n(_Backend, _ExecutionPolicy&&, _Iterator __first, _Size __n, _Func __f); + + etc... + +*/ + +template +struct __select_backend; + +template <> +struct __select_backend { + using type = __serial_backend; +}; + +template <> +struct __select_backend { + using type = __simd_backend; +}; + +# if defined(_PSTL_PAR_BACKEND_STD_THREAD) +# include <__algorithm/pstl_backends/thread.h> +template <> struct __select_backend { using type = __std_thread_backend; }; +template <> struct __select_backend { using type = __std_thread_backend; }; + +# elif defined(_PSTL_PAR_BACKEND_GCD) +# include <__algorithm/pstl_backends/gcd.h> +template <> struct __select_backend { using type = __gcd_backend; }; +template <> struct __select_backend { using type = __gcd_backend; }; + +# elif defined(_PSTL_PAR_BACKEND_TBB) +# include <__algorithm/pstl_backends/tbb.h> +template <> struct __select_backend { using type = __tbb_backend; }; +template <> struct __select_backend { using type = __tbb_backend; }; + +# elif defined(_PSTL_PAR_BACKEND_SERIAL) +# include <__algorithm/pstl_backends/serial.h> +template <> struct __select_backend { using type = __serial_backend; }; +template <> struct __select_backend { using type = __serial_backend; }; + +# else + +// ...New vendors can add parallel backends here... + +# error "Invalid choice of a PSTL parallel backend" +# endif + +_LIBCPP_END_NAMESPACE_STD + +#endif // defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17 + +#endif // _LIBCPP___ALGORITHM_PSTL_BACKEND_H diff --git a/libcxx/include/__algorithm/pstl_backends/serial.h b/libcxx/include/__algorithm/pstl_backends/serial.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/pstl_backends/serial.h @@ -0,0 +1,57 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___ALGORITHM_PSTL_BACKENDS_SERIAL_H +#define _LIBCPP___ALGORITHM_PSTL_BACKENDS_SERIAL_H + +#include <__config> +#include <__algorithm/for_each.h> +#include <__numeric/reduce.h> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17 + +_LIBCPP_BEGIN_NAMESPACE_STD + +struct __serial_backend { }; + +// +// Mandatory customization points +// +template +_LIBCPP_HIDE_FROM_ABI +void __pstl_for_each(__serial_backend, _Iterator __first, _Iterator __last, _Fp __f) { + std::for_each(__first, __last, __f); +} + +template +_LIBCPP_HIDE_FROM_ABI +_Tp __pstl_reduce(__serial_backend, _Iterator __first, _Iterator __last, _Tp const& __value, _BinOp __op) { + return std::reduce(__first, __last, __value, __op); +} + +// etc... + +// +// Optional customization points +// +template +_LIBCPP_HIDE_FROM_ABI +void __pstl_for_each_n(__serial_backend, _Iterator __first, _Size __n, _Fp __f) { + std::for_each_n(__first, __n, __f); +} + +_LIBCPP_END_NAMESPACE_STD + +#endif // defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17 + +#endif // _LIBCPP___ALGORITHM_PSTL_BACKENDS_SERIAL_H diff --git a/libcxx/include/__algorithm/pstl_backends/thread.h b/libcxx/include/__algorithm/pstl_backends/thread.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/pstl_backends/thread.h @@ -0,0 +1,57 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___ALGORITHM_PSTL_BACKENDS_THREAD_H +#define _LIBCPP___ALGORITHM_PSTL_BACKENDS_THREAD_H + +#include <__config> +#include <__algorithm/for_each.h> +#include <__numeric/reduce.h> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17 + +_LIBCPP_BEGIN_NAMESPACE_STD + +struct __thread_backend { }; + +// +// Mandatory customization points +// +template +_LIBCPP_HIDE_FROM_ABI +void __pstl_for_each(__thread_backend, _Iterator __first, _Iterator __last, _Fp __f) { + static_assert(__is_parallel_policy_v<_ExecutionPolicy>); + auto& __thread_pool = __get_thread_pool(); + std::__terminate_on_exception([&] { + // chunk up the range and dispatch it to the threads + for (TODO) { + __thread_pool.__dispatch([&] { + __simd_utils::__for_each<_ExecutionPolicy>(__brick_first, __brick_last, __f); + }); + } + }); +} + +template +_LIBCPP_HIDE_FROM_ABI +_Tp __pstl_reduce(__thread_backend, _Iterator __first, _Iterator __last, _Tp const& __value, _BinOp __op) { + // TODO: Implement +} + +// etc... + +_LIBCPP_END_NAMESPACE_STD + +#endif // defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17 + +#endif // _LIBCPP___ALGORITHM_PSTL_BACKENDS_THREAD_H diff --git a/libcxx/include/__algorithm/pstl_for_each.h b/libcxx/include/__algorithm/pstl_for_each.h --- a/libcxx/include/__algorithm/pstl_for_each.h +++ b/libcxx/include/__algorithm/pstl_for_each.h @@ -9,6 +9,7 @@ #ifndef _LIBCPP___ALGORITHM_PSTL_FOR_EACH_H #define _LIBCPP___ALGORITHM_PSTL_FOR_EACH_H +#include <__algorithm/pstl_backend.h> #include <__algorithm/for_each.h> #include <__algorithm/for_each_n.h> #include <__config> @@ -30,40 +31,33 @@ template >, int> = 0> + class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>, + enable_if_t, int> = 0> _LIBCPP_HIDE_FROM_ABI void for_each(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Function __func) { - if constexpr (__is_parallel_execution_policy_v<_ExecutionPolicy> && - __is_cpp17_random_access_iterator<_ForwardIterator>::value) { - std::__terminate_on_exception([&] { - __pstl::__par_backend::__parallel_for( - {}, - __policy, - __first, - __last, - [&__policy, __func](_ForwardIterator __brick_first, _ForwardIterator __brick_last) { - std::for_each(std::__remove_parallel_policy(__policy), __brick_first, __brick_last, __func); - }); - }); - } else if constexpr (__is_unsequenced_execution_policy_v<_ExecutionPolicy> && - __is_cpp17_random_access_iterator<_ForwardIterator>::value) { - __pstl::__unseq_backend::__simd_walk_1(__first, __last - __first, __func); - } else { - std::for_each(__first, __last, __func); - } + using _Backend = typename __select_backend<_RawPolicy>::type; + __pstl_for_each<_RawPolicy>(_Backend{}, std::move(__first), std::move(__last), std::move(__func)); // TODO: Can't do ADL } template >, int> = 0> + class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>, + enable_if_t, int> = 0> _LIBCPP_HIDE_FROM_ABI void for_each_n(_ExecutionPolicy&& __policy, _ForwardIterator __first, _Size __size, _Function __func) { - if constexpr (__is_cpp17_random_access_iterator<_ForwardIterator>::value) { - std::for_each(__policy, __first, __first + __size, __func); + using _Backend = typename __select_backend<_RawPolicy>::type; + if constexpr (HAS-FOR_EACH_N(_Backend)) { + __pstl_for_each_n<_RawPolicy>(_Backend{}, std::move(__first), __size, std::move(__func)); } else { - std::for_each_n(__first, __size, __func); + // Default implementation + if constexpr (__is_cpp17_random_access_iterator<_ForwardIterator>::value && + __is_parallel_execution_policy_v<_RawPolicy>) { + std::for_each(__policy, __first, __first + __size, __func); + } else { + std::for_each_n(__first, __size, __func); + } } } diff --git a/libcxx/include/__algorithm/pstl_simd_utils.h b/libcxx/include/__algorithm/pstl_simd_utils.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/pstl_simd_utils.h @@ -0,0 +1,50 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___ALGORITHM_PSTL_SIMD_UTILS_H +#define _LIBCPP___ALGORITHM_PSTL_SIMD_UTILS_H + +#include <__config> +#include <__algorithm/for_each.h> +#include <__numeric/reduce.h> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17 + +_LIBCPP_BEGIN_NAMESPACE_STD + +namespace __simd_utils { + template + _LIBCPP_HIDE_FROM_ABI + void __for_each(_ForwardIterator __first, _ForwardIterator __last, _Func __f) { + if constexpr (__is_unsequenced_policy_v<_ExecutionPolicy> && + __is_cpp17_random_access_iterator<_ForwardIterator>::value) { + __simd_walk_1(__first, __last - __first, __f); + } else { + std::for_each(__first, __last, __f); + } + } + + template + _LIBCPP_HIDE_FROM_ABI + _Tp __reduce(_Iterator __first, _Iterator __last, _Tp const& __value, _BinOp __op) { + // TODO: Implement + } +} + +// etc... + +_LIBCPP_END_NAMESPACE_STD + +#endif // defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17 + +#endif // _LIBCPP___ALGORITHM_PSTL_SIMD_UTILS_H