RMM  23.12
RAPIDS Memory Manager
Public Types | Public Member Functions | List of all members
rmm::mr::owning_wrapper< Resource, Upstreams > Class Template Reference

Resource adaptor that maintains the lifetime of upstream resources. More...

#include <owning_wrapper.hpp>

Inheritance diagram for rmm::mr::owning_wrapper< Resource, Upstreams >:
Inheritance graph
[legend]
Collaboration diagram for rmm::mr::owning_wrapper< Resource, Upstreams >:
Collaboration graph
[legend]

Public Types

using upstream_tuple = std::tuple< std::shared_ptr< Upstreams >... >
 Tuple of upstream memory resources.
 

Public Member Functions

template<typename... Args>
 owning_wrapper (upstream_tuple upstreams, Args &&... args)
 Constructs the wrapped resource using the provided upstreams and any additional arguments forwarded to the wrapped resources constructor. More...
 
Resource const & wrapped () const noexcept
 A constant reference to the wrapped resource. More...
 
Resource & wrapped () noexcept
 A reference to the wrapped resource. More...
 
bool supports_streams () const noexcept override
 Query whether the resource supports use of non-null CUDA streams for allocation/deallocation. More...
 
bool supports_get_mem_info () const noexcept override
 true if the wrapped resource supports get_mem_info, false otherwise More...
 
- Public Member Functions inherited from rmm::mr::device_memory_resource
 device_memory_resource (device_memory_resource const &)=default
 Default copy constructor.
 
 device_memory_resource (device_memory_resource &&) noexcept=default
 Default move constructor.
 
device_memory_resourceoperator= (device_memory_resource const &)=default
 Default copy assignment operator. More...
 
device_memory_resourceoperator= (device_memory_resource &&) noexcept=default
 Default move assignment operator. More...
 
void * allocate (std::size_t bytes, cuda_stream_view stream=cuda_stream_view{})
 Allocates memory of size at least bytes. More...
 
void deallocate (void *ptr, std::size_t bytes, cuda_stream_view stream=cuda_stream_view{})
 Deallocate memory pointed to by p. More...
 
bool is_equal (device_memory_resource const &other) const noexcept
 Compare this resource to another. More...
 
void * allocate (std::size_t bytes, std::size_t alignment)
 Allocates memory of size at least bytes. More...
 
void deallocate (void *ptr, std::size_t bytes, std::size_t alignment)
 Deallocate memory pointed to by p. More...
 
void * allocate_async (std::size_t bytes, std::size_t alignment, cuda_stream_view stream)
 Allocates memory of size at least bytes. More...
 
void * allocate_async (std::size_t bytes, cuda_stream_view stream)
 Allocates memory of size at least bytes. More...
 
void deallocate_async (void *ptr, std::size_t bytes, std::size_t alignment, cuda_stream_view stream)
 Deallocate memory pointed to by p. More...
 
void deallocate_async (void *ptr, std::size_t bytes, cuda_stream_view stream)
 Deallocate memory pointed to by p. More...
 
bool operator== (device_memory_resource const &other) const noexcept
 Comparison operator with another device_memory_resource. More...
 
bool operator!= (device_memory_resource const &other) const noexcept
 Comparison operator with another device_memory_resource. More...
 
std::pair< std::size_t, std::size_t > get_mem_info (cuda_stream_view stream) const
 Queries the amount of free and total memory for the resource. More...
 

Detailed Description

template<typename Resource, typename... Upstreams>
class rmm::mr::owning_wrapper< Resource, Upstreams >

Resource adaptor that maintains the lifetime of upstream resources.

Many device_memory_resource derived types allocate memory from another "upstream" resource. E.g., pool_memory_resource allocates its pool from an upstream resource. Typically, a resource does not own its upstream, and therefore it is the user's responsibility to maintain the lifetime of the upstream resource. This can be inconvenient and error prone, especially for resources with complex upstreams that may themselves also have an upstream.

