Skip to content

Commit

Permalink
Make examples portable
Browse files Browse the repository at this point in the history
  • Loading branch information
nvlukasz authored and c0d1f1ed committed Mar 22, 2024
1 parent c2663ce commit 98fef05
Show file tree
Hide file tree
Showing 37 changed files with 106 additions and 178 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ print(lengths)

## Running Examples

The `examples` directory contains a number of scripts that show how to implement different simulation methods using the Warp API. Most examples will generate USD files containing time-sampled animations (stored in the current working directory). Before running examples, users should ensure that the ``usd-core`` and ``matplotlib`` packages are installed using:
The `examples` directory contains a number of scripts that show how to implement different simulation methods using the Warp API. Most examples will generate USD files containing time-sampled animations (stored in the current working directory). Before running examples, users should ensure that the ``usd-core``, ``matplotlib``, and ``pyglet`` packages are installed using:

pip install usd-core matplotlib
pip install usd-core matplotlib pyglet

Examples can be run from the command-line as follows:

Expand Down
4 changes: 2 additions & 2 deletions warp/examples/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ def get_source_directory():
return os.path.realpath(os.path.dirname(__file__))


def get_output_directory():
return os.getcwd()
def get_asset_directory():
return os.path.join(get_source_directory(), "assets")
20 changes: 9 additions & 11 deletions warp/examples/benchmarks/benchmark.bat
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
del outputs\benchmark.csv
del benchmark.csv

python benchmark_cloth.py warp_cpu
python benchmark_cloth.py warp_gpu
python benchmark_cloth.py taichi_cpu
python benchmark_cloth.py taichi_gpu
@REM python benchmark_cloth.py taichi_cpu
@REM python benchmark_cloth.py taichi_gpu
python benchmark_cloth.py numpy
python benchmark_cloth.py cupy
python benchmark_cloth.py torch_cpu
python benchmark_cloth.py torch_gpu
python benchmark_cloth.py numba
python benchmark_cloth.py jax_cpu
python benchmark_cloth.py jax_gpu


@REM python benchmark_cloth.py cupy
@REM python benchmark_cloth.py torch_cpu
@REM python benchmark_cloth.py torch_gpu
@REM python benchmark_cloth.py numba
@REM python benchmark_cloth.py jax_cpu
@REM python benchmark_cloth.py jax_gpu
4 changes: 2 additions & 2 deletions warp/examples/benchmarks/benchmark.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
rm outputs/benchmark.csv
rm benchmark.csv

python3 benchmark_cloth.py warp_cpu
python3 benchmark_cloth.py warp_gpu
# python3 benchmark_cloth.py taichi_cpu
# python3 benchmark_cloth.py taichi_gpu
# python3 benchmark_cloth.py numpy
python3 benchmark_cloth.py numpy
# python3 benchmark_cloth.py cupy
# python3 benchmark_cloth.py torch_cpu
# python3 benchmark_cloth.py torch_gpu
Expand Down
52 changes: 25 additions & 27 deletions warp/examples/benchmarks/benchmark_cloth.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
import os
import sys, getopt
import numpy as np
import math
import ctypes

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".")))

from pxr import Usd, UsdGeom, Gf, Sdf

Expand Down Expand Up @@ -149,7 +147,7 @@ def run_benchmark(mode, dim, timers, render=False):

if render:
# set up grid for visualization
stage = Usd.Stage.CreateNew(os.path.join(os.path.dirname(__file__), "outputs/benchmark.usd"))
stage = Usd.Stage.CreateNew("benchmark.usd")
stage.SetStartTimeCode(0.0)
stage.SetEndTimeCode(sim_duration * sim_fps)
stage.SetTimeCodesPerSecond(sim_fps)
Expand All @@ -161,63 +159,63 @@ def run_benchmark(mode, dim, timers, render=False):

with wp.ScopedTimer("Initialization", dict=timers):
if mode == "warp_cpu":
import examples.benchmark_cloth_warp
import benchmark_cloth_warp

integrator = examples.benchmark_cloth_warp.WpIntegrator(cloth, "cpu")
integrator = benchmark_cloth_warp.WpIntegrator(cloth, "cpu")

elif mode == "warp_gpu":
import examples.benchmark_cloth_warp
import benchmark_cloth_warp

integrator = examples.benchmark_cloth_warp.WpIntegrator(cloth, "cuda")
integrator = benchmark_cloth_warp.WpIntegrator(cloth, "cuda")

elif mode == "taichi_cpu":
import examples.benchmark_cloth_taichi
import benchmark_cloth_taichi

integrator = examples.benchmark_cloth_taichi.TiIntegrator(cloth, "cpu")
integrator = benchmark_cloth_taichi.TiIntegrator(cloth, "cpu")

elif mode == "taichi_gpu":
import examples.benchmark_cloth_taichi
import benchmark_cloth_taichi

integrator = examples.benchmark_cloth_taichi.TiIntegrator(cloth, "cuda")
integrator = benchmark_cloth_taichi.TiIntegrator(cloth, "cuda")

