Skip to content

Commit 68589c2

Browse files
committed
Put everything under name=main
1 parent 980d9da commit 68589c2

3 files changed

Lines changed: 98 additions & 104 deletions

File tree

docs/tutorials/local_sine_tutorial.rst

Lines changed: 54 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ need to write a new allocation function.
148148
from gen import gen_random_sample
149149
from sim import sim_find_sine
150150
151-
libE_specs = LibeSpecs(nworkers=4, comms="local")
151+
if __name__ == "__main__": # Python-quirk required on macOS and windows
152+
libE_specs = LibeSpecs(nworkers=4, comms="local")
152153
153154
We configure the settings and specifications for our ``sim_f`` and ``gen_f``
154155
functions in the :ref:`GenSpecs<datastruct-gen-specs>` and
@@ -159,45 +160,43 @@ need to write a new allocation function.
159160

160161
.. code-block:: python
161162
:linenos:
162-
:lineno-start: 9
163-
164-
gen_specs = GenSpecs(
165-
gen_f=gen_random_sample, # Our generator function
166-
out=[("x", float, (1,))], # gen_f output (name, type, size)
167-
user={
168-
"lower": np.array([-3]), # lower boundary for random sampling
169-
"upper": np.array([3]), # upper boundary for random sampling
170-
"gen_batch_size": 5, # number of x's gen_f generates per call
171-
},
172-
)
173-
174-
sim_specs = SimSpecs(
175-
sim_f=sim_find_sine, # Our simulator function
176-
inputs=["x"], # InputArray field names. "x" from gen_f output
177-
out=[("y", float)], # sim_f output. "y" = sine("x")
178-
)
163+
:lineno-start: 10
164+
165+
gen_specs = GenSpecs(
166+
gen_f=gen_random_sample, # Our generator function
167+
out=[("x", float, (1,))], # gen_f output (name, type, size)
168+
user={
169+
"lower": np.array([-3]), # lower boundary for random sampling
170+
"upper": np.array([3]), # upper boundary for random sampling
171+
"gen_batch_size": 5, # number of x's gen_f generates per call
172+
},
173+
)
174+
175+
sim_specs = SimSpecs(
176+
sim_f=sim_find_sine, # Our simulator function
177+
inputs=["x"], # InputArray field names. "x" from gen_f output
178+
out=[("y", float)], # sim_f output. "y" = sine("x")
179+
)
179180
180181
We then specify the circumstances where
181182
libEnsemble should stop execution in :ref:`ExitCriteria<datastruct-exit-criteria>`.
182183

183184
.. code-block:: python
184185
:linenos:
185-
:lineno-start: 25
186+
:lineno-start: 26
186187
187-
exit_criteria = ExitCriteria(sim_max=80) # Stop libEnsemble after 80 simulations
188+
exit_criteria = ExitCriteria(sim_max=80) # Stop libEnsemble after 80 simulations
188189
189190
Now we're ready to write our libEnsemble :doc:`libE<../programming_libE>`
190191
function call. :ref:`ensemble.H<funcguides-history>` is the final version of
191192
the history array. ``ensemble.flag`` should be zero if no errors occur.
192193

193194
.. code-block:: python
194195
:linenos:
195-
:lineno-start: 27
196+
:lineno-start: 28
196197
197-
ensemble = Ensemble(sim_specs, gen_specs, exit_criteria, libE_specs)
198-
ensemble.add_random_streams() # setup the random streams unique to each worker
199-
200-
if __name__ == "__main__": # Python-quirk required on macOS and windows
198+
ensemble = Ensemble(sim_specs, gen_specs, exit_criteria, libE_specs)
199+
ensemble.add_random_streams() # setup the random streams unique to each worker
201200
ensemble.run() # start the ensemble. Blocks until completion.
202201
203202
history = ensemble.H # start visualizing our results
@@ -246,6 +245,7 @@ need to write a new allocation function.
246245

