Skip to content

Commit

Permalink
Proclaim pair and tuple trivially relocatable
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhardmgruber committed Aug 12, 2024
1 parent a3a5f9c commit 1978596
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
54 changes: 54 additions & 0 deletions thrust/testing/type_traits.cu
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
#include <thrust/iterator/iterator_traits.h>
#include <thrust/iterator/transform_iterator.h>
#include <thrust/iterator/zip_iterator.h>
#include <thrust/pair.h>
#include <thrust/tuple.h>
#include <thrust/type_traits/is_contiguous_iterator.h>

#include <cuda/std/complex>
#include <cuda/std/tuple>
#include <cuda/std/utility>

#include <unittest/unittest.h>

void TestIsContiguousIterator()
Expand Down Expand Up @@ -146,3 +152,51 @@ void TestIsCommutative()
}
}
DECLARE_UNITTEST(TestIsCommutative);

struct NonTriviallyCopyable
{
NonTriviallyCopyable(const NonTriviallyCopyable&) {}
};
THRUST_PROCLAIM_TRIVIALLY_RELOCATABLE(NonTriviallyCopyable);

static_assert(!::cuda::std::is_trivially_copyable<NonTriviallyCopyable>::value, "");
static_assert(thrust::is_trivially_relocatable<NonTriviallyCopyable>::value, "");

void TestTriviallyRelocatable()
{
static_assert(thrust::is_trivially_relocatable<int>::value, "");
#if THRUST_DEVICE_SYSTEM == THRUST_DEVICE_SYSTEM_CUDA
static_assert(thrust::is_trivially_relocatable<__half>::value, "");
static_assert(thrust::is_trivially_relocatable<int1>::value, "");
static_assert(thrust::is_trivially_relocatable<int2>::value, "");
static_assert(thrust::is_trivially_relocatable<int3>::value, "");
static_assert(thrust::is_trivially_relocatable<int4>::value, "");
# ifndef _LIBCUDACXX_HAS_NO_INT128
static_assert(thrust::is_trivially_relocatable<__int128>::value, "");
# endif // _LIBCUDACXX_HAS_NO_INT128
#endif // THRUST_DEVICE_SYSTEM == THRUST_DEVICE_SYSTEM_CUDA
static_assert(thrust::is_trivially_relocatable<thrust::complex<float>>::value, "");
static_assert(thrust::is_trivially_relocatable<::cuda::std::complex<float>>::value, "");
static_assert(thrust::is_trivially_relocatable<thrust::pair<int, thrust::complex<float>>>::value, "");
static_assert(thrust::is_trivially_relocatable<::cuda::std::pair<int, ::cuda::std::complex<float>>>::value, "");
static_assert(thrust::is_trivially_relocatable<thrust::tuple<int, thrust::complex<float>, char>>::value, "");
static_assert(thrust::is_trivially_relocatable<::cuda::std::tuple<int, ::cuda::std::complex<float>, char>>::value,
"");
static_assert(thrust::is_trivially_relocatable<
::cuda::std::tuple<thrust::pair<int, thrust::tuple<int, ::cuda::std::tuple<>>>,
thrust::tuple<::cuda::std::pair<int, thrust::tuple<>>, int>>>::value,
"");

static_assert(!thrust::is_trivially_relocatable<thrust::pair<int, std::string>>::value, "");
static_assert(!thrust::is_trivially_relocatable<::cuda::std::pair<int, std::string>>::value, "");
static_assert(!thrust::is_trivially_relocatable<thrust::tuple<int, float, std::string>>::value, "");
static_assert(!thrust::is_trivially_relocatable<::cuda::std::tuple<int, float, std::string>>::value, "");

// test propagation of relocatability through pair and tuple
static_assert(thrust::is_trivially_relocatable<NonTriviallyCopyable>::value, "");
static_assert(thrust::is_trivially_relocatable<thrust::pair<NonTriviallyCopyable, int>>::value, "");
static_assert(thrust::is_trivially_relocatable<::cuda::std::pair<NonTriviallyCopyable, int>>::value, "");
static_assert(thrust::is_trivially_relocatable<thrust::tuple<NonTriviallyCopyable>>::value, "");
static_assert(thrust::is_trivially_relocatable<::cuda::std::tuple<NonTriviallyCopyable>>::value, "");
};
DECLARE_UNITTEST(TestTriviallyRelocatable);
8 changes: 8 additions & 0 deletions thrust/thrust/pair.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
# pragma system_header
#endif // no system header

#include <thrust/type_traits/is_trivially_relocatable.h>

#include <cuda/std/__type_traits/conjunction.h>
#include <cuda/std/utility>

THRUST_NAMESPACE_BEGIN
Expand Down Expand Up @@ -117,6 +120,11 @@ make_pair(T1&& t1, T2&& t2)

using _CUDA_VSTD::get;

template <typename T, typename U>
struct proclaim_trivially_relocatable<pair<T, U>>
: ::cuda::std::conjunction<is_trivially_relocatable<T>, is_trivially_relocatable<U>>
{};

/*! \endcond
*/

Expand Down
6 changes: 6 additions & 0 deletions thrust/thrust/tuple.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
# pragma system_header
#endif // no system header

#include <thrust/type_traits/is_trivially_relocatable.h>

#include <cuda/std/tuple>
#include <cuda/std/type_traits>
#include <cuda/std/utility>
Expand Down Expand Up @@ -232,6 +234,10 @@ inline _CCCL_HOST_DEVICE tuple<Ts&...> tie(Ts&... ts) noexcept

using _CUDA_VSTD::get;

template <typename... Ts>
struct proclaim_trivially_relocatable<tuple<Ts...>> : ::cuda::std::conjunction<is_trivially_relocatable<Ts>...>
{};

/*! \endcond
*/

Expand Down
16 changes: 16 additions & 0 deletions thrust/thrust/type_traits/is_trivially_relocatable.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
#include <thrust/detail/type_traits.h>
#include <thrust/type_traits/is_contiguous_iterator.h>

#include <cuda/std/__fwd/pair.h>
#include <cuda/std/__fwd/tuple.h>
#include <cuda/std/__type_traits/conjunction.h>

#include <type_traits>

THRUST_NAMESPACE_BEGIN
Expand Down Expand Up @@ -285,6 +289,18 @@ THRUST_PROCLAIM_TRIVIALLY_RELOCATABLE(double3)
THRUST_PROCLAIM_TRIVIALLY_RELOCATABLE(double4)
#endif

THRUST_NAMESPACE_BEGIN
template <typename T, typename U>
struct proclaim_trivially_relocatable<::cuda::std::pair<T, U>>
: ::cuda::std::conjunction<is_trivially_relocatable<T>, is_trivially_relocatable<U>>
{};

template <typename... Ts>
struct proclaim_trivially_relocatable<::cuda::std::tuple<Ts...>>
: ::cuda::std::conjunction<is_trivially_relocatable<Ts>...>
{};
THRUST_NAMESPACE_END

/*! \endcond
*/

Expand Down

0 comments on commit 1978596

Please sign in to comment.