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,109 @@ +//===----------------------------------------------------------------------===// +// +// 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 <__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 type that provides the following static member functions +at minimum: + + template + static void __for_each(_ExecutionPolicy&&, _Iterator __first, _Iterator __last, _BlockFp __f); + + template + static _Tp __reduce(_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 + static void __for_each_n(_ExecutionPolicy&&, _Iterator __first, _Size __n, _Fp __f); + + etc... + +*/ + +# if defined(_PSTL_PAR_BACKEND_STD_THREAD) +# include <__algorithm/pstl_backends/thread.h> +using __pstl_par_backend = __std_thread_backend; + +# elif defined(_PSTL_PAR_BACKEND_GCD) +# include <__algorithm/pstl_backends/gcd.h> +using __pstl_par_backend = __gcd_backend; + +# elif defined(_PSTL_PAR_BACKEND_TBB) +# include <__algorithm/pstl_backends/tbb.h> +using __pstl_par_backend = __tbb_backend; + +# elif defined(_PSTL_PAR_BACKEND_SERIAL) +# include <__algorithm/pstl_backends/serial.h> +using __pstl_par_backend = __par_serial_backend; + +# else + +// ...New vendors can add parallel backends here... + +# error "Invalid choice of a PSTL parallel backend" +# endif + +/* +TODO: Documentation of how backends work + +A PSTL unsequenced backend is a type that provides the following static member functions +at minimum: + + template + static void ???? + + 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 + static void ???? + + etc... + +*/ + +# if defined(_PSTL_UNSEQ_BACKEND_SIMD) +# include <__algorithm/pstl_unseq_backends/simd.h> +using __pstl_unseq_backend = __simd_backend; + +# elif defined(_PSTL_UNSEQ_BACKEND_SERIAL) +# include <__algorithm/pstl_unseq_backends/serial.h> +using __pstl_unseq_backend = __unseq_serial_backend; + +# else + +// ...New vendors can add unsequenced backends here... + +# error "Invalid choice of a PSTL unsequenced 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,53 @@ +// -*- 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 { + template + _LIBCPP_HIDE_FROM_ABI + static void __for_each(_ExecutionPolicy&&, _Iterator __first, _Iterator __last, _Fp __f) { + std::for_each(__first, __last, __f); + } + + template + _LIBCPP_HIDE_FROM_ABI + static _Tp __reduce(_ExecutionPolicy&&, _Iterator __first, _Iterator __last, _Tp const& __value, _BinOp __op) { + return std::reduce(__first, __last, __value, __op); + } + + // etc... + + + // Let's say we decide to implement __for_each_n here. It needs to be picked up by std::for_each_n(policy, ...) + template + _LIBCPP_HIDE_FROM_ABI + static void __for_each_n(_ExecutionPolicy&&, _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_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> @@ -32,11 +33,22 @@ enable_if_t>, int> = 0> _LIBCPP_HIDE_FROM_ABI void for_each(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Function __func) { + // Possibilities: + + // + // 1. Dispatch right here to the backend, let the backend implement the whole thing. + // If we do this, I guess we don't want a separate notion of parallel backend and a + // notion of unsequenced backend? + // + std::__pstl_backend::__for_each(__policy, __first, __last, __func); + + // + // 2. Deconstruct the parallel policy and call the various backends separately. + // if constexpr (__is_parallel_execution_policy_v<_ExecutionPolicy> && __is_cpp17_random_access_iterator<_ForwardIterator>::value) { std::__terminate_on_exception([&] { - __pstl::__par_backend::__parallel_for( - {}, + std::__pstl_par_backend::__for_each( __policy, __first, __last, @@ -46,7 +58,7 @@ }); } 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); + std::__pstl_unseq_backend::__for_each(__first, __last, __func); } else { std::for_each(__first, __last, __func); } @@ -59,6 +71,8 @@ enable_if_t>, int> = 0> _LIBCPP_HIDE_FROM_ABI void for_each_n(_ExecutionPolicy&& __policy, _ForwardIterator __first, _Size __size, _Function __func) { + // TODO: Here we need to detect whether the back-end supports __for_each_n, and if so, call that? + if constexpr (__is_cpp17_random_access_iterator<_ForwardIterator>::value) { std::for_each(__policy, __first, __first + __size, __func); } else {