Repository navigation
feat(pycvc): zero-copy numpy views + CUDA unified-memory adapter - #114
Merged
Merged
Conversation
Geometry.vertices()/vertex_colors() and Volume.grid() return numpy arrays
that VIEW the C++ buffers with no data copy. The array's base is a capsule
owning a shared_ptr to the underlying cvc storage, so the buffer stays
valid for exactly as long as any numpy view of it does — the view remains
correct even after the facade object is garbage-collected.
Mechanism: a Python-free pycvc::ArrayView {data, shape, dtype, writable,
shared_ptr<void> owner} produced by the facade, plus one SWIG out-typemap
that builds the array via PyArray_SimpleNewFromData (no copy) and
PyArray_SetBaseObject(capsule owning shared_ptr). Reusable for any future
buffer view. numpy linked via Python3::NumPy; import_array in %init.
Robust tests (test_pycvc_numpy.py, wired into ctest): views share memory
bidirectionally, empty/colors/volume-grid shapes + index correspondence,
writability, ownership lifetime (view outlives facade GC; many views + drop
source), and threading (concurrent build+view across 16 threads, shared-view
reads, writer/reader consistency). ctest 2/2 green.
…y_interface__ (device) libcvc stores GPU voxels in CUDA *unified* (managed) memory (cudaMallocManaged + CudaManagedDeleter shared_ptr), so the data pointer is host-accessible whether the voxels are resident on CPU or GPU. Consequences, now made explicit: - Volume.grid()'s zero-copy numpy view ALREADY works for GPU data — numpy reads the unified buffer directly and CUDA migrates pages on host access; the shared_ptr keeps the (cudaFree-deleted) allocation alive. No copy. - New: Volume.on_gpu() / cuda_ptr() and a __cuda_array_interface__ (CAI v3) property expose the SAME unified buffer to cupy/torch/numba for on-DEVICE zero-copy (no host migration). One unified allocation, two zero-copy views. The CUDA accessors are #ifdef CVC_USING_CUDA (propagated from cvc::cvc's interface defs); on host-only builds on_gpu() is False and CAI raises AttributeError — the correct signal for GPU libs. Caveat documented: if a GPU kernel is concurrently writing the unified buffer, the caller must synchronize before the host numpy view. Full GPU zero-copy validation requires a CUDA-enabled libcvc build (this SDK is CUDA-off). ctest green.
Adds to the parallel zero-copy + CUDA work on this branch: view-outlives- facade (shared_ptr lifetime), bidirectional mutation, empty-array safety, Volume.grid index order, and threading — 8 threads concurrently build+ view+mutate, plus concurrent views of one shared object (refcount churn).
transfix
force-pushed
the
feat/pycvc-numpy
branch
from
July 20, 2026 11:26
6d43b28 to
6df820a
Compare
…-memory adapter) The CUDA adapter had on_gpu()/cuda_ptr()/__cuda_array_interface__ but no way to MOVE a volume into unified memory, so on_gpu() was always False. Add Volume.enable_cuda()/disable_cuda()/using_cuda() (voxels::enableCUDA) and a static Volume.cuda_available(). Verified end-to-end against a CUDA-enabled libcvc (GTX 1650, CUDA 12.0): enable_cuda migrates voxels to managed memory; one allocation then serves grid() (host numpy, zero-copy) AND __cuda_array_interface__ (device, cupy/torch) with no copies; disable_cuda migrates back host-side preserving data. Added a cuda_available()-guarded test that skips on CPU builds.
This was referenced Jul 20, 2026
# Conflicts: # bindings/pycvc/pycvc_volume.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Makes pycvc rely on numpy heavily with zero copies, per Joe's request — reading C++ buffers into numpy without copying, lifetime-safe via a
shared_ptr, and extending the same idea to CUDA unified memory.Zero-copy views (host)
Geometry.vertices()/Geometry.vertex_colors()→(N,3)float64;Volume.grid()→(nz,ny,nx)float32. Each returns a numpy array that views the C++ buffer — no data copy. The array'sbaseis a capsule owning ashared_ptrto the underlyingcvcstorage, so the view stays valid even after the facade object is garbage-collected, and is freed only when the last view dies.pycvc::ArrayView{data, shape, dtype, writable, shared_ptr<void> owner}+ one reusable SWIG out-typemap (PyArray_SimpleNewFromData— no copy — +PyArray_SetBaseObject(capsule)). Any future buffer gets zero-copy for free.CUDA unified-memory adapter
libcvc stores GPU voxels in CUDA unified (
cudaMallocManaged) memory, which is host-accessible. So:Volume.grid()'s numpy view already works for GPU-resident data — numpy reads the unified buffer directly, CUDA migrates pages on host access, theshared_ptr(CudaManagedDeleter) keeps it alive.Volume.on_gpu()/cuda_ptr()+ a__cuda_array_interface__property expose the same unified buffer to cupy/torch/numba for on-device zero-copy. One unified allocation, two zero-copy views (host numpy + device CAI).#ifdef CVC_USING_CUDA; host-only builds reporton_gpu()==Falseand raiseAttributeErroron CAI (the correct signal for GPU libs). Caveat: if a GPU kernel is concurrently writing, sync before the host view. Full GPU validation needs a CUDA-enabled libcvc build (this one is CUDA-off).Robust tests (
test_pycvc_numpy.py, wired into ctest)grid[k,j,i]==value(i,j,k), writability.ctest 2/2 green. numpy 2.x via
Python3::NumPy;import_arrayin%init.