Skip to content

r.sim.water: Fix flaky buffer overflow and OpenMP concurrency issues - #7515

Closed
anzalkabeer wants to merge 3 commits into
OSGeo:mainfrom
anzalkabeer:main
Closed

anzalkabeer wants to merge 3 commits into
OSGeo:mainfrom
anzalkabeer:main

Conversation

@anzalkabeer

Copy link
Copy Markdown

r.sim.water: buffer overflow #4810:

fixed the out of bounds walkers , also the threads are safe from random number generation. by adding a custom PRNG utilizing a thread indexed array . please look into my fixes

Add a second tab to the Query Results dialog that sends the queried
map data (elevation, land use, aspect, vector attributes, etc.) to
an LLM (Gemini or OpenAI) and displays a human-readable terrain
description.

Implementation uses only Python standard library (urllib.request,
threading) to avoid new dependencies. API calls run in a background
thread with results posted back via wx.CallAfter.

Larger use of AI was involved in writing this feature.
- 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 GUI wxGUI related raster Related to raster data processing Python Related code is in Python C Related code is in C module labels Jun 7, 2026
@anzalkabeer anzalkabeer changed the title bug fixes and making random number generation thread safe by adjusting the global states of simwe_rand() calls G_drand48() bug-fixes-and-making-random-number-generation-thread-safe-by-adjusting-the-global-states-of-simwe_rand()-calls-G_drand48() Jun 7, 2026
@anzalkabeer anzalkabeer changed the title bug-fixes-and-making-random-number-generation-thread-safe-by-adjusting-the-global-states-of-simwe_rand()-calls-G_drand48() r.sim.water: Fix flaky buffer overflow and OpenMP concurrency issues Jun 7, 2026
@Valyrian-Code

Valyrian-Code commented Jun 7, 2026 •

Copy link
Copy Markdown
Contributor

Hi @anzalkabeer,

Nice to see the buffer-overflow fix and the thread-safe PRNG improvements.

One thing I noticed is that this PR also modifies gui/wxpython/gui_core/query.py, adding _format_query_data_as_text and an LLM API call. That seems unrelated to the r.sim.water fix and may have been included accidentally. It would probably be best to drop those changes and keep this PR focused on r.sim.water, which should also make the review easier.

Separately, I've been working on the testing side in #7506. That work adds a synthetic, property-based pytest that validates physical behavior rather than exact cell values, making it robust to thread scheduling and other sources of nondeterminism.

I'd be happy to help verify your fix against those tests, or check whether parallel runs are now deterministic, if that would be useful.

@echoix

echoix commented Jun 7, 2026

Copy link
Copy Markdown
Member

Try to create a branch first with a change for each PR. Otherwise, you cannot have two PRs in the same repo for the same branch. You’ll probably want to update your main branch of your fork to this repo many times in the future, and you’ll be stuck if an open PR uses it.

Also, for us (or me) we’ll receive CI updates on runs in your fork if we merge your main branch as a PR, as it’ll count like if we are participants to your fork. It already happened, and it’s just a little bit of a disturbance.

So, search a bit how to do a branch of your fork from main, then either rebase interactively to remove the extra commit or learn how to cherry pick the commit you want to a new branch, learn how to keep your fork in sync on GitHub, and then try resubmitting what you intended as a fresh PR.

It’s not so fun trying to learn these quirks of git and GitHub in the beginning, but it’ll be useful for you.

@echoix echoix closed this Jun 7, 2026
@Valyrian-Code

Copy link
Copy Markdown
Contributor

No worries @anzalkabeer, the fix itself looked solid. Once you reopen it on a clean branch, I'd be glad to test it against the #7506 tests and check that parallel runs come out deterministic now. Looking forward to it.

@anzalkabeer

Copy link
Copy Markdown
Author

@Valyrian-Code i have reopened a fresh branch for the same fix and also have dropped the llm api call and _format_query_data_as_text , please take a look into it .

@Valyrian-Code

Copy link
Copy Markdown
Contributor

Thanks @anzalkabeer, and good call dropping the LLM/query.py changes. I had a look at the fresh PR and left some notes on #7521: the CI failures look like real causes rather than rate limiting (the new PRNG changes r.sim.water's output so it no longer matches the committed reference rasters, and Super Linter is failing on an editorconfig issue). Details are in that thread.

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 GUI wxGUI related module Python Related code is in Python raster Related to raster data processing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants