[libcxx-commits] [libcxx] ade9c3b - [libc++][PSTL] Implement std::fill{, _n}

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Mon May 1 14:03:46 PDT 2023


Author: Nikolas Klauser
Date: 2023-05-01T14:03:40-07:00
New Revision: ade9c3bdca2c5d9b63940569b9b2e3d33de6f82a

URL: https://github.com/llvm/llvm-project/commit/ade9c3bdca2c5d9b63940569b9b2e3d33de6f82a
DIFF: https://github.com/llvm/llvm-project/commit/ade9c3bdca2c5d9b63940569b9b2e3d33de6f82a.diff

LOG: [libc++][PSTL] Implement std::fill{,_n}

Reviewed By: ldionne, #libc

Spies: libcxx-commits

Differential Revision: https://reviews.llvm.org/D149540

Added: 
    libcxx/include/__algorithm/pstl_fill.h
    libcxx/test/std/algorithms/alg.modifying.operations/alg.fill/pstl.fill.pass.cpp
    libcxx/test/std/algorithms/alg.modifying.operations/alg.fill/pstl.fill_n.pass.cpp

Modified: 
    libcxx/include/CMakeLists.txt
    libcxx/include/__pstl/internal/algorithm_impl.h
    libcxx/include/__pstl/internal/glue_algorithm_defs.h
    libcxx/include/__pstl/internal/glue_algorithm_impl.h
    libcxx/include/algorithm

Removed: 
    


################################################################################
diff  --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 356af6db8f6c0..8ca0ce73a65fc 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -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

diff  --git a/libcxx/include/__algorithm/pstl_fill.h b/libcxx/include/__algorithm/pstl_fill.h
new file mode 100644
index 0000000000000..0ee14d6c8ee47
--- /dev/null
+++ b/libcxx/include/__algorithm/pstl_fill.h
@@ -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

diff  --git a/libcxx/include/__pstl/internal/algorithm_impl.h b/libcxx/include/__pstl/internal/algorithm_impl.h
index 40b79f1131178..64e6e6c9e853e 100644
--- a/libcxx/include/__pstl/internal/algorithm_impl.h
+++ b/libcxx/include/__pstl/internal/algorithm_impl.h
@@ -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
 //------------------------------------------------------------------------

diff  --git a/libcxx/include/__pstl/internal/glue_algorithm_defs.h b/libcxx/include/__pstl/internal/glue_algorithm_defs.h
index c251bd5c575df..f751802b47a13 100644
--- a/libcxx/include/__pstl/internal/glue_algorithm_defs.h
+++ b/libcxx/include/__pstl/internal/glue_algorithm_defs.h
@@ -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>

diff  --git a/libcxx/include/__pstl/internal/glue_algorithm_impl.h b/libcxx/include/__pstl/internal/glue_algorithm_impl.h
index 1f7d0cdc9dc05..3d207185401bd 100644
--- a/libcxx/include/__pstl/internal/glue_algorithm_impl.h
+++ b/libcxx/include/__pstl/internal/glue_algorithm_impl.h
@@ -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>

diff  --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index ed3bc05c8932b..41ce675309434 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -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

diff  --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.fill/pstl.fill.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.fill/pstl.fill.pass.cpp
new file mode 100644
index 0000000000000..69c703fae3ba3
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.fill/pstl.fill.pass.cpp
@@ -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;
+}

diff  --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.fill/pstl.fill_n.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.fill/pstl.fill_n.pass.cpp
new file mode 100644
index 0000000000000..9767f3987d581
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.fill/pstl.fill_n.pass.cpp
@@ -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;
+}


        


More information about the libcxx-commits mailing list