openvdb icon indicating copy to clipboard operation
openvdb copied to clipboard

Add NANOVDB_USE_SYNC_CUDA_MALLOC define to force sync CUDA malloc

Open w0utert opened this issue 1 year ago • 8 comments

In virtualized environments that slice up the GPU and share it between instances as vGPU's, GPU unified memory is usually disabled out of security considerations. Asynchronous CUDA malloc/free depends on GPU unified memory, so before, it was not possible to deploy and run NanoVDB code in such environments.

This commit adds macros CUDA_MALLOC and CUDA_FREE and replaces all CUDA alloc/free calls with these macros. CUDA_MALLOC and CUDA_FREE expand to asynchronous CUDA malloc & free if the following two conditions are met:

  • CUDA version needs to be >= 11.2 as this is the first version that supports cudaMallocAsync/cudaMallocFree
  • NANOVDB_USE_SYNC_CUDA_MALLOC needs to undefined

In all other cases, CUDA_MALLOC and CUDA_FREE expand to synchronous cudaMalloc/cudaFree.

Since NanoVDB is distributed as header-only, setting the NANOVDB_USE_SYNC_CUDA_MALLOC flag should be handled by the project's build system itself.

w0utert avatar Apr 25 '24 12:04 w0utert

@kmuseth that was the first solution I tried, but it didn’t work, because cudaMalloc and cudaFree calls would still be resolved to the CUDA ones and not the redefined ones from the NanoVDB header. Without any modification to our build I still got CUDA ‘not supported’ errors on allocations.

Maybe this can be worked around with some linker directives but that seems brittle and could require possible annoying changes to the build system of projects that include the NanoVDB headers.

w0utert avatar May 03 '24 08:05 w0utert

This was on Linux by the way, so it’s interesting it did work on your side, I assume there could be some link differences between our builds. I could double check next week to verify again to be sure and to find out why.

w0utert avatar May 03 '24 08:05 w0utert

@w0utert I think you're right so I changed my implementation by simply placing the definitions of the functions in a namespace (nanovdb). So in the end my solution looks very much like yours, except I define functions vs macros. Let me create a PR that includes this fix plus a ton of other (long overdue) improvements to NanoVDB. I'll point you to the relevant changes so you can validate that it does indeed work for you.

A warning, this new PR introduces new namespaces in NanoVDB so your client code might need to be tweaked. I can of course help.

kmuseth avatar May 03 '24 18:05 kmuseth

@kmuseth sounds good, that’s actually nicer than using macro’s! I will try your PR early next week and report back, but I’m pretty sure it will work.

w0utert avatar May 03 '24 18:05 w0utert

@kmuseth I tested the feature/nanovdb_v32.7 branch from your fork and it works on Linux as well, thanks!

w0utert avatar May 17 '24 10:05 w0utert

@w0utert excellent - so are you okay if we close this PR?

kmuseth avatar May 24 '24 03:05 kmuseth

@kmuseth yes, you can close this PR!

w0utert avatar May 24 '24 07:05 w0utert

not sure why I left this PR open for so long (since we agreed to close it:)

kmuseth avatar Apr 01 '25 20:04 kmuseth