Skip to content

Commit

Permalink
[libc++][PSTL] Implement std::fill{,_n}
Browse files Browse the repository at this point in the history
Reviewed By: ldionne, #libc

Spies: libcxx-commits

Differential Revision: https://reviews.llvm.org/D149540
  • Loading branch information
philnik777 committed May 1, 2023
1 parent cb3cb41 commit ade9c3b
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 110 deletions.
1 change: 1 addition & 0 deletions libcxx/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ set(files
__algorithm/pop_heap.h
__algorithm/prev_permutation.h
__algorithm/pstl_any_all_none_of.h
__algorithm/pstl_fill.h
__algorithm/pstl_find.h
__algorithm/pstl_for_each.h
__algorithm/push_heap.h
Expand Down
71 changes: 71 additions & 0 deletions libcxx/include/__algorithm/pstl_fill.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//===----------------------------------------------------------------------===//
//
// 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_FILL_H
#define _LIBCPP___ALGORITHM_PSTL_FILL_H

#include <__algorithm/fill.h>
#include <__config>
#include <__iterator/iterator_traits.h>
#include <__pstl/internal/parallel_impl.h>
#include <__pstl/internal/unseq_backend_simd.h>
#include <__type_traits/is_execution_policy.h>
#include <__utility/terminate_on_exception.h>

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

#if _LIBCPP_STD_VER >= 17

_LIBCPP_BEGIN_NAMESPACE_STD

template <class _ExecutionPolicy,
class _ForwardIterator,
class _Tp,
enable_if_t<is_execution_policy_v<__remove_cvref_t<_ExecutionPolicy>>, int> = 0>
_LIBCPP_HIDE_FROM_ABI void
fill(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
if constexpr (__is_parallel_execution_policy_v<_ExecutionPolicy> &&
__is_cpp17_random_access_iterator<_ForwardIterator>::value) {
std::__terminate_on_exception([&] {
__pstl::__par_backend::__parallel_for(
__pstl::__internal::__par_backend_tag{},
__policy,
__first,
__last,
[&__policy, &__value](_ForwardIterator __brick_first, _ForwardIterator __brick_last) {
std::fill(std::__remove_parallel_policy(__policy), __brick_first, __brick_last, __value);
});
});
} else if constexpr (__is_unsequenced_execution_policy_v<_ExecutionPolicy> &&
__is_cpp17_random_access_iterator<_ForwardIterator>::value) {
__pstl::__unseq_backend::__simd_fill_n(__first, __last - __first, __value);
} else {
std::fill(__first, __last, __value);
}
}

template <class _ExecutionPolicy,
class _ForwardIterator,
class _SizeT,
class _Tp,
enable_if_t<is_execution_policy_v<__remove_cvref_t<_ExecutionPolicy>>, int> = 0>
_LIBCPP_HIDE_FROM_ABI void
fill_n(_ExecutionPolicy&& __policy, _ForwardIterator __first, _SizeT __n, const _Tp& __value) {
if constexpr (__is_cpp17_random_access_iterator<_ForwardIterator>::value)
std::fill(__policy, __first, __first + __n, __value);
else
std::fill_n(__first, __n, __value);
}

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP_STD_VER >= 17

#endif // _LIBCPP___ALGORITHM_PSTL_FILL_H
78 changes: 0 additions & 78 deletions libcxx/include/__pstl/internal/algorithm_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2731,84 +2731,6 @@ void __pattern_nth_element(
} while (__x != __nth);
}

//------------------------------------------------------------------------
// fill, fill_n
//------------------------------------------------------------------------
template <class _RandomAccessIterator, class _Tp>
void __brick_fill(_RandomAccessIterator __first,
_RandomAccessIterator __last,
const _Tp& __value,
/* __is_vector = */ std::true_type) noexcept {
__unseq_backend::__simd_fill_n(__first, __last - __first, __value);
}

template <class _ForwardIterator, class _Tp>
void __brick_fill(_ForwardIterator __first,
_ForwardIterator __last,
const _Tp& __value,
/* __is_vector = */ std::false_type) noexcept {
std::fill(__first, __last, __value);
}

template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _Tp>
void __pattern_fill(
_Tag, _ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) noexcept {
__internal::__brick_fill(__first, __last, __value, typename _Tag::__is_vector{});
}

