Skip to content

Commit

Permalink
Backportable C++17 fixes.
Browse files Browse the repository at this point in the history
Note that CMake won't support C++17 CUDA targets via the CUDA_STANDARD
property until CMake 3.18. Until this, C++17 must be enabled explicitly
by setting `--std=c++17` in CMAKE_CUDA_FLAGS.

Once CMake 3.18 is released this can be fixed.

- Address C++17 deprecated APIs in the allocator layers.

Fixes NVIDIA#1214

Bug 200619424
Bug 3043659
  • Loading branch information
alliepiper committed Jul 24, 2020
1 parent d9d7b51 commit 862bc53
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 24 deletions.
4 changes: 3 additions & 1 deletion testing/binary_search_vector.cu
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <unittest/unittest.h>
#include <thrust/binary_search.h>

#include <thrust/detail/allocator/allocator_traits.h>
#include <thrust/sequence.h>
#include <thrust/sort.h>
#include <thrust/iterator/discard_iterator.h>
Expand All @@ -16,7 +17,8 @@ template <class ExampleVector, typename NewType>
struct vector_like
{
typedef typename ExampleVector::allocator_type alloc;
typedef typename alloc::template rebind<NewType>::other new_alloc;
typedef typename thrust::detail::allocator_traits<alloc> alloc_traits;
typedef typename alloc_traits::template rebind_alloc<NewType> new_alloc;
typedef thrust::detail::vector_base<NewType, new_alloc> type;
};

Expand Down
4 changes: 3 additions & 1 deletion testing/binary_search_vector_descending.cu
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <thrust/binary_search.h>
#include <thrust/functional.h>

#include <thrust/detail/allocator/allocator_traits.h>
#include <thrust/sequence.h>
#include <thrust/sort.h>

Expand All @@ -14,7 +15,8 @@ template <class ExampleVector, typename NewType>
struct vector_like
{
typedef typename ExampleVector::allocator_type alloc;
typedef typename alloc::template rebind<NewType>::other new_alloc;
typedef typename thrust::detail::allocator_traits<alloc> alloc_traits;
typedef typename alloc_traits::template rebind_alloc<NewType> new_alloc;
typedef thrust::detail::vector_base<NewType, new_alloc> type;
};

Expand Down
8 changes: 5 additions & 3 deletions testing/functional_placeholders_bitwise.cu
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
#include <thrust/transform.h>
#include <thrust/iterator/constant_iterator.h>

#include <thrust/detail/allocator/allocator_traits.h>

static const size_t num_samples = 10000;

template<typename Vector, typename U> struct rebind_vector;

// TODO: C++11: use rebind from allocator_traits
template<typename T, typename U, typename Allocator>
struct rebind_vector<thrust::host_vector<T, Allocator>, U>
{
typedef thrust::host_vector<U,
typename Allocator::template rebind<U>::other> type;
typedef typename thrust::detail::allocator_traits<Allocator> alloc_traits;
typedef typename alloc_traits::template rebind_alloc<U> new_alloc;
typedef thrust::host_vector<U, new_alloc> type;
};

template<typename T, typename U, typename Allocator>
Expand Down
8 changes: 5 additions & 3 deletions testing/functional_placeholders_logical.cu
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
#include <thrust/functional.h>
#include <thrust/transform.h>

#include <thrust/detail/allocator/allocator_traits.h>

static const size_t num_samples = 10000;

template<typename Vector, typename U> struct rebind_vector;

// TODO: C++11: use rebind from allocator_traits
template<typename T, typename U, typename Allocator>
struct rebind_vector<thrust::host_vector<T, Allocator>, U>
{
typedef thrust::host_vector<U,
typename Allocator::template rebind<U>::other> type;
typedef typename thrust::detail::allocator_traits<Allocator> alloc_traits;
typedef typename alloc_traits::template rebind_alloc<U> new_alloc;
typedef thrust::host_vector<U, new_alloc> type;
};

template<typename T, typename U, typename Allocator>
Expand Down
8 changes: 5 additions & 3 deletions testing/functional_placeholders_relational.cu
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
#include <thrust/functional.h>
#include <thrust/transform.h>

