Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/cosmic/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "4.1.0"
__version__ = "4.1.1"
8 changes: 7 additions & 1 deletion src/cosmic/sample/sampler/independent.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import pandas as pd
import warnings
from multiprocessing import Pool
import os

from cosmic import utils

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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",
Expand Down
15 changes: 15 additions & 0 deletions src/cosmic/tests/test_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading