-
Notifications
You must be signed in to change notification settings - Fork 484
Expand file tree
/
Copy pathexample_graph_capture.py
More file actions
138 lines (106 loc) · 3.92 KB
/
example_graph_capture.py
File metadata and controls
138 lines (106 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# SPDX-FileCopyrightText: Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
###########################################################################
# Example Graph Capture
#
# Shows how to implement CUDA graph capture using wp.ScopedCapture().
#
###########################################################################
import numpy as np
import warp as wp
@wp.kernel
def fbm(
kernel_seed: int,
frequency: float,
amplitude: float,
x: wp.array[float],
y: wp.array[float],
z: wp.array2d[float],
):
i, j = wp.tid()
state = wp.rand_init(kernel_seed)
p = frequency * wp.vec2(x[j], y[i])
n = amplitude * wp.noise(state, p)
z[i, j] += n
@wp.kernel
def slide(x: wp.array[float], shift: float):
tid = wp.tid()
x[tid] += shift
class Example:
def __init__(self):
self.width = 128
self.height = 128
min_x, max_x = 0.0, 2.0
min_y, max_y = 0.0, 2.0
# create a grid of pixels
x = np.linspace(min_x, max_x, self.width)
y = np.linspace(min_y, max_y, self.height)
self.x = wp.array(x, dtype=float)
self.y = wp.array(y, dtype=float)
self.pixel_values = wp.zeros((self.width, self.height), dtype=float)
self.seed = 42
self.shift = 2e-2
self.frequency = 1.0
self.amplitude = 1.0
# use graph capture if launching from a CUDA-capable device
self.use_cuda_graph = wp.get_device().is_cuda
if self.use_cuda_graph:
# record launches
with wp.ScopedCapture() as capture:
self.fbm()
self.graph = capture.graph
def fbm(self):
for _ in range(16):
wp.launch(
kernel=fbm,
dim=(self.height, self.width),
inputs=[self.seed, self.frequency, self.amplitude, self.x, self.y],
outputs=[self.pixel_values],
)
self.frequency *= 2.0
self.amplitude *= 0.5
def step(self):
self.pixel_values.zero_()
self.frequency = 1.0
self.amplitude = 1.0
with wp.ScopedTimer("step", active=True):
wp.launch(kernel=slide, dim=self.width, inputs=[self.x, self.shift])
if self.use_cuda_graph:
wp.capture_launch(self.graph)
else: # cpu path
self.fbm()
def step_and_render(self, frame_num=None, img=None):
self.step()
with wp.ScopedTimer("render"):
if img:
pixels = self.pixel_values.numpy()
pixels = (pixels + 1.0) / 2.0
img.set_array(pixels)
return (img,)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--device", type=str, default=None, help="Override the default Warp device.")
parser.add_argument("--num-frames", type=int, default=1000, help="Total number of frames.")
parser.add_argument(
"--headless",
action="store_true",
help="Run in headless mode, suppressing the opening of any graphical windows.",
)
args = parser.parse_known_args()[0]
with wp.ScopedDevice(args.device):
example = Example()
if not args.headless:
import matplotlib.colors
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# Create the animation
fig = plt.figure()
img = plt.imshow(example.pixel_values.numpy(), "gray", origin="lower", animated=True)
img.set_norm(matplotlib.colors.Normalize(0.0, 1.0))
ani = FuncAnimation(fig, example.step_and_render, fargs=(img,), frames=1000, interval=30)
# Display the animation
plt.show()
else:
for _ in range(args.num_frames):
example.step()