247246
.. code-block:: python
248247
:linenos:
248+
:lineno-start: 37
249249
250250
import matplotlib.pyplot as plt
251251
@@ -278,37 +278,37 @@ need to write a new allocation function.
278278

279279
.. code-block:: python
280280
:linenos:
281-
:emphasize-lines: 13,14,15,25,32,33
281+
:emphasize-lines: 14,15,16,26,32,33
282282
283283
import numpy as np
284284
from libensemble import Ensemble
285285
from libensemble.specs import LibeSpecs, SimSpecs, GenSpecs, ExitCriteria
286286
from gen import gen_random_sample
287287
from sim import sim_find_sine
288288
289-
libE_specs = LibeSpecs(nworkers=4, comms="local")
290-
291-
gen_specs = GenSpecs(
292-
gen_f=gen_random_sample, # Our generator function
293-
out=[("x", float, (1,))], # gen_f output (name, type, size)
294-
user={
295-
"lower": np.array([-6]), # lower boundary for random sampling
296-
"upper": np.array([6]), # upper boundary for random sampling
297-
"gen_batch_size": 10, # number of x's gen_f generates per call
298-
},
299-
)
300-
301-
sim_specs = SimSpecs(
302-
sim_f=sim_find_sine, # Our simulator function
303-
inputs=["x"], # InputArray field names. "x" from gen_f output
304-
out=[("y", float)], # sim_f output. "y" = sine("x")
305-
)
306-
307-
exit_criteria = ExitCriteria(gen_max=160)
308-
309-
ensemble = Ensemble(sim_specs, gen_specs, exit_criteria, libE_specs)
310-
ensemble.add_random_streams()
311289
if __name__ == "__main__":
290+
libE_specs = LibeSpecs(nworkers=4, comms="local")
291+
292+
gen_specs = GenSpecs(
293+
gen_f=gen_random_sample, # Our generator function
294+
out=[("x", float, (1,))], # gen_f output (name, type, size)
295+
user={
296+
"lower": np.array([-6]), # lower boundary for random sampling
297+
"upper": np.array([6]), # upper boundary for random sampling
298+
"gen_batch_size": 10, # number of x's gen_f generates per call
299+
},
300+
)
301+
302+
sim_specs = SimSpecs(
303+
sim_f=sim_find_sine, # Our simulator function
304+
inputs=["x"], # InputArray field names. "x" from gen_f output
305+
out=[("y", float)], # sim_f output. "y" = sine("x")
306+
)
307+
308+
exit_criteria = ExitCriteria(gen_max=160)
309+
310+
ensemble = Ensemble(sim_specs, gen_specs, exit_criteria, libE_specs)
311+
ensemble.add_random_streams()
312312
ensemble.run()
313313
314314
if ensemble.flag != 0:
@@ -341,9 +341,9 @@ need to write a new allocation function.
341341

342342
.. code-block:: python
343343
:linenos:
344-
:lineno-start: 7
344+
:lineno-start: 8
345345
346-
# libE_specs = LibeSpecs(nworkers=4, comms="local")
346+
# libE_specs = LibeSpecs(nworkers=4, comms="local")
347347
348348
We'll be parameterizing our MPI runtime with a ``parse_args=True`` argument to
349349
the ``Ensemble`` class instead of ``libE_specs``. We'll also use an ``ensemble.is_manager``
@@ -353,15 +353,12 @@ need to write a new allocation function.
353353

354354
.. code-block:: python
355355
:linenos:
356-
:lineno-start: 27
357-
358-
# replace libE_specs with parse_args=True. Detects MPI runtime
359-
ensemble = Ensemble(sim_specs, gen_specs, exit_criteria, parse_args=True)
356+
:lineno-start: 28
360357
361-
ensemble.add_random_streams()
362-
363-
if __name__ == "__main__":
358+
# replace libE_specs with parse_args=True. Detects MPI runtime
359+
ensemble = Ensemble(sim_specs, gen_specs, exit_criteria, parse_args=True)
364360
361+
ensemble.add_random_streams()
365362
ensemble.run() # start the ensemble. Blocks until completion.
366363
367364
if ensemble.is_manager: # only True on rank 0