elif mode == "numpy":
import examples.benchmark_cloth_numpy
import benchmark_cloth_numpy

integrator = examples.benchmark_cloth_numpy.NpIntegrator(cloth)
integrator = benchmark_cloth_numpy.NpIntegrator(cloth)

elif mode == "cupy":
import examples.benchmark_cloth_cupy
import benchmark_cloth_cupy

integrator = examples.benchmark_cloth_cupy.CpIntegrator(cloth)
integrator = benchmark_cloth_cupy.CpIntegrator(cloth)

elif mode == "numba":
import examples.benchmark_cloth_numba
import benchmark_cloth_numba

integrator = examples.benchmark_cloth_numba.NbIntegrator(cloth)
integrator = benchmark_cloth_numba.NbIntegrator(cloth)

elif mode == "torch_cpu":
import examples.benchmark_cloth_pytorch
import benchmark_cloth_pytorch

integrator = examples.benchmark_cloth_pytorch.TrIntegrator(cloth, "cpu")
integrator = benchmark_cloth_pytorch.TrIntegrator(cloth, "cpu")

elif mode == "torch_gpu":
import examples.benchmark_cloth_pytorch
import benchmark_cloth_pytorch

integrator = examples.benchmark_cloth_pytorch.TrIntegrator(cloth, "cuda")
integrator = benchmark_cloth_pytorch.TrIntegrator(cloth, "cuda")

elif mode == "jax_cpu":
os.environ["JAX_PLATFORM_NAME"] = "cpu"

import examples.benchmark_cloth_jax
import benchmark_cloth_jax

integrator = examples.benchmark_cloth_jax.JxIntegrator(cloth)
integrator = benchmark_cloth_jax.JxIntegrator(cloth)

elif mode == "jax_gpu":
os.environ["JAX_PLATFORM_NAME"] = "gpu"

import examples.benchmark_cloth_jax
import benchmark_cloth_jax

integrator = examples.benchmark_cloth_jax.JxIntegrator(cloth)
integrator = benchmark_cloth_jax.JxIntegrator(cloth)

else:
raise RuntimeError("Unknown simulation backend")
Expand Down Expand Up @@ -260,7 +258,7 @@ def run_benchmark(mode, dim, timers, render=False):
for k, v in timers.items():
print("{:16} min: {:8.2f} max: {:8.2f} avg: {:8.2f}".format(k, np.min(v), np.max(v), np.mean(v)))

report = open(os.path.join(os.path.dirname(__file__), "outputs/benchmark.csv"), "a")
report = open(os.path.join("benchmark.csv"), "a")
writer = csv.writer(report, delimiter=",")

if report.tell() == 0:
Expand Down
2 changes: 1 addition & 1 deletion warp/examples/browse.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def open_file(filename):
if __name__ == "__main__":
import warp.examples

source_dir = wp.examples.get_source_directory()
source_dir = warp.examples.get_source_directory()
print(f"Example source directory: {source_dir}")

try:
Expand Down
6 changes: 1 addition & 5 deletions warp/examples/core/example_dem.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
#
###########################################################################

import os

import numpy as np

import warp as wp
Expand Down Expand Up @@ -209,9 +207,7 @@ def particle_grid(self, dim_x, dim_y, dim_z, lower, radius, jitter):


if __name__ == "__main__":
import warp.examples

stage_path = os.path.join(wp.examples.get_output_directory(), "example_dem.usd")
stage_path = "example_dem.usd"

example = Example(stage_path)

Expand Down
6 changes: 1 addition & 5 deletions warp/examples/core/example_marching_cubes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
###########################################################################


import os
from typing import Optional

import warp as wp
Expand Down Expand Up @@ -163,10 +162,7 @@ def render(self):


if __name__ == "__main__":
import warp.examples

this_file = os.path.basename(__file__).split(".")[0]
stage_path = os.path.join(wp.examples.get_output_directory(), f"{this_file}.usd")
stage_path = "example_marching_cubes.usd"

example = Example(stage_path)
for _ in range(240):
Expand Down
7 changes: 3 additions & 4 deletions warp/examples/core/example_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from pxr import Usd, UsdGeom

import warp as wp
import warp.examples
import warp.render

wp.init()
Expand Down Expand Up @@ -93,7 +94,7 @@ def __init__(self, stage):

self.sim_margin = 0.1

usd_stage = Usd.Stage.Open(os.path.join(os.path.dirname(__file__), "../assets/bunny.usd"))
usd_stage = Usd.Stage.Open(os.path.join(warp.examples.get_asset_directory(), "bunny.usd"))
usd_geom = UsdGeom.Mesh(usd_stage.GetPrimAtPath("/bunny/bunny"))
usd_scale = 10.0

Expand Down Expand Up @@ -142,9 +143,7 @@ def render(self):


if __name__ == "__main__":
import warp.examples

stage_path = os.path.join(wp.examples.get_output_directory(), "example_mesh.usd")
stage_path = "example_mesh.usd"

example = Example(stage_path)

