https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110512
Bug ID: 110512
Summary: C++20 random access iterators run sequentially with
PSTL
Product: gcc
Version: 14.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: gonzalo.gadeschi at gmail dot com
Target Milestone: ---
See https://github.com/llvm/llvm-project/issues/63447
The problem is the following:
std::vector<int> x(n), y(n);
auto ids = std::views::iota((int)0, (int)x.size());
std::for_each(std::execution::par_unseq, ids.begin(), ids.end(), [x = x.data(),
y = y.data()](int i) {
x[i] = y[i];
});
Iterators from C++20 ranges model the C++20 random_access_iterator concept, but
do not necessarily have a random access iterator tag. They are not recognized
by the PSTL as random access iterators (but forward iterators), causing the
parallel algorithms to fall back to sequential execution.
This is significantly impacting the performance a couple of large HPC
applications.
A quick and dirty workaround is to modify pstl/executors_impls.hpp by changing
the random_access_iterator<IteratorType> to:
template <typename _IteratorType>
struct __is_random_access_iterator<_IteratorType> {
static constexpr bool value = (
(bool)std::is_same_v<typename
std::iterator_traits<_IteratorType>::iterator_category,
std::random_access_iterator_tag>
|| (bool)::std::random_access_iterator<_IteratorType>
);
typedef std::integral_constant<bool, value> type;
};
Since llvm-project/pstl has been forked by libc++, it does no longer make sense
to try to patch pstl upstream to fix this issue.