Skip to content

Session

Compute-session management. A CarverSession keeps the CUDA context alive across pipeline calls and holds a value cache for results that are expensive to rebuild (voxelized grids, Tregenza patch directions and solid angles, and the sky-patch weights of the weighted modes). Compiled Warp kernels are cached by Warp itself, on disk.

Cache keys cover everything a value depends on (for a voxel grid: a hash of the mesh, voxel size, margin and device), so an entry stays valid for later runs. The cache holds at most max_cache_bytes (1 GiB by default) and drops the least recently used entries beyond that. The Grasshopper daemon runs one long-lived session and keeps its cache between commands, so re-running preprocessing on unchanged geometry skips voxelization; after each command it returns only unused GPU memory to the driver. The Python API and CLI run without a session unless you open one with with CarverSession():. The session_cache decorator stores a function's result in the active session under a key built from its arguments.

urbansolarcarver.session

Long-lived compute session shared by consecutive pipeline runs.

CarverSession keeps the CUDA context alive (the daemon holds one for its whole lifetime) and caches values that are expensive to rebuild, such as voxel grids and the Tregenza patch directions and solid angles. Compiled Warp kernels are cached by Warp itself, on disk.

Cache keys must describe everything the cached value depends on, including the device it lives on, so an entry can be reused by any later run. The cache holds at most max_cache_bytes of arrays and tensors and drops the least recently used entries beyond that; bump() clears it.

Public API: CarverSession context manager owning the cache get_active_session the session registered for a device, if any session_cache decorator caching a function's result in the session

CarverSession(device=None, max_cache_bytes=DEFAULT_CACHE_BYTES)

Bases: AbstractContextManager['CarverSession']

A compute session with a value cache, registered per device.

Creating a session registers it as the active one for its device (replacing any earlier one); leaving its with block, or close(), clears the cache and unregisters it.

device: "auto" or None (CUDA if available, else CPU), "cpu", "cuda", "cuda:N" or a torch.device, resolved by :func:~urbansolarcarver.compute_device.resolve_compute_device.

max_cache_bytes: memory the cache may hold; a value larger than this on its own is returned but not cached.

bump(flush=False)

Clear the cache; flush also returns unused CUDA memory to the driver.

release_memory()

Return CUDA memory not held by live tensors (including cached ones) to the driver.

get_tensor(key, factory)

Return the cached value for key, computing it with factory on a miss.

The value is stored as returned, so callers must not modify it in place, and the key must identify its device. Storing it may drop the least recently used entries to stay within max_cache_bytes.

close(flush=True)

Clear the cache and stop being the active session for this device.

get_active_session(device=None)

The session registered for device, or None.

With no device (or "auto"), a single registered session is returned whatever its device; otherwise the session for the default device.

session_cache(key)

Cache a function's return value in the active session.

key is a format string over args and kwargs (e.g. "{args[0]}" for a device passed positionally) or a callable key(args, kwargs) -> str for keys that need computing (e.g. a hash of mesh contents). The function name is prefixed to the key. The key must cover everything the result depends on, including its device.

The wrapped function accepts an extra session= keyword; otherwise the active session is used. Without a session the function simply runs, and if the key cannot be formatted from the call's arguments it runs uncached, with a one-time log warning.

Choosing the compute device

compute_device.resolve_compute_device is the one rule every part of the package uses to turn a device choice into a torch.device: "auto" (or None) gives CUDA when torch sees a CUDA GPU, else the CPU; "cpu", "cuda" and "cuda:N" are taken as given, and a CUDA choice on a machine without CUDA raises at once. warp_device_name gives the matching Warp device name.

urbansolarcarver.compute_device

The one rule for choosing the compute device.

Every place that turns a device choice into a torch.device (the stages, the session, the daemon, the weight and voxelization helpers, the kernel warmup) calls :func:resolve_compute_device, and every Warp launch names its device with :func:warp_device_name.

resolve_compute_device(choice='auto')

Turn a device choice into a torch.device.

None or "auto" (any case) gives CUDA when torch sees a CUDA GPU, else the CPU. "cpu", "cuda", "cuda:N" and a torch.device are taken as given.

Raises:

Type Description
RuntimeError

If the choice names a CUDA device and torch sees no CUDA GPU.

ValueError

If a string names neither auto, cpu nor a CUDA device.

TypeError

If the choice is not a string, a torch.device or None.

cuda_device_supported(index=0, capability=None, arch_list=None)

Whether this torch build has GPU code for CUDA device index.

torch reports a GPU as available even when its build has no kernels for that GPU's generation: torch 2.14.1 for CUDA 12.6 stops at sm_90, so an RTX 50-series card (sm_120) failed at its first GPU operation. A build runs on a GPU when it has machine code of the same major version and an equal or lower minor (sm_80 runs on sm_86), or PTX of an equal or lower version, which the driver compiles for newer GPUs. capability and arch_list stand in for the device's and the build's in tests.

unsupported_gpu_message(index=0)

Why the GPU at index cannot be used, and what to do.

cuda_is_usable()

Whether torch sees a CUDA GPU and has code for it; logs once why a GPU it sees cannot be used.

warp_device_name(device)

The Warp name of a torch device: "cuda:N" (N defaults to 0) or "cpu".