Skip to content

grass.temporal: Start r.mapcalc, r3.mapcalc and v.extract directly in parallel - #8288

Merged
echoix merged 3 commits into
OSGeo:mainfrom
echoix:claude/elegant-cori-ybtekr
Dec 1, 2026
Merged

echoix merged 3 commits into
OSGeo:mainfrom
echoix:claude/elegant-cori-ybtekr

Conversation

@echoix

@echoix echoix commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

This is kinda similar to #8286. Here too there was some parallel work to touch up so it works correctly, but it ends up that the multiprocessing context used here is only to do a single call. By not spawning a new interpreter (or using forkserver) and using gs.start_command() and waiting for the calls in parallel, it ends up being faster (on Linux, to be validated on macOS and Windows just now),

I’ll revert the last commit when the benchmark numbers are in for Windows and macOS. Done

Description

dataset_mapcalculator() (t.rast.mapcalc, t.rast3d.mapcalc) and extract_dataset() (t.rast.extract, t.rast3d.extract, t.vect.extract) started one Python process per output map only to call gs.run_command() in it. They now start r.mapcalc, r3.mapcalc and v.extract with gs.start_command() and wait for them. The number of tools running at once (nprocs) and the exit code handling are unchanged.

Two behaviours are made explicit while doing this:

  • The last batch of tools was waited for only if the last sample or row started a tool. If the last one was skipped (output map already in the temporal database without overwrite, or a missing map in a mapcalc sample), the last batch was never waited for, so its maps were not registered and its exit codes were not checked. The remaining tools are now always waited for.
  • The multiprocessing workers were joined at interpreter exit. The started tools are now also waited for if an exception is raised while a batch is running.

run_mapcalc2d(), run_mapcalc3d() and run_vector_extraction() are exported by grass.temporal. They are no longer used by extract_dataset() but are kept, with their docstrings updated.

The last two commits add a temporary benchmark step to the macOS and OSGeo4W workflows and revert it. They were only used to get the numbers below on the CI runners and are to be dropped when merging.

Motivation and context

With the spawn start method (macOS, Windows), each worker imports grass.temporal again, which takes about 170 ms. This also removes the pickling and start method concerns for these workers.

Benchmarks

Each setting was timed on 300 maps (Linux) or 60 maps (macOS and Windows) of 50x50 cells in a space-time raster dataset. Times are the wall time of the whole command, in seconds (lower is better). All runs exited with status 0 and registered all the maps.

_get_multiprocessing_context() forces fork on Linux, so to measure the three start methods with the code before this change, its call in mapcalc.py and extract.py was replaced by multiprocessing.get_context(<method>) for that measurement only. The code after this change does not use multiprocessing for these tools, so it has a single result per nprocs. "Before, default" is the code as it was, which used the default start method on macOS and Windows (spawn).

Linux

4 CPUs, Python 3.15.2, whose default start method on Linux is forkserver. Measured with hyperfine 1.18.0, 1 warmup run and 5 timed runs, mean ± standard deviation.

t.rast.mapcalc

nprocs Before, fork Before, forkserver Before, spawn After After vs fork After vs forkserver After vs spawn
1 14.14 ± 0.69 70.65 ± 1.35 97.36 ± 0.51 13.39 ± 0.62 5% faster 5.3x faster 7.3x faster
4 7.78 ± 0.11 31.74 ± 0.83 40.24 ± 0.78 6.35 ± 0.19 18% faster 5.0x faster 6.3x faster
16 6.07 ± 0.18 26.27 ± 0.83 32.83 ± 0.83 4.78 ± 0.10 21% faster 5.5x faster 6.9x faster

t.rast.extract

