Allocation Functions
Below are example allocation functions available in libEnsemble.
Important
See the API for allocation functions here.
Note
The default allocation function is give_sim_work_first
.
give_sim_work_first
- give_sim_work_first.give_sim_work_first(W, H, sim_specs, gen_specs, alloc_specs, persis_info, libE_info)
Decide what should be given to workers. This allocation function gives any available simulation work first, and only when all simulations are completed or running does it start (at most
alloc_specs["user"]["num_active_gens"]
) generator instances.Allows for a
alloc_specs["user"]["batch_mode"]
where no generation work is given out unless all entries inH
are returned.Can give points in highest priority, if
"priority"
is a field inH
. If alloc_specs[“user”][“give_all_with_same_priority”] is set to True, then all points with the same priority value are given as a batch to the sim.Workers performing sims will be assigned resources given in H[“resource_sets”] this field exists, else defaulting to one. Workers performing gens are assigned resource_sets given by persis_info[“gen_resources”] or zero.
This is the default allocation function if one is not defined.
tags: alloc, default, batch, priority
See also
test_uniform_sampling.py # noqa
- Parameters:
W (ndarray[Any, dtype[_ScalarType_co]])
H (ndarray[Any, dtype[_ScalarType_co]])
sim_specs (dict)
gen_specs (dict)
alloc_specs (dict)
persis_info (dict)
libE_info (dict)
- Return type:
Tuple[dict]
give_sim_work_first.py
1import time
2from typing import Tuple
3
4import numpy as np
5import numpy.typing as npt
6
7from libensemble.tools.alloc_support import AllocSupport, InsufficientFreeResources
8
9
10def give_sim_work_first(
11 W: npt.NDArray,
12 H: npt.NDArray,
13 sim_specs: dict,
14 gen_specs: dict,
15 alloc_specs: dict,
16 persis_info: dict,
17 libE_info: dict,
18) -> Tuple[dict]:
19 """
20 Decide what should be given to workers. This allocation function gives any
21 available simulation work first, and only when all simulations are
22 completed or running does it start (at most ``alloc_specs["user"]["num_active_gens"]``)
23 generator instances.
24
25 Allows for a ``alloc_specs["user"]["batch_mode"]`` where no generation
26 work is given out unless all entries in ``H`` are returned.
27
28 Can give points in highest priority, if ``"priority"`` is a field in ``H``.
29 If alloc_specs["user"]["give_all_with_same_priority"] is set to True, then
30 all points with the same priority value are given as a batch to the sim.
31
32 Workers performing sims will be assigned resources given in H["resource_sets"]
33 this field exists, else defaulting to one. Workers performing gens are
34 assigned resource_sets given by persis_info["gen_resources"] or zero.
35
36 This is the default allocation function if one is not defined.
37
38 tags: alloc, default, batch, priority
39
40 .. seealso::
41 `test_uniform_sampling.py <https://github.com/Libensemble/libensemble/blob/develop/libensemble/tests/functionality_tests/test_uniform_sampling.py>`_ # noqa
42 """
43
44 user = alloc_specs.get("user", {})
45
46 if "cancel_sims_time" in user:
47 # Cancel simulations that are taking too long
48 rows = np.where(np.logical_and.reduce((H["sim_started"], ~H["sim_ended"], ~H["cancel_requested"])))[0]
49 inds = time.time() - H["sim_started_time"][rows] > user["cancel_sims_time"]
50 to_request_cancel = rows[inds]
51 for row in to_request_cancel:
52 H[row]["cancel_requested"] = True
53
54 if libE_info["sim_max_given"] or not libE_info["any_idle_workers"]:
55 return {}, persis_info
56
57 # Initialize alloc_specs["user"] as user.
58 batch_give = user.get("give_all_with_same_priority", False)
59 gen_in = gen_specs.get("in", [])
60
61 manage_resources = libE_info["use_resource_sets"]
62 support = AllocSupport(W, manage_resources, persis_info, libE_info)
63 gen_count = support.count_gens()
64 Work = {}
65
66 points_to_evaluate = ~H["sim_started"] & ~H["cancel_requested"]
67
68 if np.any(points_to_evaluate):
69 for wid in support.avail_worker_ids(gen_workers=False):
70 sim_ids_to_send = support.points_by_priority(H, points_avail=points_to_evaluate, batch=batch_give)
71 try:
72 Work[wid] = support.sim_work(wid, H, sim_specs["in"], sim_ids_to_send, persis_info.get(wid))
73 except InsufficientFreeResources:
74 break
75 points_to_evaluate[sim_ids_to_send] = False
76 if not np.any(points_to_evaluate):
77 break
78 else:
79 for wid in support.avail_worker_ids(gen_workers=True):
80 # Allow at most num_active_gens active generator instances
81 if gen_count >= user.get("num_active_gens", gen_count + 1):
82 break
83
84 # Do not start gen instances in batch mode if workers still working
85 if user.get("batch_mode") and not support.all_sim_ended(H):
86 break
87
88 # Give gen work
89 return_rows = range(len(H)) if gen_in else []
90 try:
91 Work[wid] = support.gen_work(wid, gen_in, return_rows, persis_info.get(wid))
92 except InsufficientFreeResources:
93 break
94 gen_count += 1
95
96 return Work, persis_info
fast_alloc
- fast_alloc.give_sim_work_first(W, H, sim_specs, gen_specs, alloc_specs, persis_info, libE_info)
This allocation function gives (in order) entries in
H
to idle workers to evaluate in the simulation function. The fields insim_specs["in"]
are given. If all entries in H have been given a be evaluated, a worker is told to call the generator function, provided this wouldn’t result in more thanalloc_specs["user"]["num_active_gen"]
active generators.This fast_alloc variation of give_sim_work_first is useful for cases that simply iterate through H, issuing evaluations in order and, in particular, is likely to be faster if there will be many short simulation evaluations, given that this function contains fewer column length operations.
tags: alloc, simple, fast
See also
test_fast_alloc.py # noqa
fast_alloc.py
1from libensemble.tools.alloc_support import AllocSupport, InsufficientFreeResources
2
3
4def give_sim_work_first(W, H, sim_specs, gen_specs, alloc_specs, persis_info, libE_info):
5 """
6 This allocation function gives (in order) entries in ``H`` to idle workers
7 to evaluate in the simulation function. The fields in ``sim_specs["in"]``
8 are given. If all entries in `H` have been given a be evaluated, a worker
9 is told to call the generator function, provided this wouldn't result in
10 more than ``alloc_specs["user"]["num_active_gen"]`` active generators.
11
12 This fast_alloc variation of give_sim_work_first is useful for cases that
13 simply iterate through H, issuing evaluations in order and, in particular,
14 is likely to be faster if there will be many short simulation evaluations,
15 given that this function contains fewer column length operations.
16
17 tags: alloc, simple, fast
18
19 .. seealso::
20 `test_fast_alloc.py <https://github.com/Libensemble/libensemble/blob/develop/libensemble/tests/functionality_tests/test_fast_alloc.py>`_ # noqa
21 """
22
23 if libE_info["sim_max_given"] or not libE_info["any_idle_workers"]:
24 return {}, persis_info
25
26 user = alloc_specs.get("user", {})
27 manage_resources = libE_info["use_resource_sets"]
28
29 support = AllocSupport(W, manage_resources, persis_info, libE_info)
30
31 gen_count = support.count_gens()
32 Work = {}
33 gen_in = gen_specs.get("in", [])
34
35 # Give sim work if possible
36 for wid in support.avail_worker_ids(gen_workers=False):
37 persis_info = support.skip_canceled_points(H, persis_info)
38 if persis_info["next_to_give"] < len(H):
39 try:
40 Work[wid] = support.sim_work(wid, H, sim_specs["in"], [persis_info["next_to_give"]], [])
41 except InsufficientFreeResources:
42 break
43 persis_info["next_to_give"] += 1
44
45 # Give gen work if possible
46 if persis_info["next_to_give"] >= len(H):
47 for wid in support.avail_worker_ids(gen_workers=True):
48 if wid not in Work and gen_count < user.get("num_active_gens", gen_count + 1):
49 return_rows = range(len(H)) if gen_in else []
50 try:
51 Work[wid] = support.gen_work(wid, gen_in, return_rows, persis_info.get(wid))
52 except InsufficientFreeResources:
53 break
54 gen_count += 1
55 persis_info["total_gen_calls"] += 1
56
57 return Work, persis_info
start_only_persistent
- start_only_persistent.only_persistent_gens(W, H, sim_specs, gen_specs, alloc_specs, persis_info, libE_info)
This allocation function will give simulation work if possible, but otherwise start up to
alloc_specs["user"]["num_active_gens"]
persistent generators (defaulting to one).By default, evaluation results are given back to the generator once all generated points have been returned from the simulation evaluation. If
alloc_specs["user"]["async_return"]
is set to True, then any returned points are given back to the generator.If any workers are marked as zero_resource_workers, then these will only be used for generators.
If any of the persistent generators has exited, then ensemble shutdown is triggered.
User options:
To be provided in calling script: E.g.,
alloc_specs["user"]["async_return"] = True
- init_sample_size: int, optional
Initial sample size - always return in batch. Default: 0
- num_active_gens: int, optional
Maximum number of persistent generators to start. Default: 1
- async_return: Boolean, optional
Return results to gen as they come in (after sample). Default: False (batch return).
- active_recv_gen: Boolean, optional
Create gen in active receive mode. If True, the manager does not need to wait for a return from the generator before sending further returned points. Default: False
tags: alloc, batch, async, persistent, priority
See also
test_persistent_uniform_sampling.py # noqa test_persistent_uniform_sampling_async.py # noqa test_persistent_surmise_calib.py # noqa test_persistent_uniform_gen_decides_stop.py # noqa
start_only_persistent.py
1import numpy as np
2
3from libensemble.message_numbers import EVAL_GEN_TAG, EVAL_SIM_TAG
4from libensemble.tools.alloc_support import AllocSupport, InsufficientFreeResources
5
6
7def only_persistent_gens(W, H, sim_specs, gen_specs, alloc_specs, persis_info, libE_info):
8 """
9 This allocation function will give simulation work if possible, but
10 otherwise start up to ``alloc_specs["user"]["num_active_gens"]``
11 persistent generators (defaulting to one).
12
13 By default, evaluation results are given back to the generator once
14 all generated points have been returned from the simulation evaluation.
15 If ``alloc_specs["user"]["async_return"]`` is set to True, then any
16 returned points are given back to the generator.
17
18 If any workers are marked as zero_resource_workers, then these will only
19 be used for generators.
20
21 If any of the persistent generators has exited, then ensemble shutdown
22 is triggered.
23
24 **User options**:
25
26 To be provided in calling script: E.g., ``alloc_specs["user"]["async_return"] = True``
27
28 init_sample_size: int, optional
29 Initial sample size - always return in batch. Default: 0
30
31 num_active_gens: int, optional
32 Maximum number of persistent generators to start. Default: 1
33
34 async_return: Boolean, optional
35 Return results to gen as they come in (after sample). Default: False (batch return).
36
37 active_recv_gen: Boolean, optional
38 Create gen in active receive mode. If True, the manager does not need to wait
39 for a return from the generator before sending further returned points.
40 Default: False
41
42 tags: alloc, batch, async, persistent, priority
43
44 .. seealso::
45 `test_persistent_uniform_sampling.py <https://github.com/Libensemble/libensemble/blob/develop/libensemble/tests/functionality_tests/test_persistent_uniform_sampling.py>`_ # noqa
46 `test_persistent_uniform_sampling_async.py <https://github.com/Libensemble/libensemble/blob/develop/libensemble/tests/functionality_tests/test_persistent_uniform_sampling_async.py>`_ # noqa
47 `test_persistent_surmise_calib.py <https://github.com/Libensemble/libensemble/blob/develop/libensemble/tests/regression_tests/test_persistent_surmise_calib.py>`_ # noqa
48 `test_persistent_uniform_gen_decides_stop.py <https://github.com/Libensemble/libensemble/blob/develop/libensemble/tests/functionality_tests/test_persistent_uniform_gen_decides_stop.py>`_ # noqa
49 """
50
51 if libE_info["sim_max_given"] or not libE_info["any_idle_workers"]:
52 return {}, persis_info
53
54 # Initialize alloc_specs["user"] as user.
55 user = alloc_specs.get("user", {})
56 manage_resources = libE_info["use_resource_sets"]
57
58 active_recv_gen = user.get("active_recv_gen", False) # Persistent gen can handle irregular communications
59 init_sample_size = user.get("init_sample_size", 0) # Always batch return until this many evals complete
60 batch_give = user.get("give_all_with_same_priority", False)
61
62 support = AllocSupport(W, manage_resources, persis_info, libE_info)
63 gen_count = support.count_persis_gens()
64 Work = {}
65
66 # Asynchronous return to generator
67 async_return = user.get("async_return", False) and sum(H["sim_ended"]) >= init_sample_size
68
69 if gen_count < persis_info.get("num_gens_started", 0):
70 # When a persistent worker is done, trigger a shutdown (returning exit condition of 1)
71 return Work, persis_info, 1
72
73 # Give evaluated results back to a running persistent gen
74 for wid in support.avail_worker_ids(persistent=EVAL_GEN_TAG, active_recv=active_recv_gen):
75 gen_inds = H["gen_worker"] == wid
76 returned_but_not_given = np.logical_and.reduce((H["sim_ended"], ~H["gen_informed"], gen_inds))
77 if np.any(returned_but_not_given):
78 if async_return or support.all_sim_ended(H, gen_inds):
79 point_ids = np.where(returned_but_not_given)[0]
80 Work[wid] = support.gen_work(
81 wid,
82 gen_specs["persis_in"],
83 point_ids,
84 persis_info.get(wid),
85 persistent=True,
86 active_recv=active_recv_gen,
87 )
88 returned_but_not_given[point_ids] = False
89
90 # Now the give_sim_work_first part
91 points_to_evaluate = ~H["sim_started"] & ~H["cancel_requested"]
92 avail_workers = support.avail_worker_ids(persistent=False, zero_resource_workers=False, gen_workers=False)
93 if user.get("alt_type"):
94 avail_workers = list(
95 set(support.avail_worker_ids(persistent=False, zero_resource_workers=False))
96 | set(support.avail_worker_ids(persistent=EVAL_SIM_TAG, zero_resource_workers=False))
97 )
98 for wid in avail_workers:
99 if not np.any(points_to_evaluate):
100 break
101
102 sim_ids_to_send = support.points_by_priority(H, points_avail=points_to_evaluate, batch=batch_give)
103
104 try:
105 if user.get("alt_type"):
106 Work[wid] = support.sim_work(
107 wid, H, sim_specs["in"], sim_ids_to_send, persis_info.get(wid), persistent=True
108 )
109 else:
110 Work[wid] = support.sim_work(wid, H, sim_specs["in"], sim_ids_to_send, persis_info.get(wid))
111 except InsufficientFreeResources:
112 break
113
114 points_to_evaluate[sim_ids_to_send] = False
115
116 # Start persistent gens if no worker to give out. Uses zero_resource_workers if defined.
117 if not np.any(points_to_evaluate):
118 avail_workers = support.avail_worker_ids(persistent=False, zero_resource_workers=True, gen_workers=True)
119
120 for wid in avail_workers:
121 if gen_count < user.get("num_active_gens", 1):
122 # Finally, start a persistent generator as there is nothing else to do.
123 try:
124 Work[wid] = support.gen_work(
125 wid,
126 gen_specs.get("in", []),
127 range(len(H)),
128 persis_info.get(wid),
129 persistent=True,
130 active_recv=active_recv_gen,
131 )
132 except InsufficientFreeResources:
133 break
134
135 persis_info["num_gens_started"] = persis_info.get("num_gens_started", 0) + 1
136 gen_count += 1
137
138 return Work, persis_info, 0
start_persistent_local_opt_gens
- start_persistent_local_opt_gens.start_persistent_local_opt_gens(W, H, sim_specs, gen_specs, alloc_specs, persis_info, libE_info)
This allocation function will do the following:
Start up a persistent generator that is a local opt run at the first point identified by APOSMM’s decide_where_to_start_localopt. Note, it will do this only if at least one worker will be left to perform simulation evaluations.
If multiple starting points are available, the one with smallest function value is chosen.
If no candidate starting points exist, points from existing runs will be evaluated (oldest first).
If no points are left, call the generation function.
tags: alloc, persistent, aposmm
See also
test_uniform_sampling_then_persistent_localopt_runs.py # noqa