OpenVDB 13.1.0
Loading...
Searching...
No Matches
ManagedResource.h
Go to the documentation of this file.
1// Copyright Contributors to the OpenVDB Project
2// SPDX-License-Identifier: Apache-2.0
3//
4#ifndef NANOVDB_CUDA_MANAGEDRESOURCE_H_HAS_BEEN_INCLUDED
5#define NANOVDB_CUDA_MANAGEDRESOURCE_H_HAS_BEEN_INCLUDED
6
7#include <cuda_runtime_api.h>
9
10#include <cstddef>
11
12namespace nanovdb {
13
14namespace cuda {
15
16/// @brief Managed (unified) memory resource. Allocations
17/// (cudaMallocManaged) are accessible from both the host and the
18/// device, with the driver migrating pages on demand, so a container
19/// over this resource serves grids that are read on both sides --
20/// the replacement for the legacy UnifiedBuffer. A GridHandle over
21/// this resource parses (and validates) its metadata through the
22/// device like any device buffer -- a host-side parse could race
23/// still-running producer kernels -- while the host accessors remain
24/// available afterwards; ordering host reads after device writes is
25/// the caller's responsibility, exactly as with UnifiedBuffer.
26/// @note This resource is *synchronous*: cudaMallocManaged / cudaFree have
27/// no stream-ordered form, so it models the synchronous Resource
28/// concept (allocate / deallocate, no stream). The usual contract
29/// applies: the caller ensures device work touching an allocation has
30/// completed before it is freed.
32{
33public:
34 // cudaMallocManaged aligns to at least 256 bytes.
35 static constexpr size_t DEFAULT_ALIGNMENT = 256;
36
37 /// @brief Managed allocations are mapped into the host address space
38 /// (detected by nanovdb::cuda::is_host_accessible_resource).
39 static constexpr bool HOST_ACCESSIBLE = true;
40
41 /// @brief Managed allocations are also valid device addresses (detected
42 /// by nanovdb::cuda::is_device_accessible_resource), so a handle
43 /// over this resource exposes the device accessors as well as the
44 /// host ones.
45 static constexpr bool DEVICE_ACCESSIBLE = true;
46
47 /// @brief Synchronous allocation of managed memory.
48 /// @param bytes number of bytes to allocate
49 /// @param alignment requested alignment (ignored; cudaMallocManaged
50 /// satisfies at least DEFAULT_ALIGNMENT)
51 void* allocate(size_t bytes, size_t alignment) {
52 (void)alignment;
53 void* p = nullptr;
54 cudaCheck(cudaMallocManaged(&p, bytes));
55 return p;
56 }
57
58 /// @brief Synchronous deallocation.
59 /// @param p pointer previously returned by allocate
60 /// @param bytes size passed to the matching allocate (unused)
61 /// @param alignment alignment passed to the matching allocate (unused)
62 void deallocate(void* p, size_t bytes, size_t alignment) {
63 (void)bytes;
64 (void)alignment;
65 cudaCheck(cudaFree(p));
66 }
67};
68
69}// namespace cuda
70
71}// namespace nanovdb
72
73#endif // NANOVDB_CUDA_MANAGEDRESOURCE_H_HAS_BEEN_INCLUDED
Managed (unified) memory resource. Allocations (cudaMallocManaged) are accessible from both the host ...
Definition ManagedResource.h:32
static constexpr bool HOST_ACCESSIBLE
Managed allocations are mapped into the host address space (detected by nanovdb::cuda::is_host_access...
Definition ManagedResource.h:39
void deallocate(void *p, size_t bytes, size_t alignment)
Synchronous deallocation.
Definition ManagedResource.h:62
void * allocate(size_t bytes, size_t alignment)
Synchronous allocation of managed memory.
Definition ManagedResource.h:51
static constexpr bool DEVICE_ACCESSIBLE
Managed allocations are also valid device addresses (detected by nanovdb::cuda::is_device_accessible_...
Definition ManagedResource.h:45
static constexpr size_t DEFAULT_ALIGNMENT
Definition ManagedResource.h:35
Definition GridHandle.h:37
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