Skip to content

Commit 82b226e

Browse files
authored
Drop most parts of thrust::allocator_traits (#7286)
* Drop most parts of `thrust::allocator_traits` We already have standard conforming `cuda::std::allocator_traits` Reuse those and only keep those pieces we require for legacy / systems support * Avoidnarrowing warning with old gcc * Drop `reference` and `const_reference` members * Drop unused `is_allocator` * Drop the deprecated `other` type alias * Fully drop `detail::allocator_traits` * Fix formatting
1 parent 8901444 commit 82b226e

23 files changed

+224
-727
lines changed

libcudacxx/include/cuda/std/__memory/allocator_traits.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,11 +326,13 @@ struct _CCCL_TYPE_VISIBILITY_DEFAULT allocator_traits
326326
template <class _Tp>
327327
using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
328328

329+
_CCCL_EXEC_CHECK_DISABLE
329330
[[nodiscard]] _CCCL_API inline _CCCL_CONSTEXPR_CXX20 static pointer allocate(allocator_type& __a, size_type __n)
330331
{
331332
return __a.allocate(__n);
332333
}
333334

335+
_CCCL_EXEC_CHECK_DISABLE
334336
[[nodiscard]] _CCCL_API inline _CCCL_CONSTEXPR_CXX20 static pointer
335337
allocate(allocator_type& __a, size_type __n, [[maybe_unused]] const_void_pointer __hint)
336338
{
@@ -344,11 +346,13 @@ struct _CCCL_TYPE_VISIBILITY_DEFAULT allocator_traits
344346
}
345347
}
346348

349+
_CCCL_EXEC_CHECK_DISABLE
347350
_CCCL_API inline _CCCL_CONSTEXPR_CXX20 static void deallocate(allocator_type& __a, pointer __p, size_type __n) noexcept
348351
{
349352
__a.deallocate(__p, __n);
350353
}
351354

355+
_CCCL_EXEC_CHECK_DISABLE
352356
template <class _Tp, class... _Args>
353357
_CCCL_API inline _CCCL_CONSTEXPR_CXX20 static void
354358
construct([[maybe_unused]] allocator_type& __a, _Tp* __p, _Args&&... __args)
@@ -359,10 +363,15 @@ struct _CCCL_TYPE_VISIBILITY_DEFAULT allocator_traits
359363
}
360364
else
361365
{
366+
#if _CCCL_COMPILER(GCC, <, 8) // GCC7 and below fail with narrowing conversions
367+
::new (const_cast<void*>(static_cast<const volatile void*>(__p))) _Tp(::cuda::std::forward<_Args>(__args)...);
368+
#else // ^^^ _CCCL_COMPILER(GCC, <, 8) ^^^ / vvv _CCCL_COMPILER(GCC, >=, 8) vvv
362369
::cuda::std::__construct_at(__p, ::cuda::std::forward<_Args>(__args)...);
370+
#endif // _CCCL_COMPILER(GCC, >=, 8)
363371
}
364372
}
365373

374+
_CCCL_EXEC_CHECK_DISABLE
366375
template <class _Tp>
367376
_CCCL_API inline _CCCL_CONSTEXPR_CXX20 static void destroy([[maybe_unused]] allocator_type& __a, _Tp* __p) noexcept
368377
{
@@ -376,6 +385,7 @@ struct _CCCL_TYPE_VISIBILITY_DEFAULT allocator_traits
376385
}
377386
}
378387

388+
_CCCL_EXEC_CHECK_DISABLE
379389
_CCCL_API inline _CCCL_CONSTEXPR_CXX20 static size_type max_size([[maybe_unused]] const allocator_type& __a) noexcept
380390
{
381391
if constexpr (__has_max_size<const _Alloc>)
@@ -388,6 +398,7 @@ struct _CCCL_TYPE_VISIBILITY_DEFAULT allocator_traits
388398
}
389399
}
390400

401+
_CCCL_EXEC_CHECK_DISABLE
391402
_CCCL_API inline _CCCL_CONSTEXPR_CXX20 static allocator_type
392403
select_on_container_copy_construction(const allocator_type& __a)
393404
{
@@ -401,6 +412,7 @@ struct _CCCL_TYPE_VISIBILITY_DEFAULT allocator_traits
401412
}
402413
}
403414