nprocs Before, fork Before, forkserver Before, spawn After After vs fork After vs forkserver After vs spawn
1 16.00 ± 0.27 71.49 ± 0.73 102.00 ± 0.68 13.35 ± 0.18 17% faster 5.4x faster 7.6x faster
4 8.13 ± 0.17 31.10 ± 0.94 41.20 ± 0.70 6.32 ± 0.08 22% faster 4.9x faster 6.5x faster
16 6.63 ± 0.16 26.30 ± 0.66 34.54 ± 0.94 5.21 ± 0.16 21% faster 5.0x faster 6.6x faster
xychart-beta
    title "Linux, t.rast.mapcalc, nprocs=4: before (fork, forkserver, spawn), after"
    x-axis ["fork", "forkserver", "spawn", "after"]
    y-axis "Wall time (s)" 0 --> 45
    bar [7.78, 31.74, 40.24, 6.35]
Loading

macOS and Windows

GitHub Actions runners, built from this branch: macos-26 (arm64, 3 CPUs, Python 3.12.15) and windows-2022 (4 CPUs, Python 3.12.16). 3 repetitions of each setting, median of the 3. The variants were run in turns within each repetition, on the same machine. Fork is not available on Windows, and forkserver neither.

macOS

Tool nprocs Before, default (spawn) Before, fork Before, forkserver After After vs default After vs fork
t.rast.mapcalc 1 20.30 4.19 16.70 4.08 5.0x faster 3% faster
t.rast.mapcalc 4 9.13 2.76 7.46 2.68 3.4x faster 3% faster
t.rast.extract 1 19.34 4.36 17.78 4.10 4.7x faster 6% faster
t.rast.extract 4 8.61 2.93 7.53 2.95 2.9x faster 1% slower

Windows

Tool nprocs Before, default (spawn) After After vs default
t.rast.mapcalc 1 24.68 4.90 5.0x faster
t.rast.mapcalc 4 11.20 3.44 3.3x faster
t.rast.extract 1 24.55 4.99 4.9x faster
t.rast.extract 4 10.96 3.84 2.9x faster

With nprocs=1, the extra worker processes cost about 0.27 s per map on macOS and 0.33 s per map on Windows. After this change the times are close to fork on macOS, which is not safe there: it prints a warning about forking a multi-threaded process.

xychart-beta
    title "macOS, t.rast.mapcalc, nprocs=1: before (spawn, fork, forkserver), after"
    x-axis ["spawn", "fork", "forkserver", "after"]
    y-axis "Wall time (s)" 0 --> 25
    bar [20.30, 4.19, 16.70, 4.08]
Loading
xychart-beta
    title "Windows, t.rast.mapcalc, nprocs=1: before (spawn), after"
    x-axis ["spawn", "after"]
    y-axis "Wall time (s)" 0 --> 30
    bar [24.68, 4.90]
Loading

How has this been tested?

  • pytest for t.rast.mapcalc and t.rast.extract, and the testsuite of t.rast.extract, t.vect.extract and t.rast3d.extract, with Python 3.13 and 3.15.2 (also with ResourceWarning as an error on 3.15.2).
  • t.vect.extract with attribute tables in PostgreSQL and nprocs=4.
  • Runs of t.rast.mapcalc, t.rast.extract, t.rast3d.extract and t.vect.extract with the last output map already registered, and with an exception raised while a batch is running: no tool is left running and the other maps are registered.
  • A failing expression still ends with an error and exit status 1.
  • The macOS and OSGeo4W workflows on the CI runners.

AI assistance was used to write and review this change.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing
    functionality to not work as before)

Checklist

  • PR title provides summary of the changes and starts with one of the
    pre-defined prefixes
  • My code follows the code style
    of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
Suggested squash commit message
grass.temporal: Start r.mapcalc, r3.mapcalc and v.extract directly in parallel

dataset_mapcalculator() and extract_dataset() started one Python process
per output map only to call gs.run_command() in it. With the spawn start
method, used on macOS and Windows, each of these workers imports
grass.temporal again, which takes about 170 ms.

Start the tools with gs.start_command() and wait for them instead. The
number of tools running at once (nprocs) and the handling of their exit
codes are unchanged.

