From 7e427c3c9512d8e0811ac99df5b4ea3172e1ae01 Mon Sep 17 00:00:00 2001 From: Bernhard Manfred Gruber Date: Tue, 28 Jan 2025 12:20:11 +0100 Subject: [PATCH 1/2] Deprecate and replace some thrust iterator traits Deprecate and remove use of thrust:: * iterator_traits * iterator_value * iterator_value_t * iterator_pointer * iterator_pointer_t * iterator_reference * iterator_reference_t * iterator_difference * iterator_difference_t Add as replacement thrust::detail:: * it_value_t * it_reference_t * it_difference_t * it_pointer_t --- thrust/examples/cuda/range_view.cu | 10 ++- thrust/examples/expand.cu | 2 +- thrust/examples/repeated_range.cu | 2 +- thrust/examples/strided_range.cu | 2 +- thrust/examples/tiled_range.cu | 2 +- thrust/testing/counting_iterator.cu | 2 +- thrust/testing/cuda/adjacent_difference.cu | 4 +- thrust/testing/omp/reduce_intervals.cu | 2 +- thrust/testing/sort_permutation_iterator.cu | 2 +- thrust/testing/trivial_sequence.cu | 2 +- thrust/testing/unique.cu | 5 +- thrust/testing/unittest/assertions.h | 8 +- thrust/testing/zip_iterator.cu | 4 +- thrust/thrust/advance.h | 4 +- thrust/thrust/count.h | 9 +-- thrust/thrust/detail/advance.inl | 7 +- .../detail/allocator/copy_construct_range.inl | 10 +-- .../detail/allocator/tagged_allocator.h | 4 +- thrust/thrust/detail/count.h | 9 +-- thrust/thrust/detail/count.inl | 9 +-- thrust/thrust/detail/distance.inl | 3 +- thrust/thrust/detail/get_iterator_value.h | 3 +- thrust/thrust/detail/memory_algorithms.h | 12 +-- thrust/thrust/detail/overlapped_copy.h | 2 +- thrust/thrust/detail/pointer.h | 22 +----- thrust/thrust/detail/range/head_flags.h | 4 +- thrust/thrust/detail/range/tail_flags.h | 4 +- thrust/thrust/detail/reduce.inl | 4 +- thrust/thrust/detail/temporary_array.h | 2 +- thrust/thrust/detail/trivial_sequence.h | 2 +- .../type_traits/iterator/is_output_iterator.h | 30 ++------ thrust/thrust/detail/unique.inl | 8 +- thrust/thrust/detail/vector_base.inl | 2 +- thrust/thrust/distance.h | 2 +- thrust/thrust/iterator/counting_iterator.h | 2 +- .../iterator/detail/iterator_adaptor_base.h | 6 +- .../thrust/iterator/detail/tagged_iterator.h | 6 +- thrust/thrust/iterator/iterator_traits.h | 77 ++++++++++++++----- thrust/thrust/iterator/permutation_iterator.h | 4 +- .../transform_input_output_iterator.h | 4 +- thrust/thrust/iterator/transform_iterator.h | 10 +-- thrust/thrust/iterator/zip_iterator.h | 25 +++++- thrust/thrust/reduce.h | 4 +- .../system/cuda/detail/adjacent_difference.h | 6 +- .../system/cuda/detail/async/inclusive_scan.h | 6 +- thrust/thrust/system/cuda/detail/copy_if.h | 2 +- .../system/cuda/detail/core/load_iterator.h | 4 +- thrust/thrust/system/cuda/detail/core/util.h | 2 +- thrust/thrust/system/cuda/detail/count.h | 6 +- thrust/thrust/system/cuda/detail/equal.h | 2 +- thrust/thrust/system/cuda/detail/extrema.h | 15 ++-- thrust/thrust/system/cuda/detail/find.h | 6 +- thrust/thrust/system/cuda/detail/for_each.h | 2 +- thrust/thrust/system/cuda/detail/get_value.h | 4 +- .../cuda/detail/internal/copy_cross_system.h | 6 +- .../detail/internal/copy_device_to_device.h | 2 +- thrust/thrust/system/cuda/detail/merge.h | 4 +- thrust/thrust/system/cuda/detail/mismatch.h | 4 +- thrust/thrust/system/cuda/detail/partition.h | 4 +- thrust/thrust/system/cuda/detail/reduce.h | 7 +- .../thrust/system/cuda/detail/reduce_by_key.h | 16 ++-- thrust/thrust/system/cuda/detail/replace.h | 4 +- thrust/thrust/system/cuda/detail/reverse.h | 4 +- thrust/thrust/system/cuda/detail/scan.h | 10 +-- .../thrust/system/cuda/detail/scan_by_key.h | 4 +- .../system/cuda/detail/set_operations.h | 38 ++++----- thrust/thrust/system/cuda/detail/sort.h | 18 ++--- .../thrust/system/cuda/detail/swap_ranges.h | 6 +- thrust/thrust/system/cuda/detail/tabulate.h | 2 +- thrust/thrust/system/cuda/detail/transform.h | 8 +- .../system/cuda/detail/transform_reduce.h | 3 +- .../system/cuda/detail/transform_scan.h | 10 +-- .../system/cuda/detail/uninitialized_copy.h | 4 +- .../system/cuda/detail/uninitialized_fill.h | 2 +- thrust/thrust/system/cuda/detail/unique.h | 12 +-- .../thrust/system/cuda/detail/unique_by_key.h | 6 +- .../detail/generic/adjacent_difference.inl | 4 +- .../system/detail/generic/binary_search.inl | 8 +- .../thrust/system/detail/generic/copy_if.inl | 2 +- thrust/thrust/system/detail/generic/count.h | 4 +- thrust/thrust/system/detail/generic/count.inl | 8 +- .../thrust/system/detail/generic/distance.h | 2 +- .../thrust/system/detail/generic/distance.inl | 9 +-- .../thrust/system/detail/generic/extrema.inl | 18 ++--- thrust/thrust/system/detail/generic/find.inl | 2 +- .../thrust/system/detail/generic/generate.inl | 6 +- thrust/thrust/system/detail/generic/merge.inl | 4 +- .../system/detail/generic/partition.inl | 9 +-- thrust/thrust/system/detail/generic/reduce.h | 2 +- .../thrust/system/detail/generic/reduce.inl | 4 +- .../system/detail/generic/reduce_by_key.inl | 16 ++-- .../thrust/system/detail/generic/remove.inl | 4 +- .../thrust/system/detail/generic/replace.inl | 4 +- .../thrust/system/detail/generic/reverse.inl | 2 +- .../detail/generic/scalar/binary_search.inl | 4 +- thrust/thrust/system/detail/generic/scan.inl | 3 +- .../system/detail/generic/scan_by_key.inl | 4 +- .../thrust/system/detail/generic/sequence.inl | 2 +- .../system/detail/generic/set_operations.inl | 18 ++--- .../thrust/system/detail/generic/shuffle.inl | 2 +- thrust/thrust/system/detail/generic/sort.inl | 10 +-- .../thrust/system/detail/generic/tabulate.inl | 2 +- .../system/detail/generic/transform_scan.inl | 5 +- .../detail/generic/uninitialized_copy.inl | 14 ++-- .../detail/generic/uninitialized_fill.inl | 8 +- thrust/thrust/system/detail/generic/unique.h | 4 +- .../thrust/system/detail/generic/unique.inl | 12 +-- .../system/detail/generic/unique_by_key.inl | 10 +-- .../detail/sequential/adjacent_difference.h | 2 +- .../system/detail/sequential/binary_search.h | 4 +- .../thrust/system/detail/sequential/copy.inl | 2 +- .../system/detail/sequential/general_copy.h | 28 ++++--- .../system/detail/sequential/get_value.h | 2 +- .../system/detail/sequential/insertion_sort.h | 6 +- .../system/detail/sequential/partition.h | 6 +- .../system/detail/sequential/reduce_by_key.h | 6 +- thrust/thrust/system/detail/sequential/scan.h | 7 +- .../system/detail/sequential/scan_by_key.h | 6 +- .../thrust/system/detail/sequential/sort.inl | 8 +- .../detail/sequential/stable_merge_sort.inl | 17 ++-- .../sequential/stable_primitive_sort.inl | 4 +- .../detail/sequential/stable_radix_sort.inl | 16 ++-- .../thrust/system/detail/sequential/unique.h | 8 +- .../system/detail/sequential/unique_by_key.h | 4 +- thrust/thrust/system/omp/detail/for_each.inl | 2 +- thrust/thrust/system/omp/detail/reduce.inl | 2 +- .../system/omp/detail/reduce_intervals.inl | 2 +- thrust/thrust/system/omp/detail/sort.inl | 10 +-- thrust/thrust/system/omp/detail/unique.h | 2 +- thrust/thrust/system/omp/detail/unique.inl | 2 +- thrust/thrust/system/tbb/detail/copy_if.inl | 2 +- thrust/thrust/system/tbb/detail/reduce.inl | 2 +- .../system/tbb/detail/reduce_by_key.inl | 22 +++--- .../system/tbb/detail/reduce_intervals.h | 4 +- thrust/thrust/system/tbb/detail/scan.inl | 13 ++-- thrust/thrust/system/tbb/detail/sort.inl | 10 +-- thrust/thrust/system/tbb/detail/unique.h | 2 +- thrust/thrust/system/tbb/detail/unique.inl | 2 +- .../type_traits/is_trivially_relocatable.h | 9 +-- thrust/thrust/unique.h | 8 +- 140 files changed, 511 insertions(+), 481 deletions(-) diff --git a/thrust/examples/cuda/range_view.cu b/thrust/examples/cuda/range_view.cu index f045d67bca7..5b7e3825d79 100644 --- a/thrust/examples/cuda/range_view.cu +++ b/thrust/examples/cuda/range_view.cu @@ -3,6 +3,8 @@ #include #include +#include + #include #include "../include/host_device.h" @@ -20,10 +22,10 @@ class range_view { public: using iterator = Iterator; - using value_type = typename thrust::iterator_traits::value_type; - using pointer = typename thrust::iterator_traits::pointer; - using difference_type = typename thrust::iterator_traits::difference_type; - using reference = typename thrust::iterator_traits::reference; + using value_type = typename cuda::std::iterator_traits::value_type; + using pointer = typename cuda::std::iterator_traits::pointer; + using difference_type = typename cuda::std::iterator_traits::difference_type; + using reference = typename cuda::std::iterator_traits::reference; private: const iterator first; diff --git a/thrust/examples/expand.cu b/thrust/examples/expand.cu index ecded38ffd8..350ec1d4099 100644 --- a/thrust/examples/expand.cu +++ b/thrust/examples/expand.cu @@ -20,7 +20,7 @@ template OutputIterator expand(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator output) { - using difference_type = typename thrust::iterator_difference::type; + using difference_type = thrust::detail::it_difference_t; difference_type input_size = thrust::distance(first1, last1); difference_type output_size = thrust::reduce(first1, last1); diff --git a/thrust/examples/repeated_range.cu b/thrust/examples/repeated_range.cu index c86bb22f64e..debd13c58f9 100644 --- a/thrust/examples/repeated_range.cu +++ b/thrust/examples/repeated_range.cu @@ -21,7 +21,7 @@ template class repeated_range { public: - using difference_type = typename thrust::iterator_difference::type; + using difference_type = thrust::detail::it_difference_t; struct repeat_functor { diff --git a/thrust/examples/strided_range.cu b/thrust/examples/strided_range.cu index 40ad38fcb9a..dbdbac82d62 100644 --- a/thrust/examples/strided_range.cu +++ b/thrust/examples/strided_range.cu @@ -21,7 +21,7 @@ template class strided_range { public: - using difference_type = typename thrust::iterator_difference::type; + using difference_type = thrust::detail::it_difference_t; struct stride_functor { diff --git a/thrust/examples/tiled_range.cu b/thrust/examples/tiled_range.cu index ca69dea2f77..220591dc8f5 100644 --- a/thrust/examples/tiled_range.cu +++ b/thrust/examples/tiled_range.cu @@ -21,7 +21,7 @@ template class tiled_range { public: - using difference_type = typename thrust::iterator_difference::type; + using difference_type = thrust::detail::it_difference_t; struct tile_functor { diff --git a/thrust/testing/counting_iterator.cu b/thrust/testing/counting_iterator.cu index 69905636c43..24371d12de6 100644 --- a/thrust/testing/counting_iterator.cu +++ b/thrust/testing/counting_iterator.cu @@ -235,7 +235,7 @@ DECLARE_UNITTEST(TestCountingIteratorLowerBound); void TestCountingIteratorDifference() { using Iterator = thrust::counting_iterator; - using Difference = thrust::iterator_difference::type; + using Difference = thrust::detail::it_difference_t; Difference diff = std::numeric_limits::max() + 1; diff --git a/thrust/testing/cuda/adjacent_difference.cu b/thrust/testing/cuda/adjacent_difference.cu index d05defe5a58..cbc8fcd515e 100644 --- a/thrust/testing/cuda/adjacent_difference.cu +++ b/thrust/testing/cuda/adjacent_difference.cu @@ -96,9 +96,9 @@ DECLARE_UNITTEST(TestAdjacentDifferenceCudaStreams); struct detect_wrong_difference { using difference_type = void; - using value_type = void; + using value_type = long long; using pointer = void; - using reference = void; + using reference = detect_wrong_difference; using iterator_category = ::cuda::std::output_iterator_tag; bool* flag; diff --git a/thrust/testing/omp/reduce_intervals.cu b/thrust/testing/omp/reduce_intervals.cu index 309faf69cc9..c3995570a3b 100644 --- a/thrust/testing/omp/reduce_intervals.cu +++ b/thrust/testing/omp/reduce_intervals.cu @@ -8,7 +8,7 @@ template void reduce_intervals(InputIterator input, OutputIterator output, BinaryFunction binary_op, Decomposition decomp) { - using OutputType = typename thrust::iterator_value::type; + using OutputType = thrust::detail::it_value_t; using index_type = typename Decomposition::index_type; // wrap binary_op diff --git a/thrust/testing/sort_permutation_iterator.cu b/thrust/testing/sort_permutation_iterator.cu index d17281be5a0..0e51a00a58b 100644 --- a/thrust/testing/sort_permutation_iterator.cu +++ b/thrust/testing/sort_permutation_iterator.cu @@ -10,7 +10,7 @@ template class strided_range { public: - using difference_type = typename thrust::iterator_difference::type; + using difference_type = thrust::detail::it_difference_t; struct stride_functor { diff --git a/thrust/testing/trivial_sequence.cu b/thrust/testing/trivial_sequence.cu index 7438b7e406a..b9072bcb382 100644 --- a/thrust/testing/trivial_sequence.cu +++ b/thrust/testing/trivial_sequence.cu @@ -10,7 +10,7 @@ void test(Iterator first, Iterator last) using System = typename thrust::iterator_system::type; System system; thrust::detail::trivial_sequence ts(system, first, last); - using ValueType = typename thrust::iterator_traits::value_type; + using ValueType = typename ::cuda::std::iterator_traits::value_type; ASSERT_EQUAL_QUIET((ValueType) ts.begin()[0], ValueType(0, 11)); ASSERT_EQUAL_QUIET((ValueType) ts.begin()[1], ValueType(2, 11)); diff --git a/thrust/testing/unique.cu b/thrust/testing/unique.cu index 8a03353b517..fa7e41c308c 100644 --- a/thrust/testing/unique.cu +++ b/thrust/testing/unique.cu @@ -79,7 +79,7 @@ void TestUniqueCopyDispatchImplicit() DECLARE_UNITTEST(TestUniqueCopyDispatchImplicit); template -typename thrust::iterator_traits::difference_type +typename ::cuda::std::iterator_traits::difference_type unique_count(my_system& system, ForwardIterator, ForwardIterator) { system.validate_dispatch(); @@ -98,7 +98,8 @@ void TestUniqueCountDispatchExplicit() DECLARE_UNITTEST(TestUniqueCountDispatchExplicit); template -typename thrust::iterator_traits::difference_type unique_count(my_tag, ForwardIterator, ForwardIterator) +typename ::cuda::std::iterator_traits::difference_type +unique_count(my_tag, ForwardIterator, ForwardIterator) { return 13; } diff --git a/thrust/testing/unittest/assertions.h b/thrust/testing/unittest/assertions.h index c6404d156e4..7fb924ade7f 100644 --- a/thrust/testing/unittest/assertions.h +++ b/thrust/testing/unittest/assertions.h @@ -409,8 +409,8 @@ void assert_equal( const std::string& filename = "unknown", int lineno = -1) { - using difference_type = typename THRUST_NS_QUALIFIER::iterator_difference::type; - using InputType = typename THRUST_NS_QUALIFIER::iterator_value::type; + using difference_type = THRUST_NS_QUALIFIER::detail::it_difference_t; + using InputType = THRUST_NS_QUALIFIER::detail::it_value_t; bool failure = false; @@ -486,7 +486,7 @@ void assert_equal( const std::string& filename = "unknown", int lineno = -1) { - using InputType = typename THRUST_NS_QUALIFIER::iterator_traits::value_type; + using InputType = typename ::cuda::std::iterator_traits::value_type; assert_equal(first1, last1, first2, last2, THRUST_NS_QUALIFIER::equal_to(), filename, lineno); } @@ -501,7 +501,7 @@ void assert_almost_equal( const double a_tol = DEFAULT_ABSOLUTE_TOL, const double r_tol = DEFAULT_RELATIVE_TOL) { - using InputType = typename THRUST_NS_QUALIFIER::iterator_traits::value_type; + using InputType = typename ::cuda::std::iterator_traits::value_type; assert_equal(first1, last1, first2, last2, almost_equal_to(a_tol, r_tol), filename, lineno); } diff --git a/thrust/testing/zip_iterator.cu b/thrust/testing/zip_iterator.cu index 786b839099b..aad7afc0dd8 100644 --- a/thrust/testing/zip_iterator.cu +++ b/thrust/testing/zip_iterator.cu @@ -109,7 +109,7 @@ struct TestZipIteratorReference using IteratorTuple1 = tuple; using ZipIterator1 = zip_iterator; - using zip_iterator_reference_type1 = typename iterator_reference::type; + using zip_iterator_reference_type1 = thrust::detail::it_reference_t; host_vector h_variable(1); @@ -128,7 +128,7 @@ struct TestZipIteratorReference using IteratorTuple2 = tuple; using ZipIterator2 = zip_iterator; - using zip_iterator_reference_type2 = typename iterator_reference::type; + using zip_iterator_reference_type2 = thrust::detail::it_reference_t; device_vector d_variable(1); diff --git a/thrust/thrust/advance.h b/thrust/thrust/advance.h index 801568b6e00..404e8a31c88 100644 --- a/thrust/thrust/advance.h +++ b/thrust/thrust/advance.h @@ -101,7 +101,7 @@ template _CCCL_HOST_DEVICE InputIterator next( InputIterator i -, typename iterator_traits::difference_type n = 1 +, typename ::cuda::std::iterator_traits::difference_type n = 1 ); #endif @@ -135,7 +135,7 @@ template _CCCL_HOST_DEVICE BidirectionalIterator prev( BidirectionalIterator i -, typename iterator_traits::difference_type n = 1 +, typename ::cuda::std::iterator_traits::difference_type n = 1 ); #endif diff --git a/thrust/thrust/count.h b/thrust/thrust/count.h index 079322802b3..aac7457e59a 100644 --- a/thrust/thrust/count.h +++ b/thrust/thrust/count.h @@ -89,7 +89,7 @@ THRUST_NAMESPACE_BEGIN * \see https://en.cppreference.com/w/cpp/algorithm/count */ template -_CCCL_HOST_DEVICE typename thrust::iterator_traits::difference_type +_CCCL_HOST_DEVICE thrust::detail::it_difference_t count(const thrust::detail::execution_policy_base& exec, InputIterator first, InputIterator last, @@ -131,7 +131,7 @@ count(const thrust::detail::execution_policy_base& exec, * \see https://en.cppreference.com/w/cpp/algorithm/count */ template -typename thrust::iterator_traits::difference_type +thrust::detail::it_difference_t count(InputIterator first, InputIterator last, const EqualityComparable& value); /*! \p count_if finds the number of elements in [first,last) for which @@ -184,7 +184,7 @@ count(InputIterator first, InputIterator last, const EqualityComparable& value); * \see https://en.cppreference.com/w/cpp/algorithm/count */ template -_CCCL_HOST_DEVICE typename thrust::iterator_traits::difference_type +_CCCL_HOST_DEVICE thrust::detail::it_difference_t count_if(const thrust::detail::execution_policy_base& exec, InputIterator first, InputIterator last, @@ -234,8 +234,7 @@ count_if(const thrust::detail::execution_policy_base& exec, * \see https://en.cppreference.com/w/cpp/algorithm/count */ template -typename thrust::iterator_traits::difference_type -count_if(InputIterator first, InputIterator last, Predicate pred); +thrust::detail::it_difference_t count_if(InputIterator first, InputIterator last, Predicate pred); /*! \} // end counting * \} // end reductions diff --git a/thrust/thrust/detail/advance.inl b/thrust/thrust/detail/advance.inl index fe84d373b59..ec49d604f92 100644 --- a/thrust/thrust/detail/advance.inl +++ b/thrust/thrust/detail/advance.inl @@ -43,7 +43,7 @@ _CCCL_HOST_DEVICE void advance(InputIterator& i, Distance n) } template -_CCCL_HOST_DEVICE InputIterator next(InputIterator i, typename iterator_traits::difference_type n = 1) +_CCCL_HOST_DEVICE InputIterator next(InputIterator i, thrust::detail::it_difference_t n = 1) { thrust::system::detail::generic::advance(i, n); return i; @@ -51,15 +51,16 @@ _CCCL_HOST_DEVICE InputIterator next(InputIterator i, typename iterator_traits _CCCL_HOST_DEVICE BidirectionalIterator -prev(BidirectionalIterator i, typename iterator_traits::difference_type n = 1) +prev(BidirectionalIterator i, thrust::detail::it_difference_t n = 1) { thrust::system::detail::generic::advance(i, -n); return i; } +// FIXME(bgruber): what does this prevent against? template _CCCL_HOST_DEVICE -typename detail::disable_if>::value, +typename detail::disable_if>::value, BidirectionalIterator>::type prev(BidirectionalIterator i, typename detail::pointer_traits::difference_type n = 1) { diff --git a/thrust/thrust/detail/allocator/copy_construct_range.inl b/thrust/thrust/detail/allocator/copy_construct_range.inl index 42dd32f045e..c02f373b16e 100644 --- a/thrust/thrust/detail/allocator/copy_construct_range.inl +++ b/thrust/thrust/detail/allocator/copy_construct_range.inl @@ -100,12 +100,12 @@ _CCCL_HOST_DEVICE enable_if_convertible_t uniniti ZipIterator end = begin; // get a zip_iterator pointing to the end - const typename thrust::iterator_difference::type n = thrust::distance(first, last); + const thrust::detail::it_difference_t n = thrust::distance(first, last); thrust::advance(end, n); // create a functor - using InputType = typename iterator_traits::value_type; - using OutputType = typename iterator_traits::value_type; + using InputType = it_value_t; + using OutputType = it_value_t; // do the for_each // note we use to_system to dispatch the for_each @@ -135,8 +135,8 @@ _CCCL_HOST_DEVICE enable_if_convertible_t uniniti ZipIterator begin = thrust::make_zip_iterator(thrust::make_tuple(first, result)); // create a functor - using InputType = typename iterator_traits::value_type; - using OutputType = typename iterator_traits::value_type; + using InputType = it_value_t; + using OutputType = it_value_t; // do the for_each_n // note we use to_system to dispatch the for_each_n diff --git a/thrust/thrust/detail/allocator/tagged_allocator.h b/thrust/thrust/detail/allocator/tagged_allocator.h index bdac004d9bb..c7cc09ea713 100644 --- a/thrust/thrust/detail/allocator/tagged_allocator.h +++ b/thrust/thrust/detail/allocator/tagged_allocator.h @@ -60,8 +60,8 @@ class tagged_allocator using value_type = T; using pointer = typename thrust::detail::pointer_traits::template rebind::other; using const_pointer = typename thrust::detail::pointer_traits::template rebind::other; - using reference = typename thrust::iterator_reference::type; - using const_reference = typename thrust::iterator_reference::type; + using reference = thrust::detail::it_reference_t; + using const_reference = thrust::detail::it_reference_t; using size_type = std::size_t; using difference_type = typename thrust::detail::pointer_traits::difference_type; using system_type = Tag; diff --git a/thrust/thrust/detail/count.h b/thrust/thrust/detail/count.h index e5ce2e2b30b..ac3668190f1 100644 --- a/thrust/thrust/detail/count.h +++ b/thrust/thrust/detail/count.h @@ -30,26 +30,25 @@ THRUST_NAMESPACE_BEGIN template -_CCCL_HOST_DEVICE typename thrust::iterator_traits::difference_type +_CCCL_HOST_DEVICE thrust::detail::it_difference_t count(const thrust::detail::execution_policy_base& exec, InputIterator first, InputIterator last, const EqualityComparable& value); template -_CCCL_HOST_DEVICE typename thrust::iterator_traits::difference_type +_CCCL_HOST_DEVICE thrust::detail::it_difference_t count_if(const thrust::detail::execution_policy_base& exec, InputIterator first, InputIterator last, Predicate pred); template -typename thrust::iterator_traits::difference_type +thrust::detail::it_difference_t count(InputIterator first, InputIterator last, const EqualityComparable& value); template -typename thrust::iterator_traits::difference_type -count_if(InputIterator first, InputIterator last, Predicate pred); +thrust::detail::it_difference_t count_if(InputIterator first, InputIterator last, Predicate pred); THRUST_NAMESPACE_END diff --git a/thrust/thrust/detail/count.inl b/thrust/thrust/detail/count.inl index 28876d8f12f..7813b48c28b 100644 --- a/thrust/thrust/detail/count.inl +++ b/thrust/thrust/detail/count.inl @@ -35,7 +35,7 @@ THRUST_NAMESPACE_BEGIN _CCCL_EXEC_CHECK_DISABLE template -_CCCL_HOST_DEVICE typename thrust::iterator_traits::difference_type +_CCCL_HOST_DEVICE thrust::detail::it_difference_t count(const thrust::detail::execution_policy_base& exec, InputIterator first, InputIterator last, @@ -47,7 +47,7 @@ count(const thrust::detail::execution_policy_base& exec, _CCCL_EXEC_CHECK_DISABLE template -_CCCL_HOST_DEVICE typename thrust::iterator_traits::difference_type +_CCCL_HOST_DEVICE thrust::detail::it_difference_t count_if(const thrust::detail::execution_policy_base& exec, InputIterator first, InputIterator last, @@ -58,7 +58,7 @@ count_if(const thrust::detail::execution_policy_base& exec, } // end count_if() template -typename thrust::iterator_traits::difference_type +thrust::detail::it_difference_t count(InputIterator first, InputIterator last, const EqualityComparable& value) { using thrust::system::detail::generic::select_system; @@ -71,8 +71,7 @@ count(InputIterator first, InputIterator last, const EqualityComparable& value) } // end count() template -typename thrust::iterator_traits::difference_type -count_if(InputIterator first, InputIterator last, Predicate pred) +thrust::detail::it_difference_t count_if(InputIterator first, InputIterator last, Predicate pred) { using thrust::system::detail::generic::select_system; diff --git a/thrust/thrust/detail/distance.inl b/thrust/thrust/detail/distance.inl index 7316dfa91dd..b8a71b95c1d 100644 --- a/thrust/thrust/detail/distance.inl +++ b/thrust/thrust/detail/distance.inl @@ -34,8 +34,7 @@ THRUST_NAMESPACE_BEGIN _CCCL_EXEC_CHECK_DISABLE template -inline _CCCL_HOST_DEVICE typename thrust::iterator_traits::difference_type -distance(InputIterator first, InputIterator last) +inline _CCCL_HOST_DEVICE thrust::detail::it_difference_t distance(InputIterator first, InputIterator last) { return thrust::system::detail::generic::distance(first, last); } // end distance() diff --git a/thrust/thrust/detail/get_iterator_value.h b/thrust/thrust/detail/get_iterator_value.h index 330aea557cc..5779ded3c9f 100644 --- a/thrust/thrust/detail/get_iterator_value.h +++ b/thrust/thrust/detail/get_iterator_value.h @@ -39,8 +39,7 @@ namespace detail // -------------------------------------------------- // it is okay to dereference iterator in the usual way template -_CCCL_HOST_DEVICE typename thrust::iterator_traits::value_type -get_iterator_value(thrust::execution_policy&, Iterator it) +_CCCL_HOST_DEVICE it_value_t get_iterator_value(thrust::execution_policy&, Iterator it) { return *it; } // get_iterator_value(exec,Iterator); diff --git a/thrust/thrust/detail/memory_algorithms.h b/thrust/thrust/detail/memory_algorithms.h index 1da74c4b56d..7ae020c52c3 100644 --- a/thrust/thrust/detail/memory_algorithms.h +++ b/thrust/thrust/detail/memory_algorithms.h @@ -64,7 +64,7 @@ _CCCL_HOST_DEVICE ForwardIt destroy(ForwardIt first, ForwardIt last) noexcept template _CCCL_HOST_DEVICE ForwardIt destroy(Allocator const& alloc, ForwardIt first, ForwardIt last) noexcept { - using T = typename iterator_traits::value_type; + using T = detail::it_value_t; using traits = typename detail::allocator_traits< ::cuda::std::remove_cv_t<::cuda::std::remove_reference_t>>::template rebind_traits::other; @@ -92,7 +92,7 @@ _CCCL_HOST_DEVICE ForwardIt destroy_n(ForwardIt first, Size n) noexcept template _CCCL_HOST_DEVICE ForwardIt destroy_n(Allocator const& alloc, ForwardIt first, Size n) noexcept { - using T = typename iterator_traits::value_type; + using T = detail::it_value_t; using traits = typename detail::allocator_traits< ::cuda::std::remove_cv_t<::cuda::std::remove_reference_t>>::template rebind_traits::other; @@ -109,7 +109,7 @@ _CCCL_HOST_DEVICE ForwardIt destroy_n(Allocator const& alloc, ForwardIt first, S template _CCCL_HOST_DEVICE void uninitialized_construct(ForwardIt first, ForwardIt last, Args const&... args) { - using T = typename iterator_traits::value_type; + using T = detail::it_value_t; ForwardIt current = first; @@ -132,7 +132,7 @@ _CCCL_HOST_DEVICE void uninitialized_construct(ForwardIt first, ForwardIt last, template void uninitialized_construct_with_allocator(Allocator const& alloc, ForwardIt first, ForwardIt last, Args const&... args) { - using T = typename iterator_traits::value_type; + using T = detail::it_value_t; using traits = typename detail::allocator_traits< typename std::remove_cv::type>::type>::template rebind_traits; @@ -159,7 +159,7 @@ void uninitialized_construct_with_allocator(Allocator const& alloc, ForwardIt fi template void uninitialized_construct_n(ForwardIt first, Size n, Args const&... args) { - using T = typename iterator_traits::value_type; + using T = detail::it_value_t; ForwardIt current = first; @@ -182,7 +182,7 @@ void uninitialized_construct_n(ForwardIt first, Size n, Args const&... args) template void uninitialized_construct_n_with_allocator(Allocator const& alloc, ForwardIt first, Size n, Args const&... args) { - using T = typename iterator_traits::value_type; + using T = detail::it_value_t; using traits = typename detail::allocator_traits< typename std::remove_cv::type>::type>::template rebind_traits; diff --git a/thrust/thrust/detail/overlapped_copy.h b/thrust/thrust/detail/overlapped_copy.h index f5b11525748..98e49995215 100644 --- a/thrust/thrust/detail/overlapped_copy.h +++ b/thrust/thrust/detail/overlapped_copy.h @@ -95,7 +95,7 @@ RandomAccessIterator2 overlapped_copy( RandomAccessIterator1 last, RandomAccessIterator2 result) { - using value_type = typename thrust::iterator_value::type; + using value_type = thrust::detail::it_value_t; // make a temporary copy of [first,last), and copy into it first thrust::detail::temporary_array temp(exec, first, last); diff --git a/thrust/thrust/detail/pointer.h b/thrust/thrust/detail/pointer.h index b9f2fabc244..43b38f5bf76 100644 --- a/thrust/thrust/detail/pointer.h +++ b/thrust/thrust/detail/pointer.h @@ -1,3 +1,4 @@ + /* * Copyright 2008-2021 NVIDIA Corporation * @@ -43,29 +44,15 @@ #include THRUST_NAMESPACE_BEGIN - template class pointer; - -// Specialize `thrust::iterator_traits` to avoid problems with the name of -// pointer's constructor shadowing its nested pointer type. We do this before -// pointer is defined so the specialization is correctly used inside the -// definition. -template -struct iterator_traits> -{ - using pointer = thrust::pointer; - using iterator_category = typename pointer::iterator_category; - using value_type = typename pointer::value_type; - using difference_type = typename pointer::difference_type; - using reference = typename pointer::reference; -}; - THRUST_NAMESPACE_END +// Specialize `std::iterator_traits` (picked up by cuda::std::iterator_traits) to avoid problems with the name of +// pointer's constructor shadowing its nested pointer type. We do this before pointer is defined so the specialization +// is correctly used inside the definition. namespace std { - template struct iterator_traits> { @@ -75,7 +62,6 @@ struct iterator_traits::type>, + typename BinaryPredicate = equal_to>, typename ValueType = bool, - typename IndexType = typename thrust::iterator_difference::type> + typename IndexType = it_difference_t> class head_flags { public: diff --git a/thrust/thrust/detail/range/tail_flags.h b/thrust/thrust/detail/range/tail_flags.h index 48470c0f369..456bd2f33e3 100644 --- a/thrust/thrust/detail/range/tail_flags.h +++ b/thrust/thrust/detail/range/tail_flags.h @@ -36,9 +36,9 @@ namespace detail { template ::type>, + typename BinaryPredicate = equal_to>, typename ValueType = bool, - typename IndexType = typename thrust::iterator_difference::type> + typename IndexType = detail::it_difference_t> class tail_flags { // XXX WAR cudafe bug diff --git a/thrust/thrust/detail/reduce.inl b/thrust/thrust/detail/reduce.inl index 2d7f7cd4729..c008ec17e6e 100644 --- a/thrust/thrust/detail/reduce.inl +++ b/thrust/thrust/detail/reduce.inl @@ -38,7 +38,7 @@ THRUST_NAMESPACE_BEGIN _CCCL_EXEC_CHECK_DISABLE template -_CCCL_HOST_DEVICE typename thrust::iterator_traits::value_type +_CCCL_HOST_DEVICE detail::it_value_t reduce(const thrust::detail::execution_policy_base& exec, InputIterator first, InputIterator last) { using thrust::system::detail::generic::reduce; @@ -149,7 +149,7 @@ _CCCL_HOST_DEVICE thrust::pair reduce_by_key( } // end reduce_by_key() template -typename thrust::iterator_traits::value_type reduce(InputIterator first, InputIterator last) +detail::it_value_t reduce(InputIterator first, InputIterator last) { using thrust::system::detail::generic::select_system; diff --git a/thrust/thrust/detail/temporary_array.h b/thrust/thrust/detail/temporary_array.h index 848255aac89..4fee9053101 100644 --- a/thrust/thrust/detail/temporary_array.h +++ b/thrust/thrust/detail/temporary_array.h @@ -128,7 +128,7 @@ template struct move_to_system_base : public eval_if<::cuda::std::is_convertible::value, identity_>, - identity_::type, ToSystem>>> + identity_, ToSystem>>> {}; template diff --git a/thrust/thrust/detail/trivial_sequence.h b/thrust/thrust/detail/trivial_sequence.h index 85f51eadfd4..4ae2ef481b7 100644 --- a/thrust/thrust/detail/trivial_sequence.h +++ b/thrust/thrust/detail/trivial_sequence.h @@ -85,7 +85,7 @@ struct _trivial_sequence template struct _trivial_sequence { - using iterator_value = typename thrust::iterator_value::type; + using iterator_value = it_value_t; using iterator_type = typename thrust::detail::temporary_array::iterator; thrust::detail::temporary_array buffer; diff --git a/thrust/thrust/detail/type_traits/iterator/is_output_iterator.h b/thrust/thrust/detail/type_traits/iterator/is_output_iterator.h index 2741ca38212..1d06146955f 100644 --- a/thrust/thrust/detail/type_traits/iterator/is_output_iterator.h +++ b/thrust/thrust/detail/type_traits/iterator/is_output_iterator.h @@ -25,38 +25,22 @@ #elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) # pragma system_header #endif // no system header -#include -#include + #include #include +#include + THRUST_NAMESPACE_BEGIN namespace detail { +template +_CCCL_INLINE_VAR constexpr bool is_output_iterator = true; template -struct is_void_like - : ::cuda::std::disjunction<::cuda::std::is_void, ::cuda::std::is_same> -{}; // end is_void_like - -template -struct lazy_is_void_like : is_void_like -{}; // end lazy_is_void_like - -// XXX this meta function should first check that T is actually an iterator -// -// if thrust::iterator_value is defined and thrust::iterator_value::type == void -// return false -// else -// return true -template -struct is_output_iterator - : eval_if>::value, - lazy_is_void_like>, - thrust::detail::true_type>::type -{}; // end is_output_iterator - +_CCCL_INLINE_VAR constexpr bool is_output_iterator>> = + ::cuda::std::is_void_v> || ::cuda::std::is_same_v, any_assign>; } // namespace detail THRUST_NAMESPACE_END diff --git a/thrust/thrust/detail/unique.inl b/thrust/thrust/detail/unique.inl index 2750cb5e10d..2eec8dba58f 100644 --- a/thrust/thrust/detail/unique.inl +++ b/thrust/thrust/detail/unique.inl @@ -303,7 +303,7 @@ thrust::pair unique_by_key_copy( _CCCL_EXEC_CHECK_DISABLE template -_CCCL_HOST_DEVICE typename thrust::iterator_traits::difference_type unique_count( +_CCCL_HOST_DEVICE thrust::detail::it_difference_t unique_count( const thrust::detail::execution_policy_base& exec, ForwardIterator first, ForwardIterator last, @@ -315,7 +315,7 @@ _CCCL_HOST_DEVICE typename thrust::iterator_traits::difference_ _CCCL_EXEC_CHECK_DISABLE template -_CCCL_HOST_DEVICE typename thrust::iterator_traits::difference_type unique_count( +_CCCL_HOST_DEVICE thrust::detail::it_difference_t unique_count( const thrust::detail::execution_policy_base& exec, ForwardIterator first, ForwardIterator last) { using thrust::system::detail::generic::unique_count; @@ -324,7 +324,7 @@ _CCCL_HOST_DEVICE typename thrust::iterator_traits::difference_ _CCCL_EXEC_CHECK_DISABLE template -_CCCL_HOST_DEVICE typename thrust::iterator_traits::difference_type +_CCCL_HOST_DEVICE thrust::detail::it_difference_t unique_count(ForwardIterator first, ForwardIterator last, BinaryPredicate binary_pred) { using thrust::system::detail::generic::select_system; @@ -338,7 +338,7 @@ unique_count(ForwardIterator first, ForwardIterator last, BinaryPredicate binary _CCCL_EXEC_CHECK_DISABLE template -_CCCL_HOST_DEVICE typename thrust::iterator_traits::difference_type +_CCCL_HOST_DEVICE thrust::detail::it_difference_t unique_count(ForwardIterator first, ForwardIterator last) { using thrust::system::detail::generic::select_system; diff --git a/thrust/thrust/detail/vector_base.inl b/thrust/thrust/detail/vector_base.inl index d66765775ce..9109dd8f502 100644 --- a/thrust/thrust/detail/vector_base.inl +++ b/thrust/thrust/detail/vector_base.inl @@ -1117,7 +1117,7 @@ bool vector_equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 fi template bool vector_equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, thrust::detail::false_type) { - typename thrust::iterator_difference::type n = thrust::distance(first1, last1); + it_difference_t n = thrust::distance(first1, last1); using FromSystem1 = typename thrust::iterator_system::type; using FromSystem2 = typename thrust::iterator_system::type; diff --git a/thrust/thrust/distance.h b/thrust/thrust/distance.h index 81ca59da012..1cbff47cffe 100644 --- a/thrust/thrust/distance.h +++ b/thrust/thrust/distance.h @@ -70,7 +70,7 @@ THRUST_NAMESPACE_BEGIN * \see https://en.cppreference.com/w/cpp/iterator/distance */ template -inline _CCCL_HOST_DEVICE typename thrust::iterator_traits::difference_type +inline _CCCL_HOST_DEVICE thrust::detail::it_difference_t distance(InputIterator first, InputIterator last); /*! \} // end iterators diff --git a/thrust/thrust/iterator/counting_iterator.h b/thrust/thrust/iterator/counting_iterator.h index b29e9cbdb5f..4f159fa5432 100644 --- a/thrust/thrust/iterator/counting_iterator.h +++ b/thrust/thrust/iterator/counting_iterator.h @@ -128,7 +128,7 @@ struct make_counting_iterator_base eval_if<::cuda::std::is_integral::value, numeric_difference, identity_<::cuda::std::ptrdiff_t>>, - iterator_difference>>; + lazy_trait>>; // our implementation departs from Boost's in that counting_iterator::dereference // returns a copy of its counter, rather than a reference to it. returning a reference diff --git a/thrust/thrust/iterator/detail/iterator_adaptor_base.h b/thrust/thrust/iterator/detail/iterator_adaptor_base.h index 5e42c9edd59..32452490741 100644 --- a/thrust/thrust/iterator/detail/iterator_adaptor_base.h +++ b/thrust/thrust/iterator/detail/iterator_adaptor_base.h @@ -61,15 +61,15 @@ template >; + using value = replace_if_use_default>; using system = replace_if_use_default>; using traversal = replace_if_use_default>; using reference = replace_if_use_default, - iterator_reference, + lazy_trait, ::cuda::std::add_lvalue_reference>>; - using difference = replace_if_use_default>; + using difference = replace_if_use_default>; public: using type = iterator_facade; diff --git a/thrust/thrust/iterator/detail/tagged_iterator.h b/thrust/thrust/iterator/detail/tagged_iterator.h index 1c21fbaca65..a97f99e5de1 100644 --- a/thrust/thrust/iterator/detail/tagged_iterator.h +++ b/thrust/thrust/iterator/detail/tagged_iterator.h @@ -40,11 +40,11 @@ template using make_tagged_iterator_base = iterator_adaptor, Iterator, - typename iterator_value::type, + it_value_t, Tag, typename iterator_traversal::type, - typename iterator_reference::type, - typename iterator_difference::type>; + it_reference_t, + it_difference_t>; template class tagged_iterator : public make_tagged_iterator_base diff --git a/thrust/thrust/iterator/iterator_traits.h b/thrust/thrust/iterator/iterator_traits.h index 225f1a173c4..d8c0f6b17a2 100644 --- a/thrust/thrust/iterator/iterator_traits.h +++ b/thrust/thrust/iterator/iterator_traits.h @@ -45,68 +45,103 @@ #include #include - -#if _CCCL_COMPILER(NVRTC) -# include -#else // _CCCL_COMPILER(NVRTC) -# include -#endif // _CCCL_COMPILER(NVRTC) +#include THRUST_NAMESPACE_BEGIN +namespace detail +{ +// the following iterator helpers are not named it_value_t etc, like the C++20 facilities, because they are defined in +// terms of C++17 iterator_traits and not the new C++20 indirectly_readable trait etc. This allows them to detect nested +// value_type, difference_type and reference aliases, which the new C+20 traits do not consider (they only consider +// specializations of iterator_traits). Also, a value_type of void remains supported (needed by some output iterators). + +template +using it_value_t = typename ::cuda::std::iterator_traits::value_type; + +template +using it_reference_t = typename ::cuda::std::iterator_traits::reference; + +template +using it_difference_t = typename ::cuda::std::iterator_traits::difference_type; + +template +using it_pointer_t = typename ::cuda::std::iterator_traits::pointer; + +// use this whenever you need to lazily evaluate a trait. E.g., as an alternative in replace_if_use_default. +template