Repository navigation
grass.temporal: Start r.mapcalc, r3.mapcalc and v.extract directly in parallel - #8288
Merged
Merged
Conversation
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.
…flows" This reverts commit df5fa1c.
echoix
marked this pull request as ready for review
November 30, 2026 02:17
petrasovaa
reviewed
Nov 30, 2026
petrasovaa
reviewed
Nov 30, 2026
Contributor
|
I think a better approach would be ThreadPoolExecutor, but we can merge this as a smaller change. |
petrasovaa
approved these changes
Nov 30, 2026
petrasovaa
left a comment
Contributor
There was a problem hiding this comment.
Approved if you want to move ahead.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.DoneDescription
dataset_mapcalculator()(t.rast.mapcalc,t.rast3d.mapcalc) andextract_dataset()(t.rast.extract,t.rast3d.extract,t.vect.extract) started one Python process per output map only to callgs.run_command()in it. They now startr.mapcalc,r3.mapcalcandv.extractwithgs.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:
run_mapcalc2d(),run_mapcalc3d()andrun_vector_extraction()are exported bygrass.temporal. They are no longer used byextract_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.temporalagain, 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 inmapcalc.pyandextract.pywas replaced bymultiprocessing.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 pernprocs. "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.mapcalcnprocst.rast.extractnprocsxychart-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]macOS and Windows
GitHub Actions runners, built from this branch:
macos-26(arm64, 3 CPUs, Python 3.12.15) andwindows-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
nprocst.rast.mapcalct.rast.mapcalct.rast.extractt.rast.extractWindows
nprocst.rast.mapcalct.rast.mapcalct.rast.extractt.rast.extractWith
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]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]How has this been tested?
pytestfort.rast.mapcalcandt.rast.extract, and thetestsuiteoft.rast.extract,t.vect.extractandt.rast3d.extract, with Python 3.13 and 3.15.2 (also withResourceWarningas an error on 3.15.2).t.vect.extractwith attribute tables in PostgreSQL andnprocs=4.t.rast.mapcalc,t.rast.extract,t.rast3d.extractandt.vect.extractwith 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.AI assistance was used to write and review this change.
Types of changes
functionality to not work as before)
Checklist
pre-defined prefixes
of this project.
Suggested squash commit message