Skip to content
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

Better ensemble.add_random_streams docstring, don't erase persis_info #1074

Merged
merged 3 commits into from
Sep 7, 2023
Merged
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
20 changes: 18 additions & 2 deletions libensemble/ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,13 +494,29 @@ def from_json(self, file_path: str):
self._parameterize(loaded)

def add_random_streams(self, num_streams: int = 0, seed: str = ""):
"""Adds ``np.random`` generators for each worker to ``persis_info``"""
"""

Adds ``np.random`` generators for each worker ID to ``self.persis_info``.

Parameters
----------

num_streams: int, optional

Number of matching worker ID and random stream entries to create. Defaults to
``self.nworkers``.

seed: str, optional

Seed for NumPy's RNG

"""
if num_streams:
nstreams = num_streams
else:
nstreams = self._nworkers()

self.persis_info = add_unique_random_streams({}, nstreams + 1, seed=seed)
self.persis_info = add_unique_random_streams(self.persis_info, nstreams + 1, seed=seed)
return self.persis_info

def save_output(self, file: str):
Expand Down
3 changes: 2 additions & 1 deletion libensemble/tests/regression_tests/test_1d_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# Import libEnsemble items for this test
from libensemble.sim_funcs.one_d_func import one_d_example as sim_f
from libensemble.specs import ExitCriteria, GenSpecs, LibeSpecs, SimSpecs
from libensemble.tools import add_unique_random_streams

# Main block is necessary only when using local comms with spawn start method (default on macOS and Windows).
if __name__ == "__main__":
Expand All @@ -38,7 +39,7 @@
},
)

sampling.add_random_streams()
sampling.persis_info = add_unique_random_streams({}, sampling.nworkers + 1)
sampling.exit_criteria = ExitCriteria(gen_max=501)

sampling.run()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from libensemble.sim_funcs import six_hump_camel
from libensemble.sim_funcs.var_resources import gpu_variable_resources_from_gen as sim_f
from libensemble.specs import AllocSpecs, ExitCriteria, GenSpecs, LibeSpecs, SimSpecs
from libensemble.tools import add_unique_random_streams

# from libensemble import logger
# logger.set_level("DEBUG") # For testing the test
Expand Down Expand Up @@ -83,7 +84,7 @@
},
)

gpu_test.add_random_streams()
gpu_test.persis_info = add_unique_random_streams({}, gpu_test.nworkers + 1)
gpu_test.exit_criteria = ExitCriteria(sim_max=40)
gpu_test.run()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
from libensemble.sim_funcs import six_hump_camel
from libensemble.sim_funcs.var_resources import gpu_variable_resources_from_gen as sim_f
from libensemble.specs import AllocSpecs, ExitCriteria, GenSpecs, LibeSpecs, SimSpecs
from libensemble.tools import add_unique_random_streams

# from libensemble import logger
# logger.set_level("DEBUG") # For testing the test
Expand Down Expand Up @@ -95,7 +96,7 @@
},
)

gpu_test.add_random_streams()
gpu_test.persis_info = add_unique_random_streams({}, gpu_test.nworkers + 1)
gpu_test.exit_criteria = ExitCriteria(sim_max=40, wallclock_max=300)

if gpu_test.ready():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from libensemble.gen_funcs.persistent_inverse_bayes import persistent_updater_after_likelihood as gen_f
from libensemble.sim_funcs.inverse_bayes import likelihood_calculator as sim_f
from libensemble.specs import AllocSpecs, ExitCriteria, GenSpecs, SimSpecs
from libensemble.tools import add_unique_random_streams

# Main block is necessary only when using local comms with spawn start method (default on macOS and Windows).
if __name__ == "__main__":
Expand Down Expand Up @@ -57,7 +58,7 @@
alloc_specs=AllocSpecs(alloc_f=alloc_f),
)

bayes_test.add_random_streams()
bayes_test.persis_info = add_unique_random_streams({}, bayes_test.nworkers + 1)
gen_user = bayes_test.gen_specs.user
val = gen_user["subbatch_size"] * gen_user["num_subbatches"] * gen_user["num_batches"]
bayes_test.exit_criteria = ExitCriteria(sim_max=val, wallclock_max=300)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from libensemble.sim_funcs.noisy_vector_mapping import func_wrapper as sim_f
from libensemble.sim_funcs.noisy_vector_mapping import noisy_function
from libensemble.specs import AllocSpecs, ExitCriteria, GenSpecs, SimSpecs
from libensemble.tools import add_unique_random_streams

# Main block is necessary only when using local comms with spawn start method (default on macOS and Windows).
if __name__ == "__main__":
Expand Down Expand Up @@ -62,7 +63,7 @@
alloc_specs=AllocSpecs(alloc_f=alloc_f),
exit_criteria=ExitCriteria(gen_max=1000),
)
fd_test.add_random_streams()
fd_test.persis_info = add_unique_random_streams({}, fd_test.nworkers + 1)

shutil.copy("./scripts_used_by_reg_tests/ECnoise.m", "./")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from libensemble.sim_funcs.surmise_test_function import borehole as sim_f
from libensemble.sim_funcs.surmise_test_function import tstd2theta
from libensemble.specs import AllocSpecs, ExitCriteria, GenSpecs, SimSpecs
from libensemble.tools import add_unique_random_streams

if __name__ == "__main__":

Expand Down Expand Up @@ -100,7 +101,7 @@
exit_criteria=ExitCriteria(sim_max=max_evals),
)

test.add_random_streams()
test.persis_info = add_unique_random_streams({}, test.nworkers + 1)

# Perform the run
H, _, _ = test.run()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from libensemble.ensemble import Ensemble
from libensemble.executors.mpi_executor import MPIExecutor
from libensemble.tools import add_unique_random_streams

####################

Expand Down Expand Up @@ -36,7 +37,7 @@
}
)

forces.add_random_streams()
forces.persis_info = add_unique_random_streams({}, forces.nworkers + 1)

forces.run()
forces.save_output(__file__)