Expand Down
16 changes: 7 additions & 9 deletions warp/examples/core/example_mesh_intersect.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from pxr import Usd, UsdGeom

import warp as wp
import warp.examples
import warp.render


Expand Down Expand Up @@ -86,8 +87,8 @@ def __init__(self, stage):
self.query_count = 1024
self.has_queried = False

self.path_0 = "../assets/cube.usda"
self.path_1 = "../assets/sphere.usda"
self.path_0 = os.path.join(warp.examples.get_asset_directory(), "cube.usda")
self.path_1 = os.path.join(warp.examples.get_asset_directory(), "sphere.usda")

self.mesh_0 = self.load_mesh(self.path_0, "/Cube/Cube_001")
self.mesh_1 = self.load_mesh(self.path_1, "/Sphere/Sphere")
Expand Down Expand Up @@ -143,14 +144,14 @@ def render(self):
xform = self.xforms[i]
self.renderer.render_ref(
f"mesh_{i}_0",
os.path.join(os.path.dirname(__file__), self.path_0),
self.path_0,
pos=wp.vec3(xform.p[0] + offset, xform.p[1], xform.p[2]),
rot=xform.q,
scale=wp.vec3(1.0, 1.0, 1.0),
)
self.renderer.render_ref(
f"mesh_{i}_1",
os.path.join(os.path.dirname(__file__), self.path_1),
self.path_1,
pos=wp.vec3(offset, 0.0, 0.0),
rot=wp.quat_identity(),
scale=wp.vec3(1.0, 1.0, 1.0),
Expand All @@ -169,8 +170,7 @@ def render(self):

# create collision meshes
def load_mesh(self, path, prim):
usd_path = os.path.join(os.path.dirname(__file__), path)
usd_stage = Usd.Stage.Open(usd_path)
usd_stage = Usd.Stage.Open(path)
usd_geom = UsdGeom.Mesh(usd_stage.GetPrimAtPath(prim))

mesh = wp.Mesh(
Expand All @@ -182,9 +182,7 @@ def load_mesh(self, path, prim):


if __name__ == "__main__":
import warp.examples

stage_path = os.path.join(wp.examples.get_output_directory(), "example_mesh_intersect.usd")
stage_path = "example_mesh_intersect.usd"

example = Example(stage_path)

Expand Down
9 changes: 4 additions & 5 deletions warp/examples/core/example_nvdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import numpy as np

import warp as wp
import warp.examples
import warp.render

wp.init()
Expand Down Expand Up @@ -108,7 +109,7 @@ def __init__(self, stage):
self.velocities = wp.from_numpy(init_vel.astype(np.float32), dtype=wp.vec3)

# load collision volume
file = open(os.path.join(os.path.dirname(__file__), "../assets/rocks.nvdb"), "rb")
file = open(os.path.join(warp.examples.get_asset_directory(), "rocks.nvdb"), "rb")

# create Volume object
self.volume = wp.Volume.load_from_nvdb(file)
Expand Down Expand Up @@ -146,7 +147,7 @@ def render(self):

self.renderer.render_ref(
name="collision",
path=os.path.join(os.path.dirname(__file__), "../assets/rocks.usd"),
path=os.path.join(warp.examples.get_asset_directory(), "rocks.usd"),
pos=wp.vec3(0.0, 0.0, 0.0),
rot=wp.quat_from_axis_angle(wp.vec3(1.0, 0.0, 0.0), math.pi),
scale=wp.vec3(1.0, 1.0, 1.0),
Expand All @@ -157,9 +158,7 @@ def render(self):


if __name__ == "__main__":
import warp.examples

stage_path = os.path.join(wp.examples.get_output_directory(), "example_nvdb.usd")
stage_path = "example_nvdb.usd"

example = Example(stage_path)

Expand Down
3 changes: 2 additions & 1 deletion warp/examples/core/example_raycast.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from pxr import Usd, UsdGeom

import warp as wp
import warp.examples

wp.init()

Expand Down Expand Up @@ -52,7 +53,7 @@ def __init__(self, **kwargs):
self.height = 1024
self.cam_pos = (0.0, 1.0, 2.0)

asset_stage = Usd.Stage.Open(os.path.join(os.path.dirname(__file__), "../assets/bunny.usd"))
asset_stage = Usd.Stage.Open(os.path.join(warp.examples.get_asset_directory(), "bunny.usd"))
mesh_geom = UsdGeom.Mesh(asset_stage.GetPrimAtPath("/bunny/bunny"))

points = np.array(mesh_geom.GetPointsAttr().Get())
Expand Down
6 changes: 1 addition & 5 deletions warp/examples/core/example_sph.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
#
###########################################################################

import os

import numpy as np

import warp as wp
Expand Down Expand Up @@ -377,9 +375,7 @@ def render(self):


if __name__ == "__main__":
import warp.examples

stage_path = os.path.join(wp.examples.get_output_directory(), "example_sph.usd")
stage_path = "example_sph.usd"

example = Example(stage_path)

Expand Down
Loading

0 comments on commit 98fef05

Please sign in to comment.