Skip to content

feat(pycvc): zero-copy numpy views + CUDA unified-memory adapter - #114

Merged
transfix merged 5 commits into
masterfrom
feat/pycvc-numpy
Jul 21, 2026
Merged

transfix merged 5 commits into
masterfrom
feat/pycvc-numpy

Conversation

@transfix

Copy link
Copy Markdown
Owner

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's base is a capsule owning a shared_ptr to the underlying cvc storage, so the view stays valid even after the facade object is garbage-collected, and is freed only when the last view dies.

  • Mechanism: a Python-free 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, the shared_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).
  • CUDA accessors are #ifdef CVC_USING_CUDA; host-only builds report on_gpu()==False and raise AttributeError on 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)

  • Data processing: bidirectional shared memory, empty/colors/grid shapes + grid[k,j,i]==value(i,j,k), writability.
  • Ownership/lifetime state: view outlives facade GC (geometry + volume); many views then drop source; all share one buffer.
  • Threading: concurrent build+view across 16 threads; concurrent reads of a shared view; writer/reader consistency.
  • GPU adapter: host-build degradation.

ctest 2/2 green. numpy 2.x via Python3::NumPy; import_array in %init.

transfix added 3 commits July 19, 2026 18:30
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).
…-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.
# Conflicts:
#	bindings/pycvc/pycvc_volume.cpp
@transfix
transfix merged commit 660fe96 into master Jul 21, 2026
4 of 8 checks passed
@transfix
transfix deleted the feat/pycvc-numpy branch July 21, 2026 06:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant