Data on the GPU#
When ovphysx simulates on a GPU, your program can get the results in CPU memory or keep them in GPU memory. This page shows where read results are and how to pass them to NumPy or PyTorch without copying them. It also shows when to keep them on the GPU and how to replay a whole GPU loop with a CUDA graph.

Where Results Are#
CPU and GPU lists the three modes. They differ in where your program finds the results:
In the default mode with an NVIDIA GPU, physics runs on the GPU, and ovphysx copies most results to CPU memory after every step, ready for NumPy.
In DirectGPU mode, simulated values such as positions stay in GPU memory, ready for code that runs on the GPU, such as a PyTorch network.
On the CPU, physics and results both stay in CPU memory.
The copy takes time every step. DirectGPU avoids it.
Check Where an Array Is#
Every array has a device: cpu for CPU memory, cuda:0 for the first GPU. Some arrays stay in
GPU memory even in the default mode, such as the points of soft bodies. In DirectGPU mode, one
read can return arrays in both places. Check each array’s device, in reads and writes alike,
before you use it.
Device Residency and Interop lists
where each attribute is.
Pass Arrays On Without Copying#
Reads return NVIDIA Warp arrays, which other libraries can use without a copy when they work with memory on that device:
To Use an Array In |
Call |
Copies the Data? |
|---|---|---|
NumPy |
|
No for an array in CPU memory. An array in GPU memory is copied to CPU memory. |
PyTorch |
|
No. The tensor uses the same memory, on the CPU or the GPU. |
Another library that supports DLPack, the standard for sharing arrays |
That library’s |
No. |
When nothing is copied, the result shares the read’s memory: finish with it, or copy it, before the read ends, as Using the Results explains.
Turn On DirectGPU#
Use DirectGPU when your program works on the results on the GPU every step, as a PyTorch training
loop does. It has no dedicated call yet: create_physx in the
CUDA graph excerpt turns it on. The switch applies to
the whole process and is read when a scene is attached, so turn it on before you attach.
What Changes in DirectGPU Mode#
Step first. Before the first step, reads of bodies and articulations return no data, and writes of values in GPU memory fail.
GPU work needs synchronization. Before PyTorch or your own GPU code uses a read array, or fills a write array, synchronize as CUDA Synchronization shows.
Objects never sleep. DirectGPU turns sleeping off, so resting objects are still simulated.
Some features are missing. Device Residency and Interop lists them.
Replay a Loop With a CUDA Graph#
A training loop repeats the same GPU work at every step: it reads the state, runs the policy network, and writes the actions. Launched one by one, every read, kernel, and write costs CPU time, and the GPU waits between them. A CUDA graph records that GPU work once and replays it with one launch, between physics steps:

You need an NVIDIA driver for CUDA 12.5 or newer and a PhysX object in DirectGPU mode on GPU 0,
so that physics and Warp share one CUDA context. create_physx in this excerpt makes one
through PhysXConfig. Python can warn that the config argument is deprecated. A small Warp
kernel stands in for the policy and holds 1 kg
bodies up:
# An excerpt. run_with_graph takes the PhysX object from create_physx, after a scene
# of 1 kg rigid bodies is attached.
import warp as wp
from ovphysx import PhysX, PhysXConfig
from ovphysx.types import SimObjectType
def create_physx():
"""A PhysX object in DirectGPU mode, on GPU 0 like Warp."""
return PhysX(
config=PhysXConfig(carbonite_overrides={"/physics/suppressReadback": True}),
active_cuda_gpus="0",
)
@wp.kernel
def hover(velocity: wp.array2d(dtype=float), force: wp.array2d(dtype=float)):
"""Stands in for a policy: holds 1 kg bodies up and damps their motion."""
body = wp.tid()
for axis in range(3):
force[body, axis] = -2.0 * velocity[body, axis]
force[body, 2] += 9.81
def run_with_graph(physx, device, steps):
"""Records one read, the kernel, and one write, then replays them."""
stream = wp.get_stream(device)
handle = int(stream.cuda_stream) # the stream as ovphysx takes it
physx.step_sync(1.0 / 60.0) # DirectGPU data exists after the first step
bodies = physx.query(SimObjectType.RIGID_BODY).wait() # kept until the end
# Read once and close the read. The captured read reuses its GPU memory.
first = physx.read(bodies, ["linearVelocity"], cuda_stream=handle).wait()
first.fetch_next().wait() # one scene, so one group
first.close().wait()
session = physx.write(bodies, ["force"]).wait() # stays open until the end
force = session.fetch_next().wait()
wp.load_module(device=device) # load the kernel before the capture
physx.wait_all() # leave no ovphysx work to run inside the capture
with wp.ScopedCapture(device=device, stream=stream) as capture:
read = physx.read(bodies, ["linearVelocity"], cuda_stream=handle).wait()
velocity = read.fetch_next().wait()
wp.launch(hover, dim=velocity.prim_count, device=device, stream=stream,
inputs=[velocity.tensors[0], force.tensors[0]])
session.commit(force, cuda_stream=handle).wait()
for _ in range(steps):
wp.capture_launch(capture.graph, stream=stream)
wp.synchronize_stream(stream) # the replay ends before the step
physx.step(1.0 / 60.0)
physx.wait_all() # the step ends before the next replay
session.close().wait() # release everything outside the capture
read.close().wait()
bodies.close().wait()
These rules matter most:
What you can capture: reads and writes of values in GPU memory, and your own GPU work. Values in CPU memory, contact data, and physics steps stay outside the graph.
Before the capture: keep a query, read once with the same attributes and close that read, open the write and fetch its group, load your kernels, and call
wait_all.While you replay: keep the query, the captured read, and the write open. Each replay must end before the next step, and each step before the next replay.
Capture again after objects are added or removed, a rigid body is enabled or disabled, or the whole scene is reset or replaced. An old graph can write to the wrong bodies without any error.
Articulations: if the graph reads center-of-mass values, call
update_articulations_kinematicon the stream you replay on after you write joint positions or root poses outside the graph. An episode reset in a training loop is a typical case.
Capturing a Read in a CUDA Graph and Capturing a Write Step list every rule.
More Detail#
Device Residency and Interop lists where every value lives and covers DLPack ownership, CUDA synchronization, and CUDA graphs.
Step-First Precondition explains reads and writes before the first step.
Fast Simulations has more ways to save time.
Next: Changing the Scene adds, removes, and changes objects while the simulation runs.