|
1 | 1 | import matplotlib.pyplot as plt |
2 | 2 | import numpy as np |
3 | | -from mpi4py import MPI |
4 | | -from tutorial_gen import gen_random_sample |
5 | | -from tutorial_sim import sim_find_sine |
| 3 | +from gen import gen_random_sample |
| 4 | +from sim import sim_find_sine |
6 | 5 |
|
7 | | -from libensemble.libE import libE |
8 | | -from libensemble.tools import add_unique_random_streams |
| 6 | +from libensemble import Ensemble |
| 7 | +from libensemble.specs import ExitCriteria, GenSpecs, SimSpecs |
9 | 8 |
|
10 | | -libE_specs = {"comms": "mpi"} # 'nworkers' removed, 'comms' now 'mpi' |
| 9 | +# libE_specs = LibeSpecs(nworkers=4, comms="local") |
11 | 10 |
|
12 | | -nworkers = MPI.COMM_WORLD.Get_size() - 1 # one process belongs to manager |
13 | | -is_manager = MPI.COMM_WORLD.Get_rank() == 0 # manager process has MPI rank 0 |
14 | | - |
15 | | -gen_specs = { |
16 | | - "gen_f": gen_random_sample, # Our generator function |
17 | | - "out": [("x", float, (1,))], # gen_f output (name, type, size). |
18 | | - "user": { |
19 | | - "lower": np.array([-3]), # random sampling lower bound |
20 | | - "upper": np.array([3]), # random sampling upper bound |
21 | | - "gen_batch_size": 5, # number of values gen_f will generate per call |
| 11 | +gen_specs = GenSpecs( |
| 12 | + gen_f=gen_random_sample, # Our generator function |
| 13 | + out=[("x", float, (1,))], # gen_f output (name, type, size) |
| 14 | + user={ |
| 15 | + "lower": np.array([-3]), # lower boundary for random sampling |
| 16 | + "upper": np.array([3]), # upper boundary for random sampling |
| 17 | + "gen_batch_size": 5, # number of x's gen_f generates per call |
22 | 18 | }, |
23 | | -} |
| 19 | +) |
| 20 | + |
| 21 | +sim_specs = SimSpecs( |
| 22 | + sim_f=sim_find_sine, # Our simulator function |
| 23 | + inputs=["x"], # Input field names. "x" from gen_f output |
| 24 | + out=[("y", float)], # sim_f output. "y" = sine("x") |
| 25 | +) |
| 26 | + |
| 27 | +exit_criteria = ExitCriteria(sim_max=80) # Stop libEnsemble after 80 simulations |
24 | 28 |
|
25 | | -sim_specs = { |
26 | | - "sim_f": sim_find_sine, # Our simulator function |
27 | | - "in": ["x"], # Input field names. 'x' from gen_f output |
28 | | - "out": [("y", float)], # sim_f output. 'y' = sine('x') |
29 | | -} |
| 29 | +ensemble = Ensemble(sim_specs, gen_specs, exit_criteria, parse_args=True) |
30 | 30 |
|
31 | | -persis_info = add_unique_random_streams({}, nworkers + 1) # Initialize manager/workers random streams |
| 31 | +ensemble.add_random_streams() # setup the random streams unique to each worker |
32 | 32 |
|
33 | | -exit_criteria = {"sim_max": 80} # Stop libEnsemble after 80 simulations |
| 33 | +if __name__ == "__main__": |
34 | 34 |
|
35 | | -H, persis_info, flag = libE(sim_specs, gen_specs, exit_criteria, persis_info, libE_specs=libE_specs) |
| 35 | + ensemble.run() # start the ensemble. Blocks until completion. |
36 | 36 |
|
37 | | -# Some (optional) statements to visualize our History array |
38 | | -# Only the manager process should execute this |
39 | | -if is_manager: |
40 | | - print([i for i in H.dtype.fields]) |
41 | | - print(H) |
| 37 | + if ensemble.is_manager: # only True on rank 0 |
| 38 | + history = ensemble.H # start visualizing our results |
42 | 39 |
|
43 | | - colors = ["b", "g", "r", "y", "m", "c", "k", "w"] |
| 40 | + colors = ["b", "g", "r", "y", "m", "c", "k", "w"] |
44 | 41 |
|
45 | | - for i in range(1, nworkers + 1): |
46 | | - worker_xy = np.extract(H["sim_worker"] == i, H) |
47 | | - x = [entry.tolist()[0] for entry in worker_xy["x"]] |
48 | | - y = [entry for entry in worker_xy["y"]] |
49 | | - plt.scatter(x, y, label=f"Worker {i}", c=colors[i - 1]) |
| 42 | + for i in range(1, ensemble.nworkers + 1): |
| 43 | + worker_xy = np.extract(history["sim_worker"] == i, history) |
| 44 | + x = [entry.tolist()[0] for entry in worker_xy["x"]] |
| 45 | + y = [entry for entry in worker_xy["y"]] |
| 46 | + plt.scatter(x, y, label="Worker {}".format(i), c=colors[i - 1]) |
50 | 47 |
|
51 | | - plt.title("Sine calculations for a uniformly sampled random distribution") |
52 | | - plt.xlabel("x") |
53 | | - plt.ylabel("sine(x)") |
54 | | - plt.legend(loc="lower right") |
55 | | - plt.savefig("tutorial_sines.png") |
| 48 | + plt.title("Sine calculations for a uniformly sampled random distribution") |
| 49 | + plt.xlabel("x") |
| 50 | + plt.ylabel("sine(x)") |
| 51 | + plt.legend(loc="lower right") |
| 52 | + plt.savefig("tutorial_sines.png") |
0 commit comments