Skip to content

r.sim: Keep the diffusion coefficient in a walker-local variable - #7965

Merged
wenzeslaus merged 2 commits into
OSGeo:mainfrom
wenzeslaus:simwe-nprocs-dif-local
Sep 29, 2026
Merged

wenzeslaus merged 2 commits into
OSGeo:mainfrom
wenzeslaus:simwe-nprocs-dif-local

Conversation

@wenzeslaus

Copy link
Copy Markdown
Member

In the r.sim.water and r.sim.sediment walker loop, the diffusion coefficient of a walker's move was written to a raster-sized grid and read back by the same walker a few lines later. With nprocs > 1, a walker on another thread in the same cell could overwrite it in between. This makes it a local variable and drops the grid, which also saves one float grid of memory. Single-threaded results do not change.

Details (AI generated)
  • grids->dif was used only in main_loop() in simlib/hydro.c, so it is removed from Grids and from both allocation functions in simlib/input.c.
  • The local variable is a float, the type of the grid, rather than a double, so that single-threaded results do not change: depth and discharge (with and without infiltration), sediment flux and erosion/deposition on nc_spm are byte-identical to main.
  • With more threads, results can differ from before, because a walker no longer moves with a coefficient another walker wrote.

The analysis and code were done with Claude Code (Opus) and reviewed by me.

The diffusion coefficient of a walker's move was stored in a raster-sized
grid cell only to be read back by the same walker a few lines later, so
concurrent walkers in the same cell could read each other's value. Keep
it in a local variable instead and drop the grid.

The local variable is a float, the type of the grid, rather than a
double, so that single-threaded results do not change: the depth,
discharge, sediment flux and erosion/deposition maps of single-threaded
runs on the nc_spm elevation are byte-identical to those before this
change. With more threads, results can differ from before, because a
walker no longer moves with a coefficient another walker wrote.

Written with the help of Claude Code.
@github-actions github-actions Bot added raster Related to raster data processing C Related code is in C module labels Sep 28, 2026
@wenzeslaus
wenzeslaus enabled auto-merge (squash) September 29, 2026 14:36
@wenzeslaus
wenzeslaus merged commit c69a52a into OSGeo:main Sep 29, 2026
25 checks passed
@wenzeslaus
wenzeslaus deleted the simwe-nprocs-dif-local branch September 29, 2026 16:21
@wenzeslaus wenzeslaus added this to the 8.6.0 milestone Sep 29, 2026
wenzeslaus added a commit to wenzeslaus/grass that referenced this pull request Sep 29, 2026
…eo#7965)

The diffusion coefficient of a walker's move was stored in a raster-sized
grid cell only to be read back by the same walker a few lines later, so
with nprocs > 1, concurrent walkers in the same cell could read each
other's value. Keep it in a local variable instead and drop the grid,
which also saves one float grid of memory.

The local variable is a float, the type of the grid, rather than a
double, so that single-threaded results do not change: the depth,
discharge, sediment flux and erosion/deposition maps of single-threaded
runs on the nc_spm elevation are byte-identical to those before this
change. With more threads, results can differ from before, because a
walker no longer moves with a coefficient another walker wrote.

Written with the help of Claude Code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C Related code is in C module raster Related to raster data processing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants