Skip to content

Commit 7b80384

Browse files
authored
[libc++] Move the implementation of CPU-based basis operations to namespace __pstl (#95267)
They were always intended to be in that namespace but I was trying to keep changes orthogonal.
1 parent abedb3b commit 7b80384

File tree

8 files changed

+41
-25
lines changed

8 files changed

+41
-25
lines changed

libcxx/include/__pstl/cpu_algos/any_of.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@ _LIBCPP_PUSH_MACROS
2929
# include <__undef_macros>
3030

3131
_LIBCPP_BEGIN_NAMESPACE_STD
32+
namespace __pstl {
3233

3334
template <class _Backend, class _Index, class _Brick>
3435
_LIBCPP_HIDE_FROM_ABI optional<bool> __parallel_or(_Index __first, _Index __last, _Brick __f) {
3536
std::atomic<bool> __found(false);
36-
auto __ret = __pstl::__cpu_traits<_Backend>::__for_each(__first, __last, [__f, &__found](_Index __i, _Index __j) {
37+
auto __ret = __cpu_traits<_Backend>::__for_each(__first, __last, [__f, &__found](_Index __i, _Index __j) {
3738
if (!__found.load(std::memory_order_relaxed) && __f(__i, __j)) {
3839
__found.store(true, std::memory_order_relaxed);
39-
__pstl::__cpu_traits<_Backend>::__cancel_execution();
40+
__cpu_traits<_Backend>::__cancel_execution();
4041
}
4142
});
4243
if (!__ret)
@@ -76,7 +77,7 @@ struct __cpu_parallel_any_of {
7677
operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) const noexcept {
7778
if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> &&
7879
__has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
79-
return std::__parallel_or<_Backend>(
80+
return __pstl::__parallel_or<_Backend>(
8081
__first, __last, [&__policy, &__pred](_ForwardIterator __brick_first, _ForwardIterator __brick_last) {
8182
using _AnyOfUnseq = __pstl::__any_of<_Backend, __remove_parallel_policy_t<_RawExecutionPolicy>>;
8283
auto __res = _AnyOfUnseq()(std::__remove_parallel_policy(__policy), __brick_first, __brick_last, __pred);
@@ -85,13 +86,14 @@ struct __cpu_parallel_any_of {
8586
});
8687
} else if constexpr (__is_unsequenced_execution_policy_v<_RawExecutionPolicy> &&
8788
__has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
88-
return std::__simd_or(__first, __last - __first, __pred);
89+
return __pstl::__simd_or(__first, __last - __first, __pred);
8990
} else {
9091
return std::any_of(__first, __last, __pred);
9192
}
9293
}
9394
};
9495

96+
} // namespace __pstl
9597
_LIBCPP_END_NAMESPACE_STD
9698

9799
_LIBCPP_POP_MACROS

libcxx/include/__pstl/cpu_algos/fill.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
2727

2828
_LIBCPP_BEGIN_NAMESPACE_STD
29+
namespace __pstl {
2930

3031
template <class _Index, class _DifferenceType, class _Tp>
3132
_LIBCPP_HIDE_FROM_ABI _Index __simd_fill_n(_Index __first, _DifferenceType __n, const _Tp& __value) noexcept {
@@ -43,7 +44,7 @@ struct __cpu_parallel_fill {
4344
operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) const noexcept {
4445
if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> &&
4546
__has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
46-
return __pstl::__cpu_traits<_Backend>::__for_each(
47+
return __cpu_traits<_Backend>::__for_each(
4748
__first, __last, [&__policy, &__value](_ForwardIterator __brick_first, _ForwardIterator __brick_last) {
4849
using _FillUnseq = __pstl::__fill<_Backend, __remove_parallel_policy_t<_RawExecutionPolicy>>;
4950
[[maybe_unused]] auto __res =
@@ -52,7 +53,7 @@ struct __cpu_parallel_fill {
5253
});
5354
} else if constexpr (__is_unsequenced_execution_policy_v<_RawExecutionPolicy> &&
5455
__has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
55-
std::__simd_fill_n(__first, __last - __first, __value);
56+
__pstl::__simd_fill_n(__first, __last - __first, __value);
5657
return __empty{};
5758
} else {
5859
std::fill(__first, __last, __value);
@@ -61,6 +62,7 @@ struct __cpu_parallel_fill {
6162
}
6263
};
6364

65+
} // namespace __pstl
6466
_LIBCPP_END_NAMESPACE_STD
6567

6668
#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17

libcxx/include/__pstl/cpu_algos/find_if.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ _LIBCPP_PUSH_MACROS
3434
# include <__undef_macros>
3535

3636
_LIBCPP_BEGIN_NAMESPACE_STD
37+
namespace __pstl {
3738

3839
template <class _Backend, class _Index, class _Brick, class _Compare>
3940
_LIBCPP_HIDE_FROM_ABI optional<_Index>
@@ -43,8 +44,8 @@ __parallel_find(_Index __first, _Index __last, _Brick __f, _Compare __comp, bool
4344
_DifferenceType __initial_dist = __b_first ? __n : -1;
4445
std::atomic<_DifferenceType> __extremum(__initial_dist);
4546
// TODO: find out what is better here: parallel_for or parallel_reduce
46-
auto __res = __pstl::__cpu_traits<_Backend>::__for_each(
47-
__first, __last, [__comp, __f, __first, &__extremum](_Index __i, _Index __j) {
47+
auto __res =
48+
__cpu_traits<_Backend>::__for_each(__first, __last, [__comp, __f, __first, &__extremum](_Index __i, _Index __j) {
4849
// See "Reducing Contention Through Priority Updates", PPoPP '13, for discussion of
4950
// why using a shared variable scales fairly well in this situation.
5051
if (__comp(__i - __first, __extremum)) {
@@ -67,8 +68,8 @@ template <class _Backend, class _Index, class _DifferenceType, class _Compare>
6768
_LIBCPP_HIDE_FROM_ABI _Index
6869
__simd_first(_Index __first, _DifferenceType __begin, _DifferenceType __end, _Compare __comp) noexcept {
6970
// Experiments show good block sizes like this
70-
const _DifferenceType __block_size = 8;
71-
alignas(__pstl::__cpu_traits<_Backend>::__lane_size) _DifferenceType __lane[__block_size] = {0};
71+
const _DifferenceType __block_size = 8;
72+
alignas(__cpu_traits<_Backend>::__lane_size) _DifferenceType __lane[__block_size] = {0};
7273
while (__end - __begin >= __block_size) {
7374
_DifferenceType __found = 0;
7475
_PSTL_PRAGMA_SIMD_REDUCTION(| : __found) for (_DifferenceType __i = __begin; __i < __begin + __block_size; ++__i) {
@@ -106,7 +107,7 @@ struct __cpu_parallel_find_if {
106107
operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) const noexcept {
107108
if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> &&
108109
__has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
109-
return std::__parallel_find<_Backend>(
110+
return __pstl::__parallel_find<_Backend>(
110111
__first,
111112
__last,
112113
[&__policy, &__pred](_ForwardIterator __brick_first, _ForwardIterator __brick_last) {
@@ -120,7 +121,7 @@ struct __cpu_parallel_find_if {
120121
} else if constexpr (__is_unsequenced_execution_policy_v<_RawExecutionPolicy> &&
121122
__has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
122123
using __diff_t = __iter_diff_t<_ForwardIterator>;
123-
return std::__simd_first<_Backend>(
124+
return __pstl::__simd_first<_Backend>(
124125
__first, __diff_t(0), __last - __first, [&__pred](_ForwardIterator __iter, __diff_t __i) {
125126
return __pred(__iter[__i]);
126127
});
@@ -130,6 +131,7 @@ struct __cpu_parallel_find_if {
130131
}
131132
};
132133

134+
} // namespace __pstl
133135
_LIBCPP_END_NAMESPACE_STD
134136

135137
_LIBCPP_POP_MACROS

libcxx/include/__pstl/cpu_algos/for_each.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
2727

2828
_LIBCPP_BEGIN_NAMESPACE_STD
29+
namespace __pstl {
2930

3031
template <class _Iterator, class _DifferenceType, class _Function>
3132
_LIBCPP_HIDE_FROM_ABI _Iterator __simd_for_each(_Iterator __first, _DifferenceType __n, _Function __f) noexcept {
@@ -43,7 +44,7 @@ struct __cpu_parallel_for_each {
4344
operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Function __func) const noexcept {
4445
if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> &&
4546
__has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
46-
return __pstl::__cpu_traits<_Backend>::__for_each(
47+
return __cpu_traits<_Backend>::__for_each(
4748
__first, __last, [&__policy, __func](_ForwardIterator __brick_first, _ForwardIterator __brick_last) {
4849
using _ForEachUnseq = __pstl::__for_each<_Backend, __remove_parallel_policy_t<_RawExecutionPolicy>>;
4950
[[maybe_unused]] auto __res =
@@ -52,7 +53,7 @@ struct __cpu_parallel_for_each {
5253
});
5354
} else if constexpr (__is_unsequenced_execution_policy_v<_RawExecutionPolicy> &&
5455
__has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
55-
std::__simd_for_each(__first, __last - __first, __func);
56+
__pstl::__simd_for_each(__first, __last - __first, __func);
5657
return __empty{};
5758
} else {
5859
std::for_each(__first, __last, __func);
@@ -61,6 +62,7 @@ struct __cpu_parallel_for_each {
6162
}
6263
};
6364

65+
} // namespace __pstl
6466
_LIBCPP_END_NAMESPACE_STD
6567

6668
#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17

libcxx/include/__pstl/cpu_algos/merge.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ _LIBCPP_PUSH_MACROS
2929
# include <__undef_macros>
3030

3131
_LIBCPP_BEGIN_NAMESPACE_STD
32+
namespace __pstl {
3233

3334
template <class _Backend, class _RawExecutionPolicy>
3435
struct __cpu_parallel_merge {
@@ -45,7 +46,7 @@ struct __cpu_parallel_merge {
4546
__has_random_access_iterator_category_or_concept<_ForwardIterator1>::value &&
4647
__has_random_access_iterator_category_or_concept<_ForwardIterator2>::value &&
4748
__has_random_access_iterator_category_or_concept<_ForwardOutIterator>::value) {
48-
auto __res = __pstl::__cpu_traits<_Backend>::__merge(
49+
auto __res = __cpu_traits<_Backend>::__merge(
4950
__first1,
5051
__last1,
5152
__first2,
@@ -78,6 +79,7 @@ struct __cpu_parallel_merge {
7879
}
7980
};
8081

82+
} // namespace __pstl
8183
_LIBCPP_END_NAMESPACE_STD
8284

8385
_LIBCPP_POP_MACROS

libcxx/include/__pstl/cpu_algos/stable_sort.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@
2424
#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
2525

2626
_LIBCPP_BEGIN_NAMESPACE_STD
27+
namespace __pstl {
2728

2829
template <class _Backend, class _RawExecutionPolicy>
2930
struct __cpu_parallel_stable_sort {
3031
template <class _Policy, class _RandomAccessIterator, class _Comp>
3132
_LIBCPP_HIDE_FROM_ABI optional<__empty>
3233
operator()(_Policy&&, _RandomAccessIterator __first, _RandomAccessIterator __last, _Comp __comp) const noexcept {
3334
if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy>) {
34-
return __pstl::__cpu_traits<_Backend>::__stable_sort(
35+
return __cpu_traits<_Backend>::__stable_sort(
3536
__first, __last, __comp, [](_RandomAccessIterator __g_first, _RandomAccessIterator __g_last, _Comp __g_comp) {
3637
std::stable_sort(__g_first, __g_last, __g_comp);
3738
});
@@ -42,6 +43,7 @@ struct __cpu_parallel_stable_sort {
4243
}
4344
};
4445

46+
} // namespace __pstl
4547
_LIBCPP_END_NAMESPACE_STD
4648

4749
#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17

libcxx/include/__pstl/cpu_algos/transform.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ _LIBCPP_PUSH_MACROS
3030
# include <__undef_macros>
3131

3232
_LIBCPP_BEGIN_NAMESPACE_STD
33+
namespace __pstl {
3334

3435
template <class _Iterator1, class _DifferenceType, class _Iterator2, class _Function>
3536
_LIBCPP_HIDE_FROM_ABI _Iterator2
@@ -61,7 +62,7 @@ struct __cpu_parallel_transform {
6162
if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> &&
6263
__has_random_access_iterator_category_or_concept<_ForwardIterator>::value &&
6364
__has_random_access_iterator_category_or_concept<_ForwardOutIterator>::value) {
64-
__pstl::__cpu_traits<_Backend>::__for_each(
65+
__cpu_traits<_Backend>::__for_each(
6566
__first,
6667
__last,
6768
[&__policy, __op, __first, __result](_ForwardIterator __brick_first, _ForwardIterator __brick_last) {
@@ -79,7 +80,7 @@ struct __cpu_parallel_transform {
7980
} else if constexpr (__is_unsequenced_execution_policy_v<_RawExecutionPolicy> &&
8081
__has_random_access_iterator_category_or_concept<_ForwardIterator>::value &&
8182
__has_random_access_iterator_category_or_concept<_ForwardOutIterator>::value) {
82-
return std::__simd_transform(
83+
return __pstl::__simd_transform(
8384
__first,
8485
__last - __first,
8586
__result,
@@ -110,7 +111,7 @@ struct __cpu_parallel_transform_binary {
110111
__has_random_access_iterator_category_or_concept<_ForwardIterator1>::value &&
111112
__has_random_access_iterator_category_or_concept<_ForwardIterator2>::value &&
112113
__has_random_access_iterator_category_or_concept<_ForwardOutIterator>::value) {
113-
auto __res = __pstl::__cpu_traits<_Backend>::__for_each(
114+
auto __res = __cpu_traits<_Backend>::__for_each(
114115
__first1,
115116
__last1,
116117
[&__policy, __op, __first1, __first2, __result](
@@ -132,7 +133,7 @@ struct __cpu_parallel_transform_binary {
132133
__has_random_access_iterator_category_or_concept<_ForwardIterator1>::value &&
133134
__has_random_access_iterator_category_or_concept<_ForwardIterator2>::value &&
134135
__has_random_access_iterator_category_or_concept<_ForwardOutIterator>::value) {
135-
return std::__simd_transform(
136+
return __pstl::__simd_transform(
136137
__first1,
137138
__last1 - __first1,
138139
__first2,
@@ -146,6 +147,7 @@ struct __cpu_parallel_transform_binary {
146147
}
147148
};
148149

150+
} // namespace __pstl
149151
_LIBCPP_END_NAMESPACE_STD
150152

151153
_LIBCPP_POP_MACROS

libcxx/include/__pstl/cpu_algos/transform_reduce.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ _LIBCPP_PUSH_MACROS
3434
#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
3535

3636
_LIBCPP_BEGIN_NAMESPACE_STD
37+
namespace __pstl {
3738

3839
template <typename _Backend,
3940
typename _DifferenceType,
@@ -63,7 +64,7 @@ template <typename _Backend,
6364
int> = 0>
6465
_LIBCPP_HIDE_FROM_ABI _Tp
6566
__simd_transform_reduce(_Size __n, _Tp __init, _BinaryOperation __binary_op, _UnaryOperation __f) noexcept {
66-
constexpr size_t __lane_size = __pstl::__cpu_traits<_Backend>::__lane_size;
67+
constexpr size_t __lane_size = __cpu_traits<_Backend>::__lane_size;
6768
const _Size __block_size = __lane_size / sizeof(_Tp);
6869
if (__n > 2 * __block_size && __block_size > 1) {
6970
alignas(__lane_size) char __lane_buffer[__lane_size];
@@ -124,7 +125,7 @@ struct __cpu_parallel_transform_reduce_binary {
124125
if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> &&
125126
__has_random_access_iterator_category_or_concept<_ForwardIterator1>::value &&
126127
__has_random_access_iterator_category_or_concept<_ForwardIterator2>::value) {
127-
return __pstl::__cpu_traits<_Backend>::__transform_reduce(
128+
return __cpu_traits<_Backend>::__transform_reduce(
128129
__first1,
129130
std::move(__last1),
130131
[__first1, __first2, __transform](_ForwardIterator1 __iter) {
@@ -148,7 +149,7 @@ struct __cpu_parallel_transform_reduce_binary {
148149
} else if constexpr (__is_unsequenced_execution_policy_v<_RawExecutionPolicy> &&
149150
__has_random_access_iterator_category_or_concept<_ForwardIterator1>::value &&
150151
__has_random_access_iterator_category_or_concept<_ForwardIterator2>::value) {
151-
return std::__simd_transform_reduce<_Backend>(
152+
return __pstl::__simd_transform_reduce<_Backend>(
152153
__last1 - __first1, std::move(__init), std::move(__reduce), [&](__iter_diff_t<_ForwardIterator1> __i) {
153154
return __transform(__first1[__i], __first2[__i]);
154155
});
@@ -176,7 +177,7 @@ struct __cpu_parallel_transform_reduce {
176177
_UnaryOperation __transform) const noexcept {
177178
if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> &&
178179
__has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
179-
return __pstl::__cpu_traits<_Backend>::__transform_reduce(
180+
return __cpu_traits<_Backend>::__transform_reduce(
180181
std::move(__first),
181182
std::move(__last),
182183
[__transform](_ForwardIterator __iter) { return __transform(*__iter); },
@@ -197,7 +198,7 @@ struct __cpu_parallel_transform_reduce {
197198
});
198199
} else if constexpr (__is_unsequenced_execution_policy_v<_RawExecutionPolicy> &&
199200
__has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
200-
return std::__simd_transform_reduce<_Backend>(
201+
return __pstl::__simd_transform_reduce<_Backend>(
201202
__last - __first,
202203
std::move(__init),
203204
std::move(__reduce),
@@ -209,6 +210,7 @@ struct __cpu_parallel_transform_reduce {
209210
}
210211
};
211212

213+
} // namespace __pstl
212214
_LIBCPP_END_NAMESPACE_STD
213215

214216
#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17

0 commit comments

Comments
 (0)