diff --git a/thrust/thrust/detail/contiguous_storage.h b/thrust/thrust/detail/contiguous_storage.h index 0092f0a1f61..003d7b3e396 100644 --- a/thrust/thrust/detail/contiguous_storage.h +++ b/thrust/thrust/detail/contiguous_storage.h @@ -30,6 +30,8 @@ #include #include +#include + THRUST_NAMESPACE_BEGIN namespace detail @@ -38,6 +40,13 @@ namespace detail struct copy_allocator_t {}; +struct allocator_mismatch_on_swap : std::runtime_error +{ + allocator_mismatch_on_swap() + : std::runtime_error("swap called on containers with allocators that propagate on swap, but compare non-equal") + {} +}; + // XXX parameter T is redundant with parameter Alloc template class contiguous_storage @@ -70,6 +79,8 @@ class contiguous_storage _CCCL_EXEC_CHECK_DISABLE _CCCL_HOST_DEVICE explicit contiguous_storage(copy_allocator_t, const contiguous_storage& other, size_type n); + contiguous_storage& operator=(const contiguous_storage& x) = delete; + _CCCL_EXEC_CHECK_DISABLE _CCCL_HOST_DEVICE ~contiguous_storage(); @@ -100,7 +111,29 @@ class contiguous_storage _CCCL_HOST_DEVICE void deallocate() noexcept; - _CCCL_HOST_DEVICE void swap(contiguous_storage& x); + _CCCL_EXEC_CHECK_DISABLE + _CCCL_HOST_DEVICE void swap(contiguous_storage& x) + { + using ::cuda::std::swap; + swap(m_begin, x.m_begin); + swap(m_size, x.m_size); + + // From C++ standard [container.reqmts] + // If allocator_traits::propagate_on_container_swap::value is true, then allocator_type + // shall meet the Cpp17Swappable requirements and the allocators of a and b shall also be exchanged by calling + // swap as described in [swappable.requirements]. Otherwise, the allocators shall not be swapped, and the behavior + // is undefined unless a.get_allocator() == b.get_allocator(). + _CCCL_IF_CONSTEXPR (allocator_traits::propagate_on_container_swap::value) + { + swap(m_allocator, x.m_allocator); + } + else _CCCL_IF_CONSTEXPR (!allocator_traits::is_always_equal::value) + { + NV_IF_TARGET(NV_IS_DEVICE, (assert(m_allocator == other);), (if (m_allocator != x.m_allocator) { + throw allocator_mismatch_on_swap(); + })); + } + } _CCCL_HOST_DEVICE void value_initialize_n(iterator first, size_type n); @@ -122,23 +155,42 @@ class contiguous_storage _CCCL_HOST_DEVICE void destroy(iterator first, iterator last) noexcept; - _CCCL_HOST_DEVICE void deallocate_on_allocator_mismatch(const contiguous_storage& other) noexcept; + _CCCL_HOST_DEVICE void deallocate_on_allocator_mismatch(const contiguous_storage& other) noexcept + { + // TODO(bgruber): replace dispatch by if constexpr in C++17 + integral_constant::propagate_on_container_copy_assignment::value> c; + deallocate_on_allocator_mismatch_dispatch(c, other); + } _CCCL_HOST_DEVICE void - destroy_on_allocator_mismatch(const contiguous_storage& other, iterator first, iterator last) noexcept; + destroy_on_allocator_mismatch(const contiguous_storage& other, iterator first, iterator last) noexcept + { + // TODO(bgruber): replace dispatch by if constexpr in C++17 + integral_constant::propagate_on_container_copy_assignment::value> c; + destroy_on_allocator_mismatch_dispatch(c, other, first, last); + } _CCCL_HOST_DEVICE void set_allocator(const allocator_type& alloc); - _CCCL_HOST_DEVICE bool is_allocator_not_equal(const allocator_type& alloc) const; - - _CCCL_HOST_DEVICE bool is_allocator_not_equal(const contiguous_storage& other) const; - - _CCCL_HOST_DEVICE void propagate_allocator(const contiguous_storage& other); + _CCCL_EXEC_CHECK_DISABLE + _CCCL_HOST_DEVICE void propagate_allocator(const contiguous_storage& other) + { + _CCCL_IF_CONSTEXPR (allocator_traits::propagate_on_container_copy_assignment::value) + { + m_allocator = other.m_allocator; + } + } - _CCCL_HOST_DEVICE void propagate_allocator(contiguous_storage& other); + _CCCL_EXEC_CHECK_DISABLE + _CCCL_HOST_DEVICE void propagate_allocator(contiguous_storage& other) + { + _CCCL_IF_CONSTEXPR (allocator_traits::propagate_on_container_move_assignment::value) + { + m_allocator = ::cuda::std::move(other.m_allocator); + } + } // allow move assignment for a sane implementation of allocator propagation - // on move assignment _CCCL_HOST_DEVICE contiguous_storage& operator=(contiguous_storage&& other); _CCCL_SYNTHESIZE_SEQUENCE_ACCESS(contiguous_storage, const_iterator); @@ -151,34 +203,31 @@ class contiguous_storage size_type m_size; - // disallow assignment - contiguous_storage& operator=(const contiguous_storage& x); - - _CCCL_HOST_DEVICE void swap_allocators(true_type, const allocator_type&); - - _CCCL_HOST_DEVICE void swap_allocators(false_type, allocator_type&); - - _CCCL_HOST_DEVICE bool is_allocator_not_equal_dispatch(true_type, const allocator_type&) const; - - _CCCL_HOST_DEVICE bool is_allocator_not_equal_dispatch(false_type, const allocator_type&) const; - - _CCCL_HOST_DEVICE void deallocate_on_allocator_mismatch_dispatch(true_type, const contiguous_storage& other) noexcept; - - _CCCL_HOST_DEVICE void deallocate_on_allocator_mismatch_dispatch(false_type, const contiguous_storage& other) noexcept; + _CCCL_EXEC_CHECK_DISABLE + _CCCL_HOST_DEVICE void deallocate_on_allocator_mismatch_dispatch(true_type, const contiguous_storage& other) noexcept + { + if (m_allocator != other.m_allocator) + { + deallocate(); + } + } - _CCCL_HOST_DEVICE void destroy_on_allocator_mismatch_dispatch( - true_type, const contiguous_storage& other, iterator first, iterator last) noexcept; + _CCCL_HOST_DEVICE static void deallocate_on_allocator_mismatch_dispatch(false_type, const contiguous_storage&) noexcept + {} + _CCCL_EXEC_CHECK_DISABLE _CCCL_HOST_DEVICE void destroy_on_allocator_mismatch_dispatch( - false_type, const contiguous_storage& other, iterator first, iterator last) noexcept; - - _CCCL_HOST_DEVICE void propagate_allocator_dispatch(true_type, const contiguous_storage& other); - - _CCCL_HOST_DEVICE void propagate_allocator_dispatch(false_type, const contiguous_storage& other); - - _CCCL_HOST_DEVICE void propagate_allocator_dispatch(true_type, contiguous_storage& other); - - _CCCL_HOST_DEVICE void propagate_allocator_dispatch(false_type, contiguous_storage& other); + true_type, const contiguous_storage& other, iterator first, iterator last) noexcept + { + if (m_allocator != other.m_allocator) + { + destroy(first, last); + } + } // end contiguous_storage::destroy_on_allocator_mismatch() + + _CCCL_HOST_DEVICE static void destroy_on_allocator_mismatch_dispatch( + false_type, const contiguous_storage& other, iterator first, iterator last) noexcept + {} friend _CCCL_HOST_DEVICE void swap(contiguous_storage& lhs, contiguous_storage& rhs) noexcept(noexcept(lhs.swap(rhs))) { diff --git a/thrust/thrust/detail/contiguous_storage.inl b/thrust/thrust/detail/contiguous_storage.inl index f85638ffb6f..df78e0e0fc6 100644 --- a/thrust/thrust/detail/contiguous_storage.inl +++ b/thrust/thrust/detail/contiguous_storage.inl @@ -25,15 +25,13 @@ #elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) # pragma system_header #endif // no system header -#include + #include #include #include #include #include -#include - #include // for std::runtime_error #include // for use of std::swap in the WAR below @@ -44,14 +42,6 @@ THRUST_NAMESPACE_BEGIN namespace detail { -class allocator_mismatch_on_swap : public std::runtime_error -{ -public: - allocator_mismatch_on_swap() - : std::runtime_error("swap called on containers with allocators that propagate on swap, but compare non-equal") - {} -}; - _CCCL_EXEC_CHECK_DISABLE template _CCCL_HOST_DEVICE contiguous_storage::contiguous_storage(const Alloc& alloc) @@ -191,20 +181,6 @@ _CCCL_HOST_DEVICE void contiguous_storage::deallocate() noexcept } // end if } // end contiguous_storage::deallocate() -_CCCL_EXEC_CHECK_DISABLE -template -_CCCL_HOST_DEVICE void contiguous_storage::swap(contiguous_storage& x) -{ - using ::cuda::std::swap; - swap(m_begin, x.m_begin); - swap(m_size, x.m_size); - - // FIXME(bgruber): swap_allocators already swaps m_allocator, so we are swapping twice here !! - swap_allocators(integral_constant::propagate_on_container_swap::value>(), - x.m_allocator); - swap(m_allocator, x.m_allocator); -} // end contiguous_storage::swap() - template _CCCL_HOST_DEVICE void contiguous_storage::value_initialize_n(iterator first, size_type n) { @@ -262,24 +238,6 @@ _CCCL_HOST_DEVICE void contiguous_storage::destroy(iterator first, ite destroy_range(m_allocator, first.base(), last - first); } // end contiguous_storage::destroy() -template -_CCCL_HOST_DEVICE void -contiguous_storage::deallocate_on_allocator_mismatch(const contiguous_storage& other) noexcept -{ - integral_constant::propagate_on_container_copy_assignment::value> c; - - deallocate_on_allocator_mismatch_dispatch(c, other); -} // end contiguous_storage::deallocate_on_allocator_mismatch - -template -_CCCL_HOST_DEVICE void contiguous_storage::destroy_on_allocator_mismatch( - const contiguous_storage& other, iterator first, iterator last) noexcept -{ - integral_constant::propagate_on_container_copy_assignment::value> c; - - destroy_on_allocator_mismatch_dispatch(c, other, first, last); -} // end contiguous_storage::destroy_on_allocator_mismatch - _CCCL_EXEC_CHECK_DISABLE template _CCCL_HOST_DEVICE void contiguous_storage::set_allocator(const Alloc& alloc) @@ -287,36 +245,7 @@ _CCCL_HOST_DEVICE void contiguous_storage::set_allocator(const Alloc& m_allocator = alloc; } // end contiguous_storage::set_allocator() -template -_CCCL_HOST_DEVICE bool contiguous_storage::is_allocator_not_equal(const Alloc& alloc) const -{ - return is_allocator_not_equal_dispatch( - integral_constant::is_always_equal::value>(), alloc); -} // end contiguous_storage::is_allocator_not_equal() - -template -_CCCL_HOST_DEVICE bool -contiguous_storage::is_allocator_not_equal(const contiguous_storage& other) const -{ - return is_allocator_not_equal(m_allocator, other.m_allocator); -} // end contiguous_storage::is_allocator_not_equal() - -template -_CCCL_HOST_DEVICE void contiguous_storage::propagate_allocator(const contiguous_storage& other) -{ - integral_constant::propagate_on_container_copy_assignment::value> c; - - propagate_allocator_dispatch(c, other); -} // end contiguous_storage::propagate_allocator() - -template -_CCCL_HOST_DEVICE void contiguous_storage::propagate_allocator(contiguous_storage& other) -{ - integral_constant::propagate_on_container_move_assignment::value> c; - - propagate_allocator_dispatch(c, other); -} // end contiguous_storage::propagate_allocator() - +_CCCL_EXEC_CHECK_DISABLE template _CCCL_HOST_DEVICE contiguous_storage& contiguous_storage::operator=(contiguous_storage&& other) { @@ -324,7 +253,11 @@ _CCCL_HOST_DEVICE contiguous_storage& contiguous_storage::op { deallocate(); } - propagate_allocator(other); + _CCCL_IF_CONSTEXPR (allocator_traits::propagate_on_container_move_assignment::value) + { + m_allocator = ::cuda::std::move(other.m_allocator); + } + m_begin = std::move(other.m_begin); m_size = std::move(other.m_size); @@ -332,96 +265,7 @@ _CCCL_HOST_DEVICE contiguous_storage& contiguous_storage::op other.m_size = 0; return *this; -} // end contiguous_storage::propagate_allocator() - -template -_CCCL_HOST_DEVICE void contiguous_storage::swap_allocators(true_type, const Alloc&) -{} // end contiguous_storage::swap_allocators() - -_CCCL_EXEC_CHECK_DISABLE -template -_CCCL_HOST_DEVICE void contiguous_storage::swap_allocators(false_type, Alloc& other) -{ - // FIXME(bgruber): it is really concerning, that swapping an allocator can throw. swap() should be noexcept in - // general. - NV_IF_TARGET(NV_IS_DEVICE, - ( - // allocators must be equal when swapping containers with allocators that propagate on swap - assert(!is_allocator_not_equal(other));), - (if (is_allocator_not_equal(other)) { throw allocator_mismatch_on_swap(); })); - using ::cuda::std::swap; - swap(m_allocator, other); -} // end contiguous_storage::swap_allocators() - -template -_CCCL_HOST_DEVICE bool -contiguous_storage::is_allocator_not_equal_dispatch(true_type /*is_always_equal*/, const Alloc&) const -{ - return false; -} // end contiguous_storage::is_allocator_not_equal_dispatch() - -_CCCL_EXEC_CHECK_DISABLE -template -_CCCL_HOST_DEVICE bool -contiguous_storage::is_allocator_not_equal_dispatch(false_type /*!is_always_equal*/, const Alloc& other) const -{ - return m_allocator != other; -} // end contiguous_storage::is_allocator_not_equal_dispatch() - -_CCCL_EXEC_CHECK_DISABLE -template -_CCCL_HOST_DEVICE void contiguous_storage::deallocate_on_allocator_mismatch_dispatch( - true_type, const contiguous_storage& other) noexcept -{ - if (m_allocator != other.m_allocator) - { - deallocate(); - } -} // end contiguous_storage::deallocate_on_allocator_mismatch() - -template -_CCCL_HOST_DEVICE void -contiguous_storage::deallocate_on_allocator_mismatch_dispatch(false_type, const contiguous_storage&) noexcept -{} // end contiguous_storage::deallocate_on_allocator_mismatch() - -_CCCL_EXEC_CHECK_DISABLE -template -_CCCL_HOST_DEVICE void contiguous_storage::destroy_on_allocator_mismatch_dispatch( - true_type, const contiguous_storage& other, iterator first, iterator last) noexcept -{ - if (m_allocator != other.m_allocator) - { - destroy(first, last); - } -} // end contiguous_storage::destroy_on_allocator_mismatch() - -template -_CCCL_HOST_DEVICE void contiguous_storage::destroy_on_allocator_mismatch_dispatch( - false_type, const contiguous_storage&, iterator, iterator) noexcept -{} // end contiguous_storage::destroy_on_allocator_mismatch() - -_CCCL_EXEC_CHECK_DISABLE -template -_CCCL_HOST_DEVICE void -contiguous_storage::propagate_allocator_dispatch(true_type, const contiguous_storage& other) -{ - m_allocator = other.m_allocator; -} // end contiguous_storage::propagate_allocator() - -template -_CCCL_HOST_DEVICE void contiguous_storage::propagate_allocator_dispatch(false_type, const contiguous_storage&) -{} // end contiguous_storage::propagate_allocator() - -_CCCL_EXEC_CHECK_DISABLE -template -_CCCL_HOST_DEVICE void contiguous_storage::propagate_allocator_dispatch(true_type, contiguous_storage& other) -{ - m_allocator = std::move(other.m_allocator); -} // end contiguous_storage::propagate_allocator() - -template -_CCCL_HOST_DEVICE void contiguous_storage::propagate_allocator_dispatch(false_type, contiguous_storage&) -{} // end contiguous_storage::propagate_allocator() +} } // namespace detail