template<typename T>
tf::cudaUSMAllocator class

class to create a unified shared memory (USM) allocator

Template parameters
T element type

A cudaUSMAllocator enables using unified shared memory (USM) allocation for standard library containers. It is typically passed as template parameter when declaring standard library containers (e.g. std::vector).

Public types

template<typename U>
struct rebind
its member type U is the equivalent allocator type to allocate elements of type U
using value_type = T
element type
using pointer = T*
element pointer type
using reference = T&
element reference type
using const_pointer = const T*
const element pointer type
using const_reference = const T&
constant element reference type
using size_type = std::size_t
size type
using difference_type = std::ptrdiff_t
pointer difference type

Constructors, destructors, conversion operators

cudaUSMAllocator() noexcept
Constructs a device allocator object.
cudaUSMAllocator(const cudaUSMAllocator&) noexcept
Constructs a device allocator object from another device allocator object.
template<typename U>
cudaUSMAllocator(const cudaUSMAllocator<U>&) noexcept
Constructs a device allocator object from another device allocator object with a different element type.
~cudaUSMAllocator() noexcept
Destructs the device allocator object.

Public functions

auto address(reference x) -> pointer
Returns the address of x.
auto address(const_reference x) const -> const_pointer
Returns the address of x.
auto allocate(size_type n, const void* = 0) -> pointer
allocates block of storage.
void deallocate(pointer ptr, size_type)
Releases a block of storage previously allocated with member allocate and not yet released.
auto max_size() const -> size_type noexcept
returns the maximum number of elements that could potentially be allocated by this allocator
void construct(pointer ptr, const_reference val)
Constructs an element object on the location pointed by ptr.
void destroy(pointer ptr)
destroys in-place the object pointed by ptr
template<typename U>
auto operator==(const cudaUSMAllocator<U>&) const -> bool noexcept
compares two allocator of different types using ==
template<typename U>
auto operator!=(const cudaUSMAllocator<U>&) const -> bool noexcept
compares two allocator of different types using !=

Function documentation

template<typename T>
pointer tf::cudaUSMAllocator<T>::address(reference x)

Returns the address of x.

Parameters
x reference to an object
Returns a pointer to the object

This effectively means returning &x.

template<typename T>
const_pointer tf::cudaUSMAllocator<T>::address(const_reference x) const

Returns the address of x.

Parameters
x reference to an object
Returns a pointer to the object

This effectively means returning &x.

template<typename T>
pointer tf::cudaUSMAllocator<T>::allocate(size_type n, const void* = 0)

allocates block of storage.

Parameters
n number of elements (each of size sizeof(value_type)) to be allocated
Returns a pointer to the initial element in the block of storage.

Attempts to allocate a block of storage with a size large enough to contain n elements of member type, value_type, and returns a pointer to the first element.

The storage is aligned appropriately for object of type value_type, but they are not constructed.

The block of storage is allocated using cudaMalloc and throws std::bad_alloc if it cannot allocate the total amount of storage requested.

template<typename T>
void tf::cudaUSMAllocator<T>::deallocate(pointer ptr, size_type)

Releases a block of storage previously allocated with member allocate and not yet released.

Parameters
ptr pointer to a block of storage previously allocated with allocate

The elements in the array are not destroyed by a call to this member function.

template<typename T>
size_type tf::cudaUSMAllocator<T>::max_size() const noexcept

returns the maximum number of elements that could potentially be allocated by this allocator

Returns the number of elements that might be allocated as maximum by a call to member allocate

A call to member allocate with the value returned by this function can still fail to allocate the requested storage.

template<typename T>
void tf::cudaUSMAllocator<T>::construct(pointer ptr, const_reference val)

Constructs an element object on the location pointed by ptr.

Parameters
ptr pointer to a location with enough storage soace to contain an element of type value_type
val value to initialize the constructed element to

template<typename T>
void tf::cudaUSMAllocator<T>::destroy(pointer ptr)

destroys in-place the object pointed by ptr

Parameters
ptr pointer to the object to be destroye

Notice that this does not deallocate the storage for the element but calls its destructor.

template<typename T> template<typename U>
bool tf::cudaUSMAllocator<T>::operator==(const cudaUSMAllocator<U>&) const noexcept

compares two allocator of different types using ==

USM allocators of different types are always equal to each other because the storage allocated by the allocator a1 can be deallocated through a2.

template<typename T> template<typename U>
bool tf::cudaUSMAllocator<T>::operator!=(const cudaUSMAllocator<U>&) const noexcept

compares two allocator of different types using !=

USM allocators of different types are always equal to each other because the storage allocated by the allocator a1 can be deallocated through a2.