Public Types | Public Member Functions | List of all members
rmm::mr::failure_callback_resource_adaptor< Upstream, ExceptionType > Class Template Referencefinal

A device memory resource that calls a callback function when allocations throw a specified exception type. More...

#include <failure_callback_resource_adaptor.hpp>

Inheritance diagram for rmm::mr::failure_callback_resource_adaptor< Upstream, ExceptionType >:
Inheritance graph
[legend]
Collaboration diagram for rmm::mr::failure_callback_resource_adaptor< Upstream, ExceptionType >:
Collaboration graph
[legend]

Public Types

using exception_type = ExceptionType
 The type of exception this object catches/throws.
 

Public Member Functions

 failure_callback_resource_adaptor (Upstream *upstream, failure_callback_t callback, void *callback_arg)
 Construct a new failure_callback_resource_adaptor using upstream to satisfy allocation requests. More...
 
 failure_callback_resource_adaptor (failure_callback_resource_adaptor const &)=delete
 
failure_callback_resource_adaptoroperator= (failure_callback_resource_adaptor const &)=delete
 
 failure_callback_resource_adaptor (failure_callback_resource_adaptor &&) noexcept=default
 
failure_callback_resource_adaptoroperator= (failure_callback_resource_adaptor &&) noexcept=default
 
Upstream * get_upstream () const noexcept
 Return pointer to the upstream resource. More...
 
bool supports_streams () const noexcept override
 Checks whether the upstream resource supports streams. More...
 
bool supports_get_mem_info () const noexcept override
 Query whether the resource supports the get_mem_info API. More...
 
- Public Member Functions inherited from rmm::mr::device_memory_resource
 device_memory_resource (device_memory_resource const &)=default
 
device_memory_resourceoperator= (device_memory_resource const &)=default
 
 device_memory_resource (device_memory_resource &&) noexcept=default
 
device_memory_resourceoperator= (device_memory_resource &&) noexcept=default
 
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...
 
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 Upstream, typename ExceptionType = rmm::out_of_memory>
class rmm::mr::failure_callback_resource_adaptor< Upstream, ExceptionType >

A device memory resource that calls a callback function when allocations throw a specified exception type.

An instance of this resource must be constructed with an existing, upstream resource in order to satisfy allocation requests.

The callback function takes an allocation size and a callback argument and returns a bool representing whether to retry the allocation (true) or re-throw the caught exception (false).

When implementing a callback function for allocation retry, care must be taken to avoid an infinite loop. The following example makes sure to only retry the allocation once:

{c++}
using failure_callback_adaptor =
bool failure_handler(std::size_t bytes, void* arg)
{
bool& retried = *reinterpret_cast<bool*>(arg);
if (!retried) {
retried = true;
return true; // First time we request an allocation retry
}
return false; // Second time we let the adaptor throw std::bad_alloc
}
int main()
{
bool retried{false};
failure_callback_adaptor mr{
rmm::mr::get_current_device_resource(), failure_handler, &retried
};
}
Template Parameters
UpstreamThe type of the upstream resource used for allocation/deallocation.
ExceptionTypeThe type of exception that this adaptor should respond to

Constructor & Destructor Documentation

◆ failure_callback_resource_adaptor()

template<typename Upstream , typename ExceptionType = rmm::out_of_memory>
rmm::mr::failure_callback_resource_adaptor< Upstream, ExceptionType >::failure_callback_resource_adaptor ( Upstream *  upstream,
failure_callback_t  callback,
void *  callback_arg 
)
inline

Construct a new failure_callback_resource_adaptor using upstream to satisfy allocation requests.

Exceptions
<tt>rmm::logic_error</tt>if upstream == nullptr
Parameters
upstreamThe resource used for allocating/deallocating device memory
callbackCallback function
See also
failure_callback_t
Parameters
callback_argExtra argument passed to callback

Member Function Documentation

◆ get_upstream()

template<typename Upstream , typename ExceptionType = rmm::out_of_memory>
Upstream* rmm::mr::failure_callback_resource_adaptor< Upstream, ExceptionType >::get_upstream ( ) const
inlinenoexcept

Return pointer to the upstream resource.

Returns
Upstream* Pointer to the upstream resource.

◆ supports_get_mem_info()

template<typename Upstream , typename ExceptionType = rmm::out_of_memory>
bool rmm::mr::failure_callback_resource_adaptor< Upstream, ExceptionType >::supports_get_mem_info ( ) const
inlineoverridevirtualnoexcept

Query whether the resource supports the get_mem_info API.

Returns
bool true if the upstream resource supports get_mem_info, false otherwise.

Implements rmm::mr::device_memory_resource.

◆ supports_streams()

template<typename Upstream , typename ExceptionType = rmm::out_of_memory>
bool rmm::mr::failure_callback_resource_adaptor< Upstream, ExceptionType >::supports_streams ( ) const
inlineoverridevirtualnoexcept

Checks whether the upstream resource supports streams.

Returns
true The upstream resource supports streams
false The upstream resource does not support streams.

Implements rmm::mr::device_memory_resource.


The documentation for this class was generated from the following file:
rmm::mr::set_current_device_resource
device_memory_resource * set_current_device_resource(device_memory_resource *new_mr)
Set the memory resource for the current device.
Definition: per_device_resource.hpp:226
rmm::mr::failure_callback_resource_adaptor
A device memory resource that calls a callback function when allocations throw a specified exception ...
Definition: failure_callback_resource_adaptor.hpp:86
rmm::mr::get_current_device_resource
device_memory_resource * get_current_device_resource()
Get the memory resource for the current device.
Definition: per_device_resource.hpp:197