|
| 1 | +""" |
| 2 | +Test libEnsemble's capability to use no gen_f and instead coordinates the |
| 3 | +evaluation of an existing set of points. |
| 4 | +
|
| 5 | +Execute via one of the following commands (e.g. 3 workers): |
| 6 | + mpiexec -np 4 python test_evaluate_existing_sample.py |
| 7 | + python test_evaluate_existing_sample.py --nworkers 3 --comms local |
| 8 | + python test_evaluate_existing_sample.py --nworkers 3 --comms tcp |
| 9 | +
|
| 10 | +The number of concurrent evaluations of the objective function will be 4-1=3. |
| 11 | +""" |
| 12 | + |
| 13 | +# Do not change these lines - they are parsed by run-tests.sh |
| 14 | +# TESTSUITE_COMMS: local |
| 15 | +# TESTSUITE_NPROCS: 4 |
| 16 | +# TESTSUITE_OS_SKIP: OSX WIN |
| 17 | +# TESTSUITE_EXTRA: true |
| 18 | + |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | +import numpy as np |
| 22 | + |
| 23 | +# Import libEnsemble items for this test |
| 24 | +from libensemble import Ensemble |
| 25 | +from libensemble.alloc_funcs.give_pregenerated_work import give_pregenerated_sim_work as alloc_f |
| 26 | +from libensemble.sim_funcs.borehole import gen_borehole_input |
| 27 | +from libensemble.specs import AllocSpecs, ExitCriteria, SimSpecs, input_fields, output_data |
| 28 | + |
| 29 | + |
| 30 | +def insert_proxy(H0): |
| 31 | + from proxystore.connectors.redis import RedisConnector |
| 32 | + from proxystore.store import Store, get_store |
| 33 | + |
| 34 | + store = Store( |
| 35 | + "my-store", |
| 36 | + RedisConnector(hostname="localhost", port=6379), |
| 37 | + register=True, |
| 38 | + ) |
| 39 | + |
| 40 | + store = get_store("my-store") |
| 41 | + picture = Path("libE_logo.png").read_bytes() |
| 42 | + proxy = store.proxy(picture) |
| 43 | + for i in range(len(H0)): |
| 44 | + H0[i]["proxy"] = proxy |
| 45 | + |
| 46 | + |
| 47 | +def check_H(H): |
| 48 | + from proxystore.proxy import Proxy |
| 49 | + |
| 50 | + assert all([isinstance(H[i]["proxy"], Proxy) for i in range(len(H))]) |
| 51 | + |
| 52 | + |
| 53 | +@input_fields(["x", "proxy"]) |
| 54 | +@output_data([("f", float)]) |
| 55 | +def one_d_example(x, persis_info, sim_specs, info): |
| 56 | + |
| 57 | + H_o = np.zeros(1, dtype=sim_specs["out"]) |
| 58 | + H_o["f"] = np.linalg.norm(x["x"]) |
| 59 | + picture = bytes(x["proxy"][0]) |
| 60 | + |
| 61 | + sim_id = info["H_rows"][0] |
| 62 | + worker_id = info["workerID"] |
| 63 | + |
| 64 | + Path(f"logo_id{sim_id}_worker{worker_id}.png").write_bytes(picture) |
| 65 | + |
| 66 | + return H_o, persis_info |
| 67 | + |
| 68 | + |
| 69 | +# Main block is necessary only when using local comms with spawn start method (default on macOS and Windows). |
| 70 | +if __name__ == "__main__": |
| 71 | + |
| 72 | + n_samp = 4 |
| 73 | + H0 = np.zeros(n_samp, dtype=[("x", float, 8), ("sim_id", int), ("sim_started", bool), ("proxy", object)]) |
| 74 | + np.random.seed(0) |
| 75 | + H0["x"] = gen_borehole_input(n_samp) |
| 76 | + H0["sim_id"] = range(n_samp) |
| 77 | + H0["sim_started"] = False |
| 78 | + insert_proxy(H0) |
| 79 | + |
| 80 | + sampling = Ensemble(parse_args=True) |
| 81 | + sampling.H0 = H0 |
| 82 | + sampling.sim_specs = SimSpecs(sim_f=one_d_example) |
| 83 | + sampling.alloc_specs = AllocSpecs(alloc_f=alloc_f) |
| 84 | + sampling.exit_criteria = ExitCriteria(sim_max=len(H0)) |
| 85 | + sampling.run() |
| 86 | + |
| 87 | + if sampling.is_manager: |
| 88 | + assert len(sampling.H) == len(H0) |
| 89 | + assert np.array_equal(H0["x"], sampling.H["x"]) |
| 90 | + assert np.all(sampling.H["sim_ended"]) |
| 91 | + check_H(sampling.H) |
| 92 | + print("\nlibEnsemble correctly didn't add anything to initial sample") |
| 93 | + sampling.save_output(__file__) |
0 commit comments