OpenVDB 13.1.0
Loading...
Searching...
No Matches
TempPool.h
Go to the documentation of this file.
1// Copyright Contributors to the OpenVDB Project
2// SPDX-License-Identifier: Apache-2.0
3//
4/// @brief Defines a simple memory pool used to call cub functions that use dynamic temporary storage
5///
6/// @details See nanovdb/tools/cuda/PointToGrid.cuh and nanovdb/tools/cuda/DistributedPointToGrid.cuh
7/// for examples. Also note that this explains the somewhat unusual API with direct access to
8/// private member data.
9//
10#ifndef NANOVDB_CUDA_TEMPPOOL_H_HAS_BEEN_INCLUDED
11#define NANOVDB_CUDA_TEMPPOOL_H_HAS_BEEN_INCLUDED
12
13#include <nanovdb/cuda/Buffer.h>
15
16#include <cstddef>
17#include <cuda_runtime_api.h>
18
19namespace nanovdb {
20
21namespace cuda {
22
23template <class Resource>
24class TempPool {
26 "TempPool allocates stream-ordered scratch and requires an AsyncResource");
27 // The buffer borrows the pool's resource through a ResourceRef rather than
28 // copying it, preserving the pool's contract that all traffic reaches the
29 // caller's resource instance (which may be stateful).
31public:
32
33 /// @brief Default c-tor of an empty memory pool that uses the default
34 /// instance of @c Resource for all allocations.
36
37 /// @brief C-tor of an empty memory pool that routes all allocations through
38 /// the supplied @c Resource instance.
39 /// @param resource resource instance to allocate from; must outlive this pool.
40 explicit TempPool(Resource& resource)
41 : mResource(&resource)
42 , mBuffer(cudaStream_t{0}, ResourceRef<Resource>(resource), 0, noInit)
43 {
44 }
45
46 /// @brief Returns a non-const void pointer to the data managed by this instance.
47 void* data() {return mBuffer.data();}
48
49 /// @brief Returns a non-const reference to the actual size of the data managed by this instance.
50 /// @note Returned by reference because cub's two-pass API takes the storage
51 /// size as a size_t&, so this cannot forward Buffer::size() by value.
52 size_t& size() {return mSize;}
53
54 /// @brief Returns a non-const reference to the requested size of the data managed by this instance.
55 /// @note This requested size should always be less than or smaller than the actual size().
56 size_t& requestedSize() {return mRequestedSize;}
57
58 /// @brief Returns the stream that the managed memory was last (re)allocated on,
59 /// i.e. the stream this pool will free on at destruction.
60 cudaStream_t stream() const {return mBuffer.stream();}
61
62 /// @brief Re-allocation of the data managed by this instance. Only has affect if the pool in empty or
63 /// the requested memory is larger than the existing size.
64 /// @param stream cuda stream used for asynchronous de-allocation and allocation.
65 /// @note Scratch is discarded, never resized: preserving a prefix of
66 /// temporary storage would be a wasted copy.
67 void reallocate(cudaStream_t stream) {
68 if (mBuffer.empty() || mRequestedSize > mSize) {
69 mBuffer.destroy(stream);// free the outgrown block on this stream
70 mBuffer = BufferT(stream, ResourceRef<Resource>(*mResource), mRequestedSize, noInit);
71 mSize = mBuffer.size();
72 } else {
73 mBuffer.set_stream(stream);// retained so the d-tor frees on the most-recently-used stream
74 }
75 }
76private:
77 Resource *mResource;// non-owning; must outlive this pool and its buffer
78 BufferT mBuffer;
79 size_t mSize{0};
80 size_t mRequestedSize{0};
81};// TempPool<Resource> class
82
84
85} // namespace cuda
86
87} // namespace nanovdb
88
89#endif // end of NANOVDB_CUDA_TEMPPOOL_H_HAS_BEEN_INCLUDED
Typed containers for CUDA memory: the owning, resource-aware, stream-ordered cuda::Buffer and the non...
Owning, typed container of T elements allocated from a memory resource R held by value.
Definition Buffer.h:69
Definition TempPool.h:24
void * data()
Returns a non-const void pointer to the data managed by this instance.
Definition TempPool.h:47
TempPool(Resource &resource)
C-tor of an empty memory pool that routes all allocations through the supplied Resource instance.
Definition TempPool.h:40
size_t & size()
Returns a non-const reference to the actual size of the data managed by this instance.
Definition TempPool.h:52
TempPool()
Default c-tor of an empty memory pool that uses the default instance of Resource for all allocations.
Definition TempPool.h:35
void reallocate(cudaStream_t stream)
Re-allocation of the data managed by this instance. Only has affect if the pool in empty or the reque...
Definition TempPool.h:67
size_t & requestedSize()
Returns a non-const reference to the requested size of the data managed by this instance.
Definition TempPool.h:56
cudaStream_t stream() const
Returns the stream that the managed memory was last (re)allocated on, i.e. the stream this pool will ...
Definition TempPool.h:60
Definition GridHandle.h:37
R & default_resource()
Returns a program-lifetime, address-stable reference to a default instance of resource R.
Definition DeviceResource.h:87
constexpr NoInit noInit
Definition Buffer.h:29
TempPool< DeviceResource > TempDevicePool
Definition TempPool.h:83
Defines a simple memory pool used to call cub functions that use dynamic temporary storage.
Definition GridHandle.h:31
Non-owning reference to a memory resource that is itself a resource: copying the ref shares the under...
Definition DeviceResource.h:312
Detection trait: is_async_resource<R>::value is true iff R models the stream-ordered AsyncResource co...
Definition DeviceResource.h:118