415+
_CCCL_EXEC_CHECK_DISABLE
404416
template <class _Ptr>
405417
_CCCL_API inline static void
406418
__construct_forward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)

thrust/testing/allocator.cu

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,14 @@ DECLARE_VARIABLE_UNITTEST(TestAllocatorMinimal);
186186
void TestAllocatorTraitsRebind()
187187
{
188188
ASSERT_EQUAL(
189-
(::cuda::std::is_same<typename thrust::detail::allocator_traits<
190-
thrust::device_malloc_allocator<int>>::template rebind_traits<float>::other,
191-
typename thrust::detail::allocator_traits<thrust::device_malloc_allocator<float>>>::value),
189+
(::cuda::std::is_same<
190+
typename cuda::std::allocator_traits<thrust::device_malloc_allocator<int>>::template rebind_traits<float>,
191+
typename cuda::std::allocator_traits<thrust::device_malloc_allocator<float>>>::value),
192192
true);
193193

194194
ASSERT_EQUAL(
195-
(::cuda::std::is_same<
196-
typename thrust::detail::allocator_traits<my_minimal_allocator<int>>::template rebind_traits<float>::other,
197-
typename thrust::detail::allocator_traits<my_minimal_allocator<float>>>::value),
195+
(::cuda::std::is_same<typename cuda::std::allocator_traits<my_minimal_allocator<int>>::template rebind_traits<float>,
196+
typename cuda::std::allocator_traits<my_minimal_allocator<float>>>::value),
198197
true);
199198
}
200199
DECLARE_UNITTEST(TestAllocatorTraitsRebind);
@@ -203,24 +202,24 @@ void TestAllocatorTraitsRebindCpp11()
203202
{
204203
ASSERT_EQUAL(
205204
(::cuda::std::is_same<
206-
typename thrust::detail::allocator_traits<thrust::device_malloc_allocator<int>>::template rebind_alloc<float>,
205+
typename cuda::std::allocator_traits<thrust::device_malloc_allocator<int>>::template rebind_alloc<float>,
207206
thrust::device_malloc_allocator<float>>::value),
208207
true);
209208

210-
ASSERT_EQUAL((::cuda::std::is_same<
211-
typename thrust::detail::allocator_traits<my_minimal_allocator<int>>::template rebind_alloc<float>,
212-
my_minimal_allocator<float>>::value),
213-
true);
209+
ASSERT_EQUAL(
210+
(::cuda::std::is_same<typename cuda::std::allocator_traits<my_minimal_allocator<int>>::template rebind_alloc<float>,
211+
my_minimal_allocator<float>>::value),
212+
true);
214213

215214
ASSERT_EQUAL(
216215
(::cuda::std::is_same<
217-
typename thrust::detail::allocator_traits<thrust::device_malloc_allocator<int>>::template rebind_traits<float>,
218-
typename thrust::detail::allocator_traits<thrust::device_malloc_allocator<float>>>::value),
216+
typename cuda::std::allocator_traits<thrust::device_malloc_allocator<int>>::template rebind_traits<float>,
217+
typename cuda::std::allocator_traits<thrust::device_malloc_allocator<float>>>::value),
219218
true);
220219

221-
ASSERT_EQUAL((::cuda::std::is_same<
222-
typename thrust::detail::allocator_traits<my_minimal_allocator<int>>::template rebind_traits<float>,
223-
typename thrust::detail::allocator_traits<my_minimal_allocator<float>>>::value),
224-
true);
220+
ASSERT_EQUAL(
221+
(::cuda::std::is_same<typename cuda::std::allocator_traits<my_minimal_allocator<int>>::template rebind_traits<float>,
222+
typename cuda::std::allocator_traits<my_minimal_allocator<float>>>::value),
223+
true);
225224
}
226225
DECLARE_UNITTEST(TestAllocatorTraitsRebindCpp11);

thrust/testing/binary_search_vector.cu

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <thrust/binary_search.h>
2-
#include <thrust/detail/allocator/allocator_traits.h>
2+
#include <thrust/detail/allocator/allocator_system.h>
33
#include <thrust/iterator/discard_iterator.h>
44
#include <thrust/iterator/retag.h>
55
#include <thrust/sequence.h>
@@ -16,7 +16,7 @@ template <class ExampleVector, typename NewType>
1616
struct vector_like
1717
{
1818
using alloc = typename ExampleVector::allocator_type;
19-
using alloc_traits = typename thrust::detail::allocator_traits<alloc>;
19+
using alloc_traits = typename cuda::std::allocator_traits<alloc>;
2020
using new_alloc = typename alloc_traits::template rebind_alloc<NewType>;
2121
using type = thrust::detail::vector_base<NewType, new_alloc>;
2222
};

