Skip to content

r.sim.water: fix flaky buffer overflow and OpenMP thread-safety issues - #7521

Closed
anzalkabeer wants to merge 3 commits into
OSGeo:mainfrom
anzalkabeer:fix-r-sim-water-parallel
Closed

anzalkabeer wants to merge 3 commits into
OSGeo:mainfrom
anzalkabeer:fix-r-sim-water-parallel

Conversation

@anzalkabeer

Copy link
Copy Markdown
  • Add missing bounds check exit (continue) in hydro.c loop
  • Replace thread-unsafe G_drand48() in OpenMP threads with thread-safe LCG PRNG
  • Prevent concurrency data races on shared grids by adding OpenMP atomic and critical sections

- Add missing bounds check exit (continue) in hydro.c loop
- Replace thread-unsafe G_drand48() in OpenMP threads with thread-safe LCG PRNG
- Prevent concurrency data races on shared grids by adding OpenMP atomic and critical sections
@github-actions github-actions Bot added raster Related to raster data processing C Related code is in C module labels Jun 8, 2026
Comment on lines +206 to +207
decr; /* decrease infilt. in cell
and eliminate the walker */

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.

[pre-commit] reported by reviewdog 🐶

Suggested change
decr; /* decrease infilt. in cell
and eliminate the walker */
decr; /* decrease infilt. in
cell and eliminate the
walker */

Comment on lines +213 to +214
addac; /* use just proportional part
of the walker weight */

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.

[pre-commit] reported by reviewdog 🐶

Suggested change
addac; /* use just proportional part
of the walker weight */
addac; /* use just proportional
part of the walker
weight */

#if defined(_OPENMP)
int i;
int max_threads = omp_get_max_threads();
if (max_threads > MAX_SIMWE_THREADS) max_threads = MAX_SIMWE_THREADS;

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.

[pre-commit] reported by reviewdog 🐶

Suggested change
if (max_threads > MAX_SIMWE_THREADS) max_threads = MAX_SIMWE_THREADS;
if (max_threads > MAX_SIMWE_THREADS)
max_threads = MAX_SIMWE_THREADS;

@echoix

echoix commented Jun 8, 2026

Copy link
Copy Markdown
Member

Glad to see you managed to do the new branch and PR

@Valyrian-Code

Copy link
Copy Markdown
Contributor

Thanks @anzalkabeer, and glad the property-based approach made sense to you.

I looked into the CI, and I don't think it is rate limiting. There seem to be two real causes:

  1. The build/test jobs (cmake, macOS, ubuntu, windows) fail in the r.sim.water test suite with repeated AssertionError: r.info map=... -r difference: from assertRastersEqual. That is expected from this change: the new PRNG alters r.sim.water's output, so it no longer matches the committed reference rasters (data/depth_default.pack, etc.) that were generated with the old G_drand48 sequence. To get these green you would need to regenerate those reference .pack files for the new PRNG. Note the serial cell-wise comparisons in test_r_sim_water.py use them too, so they need updating regardless of the parallel-test change in r.sim.water: remove flaky parallel comparison from test_nodxdy #7520.

  2. Super Linter fails on EDITORCONFIG (3 errors found), which is also not rate limiting. The 403 Failed to create GitHub commit status lines are a separate fork-permissions warning, not the failure. Running pre-commit run --all-files locally should surface and fix the editorconfig issues (often trailing whitespace or a missing final newline).

Happy to help once those are sorted.

@anzalkabeer

Copy link
Copy Markdown
Author

@Valyrian-Code i will implement these things into the next PRr in some time thanks for spotting the real problem in the PR

@Valyrian-Code

Copy link
Copy Markdown
Contributor

No problem @anzalkabeer, glad it helped. Ping me when the next PR is up and I will take another look, and I am happy to help if the reference regeneration turns out to be fiddly.

@github-actions github-actions Bot added Python Related code is in Python tests Related to Test Suite labels Jun 8, 2026
@anzalkabeer

Copy link
Copy Markdown
Author

@Valyrian-Code have a look at the new PR please

self.assertGreaterEqual(depth["min"], 0)
self.assertEqual(depth["null_cells"], 0)
self.assertGreater(depth["sum"], 0)

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.

[pre-commit] reported by reviewdog 🐶

Suggested change

{
#if defined(_OPENMP)
int tid = omp_get_thread_num();
if (tid >= MAX_SIMWE_THREADS) tid = MAX_SIMWE_THREADS - 1;

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.

[pre-commit] reported by reviewdog 🐶

Suggested change
if (tid >= MAX_SIMWE_THREADS) tid = MAX_SIMWE_THREADS - 1;
if (tid >= MAX_SIMWE_THREADS)
tid = MAX_SIMWE_THREADS - 1;

@petrasovaa

Copy link
Copy Markdown
Contributor

Generating reference data in the test doesn't make sense. Small, focused PRs would be easier to review.

@petrasovaa petrasovaa closed this Jun 8, 2026
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 Python Related code is in Python raster Related to raster data processing tests Related to Test Suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants