Repository navigation
r.sim: Replace custom chunk computation with OpenMP for loop - #7995
Merged
Merged
Conversation
The walker loop computed each thread's range of walkers by hand from the thread number and count. A worksharing loop with static schedule does the same with one pragma, and ends with an implicit barrier, so code after the loop in the same parallel region sees all walkers processed without an explicit barrier. The loop variable is private in a worksharing loop, so it is no longer listed as firstprivate. The parallel region is kept, rather than merged with the loop into a parallel for, to keep the loop body at its indentation. Single-threaded results do not change: the depth and discharge maps computed from the nc_spm elevation, with and without infiltration and diffusion, are byte-identical to those before this change, and the run time is the same within noise at 1, 4 and 8 threads. Written with the help of Claude Code.
petrasovaa
approved these changes
Oct 1, 2026
wenzeslaus
enabled auto-merge (squash)
October 1, 2026 20:51
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.
The walker loop in r.sim currently computes each thread's range of walkers by hand, which is more code than OpenMP's own worksharing loop, which does basically the same. This replaces the hand-computed ranges with
#pragma omp for schedule(static). Single-threaded results do not change.Details (AI generated)
main_loop()(raster/r.sim/simlib/hydro.c), the#if defined(_OPENMP)block that computedmin_loopandmax_loopfromomp_get_thread_num()andomp_get_num_threads()is replaced by#pragma omp for schedule(static)on the plain loop over all walkers (2 lines added, 13 removed).omp forends with an implicit barrier.#pragma omp parallelregion stays, rather than being merged with the loop intoparallel for, so that the loop body keeps its indentation and the diff stays small.lwis dropped fromfirstprivate, since the loop variable of a worksharing loop is private.schedule(static)spreads the remainder instead (with GCC's libgomp: 3, 3, 2, 2 and 552, 551, 551, 551); the OpenMP standard only requires approximately equal contiguous chunks, at most one per thread, so the exact split is up to the implementation. Each thread still gets one contiguous block of walkers, and with one thread the order is unchanged. With the shared random number generator, multi-threaded results depend on scheduling anyway.Squash-merge commit message
Title:
Body:
The change was suggested by and done with Claude Code (Opus); reviewed by me.