Add reseed_rng option to p_iter_fork #39025
Open
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.
This PR implements a convenience feature for the
@parallel
decorator, or more precisely, forp_iter_fork
.By setting
reseed_rng=True
the random number generator is reset in each subprocess by runningset_random_seed(self.worker_seed)
.It is ensured that the worker seeds are deterministically derived and thus, the results are always reproducible.
The new behaviour is useful for running probabilistic experiments and taking advantage of multiple cores.
The PR adds one "doc test". It would make sense to have additional (randomized) tests to assert that future modifications don't break the reproducibility. But I failed to locate a comprehensive test suite for sage/parallel, in particular, no existing tests for reproducibility of parallel computations. The
sage/tests
folder seems to contain other non-specific tests?Coding style w.r.t. https://doc.sagemath.org/html/en/reference/misc/sage/misc/randstate.html
with seed(worker_seed)
should be used. I did not use it, because the current implementation reseeds the whole subprocess once and for all.reseed_rng
inherits this caveat. I did not make it explicit in the doc string, because if the problem occurs, then alsoreseed_rng=False
should have it, but there is no mention of it.📝 Checklist
The doc tests passed for me, but preview via running
sage --docbuild tutorial html
gives meImportError: cannot import name count_all_local_good_types_normal_form' from 'sage.quadratic_forms.count_local_2'
so doc tests don't work although that has probably nothing to with this PR.