Warp

1.12.1 · active · verified Tue Apr 14

Warp is a Python framework by NVIDIA for writing high-performance simulation and graphics code. It JIT compiles Python functions into efficient CPU or GPU kernels, enabling GPU-accelerated workloads for physics simulation, robotics, and machine learning. Currently at version 1.12.1, it receives frequent updates including both bug fixes and new features.

Warnings

Install

Imports

Quickstart

This example simulates one million particles under gravitational attraction using a Warp kernel. It demonstrates kernel definition, array allocation on a specific device, and launching a kernel. Note that for GPU execution, a compatible NVIDIA GPU and driver are required, and arrays must be explicitly placed on a 'cuda' device.

import warp as wp
import numpy as np

# Initialize Warp (optional, but good practice)
wp.init()

num_particles = 1_000_000
dt = 0.01

@wp.kernel
def gravity_step(pos: wp.array[wp.vec3], vel: wp.array[wp.vec3]):
    i = wp.tid()

    position = pos[i]
    dist_sq = wp.length_sq(position) + 0.01 # softened distance
    acc = -1000.0 / dist_sq * wp.normalize(position) # gravitational pull toward origin

    vel[i] = vel[i] + acc * dt
    pos[i] = pos[i] + vel[i] * dt

rng = np.random.default_rng(42)
positions = wp.array(rng.normal(size=(num_particles, 3)), dtype=wp.vec3, device='cuda')
velocities = wp.array(rng.normal(size=(num_particles, 3)), dtype=wp.vec3, device='cuda')

for _ in range(100):
    wp.launch(kernel=gravity_step, dim=num_particles, inputs=[positions, velocities], device='cuda')

print(f"Final position of first particle: {positions.numpy()[0]}")

view raw JSON →