examples/tutorials/simple_sine/calling.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,29 @@
66
from libensemble import Ensemble
77
from libensemble.specs import ExitCriteria, GenSpecs, LibeSpecs, SimSpecs
88

9-
libE_specs = LibeSpecs(nworkers=4, comms="local")
10-
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
18-
},
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
28-
29-
ensemble = Ensemble(sim_specs, gen_specs, exit_criteria, libE_specs)
30-
ensemble.add_random_streams() # setup the random streams unique to each worker
31-
329
if __name__ == "__main__": # Python-quirk required on macOS and windows
10+
libE_specs = LibeSpecs(nworkers=4, comms="local")
11+
12+
gen_specs = GenSpecs(
13+
gen_f=gen_random_sample, # Our generator function
14+
out=[("x", float, (1,))], # gen_f output (name, type, size)
15+
user={
16+
"lower": np.array([-3]), # lower boundary for random sampling
17+
"upper": np.array([3]), # upper boundary for random sampling
18+
"gen_batch_size": 5, # number of x's gen_f generates per call
19+
},
20+
)
21+
22+
sim_specs = SimSpecs(
23+
sim_f=sim_find_sine, # Our simulator function
24+
inputs=["x"], # Input field names. "x" from gen_f output
25+
out=[("y", float)], # sim_f output. "y" = sine("x")
26+
)
27+
28+
exit_criteria = ExitCriteria(sim_max=80) # Stop libEnsemble after 80 simulations
29+
30+
ensemble = Ensemble(sim_specs, gen_specs, exit_criteria, libE_specs)
31+
ensemble.add_random_streams() # setup the random streams unique to each worker
3332
ensemble.run() # start the ensemble. Blocks until completion.
3433

3534
history = ensemble.H # start visualizing our results

examples/tutorials/simple_sine/calling_mpi.py

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,29 @@
66
from libensemble import Ensemble
77
from libensemble.specs import ExitCriteria, GenSpecs, SimSpecs
88

9-
# libE_specs = LibeSpecs(nworkers=4, comms="local")
10-
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
18-
},
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
28-
29-
ensemble = Ensemble(sim_specs, gen_specs, exit_criteria, parse_args=True)
30-
31-
ensemble.add_random_streams() # setup the random streams unique to each worker
32-
339
if __name__ == "__main__":
10+
# libE_specs = LibeSpecs(nworkers=4, comms="local")
11+
12+
gen_specs = GenSpecs(
13+
gen_f=gen_random_sample, # Our generator function
14+
out=[("x", float, (1,))], # gen_f output (name, type, size)
15+
user={
16+
"lower": np.array([-3]), # lower boundary for random sampling
17+
"upper": np.array([3]), # upper boundary for random sampling
18+
"gen_batch_size": 5, # number of x's gen_f generates per call
19+
},
20+
)
21+
22+
sim_specs = SimSpecs(
23+
sim_f=sim_find_sine, # Our simulator function
24+
inputs=["x"], # Input field names. "x" from gen_f output
25+
out=[("y", float)], # sim_f output. "y" = sine("x")
26+
)
27+
28+
exit_criteria = ExitCriteria(sim_max=80) # Stop libEnsemble after 80 simulations
29+
30+
ensemble = Ensemble(sim_specs, gen_specs, exit_criteria, parse_args=True)
31+
ensemble.add_random_streams() # setup the random streams unique to each worker
3432
ensemble.run() # start the ensemble. Blocks until completion.
3533

3634
if ensemble.is_manager: # only True on rank 0

0 commit comments

Comments
 (0)