Skip to content

Commit c13dfc3

Browse files
committed
Add/restore support for custom resources in PointsToGrid
* Separate data from algorithm * Add custom resource support * Add docs * Add support for cudax::async_resource * Fix default alignment * Add cleanup * Split up DeviceResource into separate file * Fix file Signed-off-by: Matthew Cong <[email protected]>
1 parent c391f89 commit c13dfc3

File tree

8 files changed

+317
-284
lines changed

8 files changed

+317
-284
lines changed

nanovdb/nanovdb/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,11 @@ set(NANOVDB_INCLUDE_FILES
184184
set(NANOVDB_INCLUDE_CUDA_FILES
185185
cuda/DeviceBuffer.h
186186
cuda/DeviceMesh.h
187+
cuda/DeviceResource.h
187188
cuda/DeviceStreamMap.h
188189
cuda/GridHandle.cuh
189190
cuda/NodeManager.cuh
191+
cuda/TempPool.h
190192
cuda/UnifiedBuffer.h
191193
)
192194

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright Contributors to the OpenVDB Project
2+
// SPDX-License-Identifier: Apache-2.0
3+
//
4+
#ifndef NANOVDB_CUDA_DEVICERESOURCE_H_HAS_BEEN_INCLUDED
5+
#define NANOVDB_CUDA_DEVICERESOURCE_H_HAS_BEEN_INCLUDED
6+
7+
#include <cuda_runtime_api.h>
8+
9+
namespace nanovdb {
10+
11+
namespace cuda {
12+
13+
class DeviceResource
14+
{
15+
public:
16+
// cudaMalloc aligns memory to 256 bytes by default
17+
static constexpr size_t DEFAULT_ALIGNMENT = 256;
18+
19+
static void* allocateAsync(size_t bytes, size_t, cudaStream_t stream) {
20+
void* p = nullptr;
21+
cudaCheck(util::cuda::mallocAsync(&p, bytes, stream));
22+
return p;
23+
}
24+
25+
static void deallocateAsync(void *p, size_t, size_t, cudaStream_t stream) {
26+
cudaCheck(util::cuda::freeAsync(p, stream));
27+
}
28+
};
29+
30+
}
31+
32+
} // namespace nanovdb::cuda
33+
34+
#endif // end of NANOVDB_CUDA_DEVICERESOURCE_H_HAS_BEEN_INCLUDED

nanovdb/nanovdb/cuda/TempDevicePool.h

Lines changed: 0 additions & 49 deletions
This file was deleted.

nanovdb/nanovdb/cuda/TempPool.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright Contributors to the OpenVDB Project
2+
// SPDX-License-Identifier: Apache-2.0
3+
//
4+
#ifndef NANOVDB_CUDA_TEMPPOOL_H_HAS_BEEN_INCLUDED
5+
#define NANOVDB_CUDA_TEMPPOOL_H_HAS_BEEN_INCLUDED
6+
7+
#include <nanovdb/cuda/DeviceResource.h>
8+
9+
namespace nanovdb {
10+
11+
namespace cuda {
12+
13+
template <class Resource>
14+
class TempPool {
15+
public:
16+
TempPool() : mData(nullptr), mSize(0), mRequestedSize(0) {}
17+
~TempPool() {
18+
mRequestedSize = 0;
19+
Resource::deallocateAsync(mData, mSize, Resource::DEFAULT_ALIGNMENT, nullptr);
20+
mData = nullptr;
21+
mSize = 0;
22+
}
23+
24+
void* data() {
25+
return mData;
26+
}
27+
28+
size_t& size() {
29+
return mSize;
30+
}
31+
32+
size_t& requestedSize() {
33+
return mRequestedSize;
34+
}
35+
36+
void reallocate(cudaStream_t stream) {
37+
if (!mData || mRequestedSize > mSize) {
38+
Resource::deallocateAsync(mData, mSize, Resource::DEFAULT_ALIGNMENT, stream);
39+
mData = Resource::allocateAsync(mRequestedSize, Resource::DEFAULT_ALIGNMENT, stream);
40+
mSize = mRequestedSize;
41+
}
42+
}
43+
private:
44+
void* mData;
45+
size_t mSize;
46+
size_t mRequestedSize;
47+
};
48+
49+
using TempDevicePool = TempPool<DeviceResource>;
50+
51+
}
52+
53+
} // namespace nanovdb::cuda
54+
55+
#endif // end of NANOVDB_CUDA_TEMPPOOL_H_HAS_BEEN_INCLUDED

nanovdb/nanovdb/tools/cuda/AddBlindData.cuh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
to only include it in .cu files (or other .cuh files)
1515
*/
1616

17-
#ifndef NVIDIA_TOOLS_CUDA_ADDBLINDDATA_CUH_HAS_BEEN_INCLUDED
18-
#define NVIDIA_TOOLS_CUDA_ADDBLINDDATA_CUH_HAS_BEEN_INCLUDED
17+
#ifndef NANOVDB_TOOLS_CUDA_ADDBLINDDATA_CUH_HAS_BEEN_INCLUDED
18+
#define NANOVDB_TOOLS_CUDA_ADDBLINDDATA_CUH_HAS_BEEN_INCLUDED
1919

2020
#include <nanovdb/NanoVDB.h>
2121
#include <nanovdb/cuda/DeviceBuffer.h>
@@ -143,4 +143,4 @@ cudaAddBlindData(const NanoGrid<BuildT> *d_grid,
143143

144144
}// namespace nanovdb
145145

146-
#endif // NVIDIA_TOOLS_CUDA_ADDBLINDDATA_CUH_HAS_BEEN_INCLUDED
146+
#endif // NANOVDB_TOOLS_CUDA_ADDBLINDDATA_CUH_HAS_BEEN_INCLUDED

nanovdb/nanovdb/tools/cuda/DistributedPointsToGrid.cuh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
to only include it in .cu files (or other .cuh files)
1212
*/
1313

14-
#ifndef NVIDIA_TOOLS_CUDA_DISTRIBUTEDPOINTSTOGRID_CUH_HAS_BEEN_INCLUDED
15-
#define NVIDIA_TOOLS_CUDA_DISTRIBUTEDPOINTSTOGRID_CUH_HAS_BEEN_INCLUDED
14+
#ifndef NANOVDB_TOOLS_CUDA_DISTRIBUTEDPOINTSTOGRID_CUH_HAS_BEEN_INCLUDED
15+
#define NANOVDB_TOOLS_CUDA_DISTRIBUTEDPOINTSTOGRID_CUH_HAS_BEEN_INCLUDED
1616

1717
#include <nanovdb/GridHandle.h>
1818
#include <nanovdb/cuda/DeviceMesh.h>
19-
#include <nanovdb/cuda/TempDevicePool.h>
19+
#include <nanovdb/cuda/TempPool.h>
2020
#include <nanovdb/cuda/UnifiedBuffer.h>
2121
#include <nanovdb/tools/cuda/PointsToGrid.cuh>
2222
#include <nanovdb/util/cuda/Util.h>
@@ -279,7 +279,7 @@ private:
279279

280280
PointType mPointType;
281281
std::string mGridName;
282-
typename PointsToGrid<BuildT>::Data *mData;
282+
PointsToGridData<BuildT> *mData;
283283
CheckMode mChecksum{CheckMode::Disable};
284284

285285
size_t* mStripeCounts;
@@ -304,7 +304,7 @@ DistributedPointsToGrid<BuildT>::DistributedPointsToGrid(const nanovdb::cuda::De
304304
{
305305
mTempDevicePools = new nanovdb::cuda::TempDevicePool[mDeviceMesh.deviceCount()];
306306

307-
cudaCheck(cudaMallocManaged(&mData, sizeof(typename PointsToGrid<BuildT>::Data)));
307+
cudaCheck(cudaMallocManaged(&mData, sizeof(PointsToGridData<BuildT>)));
308308
mData->flags.initMask({GridFlags::HasBBox, GridFlags::IsBreadthFirst});
309309
mData->map = map;
310310

@@ -1106,4 +1106,4 @@ inline void DistributedPointsToGrid<BuildT>::processBBox()
11061106

11071107
} // namespace nanovdb
11081108

1109-
#endif // NVIDIA_TOOLS_CUDA_DISTRIBUTEDPOINTSTOGRID_CUH_HAS_BEEN_INCLUDED
1109+
#endif // NANOVDB_TOOLS_CUDA_DISTRIBUTEDPOINTSTOGRID_CUH_HAS_BEEN_INCLUDED

nanovdb/nanovdb/tools/cuda/IndexToGrid.cuh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
to only include it in .cu files (or other .cuh files)
1515
*/
1616

17-
#ifndef NVIDIA_TOOLS_CUDA_INDEXTOGRID_CUH_HAS_BEEN_INCLUDED
18-
#define NVIDIA_TOOLS_CUDA_INDEXTOGRID_CUH_HAS_BEEN_INCLUDED
17+
#ifndef NANOVDB_TOOLS_CUDA_INDEXTOGRID_CUH_HAS_BEEN_INCLUDED
18+
#define NANOVDB_TOOLS_CUDA_INDEXTOGRID_CUH_HAS_BEEN_INCLUDED
1919

2020
#include <nanovdb/NanoVDB.h>
2121
#include <nanovdb/cuda/DeviceBuffer.h>
@@ -415,4 +415,4 @@ cudaCreateNanoGrid(const NanoGrid<SrcBuildT> *d_srcGrid, const typename BuildToV
415415

416416
}// nanovdb namespace ===================================================================
417417

418-
#endif // NVIDIA_TOOLS_CUDA_INDEXTOGRID_CUH_HAS_BEEN_INCLUDED
418+
#endif // NANOVDB_TOOLS_CUDA_INDEXTOGRID_CUH_HAS_BEEN_INCLUDED

0 commit comments

Comments
 (0)