forked from EC527JilinZhengVarshaSingh/super-raytrace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraytracing_automation.py
More file actions
339 lines (288 loc) · 10.7 KB
/
Copy pathraytracing_automation.py
File metadata and controls
339 lines (288 loc) · 10.7 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env python3
import os
import sys
import subprocess
import argparse
import csv
import itertools
from pathlib import Path
from datetime import datetime
# --- Configuration Constants ---
PROJECT_ROOT = Path(__file__).parent.resolve()
SRC_DIR = PROJECT_ROOT / "src"
BUILD_DIR = PROJECT_ROOT / "build"
BENCHMARK_DIR = PROJECT_ROOT / "benchmarks"
SCALED_PPM_DIFF_EXEC = SRC_DIR / "ppm_diff" / "scaled_ppm_diff"
# Compiler Flags (derived from your rebuild_*.sh files)
NVCC_FLAGS = [
"-O3",
"-gencode",
"arch=compute_86,code=sm_86",
# "-gencode", "arch=compute_70,code=sm_70",
"-rdc=true",
"--fmad=false", # <--- ADD THIS to disable Fused Multiply-Add
]
# Target Definitions
# Maps a friendly name to specific source files and output paths
TARGETS = {
"cpu": {
"type": "cmake",
"dir": SRC_DIR / "InOneWeekend",
"exec_name": "inOneWeekend",
},
"gpu_global_float": {
"type": "nvcc",
"src": SRC_DIR / "GlobalFloatCUDAInOneWeekend" / "main.cu",
"out_dir": SRC_DIR / "GlobalFloatCUDAInOneWeekend",
"exec_name": "global-float-cuda-raytrace",
},
"gpu_global_double": {
"type": "nvcc",
"src": SRC_DIR / "GlobalDoubleCUDAInOneWeekend" / "main.cu",
"out_dir": SRC_DIR / "GlobalDoubleCUDAInOneWeekend",
"exec_name": "global-double-cuda-raytrace",
},
"gpu_const_float": {
"type": "nvcc",
"src": SRC_DIR / "ConstFloatCUDAInOneWeekend" / "main.cu",
"out_dir": SRC_DIR / "ConstFloatCUDAInOneWeekend",
"exec_name": "const-float-cuda-raytrace",
},
"gpu_const_double": {
"type": "nvcc",
"src": SRC_DIR / "ConstDoubleCUDAInOneWeekend" / "main.cu",
"out_dir": SRC_DIR / "ConstDoubleCUDAInOneWeekend",
"exec_name": "const-double-cuda-raytrace",
},
"gpu_tex_float": {
"type": "nvcc",
"src": SRC_DIR / "TexFloatCUDAInOneWeekend" / "main.cu",
"out_dir": SRC_DIR / "TexFloatCUDAInOneWeekend",
"exec_name": "tex-float-cuda-raytrace",
},
}
def run_command(cmd, cwd=None):
"""Helper to run shell commands and handle errors."""
try:
print(f"Running: {' '.join(str(c) for c in cmd)}")
subprocess.run(cmd, cwd=cwd, check=True)
except subprocess.CalledProcessError as e:
print(f"Error executing command: {e}")
sys.exit(1)
# --- Subcommand: Build ---
def build_target(args):
target_keys = TARGETS.keys() if "all" in args.targets else args.targets
for key in target_keys:
config = TARGETS.get(key)
if not config:
print(f"Unknown target: {key}")
continue
print(f"\n--- Building {key} ---")
if config["type"] == "cmake":
# Replicates rebuild_base.sh
build_type = "Debug" if args.debug else "Release"
cmake_build_path = BUILD_DIR / build_type
run_command(
[
"cmake",
"-B",
str(cmake_build_path),
f"-DCMAKE_BUILD_TYPE={build_type}",
],
cwd=PROJECT_ROOT,
)
run_command(["cmake", "--build", str(cmake_build_path)], cwd=PROJECT_ROOT)
elif config["type"] == "nvcc":
# Replicates rebuild_*_cuda.sh
output_exec = config["out_dir"] / config["exec_name"]
cmd = ["nvcc", str(config["src"]), "-o", str(output_exec)] + NVCC_FLAGS
# if key == "gpu_tex_float":
# cmd.append("--ptxas-options=-v")
run_command(cmd)
print(f"Successfully built: {output_exec}")
# --- Subcommand: Benchmark ---
def run_benchmark(args):
config = TARGETS.get(args.target)
if not config:
print(f"Error: Unknown target '{args.target}'")
return
# Determine executable path
if config["type"] == "cmake":
# CPU build location differs based on CMake setup
exec_path = BUILD_DIR / "Release" / config["exec_name"]
else:
exec_path = config["out_dir"] / config["exec_name"]
if not exec_path.exists():
print(f"Error: Executable not found at {exec_path}. Please build first.")
return
BENCHMARK_DIR.mkdir(exist_ok=True)
timestamp = datetime.now().strftime("%y%m%d%H%M%S")
csv_filename = BENCHMARK_DIR / f"{timestamp}_{args.target}_timing.csv"
# Configuration Space
# You can modify these defaults or pass them via CLI if expanded
scenes = [1]
# resolutions = [(320, 192), (480, 288), (640, 384), (960, 576), (1280, 768)]
resolutions = [(1280, 768)]
# sample_counts = [10, 100]
sample_counts = [100]
bounces = [25] # Fixed at 25 per your script
# threads = [4, 8, 16]
threads = [8]
runs_per_config = 1
print(f"Starting benchmark for {args.target}...")
print(f"Output will be saved to {csv_filename}")
with open(csv_filename, mode="w", newline="") as f:
writer = csv.writer(f)
writer.writerow(
[
"scene_id",
"width",
"height",
"samples",
"bounces",
"threads",
"run",
"render_only_time_ms",
"end_to_end_time_ms",
]
)
# Itertools product avoids deep nesting
combinations = itertools.product(
scenes, resolutions, sample_counts, bounces, threads
)
for scene, (w, h), samp, bounce, th in combinations:
print(
f"Config: Scene={scene}, Res={w}x{h}, Samp={samp}, Bounce={bounce}, Threads={th}"
)
for run in range(1, runs_per_config + 1):
cmd = [
str(exec_path),
"--scene_id",
str(scene),
"--width",
str(w),
"--height",
str(h),
"--samples",
str(samp),
"--bounces",
str(bounce),
"--threads",
str(th),
]
try:
# Capture stdout to get timing data
result = subprocess.run(
cmd, capture_output=True, text=True, check=True
)
# Assuming output is space separated: "render_ms, total_ms"
timing_output = result.stdout.strip().split(", ")
if len(timing_output) >= 2:
row = [
scene,
w,
h,
samp,
bounce,
th,
run,
timing_output[0],
timing_output[1],
]
writer.writerow(row)
print(
f" Run {run}: {timing_output[0]}ms / {timing_output[1]}ms"
)
else:
print(f" Run {run}: Failed to parse output: {result.stdout}")
except subprocess.CalledProcessError as e:
print(f" Run {run}: Crash/Error. {e.stderr}")
# --- Subcommand: Verify (Diff) ---
def run_verify(args):
# Replaces ppm_diff.sh
if not SCALED_PPM_DIFF_EXEC.exists():
print("Error: scaled_ppm_diff executable not found. Compile utility first.")
return
dir1 = Path(args.dir1)
dir2 = Path(args.dir2)
out_dir = Path(args.out_dir)
out_dir.mkdir(exist_ok=True)
files1 = sorted(list(dir1.glob("*.ppm")))
files2 = sorted(list(dir2.glob("*.ppm")))
if len(files1) != len(files2):
print(
f"Warning: File counts mismatch! {dir1}: {len(files1)}, {dir2}: {len(files2)}"
)
# Iterate through min length to prevent crash
limit = min(len(files1), len(files2))
for i in range(limit):
f1 = files1[i]
f2 = files2[i]
# Basic name check (optional, but good for sanity)
if f1.name != f2.name:
print(f"Warning: Comparing distinct filenames: {f1.name} vs {f2.name}")
diff_name = f"diff_{f1.name}"
out_path = out_dir / diff_name
print(f"Comparing {f1.name}...")
try:
subprocess.run(
[str(SCALED_PPM_DIFF_EXEC), str(f1), str(f2), str(out_path)], check=True
)
except subprocess.CalledProcessError:
print(f"Failed to compare pair {i}")
# --- Subcommand: Profile ---
def run_profile(args):
# Replaces profile.sh
config = TARGETS.get(args.target)
if not config:
print(f"Error: Unknown target '{args.target}'")
return
exec_path = config["out_dir"] / config["exec_name"]
output_report = f"{args.target}-render-profile"
# Construct Nsight Compute command
cmd = [
"ncu",
"--set",
"detailed",
"-k",
"render",
"-o",
output_report,
"--force-overwrite", # Safety for re-runs
str(exec_path),
"--scene_id",
"1", # Defaulting to scene 1 for profiling
]
print(f"Profiling {args.target} with Nsight Compute...")
run_command(cmd)
# --- Main Entry Point ---
def main():
parser = argparse.ArgumentParser(description="Raytracing Automation Tool")
subparsers = parser.add_subparsers(dest="command", required=True)
# Build Parser
p_build = subparsers.add_parser("build", help="Compile targets")
p_build.add_argument(
"targets", nargs="+", help="Target names (e.g., gpu_global_float) or 'all'"
)
p_build.add_argument(
"--debug", action="store_true", help="Build debug version (CMake only)"
)
p_build.set_defaults(func=build_target)
# Benchmark Parser
p_bench = subparsers.add_parser("benchmark", help="Run performance benchmarks")
p_bench.add_argument("target", help="Target to benchmark (e.g., gpu_global_float)")
p_bench.set_defaults(func=run_benchmark)
# Verify Parser
p_verify = subparsers.add_parser("verify", help="Compare PPM outputs")
p_verify.add_argument("dir1", help="First directory of PPMs")
p_verify.add_argument("dir2", help="Second directory of PPMs")
p_verify.add_argument("out_dir", help="Directory to save diff images")
p_verify.set_defaults(func=run_verify)
# Profile Parser
p_profile = subparsers.add_parser("profile", help="Profile with Nsight Compute")
p_profile.add_argument("target", help="Target to profile")
p_profile.set_defaults(func=run_profile)
args = parser.parse_args()
args.func(args)
if __name__ == "__main__":
main()