benchmark_r_slope_aspect_memory.py 2.1 KB

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