#include <thrust/detail/allocator/allocator_traits.h>

static const size_t num_samples = 10000;

template<typename Vector, typename U> struct rebind_vector;

// TODO: C++11: use rebind from allocator_traits
template<typename T, typename U, typename Allocator>
struct rebind_vector<thrust::host_vector<T, Allocator>, U>
{
typedef thrust::host_vector<U,
typename Allocator::template rebind<U>::other> type;
typedef typename thrust::detail::allocator_traits<Allocator> alloc_traits;
typedef typename alloc_traits::template rebind_alloc<U> new_alloc;
typedef thrust::host_vector<U, new_alloc> type;
};

template<typename T, typename U, typename Allocator>
Expand Down
27 changes: 22 additions & 5 deletions testing/vector_allocators.cu
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
template<typename BaseAlloc, bool PropagateOnSwap>
class stateful_allocator : public BaseAlloc
{
typedef thrust::detail::allocator_traits<BaseAlloc> base_traits;

public:
stateful_allocator(int i) : state(i)
{
Expand Down Expand Up @@ -43,20 +45,35 @@ public:
static int last_allocated;
static int last_deallocated;

typedef
typename thrust::detail::allocator_traits<BaseAlloc>::pointer
pointer;
typedef typename base_traits::pointer pointer;
typedef typename base_traits::const_pointer const_pointer;
typedef typename base_traits::reference reference;
typedef typename base_traits::const_reference const_reference;

pointer allocate(std::size_t size)
{
BaseAlloc alloc;
last_allocated = state;
return BaseAlloc::allocate(size);
return base_traits::allocate(alloc, size);
}

void deallocate(pointer ptr, std::size_t size)
{
BaseAlloc alloc;
last_deallocated = state;
return BaseAlloc::deallocate(ptr, size);
return base_traits::deallocate(alloc, ptr, size);
}

static void construct(pointer ptr)
{
BaseAlloc alloc;
return base_traits::construct(alloc, ptr);
}

static void destroy(pointer ptr)
{
BaseAlloc alloc;
return base_traits::destroy(alloc, ptr);
}

bool operator==(const stateful_allocator &rhs) const
Expand Down
4 changes: 4 additions & 0 deletions thrust/detail/allocator/allocator_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,10 @@ template<typename Alloc>
};
#endif

// Deprecated std::allocator typedefs that we need:
typedef typename thrust::detail::pointer_traits<pointer>::reference reference;
typedef typename thrust::detail::pointer_traits<const_pointer>::reference const_reference;

inline __host__ __device__
static pointer allocate(allocator_type &a, size_type n);

Expand Down
6 changes: 6 additions & 0 deletions thrust/detail/allocator/allocator_traits.inl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ public:
is_empty<allocator_type>
>::type;

// std::allocator_traits doesn't provide these, but
// thrust::detail::allocator_traits does. These used to be part of the
// std::allocator API but were deprecated in C++17.
using reference = typename thrust::detail::pointer_traits<pointer>::reference;
using const_reference = typename thrust::detail::pointer_traits<const_pointer>::reference;

template <typename U>
using rebind_alloc = std::allocator<U>;
template <typename U>
Expand Down
10 changes: 2 additions & 8 deletions thrust/detail/contiguous_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,8 @@ template<typename T, typename Alloc>
typedef typename alloc_traits::const_pointer const_pointer;
typedef typename alloc_traits::size_type size_type;
typedef typename alloc_traits::difference_type difference_type;

// XXX we should bring reference & const_reference into allocator_traits
// at the moment, it's unclear how -- we have nothing analogous to
// rebind_pointer for references
// we either need to add reference_traits or extend the existing
// pointer_traits to support wrapped references
typedef typename Alloc::reference reference;
typedef typename Alloc::const_reference const_reference;
typedef typename alloc_traits::reference reference;
typedef typename alloc_traits::const_reference const_reference;

typedef thrust::detail::normal_iterator<pointer> iterator;
typedef thrust::detail::normal_iterator<const_pointer> const_iterator;
Expand Down

0 comments on commit 862bc53

Please sign in to comment.