Debian Codegraph @

raster/r.slope.aspect/benchmark/benchmark_r_slope_aspect_memory.py

raw
1 """Benchmarking of r.slope aspect
2
3 @author Aaron Saw Min Sern
4 @author Anna Petrasova
5 """
6
7 from grass.exceptions import CalledModuleError
8 from grass.pygrass.modules import Module
9 from subprocess import DEVNULL
10
11 import grass.benchmark as bm
12
13
14 def main():
15 results = []
16
17 reference = "r_slope_aspect_reference_map"
18 generate_map(rows=10000, cols=10000, fname=reference)
19 # Users can add more or modify existing reference maps
20 benchmark(0, "r.slope.aspect_0MB", results, reference)
21 benchmark(5, "r.slope.aspect_5MB", results, reference)
22 benchmark(10, "r.slope.aspect_10MB", results, reference)
23 benchmark(100, "r.slope.aspect_100MB", results, reference)
24 benchmark(300, "r.slope.aspect_300MB", results, reference)
25
26 Module("g.remove", quiet=True, flags="f", type="raster", name=reference)
27 bm.nprocs_plot(results, filename="r_slope_aspect_benchmark_memory.svg")
28
29
30 def benchmark(memory, label, results, reference):
31 slope = "benchmark_slope"
32 aspect = "benchmark_aspect"
33 pcurv = "benchmark_pcurv"
34 tcurv = "benchmark_tcurv"
35
36 module = Module(
37 "r.slope.aspect",
38 elevation=reference,
39 slope=slope,
40 aspect=aspect,
41 pcurvature=pcurv,
42 tcurvature=tcurv,
43 run_=False,
44 stdout_=DEVNULL,
45 overwrite=True,
46 )
47 results.append(bm.benchmark_nprocs(module, label=label, max_nprocs=20, repeat=10))
48
49 Module(
50 "g.remove",
51 quiet=True,
52 flags="f",
53 type="raster",
54 name=(slope, aspect, pcurv, tcurv),
55 )
56
57
58 def generate_map(rows, cols, fname):
59 Module("g.region", flags="p", s=0, n=rows, w=0, e=cols, res=1)
60 # Generate using r.random.surface if r.surf.fractal fails
61 try:
62 print("Generating reference map using r.surf.fractal...")
63 Module("r.surf.fractal", output=fname)
64 except CalledModuleError:
65 print("r.surf.fractal fails, using r.random.surface instead...")
66 Module("r.random.surface", output=fname)
67
68
69 if __name__ == "__main__":
70 main()