cuda::experimental::mr::shared_resource

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

template<class _Resource>
struct shared_resource

Resource wrapper to share ownership of a resource

shared_resource holds a reference counted instance of a memory resource. This allows the user to pass a resource around with reference semantics while avoiding lifetime issues.

@note shared_resource satisfies the cuda::mr::async_resource concept iff tparam _Resource satisfies it. @tparam _Resource The resource type to hold.

Public Functions

template<class ..._Args>
inline explicit shared_resource(_Args&&... __args)

Constructs a shared_resource refering to an object of type _Resource that has been constructed with arguments __args. The _Resource object is dynamically allocated with new.

Parameters

__args – The arguments to be passed to the _Resource constructor.

inline shared_resource(const shared_resource &__other) noexcept

Copy-constructs a shared_resource object resulting in an copy that shares ownership of the wrapped resource with __other.

Parameters

__other – The shared_resource object to copy from.

inline shared_resource(shared_resource &&__other) noexcept

Move-constructs a shared_resource assuming ownership of the resource stored in __other.

Parameters

__other – The shared_resource object to move from.

Post

__other is left in a valid but unspecified state.

inline ~shared_resource()

Releases the reference held by this shared_resource object. If this is the last reference to the wrapped resource, the resource is deleted.

inline shared_resource &operator=(const shared_resource &__other) noexcept

Copy-assigns from __other. Self-assignment is a no-op. Otherwise, the reference held by this shared_resource object is released and a new reference is acquired to the wrapped resource of __other, if any.

Parameters

__other – The shared_resource object to copy from.

inline shared_resource &operator=(shared_resource &&__other) noexcept

Move-assigns from __other. Self-assignment is a no-op. Otherwise, the reference held by this shared_resource object is released, while the reference held by __other is transferred to this object.

Parameters

__other – The shared_resource object to move from.

Post

__other is left in a valid but unspecified state.

inline void swap(shared_resource &__other) noexcept

Swaps a shared_resource with another one.

Parameters

__other – The other shared_resource.

inline void *allocate(size_t __bytes, size_t __alignment = alignof(cuda::std::max_align_t))

Allocate memory of size at least __bytes using the stored resource.

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

  • __alignment – The requested alignment of the allocation.

Returns

Pointer to the newly allocated memory

inline void deallocate(void *__ptr, size_t __bytes, size_t __alignment = alignof(cuda::std::max_align_t)) noexcept

Deallocate memory pointed to by __ptr using the stored resource.

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.

template<class _ThisResource = _Resource, ::cuda::std::enable_if_t<cuda::mr::async_resource<_ThisResource>, int> = 0>
inline void *async_allocate(size_t __bytes, size_t __alignment, ::cuda::stream_ref __stream)

Enqueues an allocation of memory of size at least __bytes using the wrapped resource. The allocation is performed asynchronously on stream __stream.

Note

The caller is responsible for ensuring that the memory is not accessed until the operation has completed.

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

  • __alignment – The requested alignment of the allocation.

Pre

_Resource must satisfy async_resource.

Returns

Pointer to the newly allocated memory.

template<class _ThisResource = _Resource, ::cuda::std::enable_if_t<cuda::mr::async_resource<_ThisResource>, int> = 0>
inline void async_deallocate(void *__ptr, size_t __bytes, size_t __alignment, ::cuda::stream_ref __stream)

Enqueues the deallocation of memory pointed to by __ptr. The deallocation is performed asynchronously on stream __stream.

Note

The caller is responsible for ensuring that the memory is not accessed after the operation has completed.

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

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

Pre

_Resource must satisfy async_resource.

inline bool operator==(const shared_resource &__lhs, const shared_resource &__rhs)

Equality comparison between two shared_resource.

Parameters
Returns

Checks whether the objects refer to resources that compare equal.

inline bool operator!=(const shared_resource &__lhs, const shared_resource &__rhs)

Equality comparison between two shared_resource.

Parameters
Returns

Checks whether the objects refer to resources that compare unequal.

template<class _Property, ::cuda::std::enable_if_t<property_with_value<_Property>&&(has_property<_Resource, _Property>), int> = 0>
inline __property_value_t<_Property> get_property(const shared_resource &__self, _Property) noexcept

Forwards the stateful properties.

Friends

inline friend void swap(shared_resource &__left, shared_resource &__right) noexcept

Swaps a shared_resource with another one.

Parameters

__other – The other shared_resource.

template<class _Property, ::cuda::std::enable_if_t<(!property_with_value<_Property>) && (has_property<_Resource, _Property>), int> = 0>
inline friend void get_property(const shared_resource&, _Property) noexcept

Forwards the stateless properties.