Skip to content

P0a: the CUDA volume cache per device, shared by the apps the host hands it to - #1281

Merged
transfix merged 5 commits into
masterfrom
p0a/cuda-device-caches
Nov 17, 2026
Merged

transfix merged 5 commits into
masterfrom
p0a/cuda-device-caches

Conversation

@transfix

@transfix transfix commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

The CUDA volume cache is per device, and the apps the host hands it to share it. This is the last open part of roadmap §9 item 5, which Joe settled on 2026-11-14 ("The CUDA volume cache becomes per device, owned by whichever object owns the device (not duplicated per app)"). It adds no global and no allowlist line: the host owns the caches, as it owns the device.

Branched from #1276's head (1c465e1). Until #1276 merges, this diff also shows #1276's commits; the change here is the last two commits. Please merge after #1276, with a merge commit.

What changed

  • cuda_device_caches (raycaster_cuda.h): one resident cache per CUDA device, each with a budget of its own (default 512 MB per device), made the first time a render runs on that device. set_budget() sets every device's; resident() and uploaded() add up all devices.
  • raycast_cuda_cache, the app's slot, is now a handle on the caches its renders use:
    • its own cuda_device_caches by default, so apps share nothing unless the host says so;
    • share(caches) points it at caches another app (or the host) holds. Pass raycast_cuda_cache_of(a).caches() to share app a's;
    • set_budget, budget, resident, uploaded, forget_host and clear act on those caches, as before.
  • Content-generation stamps (invalidate_device_volume) come from the caches' counter, not the app's. With a counter per app, two apps sharing caches and a voxel buffer could each stamp an in-place write 1, and the second would be served the block staged before its write.
  • share() comes before the app's first render. It throws std::logic_error once the app's caches have staged a block or drawn a stamp, because the stamps its raycasters hold mean nothing in other caches. Null throws std::invalid_argument; sharing the same caches again does nothing.
  • A render holds the caches for its whole length (a shared_ptr copy), so a share() on another thread swaps the handle without pulling blocks out from under a running kernel. The caches outlive the app that made them.
  • Frees run on the device that owns the block. clear, forget_host, set_budget and the destructor can run on any thread, so each per-device cache makes its device current while it frees (device_scope), then puts the caller's back.

Behaviour changes to review

  • The budget is per device. It used to be one budget across every device an app rendered on. A single-GPU machine sees no difference.
  • raycast_cuda_cache::impl and get() are gone; cuda_device_caches::get() replaces them. Nothing outside raycast.cu used them.

Tests

  • VolrenMultiApp.AppsShareDeviceCachesOnlyWhenHandedThem (every build): two apps handed one app's caches draw their stamps from the shared counter; a third app keeps its own; each renders what it renders alone; share() after a stamp and share(nullptr) throw.
  • VolrenMultiApp.SharedDeviceCachesOutliveTheirMakerAndServeThreadsAtOnce (every build): caches whose maker is gone serve three apps rendering at once on threads, each as it would alone.
  • VolrenCudaTest.AppsHandedOneDeviceCachesShareTheirBlocks (CUDA device): a volume one app staged is not uploaded again for the other; a budget set through one app is the other's; the blocks survive the first app; an app not handed the caches stages its own copy.
  • VolrenCudaTest.SharedCachesNeverServeAStaleBlockAcrossApps (CUDA device): two apps sharing caches and one buffer each announce an in-place write; every render matches a clean app's render of the current bytes.
  • Existing: VolrenMultiApp.* and VolrenCudaTest.EachAppHasItsOwnResidentCache still hold, since apps share nothing by default.

Testing

  • Windows, CUDA off (MSVC Release): full build; the volren and multi-app ctests, 80 of 80, the two new VolrenMultiApp tests among them.
  • Windows, CUDA 12.5.1 on, on a GPU (nvcc for sm_89, an RTX 4070 Laptop GPU; the toolkit from cvcpkg's cuda-toolkit recipe):
    • volren_cuda_test: 28 of 28 pass on the device, the two new tests among them (run, not skipped);
    • VolrenMultiApp.*: 5 of 5, and multi_app_integration_test: 5 of 5, with the CUDA backend.
  • Negative control: with the stamp counter put back per app, SharedCachesNeverServeAStaleBlockAcrossApps fails ("b was served the block staged before its write"). With the shared counter it passes.
  • nvcc: no warning from the changed code (the per-device cache's unused budget() was removed after nvcc's warning 177, "declared but never referenced").
  • clang-format: clean on changed lines. The allowlist is unchanged.
  • Not run: Linux CUDA (CI compiles it in package-linux / libcvc-release-grpc, which has no GPU), and more than one GPU. This machine has one, so the split across devices and the frees on each block's own device are exercised only on device 0.

Roadmap 9 item 5, settled 2026-11-14: "The CUDA volume cache becomes
per device, owned by whichever object owns the device (not duplicated
per app)". It was one cache per app, its blocks keyed by device under
one budget.

- cuda_device_caches holds one resident cache per CUDA device, each
  with its own budget (512 MB by default), made when a render first
  runs on that device.
- raycast_cuda_cache, the app's slot, is a handle on the caches its
  renders use: its own by default, so apps share nothing unless the
  host says so; share(caches) points it at caches another app or the
  host holds. No global and no allowlist line: the host owns them.
- Content-generation stamps come from the caches' counter, not the
  app's. Two apps sharing caches and a voxel buffer, each with its own
  counter, could both stamp an in-place write 1 and the second would
  get the block staged before its write. share() therefore comes
  before an app's first render, and throws once its caches have staged
  a block or drawn a stamp.
- A render holds the caches (a shared_ptr copy) for its length, so a
  share() on another thread never frees blocks a kernel is reading;
  the caches outlive the app that made them.
- clear, forget_host, set_budget and the destructor make the cache's
  device current while they free (device_scope), so a block is freed
  on the device that allocated it whichever thread calls.

Tests: VolrenMultiApp.AppsShareDeviceCachesOnlyWhenHandedThem and
SharedDeviceCachesOutliveTheirMakerAndServeThreadsAtOnce (every
build); VolrenCudaTest.AppsHandedOneDeviceCachesShareTheirBlocks and
SharedCachesNeverServeAStaleBlockAcrossApps (with a CUDA device).
VOLREN_API.md and the roadmap say who shares the caches.
The device caches keep the budget each new device cache starts with, so
cuda_device_caches::budget() reads that; nvcc warned (#177) that the
per-device cache's own accessor was never referenced.
@transfix
transfix changed the base branch from p0a/last-globals to master November 15, 2026 21:37
@transfix
transfix enabled auto-merge November 15, 2026 23:25
#1276 moved _arihost's lent relay onto the module's own state, so
server_link() became server_link(self). #1302 (bridged users) was
tested on a base without #1276 and added five calls in the old form:
room, bridge_presence, bridge_absent, bridge_post and listen_chat.
Master has not compiled arisrvx since #1302 landed (pycvc-linux /
cp312 on #1281, run 43951076345: "too few arguments to function
server_link"). Name each one's module parameter self and pass it,
as their neighbours do.
@transfix
transfix merged commit 5f2218f into master Nov 17, 2026
29 checks passed
@transfix
transfix deleted the p0a/cuda-device-caches branch November 17, 2026 07:22
transfix added a commit that referenced this pull request Nov 17, 2026
No conflicts. #1328 fixes master's arisrvx build (the bridge ops pass self to server_link, after
#1276 and #1302 crossed), which failed this PR's pycvc-linux / cp312 job at c7735de.
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