From 3eef713772a76fd93c7df3e0f4797ed5ce1463bd Mon Sep 17 00:00:00 2001 From: Tom Wagg Date: Mon, 22 Jun 2026 17:25:08 -0400 Subject: [PATCH 1/3] seed each pool worker differently --- src/cosmic/sample/sampler/independent.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/cosmic/sample/sampler/independent.py b/src/cosmic/sample/sampler/independent.py index d0ba45d21..a9a23a7d4 100644 --- a/src/cosmic/sample/sampler/independent.py +++ b/src/cosmic/sample/sampler/independent.py @@ -24,6 +24,7 @@ import pandas as pd import warnings from multiprocessing import Pool +import os from cosmic import utils @@ -218,7 +219,7 @@ def get_independent_sampler( # if no pool was passed in, but nproc > 1, create a pool if not pool_existed_already and nproc > 1: - pool = Pool(nproc) + pool = Pool(nproc, initializer=_init_worker) # if there's no pool, simply pass the arguments to the worker if pool is None: @@ -479,6 +480,11 @@ def _independent_sampler_worker( n_binaries ) +def _init_worker(): + """Ensure that each worker process has a different random seed.""" + np.random.seed(np.random.get_state()[1][0] + os.getpid()) + + register_sampler( "independent", From 46faf992229802228e6b4087a39f31a016ee8d67 Mon Sep 17 00:00:00 2001 From: Tom Wagg Date: Mon, 22 Jun 2026 17:25:32 -0400 Subject: [PATCH 2/3] add test for unique samples --- src/cosmic/tests/test_sample.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/cosmic/tests/test_sample.py b/src/cosmic/tests/test_sample.py index 05155d22e..0168d5ed9 100644 --- a/src/cosmic/tests/test_sample.py +++ b/src/cosmic/tests/test_sample.py @@ -644,6 +644,21 @@ def test_m2min_qmin(self): it_fails = True self.assertFalse(it_fails) + def test_samples_unique(self): + # ensure that samples are unique, both serial and parallel + np.random.seed(2) + + for nproc in [1, 2]: + ibt = InitialBinaryTable.sampler( + 'independent', range(16), range(16), + binfrac_model=1.0, primary_model='kroupa01', + ecc_model='sana12', porb_model='sana12', + qmin=-1, SF_start=13700.0, SF_duration=0.0, + met=0.02, size=10_000, nproc=nproc + )[0] + + self.assertTrue(len(ibt) == len(ibt.drop_duplicates())) + class TestCMCSample(unittest.TestCase): def test_plummer_profile(self): np.random.seed(2) From 6bb1294e7668466b3d473c664682d1502d73ae01 Mon Sep 17 00:00:00 2001 From: Tom Wagg Date: Mon, 22 Jun 2026 17:26:52 -0400 Subject: [PATCH 3/3] update version and changelog --- changelog.md | 11 +++++++++++ src/cosmic/_version.py | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index b245f8881..9c909d2f4 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,17 @@ # COSMIC Changelog ## Prepend only please! +## 4.1.1 + +- Additions/changes + - + +- Code cleanup + - + +- Bug fixes + - Fixed issue from v3.7.6 where multiprocessing re-used random seeds across workers. This results in non-unique samples proportional to the number of cores that you used. + ## 4.1.0 - Additions/changes diff --git a/src/cosmic/_version.py b/src/cosmic/_version.py index 703970876..72aa75832 100644 --- a/src/cosmic/_version.py +++ b/src/cosmic/_version.py @@ -1 +1 @@ -__version__ = "4.1.0" +__version__ = "4.1.1"