cuda::experimental::mr::device_memory_resource

Defined in include/cuda/experimental/__memory_resource/device_memory_resource.cuh

class device_memory_resource

Stream ordered memory resource

device_memory_resource uses cudaMallocFromPoolAsync / cudaFreeAsync for allocation/deallocation. A device_memory_resource is a thin wrapper around a c cudaMemPool_t.

Warning

device_memory_resource does not own the pool and it is the responsibility of the user to ensure that the lifetime of the pool exceeds the lifetime of the device_memory_resource.

Public Functions

inline device_memory_resource()

Default constructs the device_memory_resource using the default cudaMemPool_t of the default device.

Throws

cuda_error – if retrieving the default cudaMemPool_t fails.

inline explicit device_memory_resource(::cuda::experimental::device_ref __device)

Constructs a device_memory_resource using the default cudaMemPool_t of a given device.

Throws

cuda_error – if retrieving the default cudaMemPool_t fails.

device_memory_resource(int) = delete
device_memory_resource(cuda::std::nullptr_t) = delete
inline explicit device_memory_resource(::cudaMemPool_t __pool) noexcept

Constructs the device_memory_resource from a cudaMemPool_t.

Parameters

__pool – The cudaMemPool_t used to allocate memory.

inline explicit device_memory_resource(device_memory_pool &__pool) noexcept

Constructs the device_memory_resource from a device_memory_pool by calling get().

Parameters

__pool – The device_memory_pool used to allocate memory.

inline void *allocate(const size_t __bytes, const size_t __alignment = cuda::mr::default_cuda_malloc_alignment)

Allocate device memory of size at least __bytes via cudaMallocFromPoolAsync.

Parameters
  • __bytes – The size in bytes of the allocation.

  • __alignment – The requested alignment of the allocation.

Throws
  • std::invalid_argument – In case of invalid alignment.

  • cuda::cuda_error – If an error code was return by the CUDA API call.

Returns

Pointer to the newly allocated memory.

inline void deallocate(void *__ptr, const size_t, const size_t __alignment = cuda::mr::default_cuda_malloc_alignment)

Deallocate memory pointed to by __ptr.

Note

The pointer passed to deallocate must not be in use in a stream. It is the caller’s responsibility to properly synchronize all relevant streams before calling deallocate.

Parameters
  • __ptr – Pointer to be deallocated. Must have been allocated through a call to allocate.

  • __bytes – The number of bytes that was passed to the allocate call that returned __ptr.

  • __alignment – The alignment that was passed to the allocate call that returned __ptr.

inline void *allocate_async(const size_t __bytes, const size_t __alignment, const ::cuda::stream_ref __stream)

Allocate device memory of size at least __bytes via cudaMallocFromPoolAsync.

Parameters
  • __bytes – The size in bytes of the allocation.

  • __alignment – The requested alignment of the allocation.

  • __stream – Stream on which to perform allocation.

Throws
  • std::invalid_argument – In case of invalid alignment.

  • cuda::cuda_error – If an error code was return by the cuda api call.

Returns

Pointer to the newly allocated memory.

inline void *allocate_async(const size_t __bytes, const ::cuda::stream_ref __stream)

Allocate device memory of size at least __bytes via cudaMallocFromPoolAsync.

Parameters
  • __bytes – The size in bytes of the allocation.

  • __stream – Stream on which to perform allocation.

Throws

cuda::cuda_error – If an error code was return by the cuda api call.

Returns

Pointer to the newly allocated memory.

inline void deallocate_async(void *__ptr, const size_t __bytes, const size_t __alignment, const ::cuda::stream_ref __stream)

Deallocate memory pointed to by __ptr.

Note

The pointer passed to deallocate_async must not be in use in a stream other than __stream. It is the caller’s responsibility to properly synchronize all relevant streams before calling deallocate_async.

Parameters
  • __ptr – Pointer to be deallocated. Must have been allocated through a call to allocate_async

  • __bytes – The number of bytes that was passed to the allocate_async call that returned __ptr.

  • __alignment – The alignment that was passed to the allocate_async call that returned __ptr.

  • __stream – A stream that has a stream ordering relationship with the stream used in the allocate_async call that returned __ptr.

inline void deallocate_async(void *__ptr, size_t, const ::cuda::stream_ref __stream)

Deallocate memory pointed to by __ptr.

Note

The pointer passed to deallocate_async must not be in use in a stream other than __stream. It is the caller’s responsibility to properly synchronize all relevant streams before calling deallocate_async.

Parameters
  • __ptr – Pointer to be deallocated. Must have been allocated through a call to allocate_async.

  • __bytes – The number of bytes that was passed to the allocate_async call that returned __ptr.

  • __stream – A stream that has a stream ordering relationship with the stream used in the allocate_async call that returned __ptr.

inline void enable_peer_access_from(const ::std::vector<device_ref> &__devices)

Enable peer access to memory allocated through this memory resource by the supplied devices.

Access is controlled through the underyling memory pool, so this setting is shared between all memory resources created from the same pool. Device on which this resource allocates memory can be included in the vector.

Parameters

__devices – A vector of device_refs listing devices to enable access for

inline void enable_peer_access_from(device_ref __device)

Enable peer access to memory allocated through this memory resource by the supplied device.

Access is controlled through the underyling memory pool, so this setting is shared between all memory resources created from the same pool.

Parameters

__devicedevice_ref indicating for which device the access should be enabled

inline void disable_peer_access_from(const ::std::vector<device_ref> &__devices)

Enable peer access to memory allocated through this memory resource by the supplied devices.

Access is controlled through the underyling memory pool, so this setting is shared between all memory resources created from the same pool. Device on which this resource allocates memory can be included in the vector.

Parameters

__devices – A vector of device_refs listing devices to disable access for

inline void disable_peer_access_from(device_ref __device)

Enable peer access to memory allocated through this memory resource by the supplied device.

Access is controlled through the underyling memory pool, so this setting is shared between all memory resources created from the same pool.

Parameters

__devicedevice_ref indicating for which device the access should be enabled

inline bool is_accessible_from(device_ref __device)

Query if memory allocated through this memory resource is accessible by the supplied device.

Parameters

__device – device for which the peer access is queried

inline constexpr bool operator==(device_memory_resource const &__rhs) const noexcept

Equality comparison with another device_memory_resource.

Returns

true if underlying cudaMemPool_t are equal.

template<class _Resource, ::cuda::std::enable_if_t<(cuda::mr::__different_resource<device_memory_resource, _Resource>), int> = 0>
inline bool operator==(_Resource const &__rhs) const noexcept

Equality comparison between a device_memory_resource and another resource.

Parameters

__rhs – The resource to compare to.

Returns

If the underlying types are equality comparable, returns the result of equality comparison of both resources. Otherwise, returns false.

inline constexpr cudaMemPool_t get() const noexcept

Returns the underlying handle to the CUDA memory pool.