4#ifndef NANOVDB_CUDA_DEVICERESOURCE_H_HAS_BEEN_INCLUDED
5#define NANOVDB_CUDA_DEVICERESOURCE_H_HAS_BEEN_INCLUDED
7#include <cuda_runtime_api.h>
55 void*
allocate(
size_t bytes,
size_t alignment) {
57 cudaCheck(cudaStreamSynchronize(cudaStream_t(0)));
66 void deallocate(
void* p,
size_t bytes,
size_t alignment) {
70 [[deprecated(
"use the instance method allocate_async")]]
71 static void*
allocateAsync(
size_t bytes,
size_t alignment, cudaStream_t stream) {
75 [[deprecated(
"use the instance method deallocate_async")]]
76 static void deallocateAsync(
void *p,
size_t bytes,
size_t alignment, cudaStream_t stream) {
117template <
class R,
class =
void>
122 decltype(std::declval<R&>().allocate_async(size_t{0}, size_t{0}, cudaStream_t{0})),
123 decltype(std::declval<R&>().deallocate_async(std::declval<void*>(), size_t{0}, size_t{0}, cudaStream_t{0})),
124 decltype(std::declval<R&>().allocate(size_t{0}, size_t{0})),
125 decltype(std::declval<R&>().deallocate(std::declval<void*>(), size_t{0}, size_t{0}))>>
134template<
typename R,
typename =
void>
146template<
typename R,
typename =
void>
164template <
class R,
class =
void>
169 decltype(std::declval<R&>().allocate(size_t{0}, size_t{0})),
170 decltype(std::declval<R&>().deallocate(std::declval<void*>(), size_t{0}, size_t{0}))>>
193template <
class Derived>
203 void* p =
static_cast<Derived&
>(*this).allocate_async(bytes, alignment, cudaStream_t{0});
204 cudaCheck(cudaStreamSynchronize(cudaStream_t{0}));
216 static_cast<Derived&
>(*this).deallocate_async(p, bytes, alignment, cudaStream_t{0});
236 if (bytes == 0)
return nullptr;
268 "AsyncFromSync requires R to model the synchronous Resource concept");
287 if (p ==
nullptr)
return;
288 cudaCheck(cudaStreamSynchronize(stream));
289 resource.deallocate(p, bytes, alignment);
293 void*
allocate(
size_t bytes,
size_t alignment) {
return resource.allocate(bytes, alignment); }
294 void deallocate(
void* p,
size_t bytes,
size_t alignment) {
resource.deallocate(p, bytes, alignment); }
314 "ResourceRef requires R to model the AsyncResource or the Resource concept");
329 template<class S = R, std::enable_if_t<is_async_resource<S>::value,
int> = 0>
332 return mResource->allocate_async(bytes, alignment, stream);
334 template<class S = R, std::enable_if_t<is_async_resource<S>::value,
int> = 0>
337 mResource->deallocate_async(p, bytes, alignment, stream);
342 void*
allocate(
size_t bytes,
size_t alignment) {
return mResource->allocate(bytes, alignment); }
343 void deallocate(
void* p,
size_t bytes,
size_t alignment) { mResource->deallocate(p, bytes, alignment); }
Default stream-ordered device memory resource. Allocations are made with cudaMallocAsync and freed wi...
Definition DeviceResource.h:28
void deallocate(void *p, size_t bytes, size_t alignment)
Synchronous deallocation; the caller guarantees that device work touching the memory has completed.
Definition DeviceResource.h:66
void * allocate(size_t bytes, size_t alignment)
Synchronous allocation; the returned memory is immediately valid on every stream.
Definition DeviceResource.h:55
void * allocate_async(size_t bytes, size_t, cudaStream_t stream)
Stream-ordered allocation.
Definition DeviceResource.h:38
void deallocate_async(void *p, size_t, size_t, cudaStream_t stream)
Stream-ordered deallocation.
Definition DeviceResource.h:47
static constexpr size_t DEFAULT_ALIGNMENT
Definition DeviceResource.h:31
static void deallocateAsync(void *p, size_t bytes, size_t alignment, cudaStream_t stream)
Definition DeviceResource.h:76
static void * allocateAsync(size_t bytes, size_t alignment, cudaStream_t stream)
Definition DeviceResource.h:71
Synchronous device memory resource backed by cudaMalloc/cudaFree. Models only the Resource concept: i...
Definition DeviceResource.h:227
void deallocate(void *p, size_t, size_t)
Frees p with cudaFree; the caller guarantees that device work touching the memory has completed.
Definition DeviceResource.h:244
static constexpr size_t DEFAULT_ALIGNMENT
Definition DeviceResource.h:230
void * allocate(size_t bytes, size_t)
Allocates bytes with cudaMalloc; valid on every stream when this returns. A zero request returns null...
Definition DeviceResource.h:234
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
cudaError_t freeAsync(void *d_ptr, cudaStream_t)
Wrapper forced to synchronous cudaFree; see the mode comment above. The trailing stream argument is a...
Definition Util.h:127
cudaError_t mallocAsync(void **d_ptr, size_t size, cudaStream_t)
Wrapper forced to synchronous cudaMalloc; see the mode comment above. The trailing stream argument is...
Definition Util.h:120
Defines a simple memory pool used to call cub functions that use dynamic temporary storage.
Definition GridHandle.h:31
Cuda specific utility functions.
#define cudaCheck(ans)
Definition Util.h:49
Wrapper presenting a synchronous resource as a stream-ordered one, so it can drive components that re...
Definition DeviceResource.h:266
static constexpr bool HOST_ACCESSIBLE
The adapter is host-accessible iff the adapted resource is.
Definition DeviceResource.h:273
void deallocate(void *p, size_t bytes, size_t alignment)
Definition DeviceResource.h:294
void * allocate(size_t bytes, size_t alignment)
Synchronous pair, forwarding to the wrapped resource.
Definition DeviceResource.h:293
static constexpr bool DEVICE_ACCESSIBLE
Definition DeviceResource.h:274
static constexpr size_t DEFAULT_ALIGNMENT
Definition DeviceResource.h:270
void * allocate_async(size_t bytes, size_t alignment, cudaStream_t)
Allocates through the synchronous resource; the result is valid on every stream, hence trivially vali...
Definition DeviceResource.h:280
void deallocate_async(void *p, size_t bytes, size_t alignment, cudaStream_t stream)
Synchronizes stream, then frees through the synchronous resource – the synchronize makes the quiescen...
Definition DeviceResource.h:285
R resource
Definition DeviceResource.h:276
static constexpr bool HOST_ACCESSIBLE
A reference is host-accessible iff the referenced resource is.
Definition DeviceResource.h:319
void deallocate(void *p, size_t bytes, size_t alignment)
Definition DeviceResource.h:343
void * allocate(size_t bytes, size_t alignment)
Synchronous pair, forwarding to the referenced resource.
Definition DeviceResource.h:342
static constexpr bool DEVICE_ACCESSIBLE
Definition DeviceResource.h:320
friend bool operator!=(ResourceRef lhs, ResourceRef rhs)
Definition DeviceResource.h:348
ResourceRef(R &resource)
Constructs a ref borrowing resource.
Definition DeviceResource.h:324
void deallocate_async(void *p, size_t bytes, size_t alignment, cudaStream_t stream)
Stream-ordered pair, present only when R models AsyncResource, so a ref over a synchronous resource d...
Definition DeviceResource.h:335
static constexpr size_t DEFAULT_ALIGNMENT
Definition DeviceResource.h:316
void * allocate_async(size_t bytes, size_t alignment, cudaStream_t stream)
Stream-ordered pair, present only when R models AsyncResource, so a ref over a synchronous resource d...
Definition DeviceResource.h:330
friend bool operator==(ResourceRef lhs, ResourceRef rhs)
Two refs compare equal iff they reference the same resource, i.e. memory allocated through one may be...
Definition DeviceResource.h:347
CRTP base supplying the synchronous half of the resource concept in terms of the stream-ordered half,...
Definition DeviceResource.h:195
void deallocate(void *p, size_t bytes, size_t alignment)
Frees p on the null stream.
Definition DeviceResource.h:214
void * allocate(size_t bytes, size_t alignment)
Allocates bytes usable on any stream when this returns.
Definition DeviceResource.h:201
Detection trait: is_async_resource<R>::value is true iff R models the stream-ordered AsyncResource co...
Definition DeviceResource.h:118
Companion detection: is_device_accessible_resource<R>::value is true iff R declares static constexpr ...
Definition DeviceResource.h:147
Detection trait: is_host_accessible_resource<R>::value is true iff R declares static constexpr bool H...
Definition DeviceResource.h:135
Detection trait: is_resource<R>::value is true iff R models the synchronous Resource concept,...
Definition DeviceResource.h:165