Repository navigation
lib: Make random number generation thread safe #6480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5ccafac
lib: correctly guard random number generation when run threaded
marisn 2c4592d
lib: ensure reproducible stream of random numbers when called from
marisn c29147e
Merge branch 'main' into rand_thread
echoix f057b67
Update lib/gis/testsuite/test_lrand48.py
echoix e5a8abe
Merge branch 'main' into rand_thread
echoix ad107d0
Merge branch 'main' into rand_thread
echoix 9ab8494
Merge branch 'main' into pr/marisn/6480
echoix 79653ff
style: Format cmake files with Gersemi
echoix 38ed9dc
libgis: Make random number generation thread safe with C11 atomics
wenzeslaus ea5d93f
Merge remote-tracking branch 'upstream/main' into rand_thread-atomics
wenzeslaus 83d31d2
Merge branch 'rand_thread' of https://github.com/marisn/grass into ra…
wenzeslaus b59e2fa
Merge branch 'main' into rand_thread
petrasovaa f852506
Update lib/gis/testsuite/test_lrand48.py
nilason 7bd84bd
Apply suggestion
nilason File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| """Test of gis library lrand48 PRNG thread-safety | ||
|
|
||
| @author Maris Nartiss | ||
|
|
||
| SPDX-FileCopyrightText: 2025 GRASS Development Team | ||
| SPDX-License-Identifier: GPL-2.0-or-later | ||
| """ | ||
|
|
||
| import threading | ||
|
|
||
| from grass.gunittest.case import TestCase | ||
| from grass.gunittest.main import test | ||
| from grass.lib.gis import G_lrand48, G_srand48 | ||
|
|
||
|
|
||
| class Lrand48ThreadSafetyTestCase(TestCase): | ||
| """Test case for lrand48 thread-safety and reproducibility.""" | ||
|
|
||
| def test_thread_safety_and_reproducibility(self): | ||
| """Verify that multi-threaded execution produces the same set of | ||
| random numbers as single-threaded execution.""" | ||
|
|
||
| seed = 1337 | ||
| num_values = 10000 | ||
| num_threads = 4 | ||
| values_per_thread = num_values // num_threads | ||
|
|
||
| list_single = [] | ||
| G_srand48(seed) | ||
| for _ in range(num_values): | ||
| list_single.append(G_lrand48()) | ||
|
|
||
| list_multi_raw = [] | ||
| lock = threading.Lock() | ||
|
|
||
| def worker(): | ||
| """Calls G_lrand48 and appends the result to a shared list.""" | ||
| local_results = [] | ||
| for _ in range(values_per_thread): | ||
| local_results.append(G_lrand48()) | ||
|
|
||
| # The lock protects only the Python result list, not the C | ||
| # generator under test (list.extend is atomic under the GIL, | ||
| # but that is a CPython implementation detail). | ||
| with lock: | ||
| list_multi_raw.extend(local_results) | ||
|
|
||
| # Reset the seed to ensure the sequence starts from the beginning. | ||
| G_srand48(seed) | ||
|
|
||
| threads = [] | ||
| for _ in range(num_threads): | ||
| thread = threading.Thread(target=worker) | ||
| threads.append(thread) | ||
| thread.start() | ||
|
|
||
| for thread in threads: | ||
| thread.join() | ||
|
|
||
| # The generator serializes state updates, so the threads together | ||
| # must consume exactly the single-threaded sequence; only the | ||
| # distribution of values between threads may differ. Sorting both | ||
| # lists removes the scheduling-dependent order before comparison. | ||
| # A set-based comparison would not work, because a correct | ||
| # sequence can contain legitimate duplicates. | ||
| self.assertListEqual( | ||
| sorted(list_single), | ||
| sorted(list_multi_raw), | ||
| "The set of generated numbers differs between " | ||
| "single-threaded and multi-threaded runs.", | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| test() |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.