template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Tp>
_RandomAccessIterator __pattern_fill(
__parallel_tag<_IsVector> __tag,
_ExecutionPolicy&& __exec,
_RandomAccessIterator __first,
_RandomAccessIterator __last,
const _Tp& __value) {
using __backend_tag = typename decltype(__tag)::__backend_tag;

return __internal::__except_handler([&__exec, __first, __last, &__value]() {
__par_backend::__parallel_for(
__backend_tag{},
std::forward<_ExecutionPolicy>(__exec),
__first,
__last,
[&__value](_RandomAccessIterator __begin, _RandomAccessIterator __end) {
__internal::__brick_fill(__begin, __end, __value, _IsVector{});
});
return __last;
});
}

template <class _RandomAccessIterator, class _Size, class _Tp>
_RandomAccessIterator
__brick_fill_n(_RandomAccessIterator __first,
_Size __count,
const _Tp& __value,
/* __is_vector = */ std::true_type) noexcept {
return __unseq_backend::__simd_fill_n(__first, __count, __value);
}

template <class _OutputIterator, class _Size, class _Tp>
_OutputIterator __brick_fill_n(
_OutputIterator __first, _Size __count, const _Tp& __value, /* __is_vector = */ std::false_type) noexcept {
return std::fill_n(__first, __count, __value);
}

template <class _Tag, class _ExecutionPolicy, class _OutputIterator, class _Size, class _Tp>
_OutputIterator
__pattern_fill_n(_Tag, _ExecutionPolicy&&, _OutputIterator __first, _Size __count, const _Tp& __value) noexcept {
return __internal::__brick_fill_n(__first, __count, __value, typename _Tag::__is_vector{});
}

template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Size, class _Tp>
_RandomAccessIterator __pattern_fill_n(
__parallel_tag<_IsVector> __tag,
_ExecutionPolicy&& __exec,
_RandomAccessIterator __first,
_Size __count,
const _Tp& __value) {
return __internal::__pattern_fill(__tag, std::forward<_ExecutionPolicy>(__exec), __first, __first + __count, __value);
}

//------------------------------------------------------------------------
// generate, generate_n
//------------------------------------------------------------------------
Expand Down
10 changes: 0 additions & 10 deletions libcxx/include/__pstl/internal/glue_algorithm_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,6 @@ __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardItera
const _Tp& __old_value,
const _Tp& __new_value);

// [alg.fill]

template <class _ExecutionPolicy, class _ForwardIterator, class _Tp>
__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void>
fill(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value);

template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Tp>
__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
fill_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __count, const _Tp& __value);

// [alg.generate]
template <class _ExecutionPolicy, class _ForwardIterator, class _Generator>
__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void>
Expand Down
22 changes: 0 additions & 22 deletions libcxx/include/__pstl/internal/glue_algorithm_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,28 +380,6 @@ __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardItera
__new_value);
}

// [alg.fill]

template <class _ExecutionPolicy, class _ForwardIterator, class _Tp>
__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void>
fill(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first);

__pstl::__internal::__pattern_fill(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, __value);
}

template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Tp>
__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
fill_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __count, const _Tp& __value) {
if (__count <= 0)
return __first;

auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first);

return __pstl::__internal::__pattern_fill_n(
__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __count, __value);
}

// [alg.generate]
template <class _ExecutionPolicy, class _ForwardIterator, class _Generator>
__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void>
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/algorithm
Original file line number Diff line number Diff line change
Expand Up @@ -1910,6 +1910,7 @@ template <class BidirectionalIterator, class Compare>

#ifdef _LIBCPP_HAS_PARALLEL_ALGORITHMS
# include <__algorithm/pstl_any_all_none_of.h>
# include <__algorithm/pstl_fill.h>
# include <__algorithm/pstl_find.h>
# include <__algorithm/pstl_for_each.h>
#endif
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <algorithm>

// UNSUPPORTED: c++03, c++11, c++14

// REQUIRES: with-pstl

// template<class ExecutionPolicy, class ForwardIterator, class T>
// void fill(ExecutionPolicy&& exec,
// ForwardIterator first, ForwardIterator last, const T& value);

#include <algorithm>
#include <cassert>
#include <vector>

#include "test_macros.h"
#include "test_execution_policies.h"
#include "test_iterators.h"

EXECUTION_POLICY_SFINAE_TEST(fill);

static_assert(sfinae_test_fill<int, int*, int*, bool (*)(int)>);
static_assert(!sfinae_test_fill<std::execution::parallel_policy, int*, int*, int>);