owning_wrapper simplifies lifetime management of a resource, wrapped, by taking shared ownership of all upstream resources via a std::shared_ptr.

For convenience, it is recommended to use the make_owning_wrapper factory instead of constructing an owning_wrapper directly.

Example:

auto cuda = std::make_shared<rmm::mr::cuda_memory_resource>();
auto pool = rmm::mr::make_owning_wrapper<rmm::mr::pool_memory_resource>(cuda,initial_pool_size,
max_pool_size);
// The `cuda` resource will be kept alive for the lifetime of `pool` and automatically be
// destroyed after `pool` is destroyed
Template Parameters
ResourceType of the wrapped resource
UpstreamsTemplate parameter pack of the types of the upstream resources used by Resource

Constructor & Destructor Documentation

◆ owning_wrapper()

template<typename Resource , typename... Upstreams>
template<typename... Args>
rmm::mr::owning_wrapper< Resource, Upstreams >::owning_wrapper ( upstream_tuple  upstreams,
Args &&...  args 
)
inline

Constructs the wrapped resource using the provided upstreams and any additional arguments forwarded to the wrapped resources constructor.

Resource is required to have a constructor whose first argument(s) are raw pointers to its upstream resources in the same order as upstreams, followed by any additional arguments in the same order as args.

Example:

template <typename Upstream1, typename Upstream2>
class example_resource{
example_resource(Upstream1 * u1, Upstream2 * u2, int n, float f);
};
using example = example_resource<cuda,cuda>;
auto cuda_mr = std::make_shared<cuda>();
// Constructs an `example_resource` wrapped by an `owning_wrapper` taking shared ownership of
//`cuda_mr` and using it as both of `example_resource`s upstream resources. Forwards the
// arguments `42` and `3.14` to the additional `n` and `f` arguments of `example_resources`
// constructor.
wrapped_example w{std::make_tuple(cuda_mr,cuda_mr), 42, 3.14};
device_memory_resource derived class that uses cudaMalloc/Free for allocation/deallocation.
Definition: cuda_memory_resource.hpp:35
Resource adaptor that maintains the lifetime of upstream resources.
Definition: owning_wrapper.hpp:105
Template Parameters
ArgsTemplate parameter pack to forward to the wrapped resource's constructor
Parameters
upstreamsTuple of std::shared_ptrs to the upstreams used by the wrapped resource, in the same order as expected by Resources constructor.
argsFunction parameter pack of arguments to forward to the wrapped resource's constructor

Member Function Documentation

◆ supports_get_mem_info()

template<typename Resource , typename... Upstreams>
bool rmm::mr::owning_wrapper< Resource, Upstreams >::supports_get_mem_info ( ) const
inlineoverridevirtualnoexcept

true if the wrapped resource supports get_mem_info, false otherwise

Returns
true if the wrapped resource supports get_mem_info, false otherwise

Implements rmm::mr::device_memory_resource.

◆ supports_streams()

template<typename Resource , typename... Upstreams>
bool rmm::mr::owning_wrapper< Resource, Upstreams >::supports_streams ( ) const
inlineoverridevirtualnoexcept

Query whether the resource supports use of non-null CUDA streams for allocation/deallocation.

Returns
bool true if the resource supports non-null CUDA streams.

Implements rmm::mr::device_memory_resource.

◆ wrapped() [1/2]

template<typename Resource , typename... Upstreams>
Resource const& rmm::mr::owning_wrapper< Resource, Upstreams >::wrapped ( ) const
inlinenoexcept

A constant reference to the wrapped resource.

Returns
A constant reference to the wrapped resource

◆ wrapped() [2/2]

template<typename Resource , typename... Upstreams>
Resource& rmm::mr::owning_wrapper< Resource, Upstreams >::wrapped ( )
inlinenoexcept

A reference to the wrapped resource.

Returns
A reference to the wrapped resource

The documentation for this class was generated from the following file: