4#ifndef NANOVDB_CUDA_DEVICERESOURCE_H_HAS_BEEN_INCLUDED
5#define NANOVDB_CUDA_DEVICERESOURCE_H_HAS_BEEN_INCLUDED
7#include <cuda_runtime_api.h>
54 void*
allocate(
size_t bytes,
size_t alignment) {
56 cudaCheck(cudaStreamSynchronize(cudaStream_t(0)));
65 void deallocate(
void* p,
size_t bytes,
size_t alignment) {
112template <
class R,
class =
void>
117 decltype(std::declval<R&>().allocate_async(size_t{0}, size_t{0}, cudaStream_t{0})),
118 decltype(std::declval<R&>().deallocate_async(std::declval<void*>(), size_t{0}, size_t{0}, cudaStream_t{0})),
119 decltype(std::declval<R&>().allocate(size_t{0}, size_t{0})),
120 decltype(std::declval<R&>().deallocate(std::declval<void*>(), size_t{0}, size_t{0}))>>
129template<
typename R,
typename =
void>
141template<
typename R,
typename =
void>
159template <
class R,
class =
void>
164 decltype(std::declval<R&>().allocate(size_t{0}, size_t{0})),
165 decltype(std::declval<R&>().deallocate(std::declval<void*>(), size_t{0}, size_t{0}))>>
174template <
class R,
class =
void>
179 std::enable_if_t<is_resource<R>::value || is_async_resource<R>::value>,
180 decltype(bool(std::declval<const R&>() == std::declval<const R&>()))>>
206template <
class Derived>
216 void* p =
static_cast<Derived&
>(*this).allocate_async(bytes, alignment, cudaStream_t{0});
217 cudaCheck(cudaStreamSynchronize(cudaStream_t{0}));
229 static_cast<Derived&
>(*this).deallocate_async(p, bytes, alignment, cudaStream_t{0});
249 if (bytes == 0)
return nullptr;
287 "AsyncFromSync requires R to model the synchronous Resource concept");
306 if (p ==
nullptr)
return;
307 cudaCheck(cudaStreamSynchronize(stream));
308 resource.deallocate(p, bytes, alignment);
312 void*
allocate(
size_t bytes,
size_t alignment) {
return resource.allocate(bytes, alignment); }
313 void deallocate(
void* p,
size_t bytes,
size_t alignment) {
resource.deallocate(p, bytes, alignment); }
317 template<class S = R, std::enable_if_t<is_equality_comparable_resource<S>::value,
int> = 0>
319 template<class S = R, std::enable_if_t<is_equality_comparable_resource<S>::value,
int> = 0>
340 "ResourceRef requires R to model the AsyncResource or the Resource concept");
342 "a memory resource needs operator==: add friend bool operator==(const R&, const R&), true when memory "
343 "allocated through one may be deallocated through the other (always, for a stateless resource), "
344 "and operator!= for cuda::mr under C++17");
359 template<class S = R, std::enable_if_t<is_async_resource<S>::value,
int> = 0>
362 return mResource->allocate_async(bytes, alignment, stream);
364 template<class S = R, std::enable_if_t<is_async_resource<S>::value,
int> = 0>
367 mResource->deallocate_async(p, bytes, alignment, stream);
372 void*
allocate(
size_t bytes,
size_t alignment) {
return mResource->allocate(bytes, alignment); }
373 void deallocate(
void* p,
size_t bytes,
size_t alignment) { mResource->deallocate(p, bytes, alignment); }
380 return lhs.mResource == rhs.mResource || *lhs.mResource == *rhs.mResource;
Default stream-ordered device memory resource. Allocations are made with cudaMallocAsync and freed wi...
Definition DeviceResource.h:27
friend bool operator!=(const DeviceResource &, const DeviceResource &)
Definition DeviceResource.h:73
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:65
void * allocate(size_t bytes, size_t alignment)
Synchronous allocation; the returned memory is immediately valid on every stream.
Definition DeviceResource.h:54
void * allocate_async(size_t bytes, size_t, cudaStream_t stream)
Stream-ordered allocation.
Definition DeviceResource.h:37
void deallocate_async(void *p, size_t, size_t, cudaStream_t stream)
Stream-ordered deallocation.
Definition DeviceResource.h:46
static constexpr size_t DEFAULT_ALIGNMENT
Definition DeviceResource.h:30
friend bool operator==(const DeviceResource &, const DeviceResource &)
Stateless: memory allocated through one instance may be deallocated through any other,...
Definition DeviceResource.h:72
Synchronous device memory resource backed by cudaMalloc/cudaFree. Models only the Resource concept: i...
Definition DeviceResource.h:240
friend bool operator!=(const MallocResource &, const MallocResource &)
Definition DeviceResource.h:263
friend bool operator==(const MallocResource &, const MallocResource &)
Stateless: memory allocated through one instance may be deallocated through any other,...
Definition DeviceResource.h:262
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:257
static constexpr size_t DEFAULT_ALIGNMENT
Definition DeviceResource.h:243
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:247
Definition VoxToNanoVDB.h:15
ResourceRef< R > borrowInternal(R &resource)
Returns a ResourceRef to resource: the one spelling NanoVDB's own components use to borrow a caller's...
Definition DeviceResource.h:393
R & default_resource()
Returns a program-lifetime, address-stable reference to a default instance of resource R.
Definition DeviceResource.h:82
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:34
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:285
static constexpr bool HOST_ACCESSIBLE
The adapter is host-accessible iff the adapted resource is.
Definition DeviceResource.h:292
void deallocate(void *p, size_t bytes, size_t alignment)
Definition DeviceResource.h:313
void * allocate(size_t bytes, size_t alignment)
Synchronous pair, forwarding to the wrapped resource.
Definition DeviceResource.h:312
static constexpr bool DEVICE_ACCESSIBLE
Definition DeviceResource.h:293
static constexpr size_t DEFAULT_ALIGNMENT
Definition DeviceResource.h:289
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:299
friend bool operator!=(const AsyncFromSync &lhs, const AsyncFromSync &rhs)
Definition DeviceResource.h:320
friend bool operator==(const AsyncFromSync &lhs, const AsyncFromSync &rhs)
Two adapters compare equal iff their wrapped resources do; present only when R itself is equality-com...
Definition DeviceResource.h:318
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:304
R resource
Definition DeviceResource.h:295
Non-owning reference to a memory resource that is itself a resource: copying the ref shares the under...
Definition DeviceResource.h:338
static constexpr bool HOST_ACCESSIBLE
A reference is host-accessible iff the referenced resource is.
Definition DeviceResource.h:349
void deallocate(void *p, size_t bytes, size_t alignment)
Definition DeviceResource.h:373
void * allocate(size_t bytes, size_t alignment)
Synchronous pair, forwarding to the referenced resource.
Definition DeviceResource.h:372
static constexpr bool DEVICE_ACCESSIBLE
Definition DeviceResource.h:350
friend bool operator!=(ResourceRef lhs, ResourceRef rhs)
Definition DeviceResource.h:382
ResourceRef(R &resource)
Constructs a ref borrowing resource.
Definition DeviceResource.h:354
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:365
static constexpr size_t DEFAULT_ALIGNMENT
Definition DeviceResource.h:346
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:360
friend bool operator==(ResourceRef lhs, ResourceRef rhs)
Two refs compare equal iff memory allocated through one may be deallocated through the other: they re...
Definition DeviceResource.h:378
CRTP base supplying the synchronous half of the resource concept in terms of the stream-ordered half,...
Definition DeviceResource.h:208
void deallocate(void *p, size_t bytes, size_t alignment)
Frees p on the null stream.
Definition DeviceResource.h:227
void * allocate(size_t bytes, size_t alignment)
Allocates bytes usable on any stream when this returns.
Definition DeviceResource.h:214
Detection trait: is_async_resource<R>::value is true iff R models the stream-ordered AsyncResource co...
Definition DeviceResource.h:113
Companion detection: is_device_accessible_resource<R>::value is true iff R declares static constexpr ...
Definition DeviceResource.h:142
Detection trait: is_equality_comparable_resource<R>::value is true iff R models a resource concept an...
Definition DeviceResource.h:175
Detection trait: is_host_accessible_resource<R>::value is true iff R declares static constexpr bool H...
Definition DeviceResource.h:130
Detection trait: is_resource<R>::value is true iff R models the synchronous Resource concept,...
Definition DeviceResource.h:160