thrust/testing/binary_search_vector_descending.cu

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <thrust/binary_search.h>
2-
#include <thrust/detail/allocator/allocator_traits.h>
2+
#include <thrust/detail/allocator/allocator_system.h>
33
#include <thrust/functional.h>
44
#include <thrust/sequence.h>
55
#include <thrust/sort.h>
@@ -15,7 +15,7 @@ template <class ExampleVector, typename NewType>
1515
struct vector_like
1616
{
1717
using alloc = typename ExampleVector::allocator_type;
18-
using alloc_traits = typename thrust::detail::allocator_traits<alloc>;
18+
using alloc_traits = typename cuda::std::allocator_traits<alloc>;
1919
using new_alloc = typename alloc_traits::template rebind_alloc<NewType>;
2020
using type = thrust::detail::vector_base<NewType, new_alloc>;
2121
};

thrust/testing/caching_allocator.cu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
template <typename Allocator>
88
void test_implementation(Allocator alloc)
99
{
10-
using Traits = typename thrust::detail::allocator_traits<Allocator>;
10+
using Traits = typename cuda::std::allocator_traits<Allocator>;
1111
using Ptr = typename Allocator::pointer;
1212

1313
Ptr p = Traits::allocate(alloc, 123);

thrust/testing/catch2_test_functional_placeholders_logical.cu

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <thrust/detail/allocator/allocator_traits.h>
1+
#include <thrust/detail/allocator/allocator_system.h>
22
#include <thrust/functional.h>
33
#include <thrust/transform.h>
44

@@ -13,7 +13,7 @@ struct rebind_vector;
1313
template <typename T, typename U, typename Allocator>
1414
struct rebind_vector<thrust::host_vector<T, Allocator>, U>
1515
{
16-
using type = thrust::host_vector<U, typename thrust::detail::allocator_traits<Allocator>::template rebind_alloc<U>>;
16+
using type = thrust::host_vector<U, typename cuda::std::allocator_traits<Allocator>::template rebind_alloc<U>>;
1717
};
1818

1919
template <typename T, typename U, typename Allocator>

thrust/testing/functional_placeholders_bitwise.cu

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <thrust/detail/allocator/allocator_traits.h>
1+
#include <thrust/detail/allocator/allocator_system.h>
22
#include <thrust/functional.h>
33
#include <thrust/iterator/constant_iterator.h>
44
#include <thrust/transform.h>
@@ -13,7 +13,7 @@ struct rebind_vector;
1313
template <typename T, typename U, typename Allocator>
1414
struct rebind_vector<thrust::host_vector<T, Allocator>, U>
1515
{
16-
using alloc_traits = typename thrust::detail::allocator_traits<Allocator>;
16+
using alloc_traits = typename cuda::std::allocator_traits<Allocator>;
1717
using new_alloc = typename alloc_traits::template rebind_alloc<U>;
1818
using type = thrust::host_vector<U, new_alloc>;
1919
};

thrust/testing/functional_placeholders_relational.cu

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <thrust/detail/allocator/allocator_traits.h>
1+
#include <thrust/detail/allocator/allocator_system.h>
22
#include <thrust/functional.h>
33
#include <thrust/transform.h>
44

@@ -12,7 +12,7 @@ struct rebind_vector;
1212
template <typename T, typename U, typename Allocator>
1313
struct rebind_vector<thrust::host_vector<T, Allocator>, U>
1414
{
15-
using alloc_traits = typename thrust::detail::allocator_traits<Allocator>;
15+
using alloc_traits = typename cuda::std::allocator_traits<Allocator>;
1616
using new_alloc = typename alloc_traits::template rebind_alloc<U>;
1717
using type = thrust::host_vector<U, new_alloc>;
1818
};

thrust/testing/vector_allocators.cu

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
template <typename BaseAlloc, bool PropagateOnSwap>
99
class stateful_allocator : public BaseAlloc
1010
{
11-
using base_traits = thrust::detail::allocator_traits<BaseAlloc>;
11+
using base_traits = cuda::std::allocator_traits<BaseAlloc>;
1212

1313
public:
1414
stateful_allocator(int i)
@@ -47,8 +47,8 @@ public:
4747

4848
using pointer = typename base_traits::pointer;
4949
using const_pointer = typename base_traits::const_pointer;
50-
using reference = typename base_traits::reference;
51-
using const_reference = typename base_traits::const_reference;
50+
using reference = typename cuda::std::iterator_traits<pointer>::reference;
51+
using const_reference = typename cuda::std::iterator_traits<const_pointer>::reference;
5252

5353
pointer allocate(std::size_t size)
5454
{
@@ -177,8 +177,7 @@ template <typename Vector>
177177
void TestVectorAllocatorPropagateOnCopyAssignment()
178178
{
179179
ASSERT_EQUAL(
180-
thrust::detail::allocator_traits<typename Vector::allocator_type>::propagate_on_container_copy_assignment::value,
181-
true);
180+
cuda::std::allocator_traits<typename Vector::allocator_type>::propagate_on_container_copy_assignment::value, true);
182181

183182
using Alloc = typename Vector::allocator_type;
184183
Alloc alloc1(1);
@@ -211,8 +210,7 @@ void TestVectorAllocatorPropagateOnMoveAssignment()
211210
{
212211
using Alloc = typename Vector::allocator_type;
213212
ASSERT_EQUAL(
214-
thrust::detail::allocator_traits<typename Vector::allocator_type>::propagate_on_container_copy_assignment::value,
215-
true);
213+
cuda::std::allocator_traits<typename Vector::allocator_type>::propagate_on_container_copy_assignment::value, true);
216214

217215
using Alloc = typename Vector::allocator_type;
218216
Alloc alloc1(1);

thrust/thrust/allocate_unique.h

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
1515
# pragma system_header
1616
#endif // no system header
17-
#include <thrust/detail/allocator/allocator_traits.h>
1817
#include <thrust/detail/memory_algorithms.h>
1918
#include <thrust/detail/raw_pointer_cast.h>
2019
#include <thrust/detail/type_deduction.h>
2120

2221
#include <cuda/std/__cccl/memory_wrapper.h>
22+
#include <cuda/std/__memory/allocator_traits.h>
2323
#include <cuda/std/__type_traits/remove_cvref.h>
2424
#include <cuda/std/__utility/move.h>
2525
#include <cuda/std/__utility/swap.h>
@@ -35,7 +35,7 @@ struct allocator_delete final
3535
{
3636
using allocator_type =
3737
typename std::remove_cv<typename std::remove_reference<Allocator>::type>::type::template rebind<T>::other;
38-
using pointer = typename detail::allocator_traits<allocator_type>::pointer;
38+
using pointer = typename ::cuda::std::allocator_traits<allocator_type>::pointer;
3939

4040
template <typename UAllocator>
4141
allocator_delete(UAllocator&& other) noexcept
@@ -66,7 +66,7 @@ struct allocator_delete final
6666

6767
void operator()(pointer p)
6868
{
69-
using traits = detail::allocator_traits<::cuda::std::remove_cvref_t<Allocator>>;
69+
using traits = ::cuda::std::allocator_traits<::cuda::std::remove_cvref_t<Allocator>>;
7070
typename traits::allocator_type alloc_T(alloc_);
7171

7272
if (nullptr != detail::pointer_traits<pointer>::get(p))
@@ -106,7 +106,7 @@ struct array_allocator_delete final
106106
{
107107
using allocator_type =
108108
typename std::remove_cv<typename std::remove_reference<Allocator>::type>::type::template rebind<T>::other;
109-
using pointer = typename detail::allocator_traits<allocator_type>::pointer;
109+
using pointer = typename ::cuda::std::allocator_traits<allocator_type>::pointer;
110110

111111
template <typename UAllocator>
112112
array_allocator_delete(UAllocator&& other, std::size_t n) noexcept
@@ -142,7 +142,7 @@ struct array_allocator_delete final
142142

143143
void operator()(pointer p)
144144
{
145-
using traits = detail::allocator_traits<::cuda::std::remove_cvref_t<Allocator>>;
145+
using traits = ::cuda::std::allocator_traits<::cuda::std::remove_cvref_t<Allocator>>;
146146
typename traits::allocator_type alloc_T(get_allocator());
147147
if (nullptr != detail::pointer_traits<pointer>::get(p))
148148
{
@@ -204,11 +204,12 @@ template <typename T, typename Allocator, typename... Args>
204204
_CCCL_HOST
205205
std::unique_ptr<T,
206206
allocator_delete<T,
207-
typename detail::allocator_traits<
207+
typename ::cuda::std::allocator_traits<
208208
::cuda::std::remove_cvref_t<Allocator>>::template rebind_traits<T>::allocator_type>>
209209
allocate_unique(Allocator const& alloc, Args&&... args)
210210
{
211-
using traits = typename detail::allocator_traits<::cuda::std::remove_cvref_t<Allocator>>::template rebind_traits<T>;
211+
using traits =
212+
typename ::cuda::std::allocator_traits<::cuda::std::remove_cvref_t<Allocator>>::template rebind_traits<T>;
212213

213214
typename traits::allocator_type alloc_T(alloc);
214215

@@ -228,12 +229,13 @@ allocate_unique(Allocator const& alloc, Args&&... args)
228229
template <typename T, typename Allocator>
229230
_CCCL_HOST std::unique_ptr<
230231
T,
231-
uninitialized_allocator_delete<
232-
T,
233-
typename detail::allocator_traits<::cuda::std::remove_cvref_t<Allocator>>::template rebind_traits<T>::allocator_type>>
232+
uninitialized_allocator_delete<T,
233+
typename ::cuda::std::allocator_traits<
234+
::cuda::std::remove_cvref_t<Allocator>>::template rebind_traits<T>::allocator_type>>
234235
uninitialized_allocate_unique(Allocator const& alloc)
235236
{
236-
using traits = typename detail::allocator_traits<::cuda::std::remove_cvref_t<Allocator>>::template rebind_traits<T>;
237+
using traits =
238+
typename ::cuda::std::allocator_traits<::cuda::std::remove_cvref_t<Allocator>>::template rebind_traits<T>;
237239

238240
typename traits::allocator_type alloc_T(alloc);
239241

@@ -253,11 +255,12 @@ template <typename T, typename Allocator, typename Size, typename... Args>
253255
_CCCL_HOST std::unique_ptr<
254256
T[],
255257
array_allocator_delete<T,
256-
typename detail::allocator_traits<typename std::remove_cv<typename std::remove_reference<
258+
typename ::cuda::std::allocator_traits<typename std::remove_cv<typename std::remove_reference<
257259
Allocator>::type>::type>::template rebind_traits<T>::allocator_type>>
258260
allocate_unique_n(Allocator const& alloc, Size n, Args&&... args)
259261
{
260-
using traits = typename detail::allocator_traits<::cuda::std::remove_cvref_t<Allocator>>::template rebind_traits<T>;
262+
using traits =
263+
typename ::cuda::std::allocator_traits<::cuda::std::remove_cvref_t<Allocator>>::template rebind_traits<T>;
261264

262265
typename traits::allocator_type alloc_T(alloc);
263266

@@ -275,14 +278,15 @@ allocate_unique_n(Allocator const& alloc, Size n, Args&&... args)
275278
//! Creates a \p std::unique_ptr holding storage for an array of objects of type \p T without constructing them, using
276279
//! \p alloc as the allocator.
277280
template <typename T, typename Allocator, typename Size>
278-
_CCCL_HOST std::unique_ptr<
279-
T[],
280-
uninitialized_array_allocator_delete<
281-
T,
282-
typename detail::allocator_traits<::cuda::std::remove_cvref_t<Allocator>>::template rebind_traits<T>::allocator_type>>
281+
_CCCL_HOST
282+
std::unique_ptr<T[],
283+
uninitialized_array_allocator_delete<T,
284+
typename ::cuda::std::allocator_traits<::cuda::std::remove_cvref_t<
285+
Allocator>>::template rebind_traits<T>::allocator_type>>
283286
uninitialized_allocate_unique_n(Allocator const& alloc, Size n)
284287
{
285-
using traits = typename detail::allocator_traits<::cuda::std::remove_cvref_t<Allocator>>::template rebind_traits<T>;
288+
using traits =
289+
typename ::cuda::std::allocator_traits<::cuda::std::remove_cvref_t<Allocator>>::template rebind_traits<T>;
286290

287291
typename traits::allocator_type alloc_T(alloc);
288292

0 commit comments

Comments
 (0)