Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cuda::std::addressof in Thrust #3363

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions thrust/thrust/addressof.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,8 @@
# pragma system_header
#endif // no system header

#include <thrust/detail/memory_wrapper.h>
#include <cuda/std/__memory/addressof.h>

THRUST_NAMESPACE_BEGIN

///////////////////////////////////////////////////////////////////////////////

/*! Obtains the actual address of the object or function arg, even in presence of overloaded operator&.
*/
template <typename T>
_CCCL_HOST_DEVICE T* addressof(T& arg)
{
return reinterpret_cast<T*>(&const_cast<char&>(reinterpret_cast<const volatile char&>(arg)));
}

///////////////////////////////////////////////////////////////////////////////

using ::cuda::std::addressof;
THRUST_NAMESPACE_END
27 changes: 14 additions & 13 deletions thrust/thrust/detail/memory_algorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
# pragma system_header
#endif // no system header
#include <thrust/addressof.h>
#include <thrust/detail/allocator/allocator_traits.h>
#include <thrust/detail/memory_wrapper.h>
#include <thrust/detail/type_traits.h>
#include <thrust/iterator/iterator_traits.h>

#include <cuda/std/__memory/addressof.h>

#include <new>
#include <utility>

Expand Down Expand Up @@ -54,7 +55,7 @@ _CCCL_HOST_DEVICE ForwardIt destroy(ForwardIt first, ForwardIt last) noexcept
{
for (; first != last; ++first)
{
destroy_at(addressof(*first));
destroy_at(::cuda::std::addressof(*first));
}

return first;
Expand All @@ -71,7 +72,7 @@ _CCCL_HOST_DEVICE ForwardIt destroy(Allocator const& alloc, ForwardIt first, For

for (; first != last; ++first)
{
destroy_at(alloc_T, addressof(*first));
destroy_at(alloc_T, ::cuda::std::addressof(*first));
}

return first;
Expand All @@ -82,7 +83,7 @@ _CCCL_HOST_DEVICE ForwardIt destroy_n(ForwardIt first, Size n) noexcept
{
for (; n > 0; (void) ++first, --n)
{
destroy_at(addressof(*first));
destroy_at(::cuda::std::addressof(*first));
}

return first;
Expand All @@ -99,7 +100,7 @@ _CCCL_HOST_DEVICE ForwardIt destroy_n(Allocator const& alloc, ForwardIt first, S

for (; n > 0; (void) ++first, --n)
{
destroy_at(alloc_T, addressof(*first));
destroy_at(alloc_T, ::cuda::std::addressof(*first));
}

return first;
Expand All @@ -119,13 +120,13 @@ _CCCL_HOST_DEVICE void uninitialized_construct(ForwardIt first, ForwardIt last,
try {
for (; current != last; ++current)
{
::new (static_cast<void*>(addressof(*current))) T(args...);
::new (static_cast<void*>(::cuda::std::addressof(*current))) T(args...);
}
} catch (...) {
destroy(first, current);
throw;
}),
(for (; current != last; ++current) { ::new (static_cast<void*>(addressof(*current))) T(args...); }));
(for (; current != last; ++current) { ::new (static_cast<void*>(::cuda::std::addressof(*current))) T(args...); }));
}

template <typename Allocator, typename ForwardIt, typename... Args>
Expand All @@ -146,13 +147,13 @@ void uninitialized_construct_with_allocator(Allocator const& alloc, ForwardIt fi
try {
for (; current != last; ++current)
{
traits::construct(alloc_T, addressof(*current), args...);
traits::construct(alloc_T, ::cuda::std::addressof(*current), args...);
}
} catch (...) {
destroy(alloc_T, first, current);
throw;
}),
(for (; current != last; ++current) { traits::construct(alloc_T, addressof(*current), args...); }));
(for (; current != last; ++current) { traits::construct(alloc_T, ::cuda::std::addressof(*current), args...); }));
}

template <typename ForwardIt, typename Size, typename... Args>
Expand All @@ -169,13 +170,13 @@ void uninitialized_construct_n(ForwardIt first, Size n, Args const&... args)
try {
for (; n > 0; ++current, --n)
{
::new (static_cast<void*>(addressof(*current))) T(args...);
::new (static_cast<void*>(::cuda::std::addressof(*current))) T(args...);
}
} catch (...) {
destroy(first, current);
throw;
}),
(for (; n > 0; ++current, --n) { ::new (static_cast<void*>(addressof(*current))) T(args...); }));
(for (; n > 0; ++current, --n) { ::new (static_cast<void*>(::cuda::std::addressof(*current))) T(args...); }));
}