A batch of tools used to be waited for only when it was full or when the
last sample or row started a tool. When the last one was skipped (output
map already in the temporal database without overwrite, or a missing map
in a mapcalc sample), the last batch was never waited for. Its maps were
then read before they were written, and its exit codes were not checked.
Because the tools are no longer joined at interpreter exit like
multiprocessing workers, the command could also exit while they were
still running. Wait for full batches in the loop and for the remaining
tools after it.

Also wait for the started tools when an exception is raised while a
batch is running, as the multiprocessing workers were joined at
interpreter exit. Without this, the function returned while the tools
were still writing their maps.

The public run_mapcalc2d(), run_mapcalc3d() and run_vector_extraction()
are no longer used by extract_dataset(), but are kept. Their docstrings
no longer say they are run in extract_dataset().

The change was written with AI assistance and checked with the tests of
t.rast.mapcalc, t.rast.extract, t.rast3d.extract and t.vect.extract, by
running t.rast.mapcalc, t.rast.extract, t.rast3d.extract and
t.vect.extract with the last output map already registered, by raising
an exception after two tools of a batch were started, and by running
t.vect.extract with attribute tables in PostgreSQL.

Claude added 2 commits November 28, 2026 17:22
… parallel

dataset_mapcalculator() and extract_dataset() started one Python process
per output map only to call gs.run_command() in it. With the spawn start
method, used on macOS and Windows, each of these workers imports
grass.temporal again, which takes about 170 ms.

Start the tools with gs.start_command() and wait for them instead. The
number of tools running at once (nprocs) and the handling of their exit
codes are unchanged.

A batch of tools used to be waited for only when it was full or when the
last sample or row started a tool. When the last one was skipped (output
map already in the temporal database without overwrite, or a missing map
in a mapcalc sample), the last batch was never waited for. Its maps were
then read before they were written, and its exit codes were not checked.
Because the tools are no longer joined at interpreter exit like
multiprocessing workers, the command could also exit while they were
still running. Wait for full batches in the loop and for the remaining
tools after it.

Also wait for the started tools when an exception is raised while a
batch is running, as the multiprocessing workers were joined at
interpreter exit. Without this, the function returned while the tools
were still writing their maps.

The public run_mapcalc2d(), run_mapcalc3d() and run_vector_extraction()
are no longer used by extract_dataset(), but are kept. Their docstrings
no longer say they are run in extract_dataset().

The change was written with AI assistance and checked with the tests of
t.rast.mapcalc, t.rast.extract, t.rast3d.extract and t.vect.extract, by
running t.rast.mapcalc, t.rast.extract, t.rast3d.extract and
t.vect.extract with the last output map already registered, by raising
an exception after two tools of a batch were started, and by running
t.vect.extract with attribute tables in PostgreSQL.
Add a last step to the macOS and OSGeo4W workflows which runs
t.rast.mapcalc and t.rast.extract in turns with the modules from before
the tools were started directly, using each available multiprocessing
start method, and with the current modules, on the same machine. The
times of each run and a summary are printed in the log.

The step runs after all the other steps and cannot change the result of
the job. Reverting this commit removes it.
@github-actions github-actions Bot added windows Microsoft Windows specific macOS macOS specific CI Continuous integration Python Related code is in Python libraries labels Nov 29, 2026
@echoix
echoix marked this pull request as ready for review November 30, 2026 02:17
@echoix
echoix requested a review from petrasovaa November 30, 2026 12:44
Comment thread python/grass/temporal/extract.py Resolved
Comment thread python/grass/temporal/mapcalc.py Resolved
@petrasovaa

Copy link
Copy Markdown
Contributor

I think a better approach would be ThreadPoolExecutor, but we can merge this as a smaller change.

@petrasovaa petrasovaa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved if you want to move ahead.

@echoix
echoix merged commit bd60934 into OSGeo:main Dec 1, 2026
29 of 33 checks passed
@github-actions github-actions Bot added this to the 8.6.0 milestone Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI Continuous integration libraries macOS macOS specific Python Related code is in Python windows Microsoft Windows specific

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants