Debian Codegraph @

raster/r.horizon/benchmark/benchmark_rhorizon.py

raw
1 """Benchmarking of r.horizon
2 point mode, one direction
3
4 @author Chung-Yuan Liang, 2024
5 """
6
7 from grass.exceptions import CalledModuleError
8 from grass.pygrass.modules import Module
9
10 import grass.benchmark as bm
11
12
13 def main():
14 results = []
15 metrics = ["time", "speedup", "efficiency"]
16 mapsizes = [1e6, 2e6, 4e6, 8e6]
17
18 # run benchmarks
19 for mapsize in mapsizes:
20 benchmark(
21 size=int(mapsize**0.5),
22 step=0,
23 label=f"r.horizon_{int(mapsize / 1e6)}M",
24 results=results,
25 )
26
27 # plot results
28 for metric in metrics:
29 bm.nprocs_plot(
30 results,
31 title=f"r.horizon raster mode {metric}",
32 metric=metric,
33 )
34
35
36 def benchmark(size, step, label, results):
37 reference = "benchmark_r_horizon_reference_map"
38 output = "benchmark_r_horizon"
39
40 generate_map(rows=size, cols=size, fname=reference)
41 module = Module(
42 "r.horizon",
43 elevation=reference,
44 output=output,
45 direction=0,
46 step=step,
47 )
48
49 results.append(bm.benchmark_nprocs(module, label=label, max_nprocs=8, repeat=3))
50 Module(
51 "g.remove", quiet=True, flags="f", type="raster", pattern="benchmark_r_horizon*"
52 )
53
54
55 def generate_map(rows, cols, fname):
56 Module("g.region", flags="p", rows=rows, cols=cols, res=1)
57 # Generate using r.random.surface if r.surf.fractal fails
58 try:
59 print("Generating reference map using r.surf.fractal...")
60 Module("r.surf.fractal", output=fname, overwrite=True)
61 except CalledModuleError:
62 print("r.surf.fractal fails, using r.random.surface instead...")
63 Module("r.random.surface", output=fname, overwrite=True)
64
65
66 if __name__ == "__main__":
67 main()