template <typename Allocator, typename ForwardIt, typename Size, typename... Args>
Expand All @@ -196,13 +197,13 @@ void uninitialized_construct_n_with_allocator(Allocator const& alloc, ForwardIt
try {
for (; n > 0; (void) ++current, --n)
{
traits::construct(alloc_T, addressof(*current), args...);
traits::construct(alloc_T, ::cuda::std::addressof(*current), args...);
}
} catch (...) {
destroy(alloc_T, first, current);
throw;
}),
(for (; n > 0; (void) ++current, --n) { traits::construct(alloc_T, addressof(*current), args...); }));
(for (; n > 0; (void) ++current, --n) { traits::construct(alloc_T, ::cuda::std::addressof(*current), args...); }));
}

///////////////////////////////////////////////////////////////////////////////
Expand Down
20 changes: 10 additions & 10 deletions thrust/thrust/optional.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
# pragma system_header
#endif // no system header
#include <thrust/addressof.h>
#include <thrust/detail/type_traits.h>
#include <thrust/swap.h>

#include <cuda/std/__memory/addressof.h>
#include <cuda/std/__type_traits/void_t.h>

#define THRUST_OPTIONAL_VERSION_MAJOR 0
Expand Down Expand Up @@ -398,7 +398,7 @@ struct optional_operations_base : optional_storage_base<T>
template <class... Args>
_CCCL_HOST_DEVICE void construct(Args&&... args) noexcept
{
new (thrust::addressof(this->m_value)) T(std::forward<Args>(args)...);
new (::cuda::std::addressof(this->m_value)) T(std::forward<Args>(args)...);
this->m_has_value = true;
}

Expand Down Expand Up @@ -1555,13 +1555,13 @@ class optional
}
else
{
new (thrust::addressof(rhs.m_value)) T(std::move(this->m_value));
new (::cuda::std::addressof(rhs.m_value)) T(std::move(this->m_value));
this->m_value.T::~T();
}
}
else if (rhs.has_value())
{
new (thrust::addressof(this->m_value)) T(std::move(rhs.m_value));
new (::cuda::std::addressof(this->m_value)) T(std::move(rhs.m_value));
rhs.m_value.T::~T();
}
}
Expand All @@ -1573,15 +1573,15 @@ class optional
_CCCL_EXEC_CHECK_DISABLE
_CCCL_HOST_DEVICE constexpr const T* operator->() const
{
return thrust::addressof(this->m_value);
return ::cuda::std::addressof(this->m_value);
}

/// \group pointer
/// \synopsis constexpr T *operator->();
_CCCL_EXEC_CHECK_DISABLE
_CCCL_HOST_DEVICE THRUST_OPTIONAL_CPP11_CONSTEXPR T* operator->()
{
return thrust::addressof(this->m_value);
return ::cuda::std::addressof(this->m_value);
}

/// \return the stored value
Expand Down Expand Up @@ -2614,7 +2614,7 @@ class optional<T&>
_CCCL_EXEC_CHECK_DISABLE
template <class U = T, detail::enable_if_t<!detail::is_optional<detail::decay_t<U>>::value>* = nullptr>
_CCCL_HOST_DEVICE constexpr optional(U&& u)
: m_value(thrust::addressof(u))
: m_value(::cuda::std::addressof(u))
{
static_assert(std::is_lvalue_reference<U>::value, "U must be an lvalue");
}
Expand Down Expand Up @@ -2656,7 +2656,7 @@ class optional<T&>
_CCCL_HOST_DEVICE optional& operator=(U&& u)
{
static_assert(std::is_lvalue_reference<U>::value, "U must be an lvalue");
m_value = thrust::addressof(u);
m_value = ::cuda::std::addressof(u);
return *this;
}

Expand All @@ -2668,7 +2668,7 @@ class optional<T&>
template <class U>
_CCCL_HOST_DEVICE optional& operator=(const optional<U>& rhs)
{
m_value = thrust::addressof(rhs.value());
m_value = ::cuda::std::addressof(rhs.value());
return *this;
}

Expand All @@ -2680,7 +2680,7 @@ class optional<T&>
template <class U>
_CCCL_HOST_DEVICE T& emplace(U& u) noexcept
{
m_value = thrust::addressof(u);
m_value = ::cuda::std::addressof(u);
return *m_value;
}

Expand Down
3 changes: 2 additions & 1 deletion thrust/thrust/system/cuda/detail/future.inl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
# include <thrust/type_traits/integer_sequence.h>
# include <thrust/type_traits/remove_cvref.h>

# include <cuda/std/__memory/addressof.h>
# include <cuda/std/__memory/unique_ptr.h>

# include <type_traits>
Expand Down Expand Up @@ -639,7 +640,7 @@ public:
// For testing only.
_CCCL_HOST_DEVICE raw_const_pointer data() const
{
return addressof(value_);
return ::cuda::std::addressof(value_);
}
# endif
};
Expand Down
Loading