template <class Iter>
struct Test {
template <class Policy>
void operator()(Policy&& policy) {
{ // simple test
int a[4];
std::fill(policy, Iter(std::begin(a)), Iter(std::end(a)), 33);
assert(std::all_of(std::begin(a), std::end(a), [](int i) { return i == 33; }));
}
{ // check that an empty range works
int a[1] = {2};
std::fill(policy, Iter(std::begin(a)), Iter(std::begin(a)), 33);
assert(a[0] == 2);
}
{ // check that a one-element range works
int a[1];
std::fill(policy, Iter(std::begin(a)), Iter(std::end(a)), 33);
assert(std::all_of(std::begin(a), std::end(a), [](int i) { return i == 33; }));
}
{ // check that a two-element range works
int a[2];
std::fill(policy, Iter(std::begin(a)), Iter(std::end(a)), 33);
assert(std::all_of(std::begin(a), std::end(a), [](int i) { return i == 33; }));
}
{ // check that a large range works
std::vector<int> a(234, 2);
std::fill(policy, Iter(std::data(a)), Iter(std::data(a) + std::size(a)), 33);
assert(std::all_of(std::begin(a), std::end(a), [](int i) { return i == 33; }));
}
}
};

struct ThrowOnCopy {
ThrowOnCopy& operator=(const ThrowOnCopy&) { throw int{}; }
};

int main(int, char**) {
types::for_each(types::forward_iterator_list<int*>{}, TestIteratorWithPolicies<Test>{});

#ifndef TEST_HAS_NO_EXCEPTIONS
std::set_terminate(terminate_successful);
ThrowOnCopy a[2];
try {
(void)std::fill(std::execution::par, std::begin(a), std::end(a), ThrowOnCopy{});
} catch (int) {
assert(false);
}
#endif

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <algorithm>

// UNSUPPORTED: c++03, c++11, c++14

// REQUIRES: with-pstl

// template<class ExecutionPolicy, class ForwardIterator, class Size, class T>
// ForwardIterator fill_n(ExecutionPolicy&& exec,
// ForwardIterator first, Size n, const T& value);

#include <algorithm>
#include <cassert>
#include <vector>

#include "test_macros.h"
#include "test_execution_policies.h"
#include "test_iterators.h"

EXECUTION_POLICY_SFINAE_TEST(fill_n);

static_assert(sfinae_test_fill_n<int, int*, int*, bool (*)(int)>);
static_assert(!sfinae_test_fill_n<std::execution::parallel_policy, int*, int*, int>);

template <class Iter>
struct Test {
template <class Policy>
void operator()(Policy&& policy) {
{ // simple test
int a[4];
std::fill_n(policy, Iter(std::begin(a)), std::size(a), 33);
assert(std::all_of(std::begin(a), std::end(a), [](int i) { return i == 33; }));
}
{ // check that an empty range works
int a[1] = {2};
std::fill_n(policy, Iter(std::begin(a)), 0, 33);
assert(a[0] == 2);
}
{ // check that a one-element range works
int a[1];
std::fill_n(policy, Iter(std::begin(a)), std::size(a), 33);
assert(std::all_of(std::begin(a), std::end(a), [](int i) { return i == 33; }));
}
{ // check that a two-element range works
int a[2];
std::fill_n(policy, Iter(std::begin(a)), std::size(a), 33);
assert(std::all_of(std::begin(a), std::end(a), [](int i) { return i == 33; }));
}
{ // check that a large range works
std::vector<int> a(234, 2);
std::fill_n(policy, Iter(std::data(a)), std::size(a), 33);
assert(std::all_of(std::begin(a), std::end(a), [](int i) { return i == 33; }));
}
}
};

struct ThrowOnCopy {
ThrowOnCopy& operator=(const ThrowOnCopy&) { throw int{}; }
};

int main(int, char**) {
types::for_each(types::forward_iterator_list<int*>{}, TestIteratorWithPolicies<Test>{});

#ifndef TEST_HAS_NO_EXCEPTIONS
std::set_terminate(terminate_successful);
ThrowOnCopy a[2];
try {
(void)std::fill_n(std::execution::par, std::begin(a), std::size(a), ThrowOnCopy{});
} catch (int) {
assert(false);
}
#endif

return 0;
}

0 comments on commit ade9c3b

Please sign in to comment.