Skip to content

r.mapcalc: Support parallel computing by OpenMP #5742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions raster/r.mapcalc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ r3_mapcalc_OBJS := $(filter-out map.o xcoor.o xres.o, $(AUTO_OBJS))
include $(MODULE_TOPDIR)/include/Make/Multi.make

EXTRA_CFLAGS = $(READLINEINCPATH) $(PTHREADINCPATH)
LIBES2 = $(CALCLIB) $(GISLIB) $(RASTERLIB) $(BTREELIB) $(READLINELIBPATH) $(READLINELIB) $(HISTORYLIB) $(PTHREADLIBPATH) $(PTHREADLIB)
LIBES3 = $(CALCLIB) $(RASTER3DLIB) $(GISLIB) $(RASTERLIB) $(BTREELIB) $(READLINELIBPATH) $(READLINELIB) $(HISTORYLIB) $(PTHREADLIBPATH) $(PTHREADLIB)
LIBES2 = $(CALCLIB) $(GISLIB) $(RASTERLIB) $(BTREELIB) $(READLINELIBPATH) $(READLINELIB) $(HISTORYLIB) $(PTHREADLIBPATH) $(PTHREADLIB) $(OPENMP_LIBPATH) $(OPENMP_LIB)
LIBES3 = $(CALCLIB) $(RASTER3DLIB) $(GISLIB) $(RASTERLIB) $(BTREELIB) $(READLINELIBPATH) $(READLINELIB) $(HISTORYLIB) $(PTHREADLIBPATH) $(PTHREADLIB) $(OPENMP_LIBPATH) $(OPENMP_LIB)
EXTRA_CFLAGS = $(OPENMP_CFLAGS)
EXTRA_INC = $(OPENMP_INCPATH)

default: multi

Expand Down
69 changes: 69 additions & 0 deletions raster/r.mapcalc/benchmark/benchmark_rmapcalc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""Benchmarking of r.mapcalc
raster (2D)

@author Chung-Yuan Liang, 2025
"""

from grass.exceptions import CalledModuleError
from grass.pygrass.modules import Module

import grass.benchmark as bm


def main():
results = []
metrics = ["time", "speedup", "efficiency"]
mapsizes = [10e6, 20e6]

# run benchmarks

for mapsize in mapsizes:
benchmark(
size=int(mapsize**0.5),
step=0,
label=f"r.mapcalc{int(mapsize / 1e6)}M",
results=results,
)

# plot results

for metric in metrics:
bm.nprocs_plot(
results,
title=f"r.mapcalc {metric}",
metric=metric,
)


def benchmark(size, step, label, results):
map1 = "benchmark_r_mapcalc_map1"
map2 = "benchmark_r_mapcalc_map2"
output = "benchmark_r_mapcalc"

generate_map(rows=size, cols=size, fname=map1)
generate_map(rows=size, cols=size, fname=map2)
module = Module(
"r.mapcalc",
expression=f"{output}=({map1} + {map2} - 2*{map2} + {map1}*{map2} - {map1}/{map2})/2",
overwrite=True,
)

results.append(bm.benchmark_nprocs(module, label=label, max_nprocs=8, repeat=3))
Module(
"g.remove", quiet=True, flags="f", type="raster", pattern="benchmark_r_mapcalc*"
)


def generate_map(rows, cols, fname):
Module("g.region", flags="p", rows=rows, cols=cols, res=1)
# Generate using r.random.surface if r.surf.fractal fails
try:
print("Generating reference map using r.surf.fractal...")
Module("r.surf.fractal", output=fname, overwrite=True)
except CalledModuleError:
print("r.surf.fractal fails, using r.random.surface instead...")
Module("r.random.surface", output=fname, overwrite=True)


if __name__ == "__main__":
main()
Loading
Loading