Skip to content

Commit 18db310

Browse files
shuds13jmlarson1
andauthored
Prevent erroneous nworkers warning (#1383)
--------- Co-authored-by: Jeffrey Larson <jmlarson@anl.gov>
1 parent 5534439 commit 18db310

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

libensemble/ensemble.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,12 @@ def __init__(
310310
raise ValueError("nworkers must be specified if comms is 'local'")
311311

312312
elif self._known_comms == "mpi" and not parse_args:
313-
self.nworkers, self.is_manager = mpi_init(self._libE_specs.mpi_comm)
313+
# Set internal _nworkers - not libE_specs (avoid "nworkers will be ignored" warning)
314+
self._nworkers, self.is_manager = mpi_init(self._libE_specs.mpi_comm)
314315

315316
def _parse_args(self) -> (int, bool, LibeSpecs):
316-
self.nworkers, self.is_manager, libE_specs_parsed, self.extra_args = parse_args_f()
317+
# Set internal _nworkers - not libE_specs (avoid "nworkers will be ignored" warning)
318+
self._nworkers, self.is_manager, libE_specs_parsed, self.extra_args = parse_args_f()
317319

318320
if not self._libE_specs:
319321
self._libE_specs = LibeSpecs(**libE_specs_parsed)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
Runs libEnsemble with Latin hypercube sampling and check no warning.
3+
4+
Execute using MPI (e.g. 3 workers):
5+
mpiexec -np 4 python test_mpi_warning.py
6+
7+
The number of concurrent evaluations of the objective function will be 4-1=3.
8+
"""
9+
10+
# Do not change these lines - they are parsed by run-tests.sh
11+
# TESTSUITE_COMMS: mpi
12+
# TESTSUITE_NPROCS: 4
13+
14+
import numpy as np
15+
import os
16+
import time
17+
18+
from libensemble import Ensemble
19+
from libensemble.gen_funcs.sampling import latin_hypercube_sample as gen_f
20+
21+
# Import libEnsemble items for this test
22+
from libensemble.sim_funcs.simple_sim import norm_eval as sim_f
23+
from libensemble.specs import ExitCriteria, GenSpecs, SimSpecs
24+
25+
from libensemble import logger
26+
27+
# Main block is necessary only when using local comms with spawn start method (default on macOS and Windows).
28+
if __name__ == "__main__":
29+
log_file = "ensemble_check_warning.log"
30+
logger.set_level("MANAGER_WARNING")
31+
logger.set_filename(log_file)
32+
33+
sampling = Ensemble()
34+
sampling.libE_specs.save_every_k_sims = 100
35+
sampling.sim_specs = SimSpecs(sim_f=sim_f)
36+
sampling.gen_specs = GenSpecs(
37+
gen_f=gen_f,
38+
outputs=[("x", float, 2)],
39+
user={
40+
"gen_batch_size": 100,
41+
"lb": np.array([-3, -2]),
42+
"ub": np.array([3, 2]),
43+
},
44+
)
45+
46+
sampling.exit_criteria = ExitCriteria(sim_max=100)
47+
sampling.add_random_streams()
48+
49+
if sampling.is_manager:
50+
if os.path.exists(log_file):
51+
os.remove(log_file)
52+
53+
sampling.run()
54+
if sampling.is_manager:
55+
print("len:", len(sampling.H))
56+
time.sleep(0.2)
57+
assert os.path.exists(log_file)
58+
assert os.stat(log_file).st_size == 0, "Unexpected warning"

0 commit comments

Comments
 (0)