Skip to content

[BUG] NanoVDB new device buffer segfault due to "cudaCpuDeviceId" #2076

@ZephirFXEC

Description

@ZephirFXEC

Bug: Segfault in DeviceBuffer constructor

OpenVDB 12.1.0
Windows 10
Cuda 12.8

Summary:
Creating a nanovdb::GridHandle<nanovdb::cuda::DeviceBuffer> on the host and passing it to a cuda function such as nanovdb::tools::cuda::voxelsToGrid causes a segmentation fault. The crash occurs because the DeviceBuffer(const HostBuffer&, ...) constructor defaults its device parameter to cudaCpuDeviceId (-1), which is then used as an invalid index for the mGpuData array. Removing the "cudaCpuDeviceId" and setting it to 0 works.

Problematic Code :

/// @brief Copy-constructor from a HostBuffer
/// @param buffer host buffer from which to copy data
/// @param device id of the device on which to initialize the buffer
/// @param stream cuda stream
DeviceBuffer(const HostBuffer& buffer, int device = cudaCpuDeviceId, cudaStream_t stream = 0)
: DeviceBuffer(buffer.size(), device, stream)
{
if (mCpuData) {
cudaCheck(cudaMemcpy(mCpuData, buffer.data(), mSize, cudaMemcpyHostToHost));
} else if (mGpuData[device]) {
cudaCheck(cudaMemcpyAsync(mGpuData[device], buffer.data(), mSize, cudaMemcpyHostToDevice, stream));
}
}

Reproduce :

Host Code :

	nanovdb::GridHandle<nanovdb::cuda::DeviceBuffer> handle;
	CreateIndexGrid(data, handle, voxelSize); // <- Function from .cu file 

Device Code :

void create_index_grid(HNS::GridIndexedData& data, nanovdb::GridHandle<nanovdb::cuda::DeviceBuffer>& handle, const float voxelSize) {
	nanovdb::Coord* d_coords = nullptr;
	cudaMalloc(&d_coords, data.size() * sizeof(nanovdb::Coord));
	cudaMemcpy(d_coords, data.pCoords(), data.size() * sizeof(nanovdb::Coord), cudaMemcpyHostToDevice);
	handle = nanovdb::tools::cuda::voxelsToGrid<nanovdb::ValueOnIndex, nanovdb::Coord*>(d_coords, data.size(), voxelSize);
	cudaFree(d_coords);
}

This is maybe not a bug rather an unexpected behaviour, as it was working fine before the DeviceBuffer rework in OpenVDB 12.0,

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions