A coalescing best-fit suballocator which uses a pool of memory allocated from an upstream memory_resource. More...
#include <pool_memory_resource.hpp>
Public Member Functions | |
pool_memory_resource (Upstream *upstream_mr, thrust::optional< std::size_t > initial_pool_size=thrust::nullopt, thrust::optional< std::size_t > maximum_pool_size=thrust::nullopt) | |
Construct a pool_memory_resource and allocate the initial device memory pool using upstream_mr . More... | |
~pool_memory_resource () override | |
Destroy the pool_memory_resource and deallocate all memory it allocated using the upstream resource. | |
pool_memory_resource (pool_memory_resource const &)=delete | |
pool_memory_resource (pool_memory_resource &&)=delete | |
pool_memory_resource & | operator= (pool_memory_resource const &)=delete |
pool_memory_resource & | operator= (pool_memory_resource &&)=delete |
bool | supports_streams () const noexcept override |
Queries whether the resource supports use of non-null CUDA streams for allocation/deallocation. More... | |
bool | supports_get_mem_info () const noexcept override |
Query whether the resource supports the get_mem_info API. More... | |
Upstream * | get_upstream () const noexcept |
Get the upstream memory_resource object. More... | |
std::size_t | pool_size () const noexcept |
Computes the size of the current pool. More... | |
![]() | |
stream_ordered_memory_resource (stream_ordered_memory_resource const &)=delete | |
stream_ordered_memory_resource (stream_ordered_memory_resource &&)=delete | |
stream_ordered_memory_resource & | operator= (stream_ordered_memory_resource const &)=delete |
stream_ordered_memory_resource & | operator= (stream_ordered_memory_resource &&)=delete |
![]() | |
pool_memory_resource< Upstream > & | underlying () |
pool_memory_resource< Upstream > const & | underlying () const |
![]() | |
device_memory_resource (device_memory_resource const &)=default | |
device_memory_resource & | operator= (device_memory_resource const &)=default |
device_memory_resource (device_memory_resource &&) noexcept=default | |
device_memory_resource & | operator= (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... | |
Protected Types | |
using | free_list = detail::coalescing_free_list |
using | block_type = free_list::block_type |
using | lock_guard = std::lock_guard< std::mutex > |
![]() | |
using | free_list = detail::coalescing_free_list |
using | block_type = typename free_list::block_type |
using | lock_guard = std::lock_guard< std::mutex > |
using | split_block = std::pair< block_type, block_type > |
Pair representing a block that has been split for allocation. | |
Protected Member Functions | |
std::size_t | get_maximum_allocation_size () const |
Get the maximum size of allocations supported by this memory resource. More... | |
block_type | try_to_expand (std::size_t try_size, std::size_t min_size, cuda_stream_view stream) |
Try to expand the pool by allocating a block of at least min_size bytes from upstream. More... | |
void | initialize_pool (thrust::optional< std::size_t > initial_size, thrust::optional< std::size_t > maximum_size) |
Allocate initial memory for the pool. More... | |
block_type | expand_pool (std::size_t size, free_list &blocks, cuda_stream_view stream) |
Allocate space from upstream to supply the suballocation pool and return a sufficiently sized block. More... | |
std::size_t | size_to_grow (std::size_t size) const |
Given a minimum size, computes an appropriate size to grow the pool. More... | |
thrust::optional< block_type > | block_from_upstream (std::size_t size, cuda_stream_view stream) |
Allocate a block from upstream to expand the suballocation pool. More... | |
split_block | allocate_from_block (block_type const &block, std::size_t size) |
Splits block if necessary to return a pointer to memory of size bytes. More... | |
block_type | free_block (void *ptr, std::size_t size) noexcept |
Finds, frees and returns the block associated with pointer ptr . More... | |
void | release () |
Free all memory allocated from the upstream memory_resource. | |
std::pair< std::size_t, std::size_t > | free_list_summary (free_list const &blocks) |
Get the largest available block size and total free size in the specified free list. More... | |
std::pair< std::size_t, std::size_t > | do_get_mem_info (cuda_stream_view stream) const override |
Get free and available memory for memory resource. More... | |
![]() | |
void | insert_block (block_type const &block, cuda_stream_view stream) |
Returns the block b (last used on stream stream_event ) to the pool. More... | |
void | insert_blocks (free_list &&blocks, cuda_stream_view stream) |
std::mutex & | get_mutex () |
Get the mutex object. More... | |
void * | do_allocate (std::size_t size, cuda_stream_view stream) override |
Allocates memory of size at least bytes . More... | |
void | do_deallocate (void *ptr, std::size_t size, cuda_stream_view stream) override |
Deallocate memory pointed to by p . More... | |
Friends | |
class | detail::stream_ordered_memory_resource< pool_memory_resource< Upstream >, detail::coalescing_free_list > |
A coalescing best-fit suballocator which uses a pool of memory allocated from an upstream memory_resource.
Allocation (do_allocate()) and deallocation (do_deallocate()) are thread-safe. Also, this class is compatible with CUDA per-thread default stream.
UpstreamResource | memory_resource to use for allocating the pool. Implements rmm::mr::device_memory_resource interface. |
|
inlineexplicit |
Construct a pool_memory_resource
and allocate the initial device memory pool using upstream_mr
.
rmm::logic_error | if upstream_mr == nullptr |
rmm::logic_error | if initial_pool_size is neither the default nor aligned to a multiple of pool_memory_resource::allocation_alignment bytes. |
rmm::logic_error | if maximum_pool_size is neither the default nor aligned to a multiple of pool_memory_resource::allocation_alignment bytes. |
upstream_mr | The memory_resource from which to allocate blocks for the pool. |
initial_pool_size | Minimum size, in bytes, of the initial pool. Defaults to half of the available memory on the current device. |
maximum_pool_size | Maximum size, in bytes, that the pool can grow to. Defaults to all of the available memory on the current device. |
|
inlineprotected |
Splits block
if necessary to return a pointer to memory of size
bytes.
If the block is split, the remainder is returned to the pool.
block | The block to allocate from. |
size | The size in bytes of the requested allocation. |
stream_event | The stream and associated event on which the allocation will be used. |
|
inlineprotected |
Allocate a block from upstream to expand the suballocation pool.
size | The size in bytes to allocate from the upstream resource |
stream | The stream on which the memory is to be used. |
|
inlineoverrideprotectedvirtual |
Get free and available memory for memory resource.
nothing |
stream | to execute on |
Implements rmm::mr::device_memory_resource.
|
inlineprotected |
Allocate space from upstream to supply the suballocation pool and return a sufficiently sized block.
size | The minimum size to allocate |
blocks | The free list (ignored in this implementation) |
stream | The stream on which the memory is to be used. |
size
bytes
|
inlineprotectednoexcept |
Finds, frees and returns the block associated with pointer ptr
.
ptr | The pointer to the memory to free. |
size | The size of the memory to free. Must be equal to the original allocation size. |
stream | The stream-event pair for the stream on which the memory was last used. |
p
. The caller is expected to return the block to the pool.
|
inlineprotected |
Get the largest available block size and total free size in the specified free list.
This is intended only for debugging
blocks | The free list from which to return the summary |
|
inlineprotected |
Get the maximum size of allocations supported by this memory resource.
Note this does not depend on the memory size of the device. It simply returns the maximum value of std::size_t
|
inlinenoexcept |
Get the upstream memory_resource object.
|
inlineprotected |
Allocate initial memory for the pool.
If initial_size is unset, then queries the upstream memory resource for available memory if upstream supports get_mem_info
, or queries the device (using CUDA API) for available memory if not. Then attempts to initialize to half the available memory.
If initial_size is set, then tries to initialize the pool to that size.
initial_size | The optional initial size for the pool |
maximum_size | The optional maximum size for the pool |
|
inlinenoexcept |
Computes the size of the current pool.
Includes allocated as well as free memory.
|
inlineprotected |
Given a minimum size, computes an appropriate size to grow the pool.
Strategy is to try to grow the pool by half the difference between the configured maximum pool size and the current pool size, if the maximum pool size is set. If it is not set, try to double the current pool size.
Returns 0 if the requested size cannot be satisfied.
size | The size of the minimum allocation immediately needed |
|
inlineoverridevirtualnoexcept |
Query whether the resource supports the get_mem_info API.
Implements rmm::mr::device_memory_resource.
|
inlineoverridevirtualnoexcept |
Queries whether the resource supports use of non-null CUDA streams for allocation/deallocation.
Implements rmm::mr::device_memory_resource.
|
inlineprotected |
Try to expand the pool by allocating a block of at least min_size
bytes from upstream.
Attempts to allocate try_size
bytes from upstream. If it fails, it iteratively reduces the attempted size by half until min_size
, returning the allocated block once it succeeds.
rmm::bad_alloc | if min_size bytes cannot be allocated from upstream or maximum pool size is exceeded. |
try_size | The initial requested size to try allocating. |
min_size | The minimum requested size to try allocating. |
stream | The stream on which the memory is to be used. |
min_size
bytes