OpenVDB 13.1.0
Loading...
Searching...
No Matches
PinnedResource.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_PINNEDRESOURCE_H_HAS_BEEN_INCLUDED
5#define NANOVDB_CUDA_PINNEDRESOURCE_H_HAS_BEEN_INCLUDED
6
7#include <cuda_runtime_api.h>
9
10#include <cstddef>
11
12namespace nanovdb {
13
14namespace cuda {
15
16/// @brief Default host-pinned memory resource. Allocations are page-locked
17/// host memory (cudaMallocHost) that is both host-accessible and
18/// device-accessible, so it can serve as the host side of an
19/// asynchronous host<->device copy.
20/// @note This resource is *synchronous*: cudaMallocHost / cudaFreeHost
21/// synchronize the context, so it models the synchronous Resource
22/// concept (allocate / deallocate, no stream) rather than the
23/// stream-ordered AsyncResource concept. It is a seam so callers can
24/// substitute a pooled / genuinely stream-ordered pinned-host resource
25/// and avoid that synchronization.
27{
28public:
29 // cudaMallocHost over-aligns (to at least the page size), so the requested
30 // alignment is always satisfied; 256 is the nominal default advertised here.
31 static constexpr size_t DEFAULT_ALIGNMENT = 256;
32
33 /// @brief Pinned allocations are mapped into the host address space, so
34 /// containers over this resource remain host-readable (detected by
35 /// nanovdb::cuda::is_host_accessible_resource).
36 static constexpr bool HOST_ACCESSIBLE = true;
37
38 /// @brief Synchronous allocation of page-locked host memory.
39 /// @param bytes number of bytes to allocate
40 /// @param alignment requested alignment (ignored; cudaMallocHost is page-aligned)
41 void* allocate(size_t bytes, size_t alignment) {
42 (void)alignment;
43 void* p = nullptr;
44 cudaCheck(cudaMallocHost(&p, bytes));
45 return p;
46 }
47
48 /// @brief Synchronous deallocation.
49 /// @param p pointer previously returned by allocate
50 /// @param bytes size passed to the matching allocate (unused)
51 /// @param alignment alignment passed to the matching allocate (unused)
52 void deallocate(void* p, size_t bytes, size_t alignment) {
53 (void)bytes;
54 (void)alignment;
55 cudaCheck(cudaFreeHost(p));
56 }
57};
58
59}
60
61} // namespace nanovdb::cuda
62
63#endif // end of NANOVDB_CUDA_PINNEDRESOURCE_H_HAS_BEEN_INCLUDED
Default host-pinned memory resource. Allocations are page-locked host memory (cudaMallocHost) that is...
Definition PinnedResource.h:27
static constexpr bool HOST_ACCESSIBLE
Pinned allocations are mapped into the host address space, so containers over this resource remain ho...
Definition PinnedResource.h:36
void deallocate(void *p, size_t bytes, size_t alignment)
Synchronous deallocation.
Definition PinnedResource.h:52
void * allocate(size_t bytes, size_t alignment)
Synchronous allocation of page-locked host memory.
Definition PinnedResource.h:41
static constexpr size_t DEFAULT_ALIGNMENT
Definition PinnedResource.h:31
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