|
1 | 1 | #!/usr/bin/env python
|
2 | 2 | import argparse
|
3 |
| -import sys |
| 3 | +# import sys |
4 | 4 |
|
5 |
| -from mpi4py import MPI |
| 5 | +# from mpi4py import MPI |
6 | 6 |
|
7 |
| -from vega import VegaInterface |
| 7 | +# from vega import VegaInterface |
8 | 8 |
|
9 | 9 | if __name__ == '__main__':
|
10 | 10 | pars = argparse.ArgumentParser(
|
11 | 11 | formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
12 | 12 | description='Run Vega in parallel.')
|
13 | 13 |
|
14 |
| - pars.add_argument('config', type=str, default=None, help='Config file') |
| 14 | + pars.add_argument('config', type=str, required=True, help='Config file') |
| 15 | + pars.add_argument( |
| 16 | + '-s', '--sampler', type=str, default='Polychord', required=False, |
| 17 | + choices=['Polychord', 'PocoMC'], help='Sampler to use' |
| 18 | + ) |
15 | 19 | args = pars.parse_args()
|
16 | 20 |
|
17 |
| - mpi_comm = MPI.COMM_WORLD |
18 |
| - cpu_rank = mpi_comm.Get_rank() |
19 |
| - |
20 |
| - def print_func(message): |
21 |
| - if cpu_rank == 0: |
22 |
| - print(message) |
23 |
| - sys.stdout.flush() |
24 |
| - mpi_comm.barrier() |
25 |
| - |
26 |
| - print_func('Initializing Vega') |
27 |
| - |
28 |
| - # Initialize Vega and get the sampling parameters |
29 |
| - vega = VegaInterface(args.config) |
30 |
| - sampling_params = vega.sample_params['limits'] |
31 |
| - |
32 |
| - print_func('Finished initializing Vega') |
33 |
| - |
34 |
| - # Check if we need the distortion |
35 |
| - use_distortion = vega.main_config['control'].getboolean('use_distortion', True) |
36 |
| - if not use_distortion: |
37 |
| - for key, data in vega.data.items(): |
38 |
| - data._distortion_mat = None |
39 |
| - test_model = vega.compute_model(vega.params, run_init=True) |
40 |
| - |
41 |
| - # Check if we need to run over a Monte Carlo mock |
42 |
| - run_montecarlo = vega.main_config['control'].getboolean('run_montecarlo', False) |
43 |
| - if run_montecarlo and vega.mc_config is not None: |
44 |
| - # Get the MC seed and forecast flag |
45 |
| - seed = vega.main_config['control'].getfloat('mc_seed', 0) |
46 |
| - forecast = vega.main_config['control'].getboolean('forecast', False) |
47 |
| - |
48 |
| - # Create the mocks |
49 |
| - vega.monte_carlo_sim(vega.mc_config['params'], seed=seed, forecast=forecast) |
50 |
| - |
51 |
| - # Set to sample the MC params |
52 |
| - sampling_params = vega.mc_config['sample']['limits'] |
53 |
| - print_func('Created Monte Carlo realization of the correlation') |
54 |
| - elif run_montecarlo: |
55 |
| - raise ValueError('You asked to run over a Monte Carlo simulation,' |
56 |
| - ' but no "[monte carlo]" section provided.') |
57 |
| - |
58 |
| - # Run sampler |
59 |
| - if not vega.run_sampler: |
60 |
| - raise ValueError('Warning: You called "run_vega_mpi.py" without asking' |
61 |
| - ' for the sampler. Add "run_sampler = True" to the "[control]" section.') |
62 |
| - |
63 |
| - if vega.sampler == 'Polychord': |
| 21 | + if args.sampler == 'Polychord': |
64 | 22 | from vega.samplers.polychord import Polychord
|
65 | 23 |
|
66 |
| - print_func('Running Polychord') |
67 |
| - sampler = Polychord(vega.main_config['Polychord'], sampling_params) |
68 |
| - sampler.run(vega.log_lik) |
69 |
| - |
70 |
| - elif vega.sampler == 'PocoMC': |
| 24 | + print('Running Polychord') |
| 25 | + sampler = Polychord(args.config) |
| 26 | + sampler.run() |
| 27 | + elif args.sampler == 'PocoMC': |
71 | 28 | from vega.samplers.pocomc import PocoMC
|
72 |
| - import pocomc |
73 |
| - from multiprocessing import Pool |
74 |
| - from schwimmbad import MPIPool |
75 |
| - |
76 |
| - print_func('Running PocoMC') |
77 |
| - sampler = PocoMC(vega.main_config['PocoMC'], sampling_params) |
78 |
| - # sampler.run(vega.log_lik) |
79 |
| - |
80 |
| - if not sampler.use_mpi: |
81 |
| - assert False |
82 |
| - |
83 |
| - def log_lik(theta): |
84 |
| - params = {name: val for name, val in zip(sampler.names, theta)} |
85 |
| - return vega.log_lik(params) |
86 |
| - |
87 |
| - mpi_comm.barrier() |
88 |
| - with MPIPool(mpi_comm) as pool: |
89 |
| - sampler.pocomc_sampler = pocomc.Sampler( |
90 |
| - sampler.prior, log_lik, |
91 |
| - pool=pool, output_dir=sampler.path, |
92 |
| - dynamic=sampler.dynamic, precondition=sampler.precondition, |
93 |
| - n_effective=sampler.n_effective, n_active=sampler.n_active, |
94 |
| - ) |
95 |
| - sampler.pocomc_sampler.run(sampler.n_total, sampler.n_evidence, save_every=sampler.save_every) |
96 |
| - |
97 |
| - # with Pool(sampler.num_cpu) as pool: |
98 |
| - # sampler.pocomc_sampler = pocomc.Sampler( |
99 |
| - # sampler.prior, log_lik, |
100 |
| - # pool=pool, output_dir=sampler.path, |
101 |
| - # dynamic=sampler.dynamic, precondition=sampler.precondition, |
102 |
| - # n_effective=sampler.n_effective, n_active=sampler.n_active, |
103 |
| - # ) |
104 |
| - # sampler.pocomc_sampler.run(sampler.n_total, sampler.n_evidence, save_every=sampler.save_every) |
105 |
| - |
106 |
| - # sampler.run(vega.log_lik) |
107 |
| - # mpi_comm.barrier() |
108 |
| - |
109 |
| - if cpu_rank == 0: |
110 |
| - sampler.write_chain() |
111 |
| - # mpi_comm.barrier() |
112 |
| - |
113 |
| - print(f'CPU #{cpu_rank}: Finished running sampler') |
| 29 | + |
| 30 | + print('Running PocoMC') |
| 31 | + sampler = PocoMC(args.config) |
| 32 | + sampler.run() |
| 33 | + |
| 34 | + # mpi_comm = MPI.COMM_WORLD |
| 35 | + # cpu_rank = mpi_comm.Get_rank() |
| 36 | + |
| 37 | + # def print_func(message): |
| 38 | + # if cpu_rank == 0: |
| 39 | + # print(message) |
| 40 | + # sys.stdout.flush() |
| 41 | + |
| 42 | + # print_func('Initializing Vega') |
| 43 | + |
| 44 | + # # Initialize Vega and get the sampling parameters |
| 45 | + # vega = VegaInterface(args.config) |
| 46 | + # sampling_params = vega.sample_params['limits'] |
| 47 | + |
| 48 | + # print_func('Finished initializing Vega') |
| 49 | + |
| 50 | + # # Check if we need the distortion |
| 51 | + # use_distortion = vega.main_config['control'].getboolean('use_distortion', True) |
| 52 | + # if not use_distortion: |
| 53 | + # for key, data in vega.data.items(): |
| 54 | + # data._distortion_mat = None |
| 55 | + # test_model = vega.compute_model(vega.params, run_init=True) |
| 56 | + |
| 57 | + # # Check if we need to run over a Monte Carlo mock |
| 58 | + # run_montecarlo = vega.main_config['control'].getboolean('run_montecarlo', False) |
| 59 | + # if run_montecarlo and vega.mc_config is not None: |
| 60 | + # # Get the MC seed and forecast flag |
| 61 | + # seed = vega.main_config['control'].getfloat('mc_seed', 0) |
| 62 | + # forecast = vega.main_config['control'].getboolean('forecast', False) |
| 63 | + |
| 64 | + # # Create the mocks |
| 65 | + # vega.monte_carlo_sim(vega.mc_config['params'], seed=seed, forecast=forecast) |
| 66 | + |
| 67 | + # # Set to sample the MC params |
| 68 | + # sampling_params = vega.mc_config['sample']['limits'] |
| 69 | + # print_func('Created Monte Carlo realization of the correlation') |
| 70 | + # elif run_montecarlo: |
| 71 | + # raise ValueError('You asked to run over a Monte Carlo simulation,' |
| 72 | + # ' but no "[monte carlo]" section provided.') |
| 73 | + |
| 74 | + # # Run sampler |
| 75 | + # if not vega.run_sampler: |
| 76 | + # raise ValueError('Warning: You called "run_vega_mpi.py" without asking' |
| 77 | + # ' for the sampler. Add "run_sampler = True" to the "[control]" section.') |
| 78 | + |
| 79 | + # if vega.sampler == 'Polychord': |
| 80 | + # from vega.samplers.polychord import Polychord |
| 81 | + |
| 82 | + # print_func('Running Polychord') |
| 83 | + # sampler = Polychord(vega.main_config['Polychord'], sampling_params) |
| 84 | + # sampler.run(vega.log_lik) |
| 85 | + |
| 86 | + # elif vega.sampler == 'PocoMC': |
| 87 | + # from vega.samplers.pocomc import PocoMC |
| 88 | + # import pocomc |
| 89 | + # from multiprocessing import Pool |
| 90 | + # from schwimmbad import MPIPool |
| 91 | + |
| 92 | + # print_func('Running PocoMC') |
| 93 | + # sampler = PocoMC(vega.main_config['PocoMC'], sampling_params) |
| 94 | + # # sampler.run(vega.log_lik) |
| 95 | + |
| 96 | + # if not sampler.use_mpi: |
| 97 | + # assert False |
| 98 | + |
| 99 | + # def log_lik(theta): |
| 100 | + # params = {name: val for name, val in zip(sampler.names, theta)} |
| 101 | + # return vega.log_lik(params) |
| 102 | + |
| 103 | + # mpi_comm.barrier() |
| 104 | + # with MPIPool(mpi_comm) as pool: |
| 105 | + # sampler.pocomc_sampler = pocomc.Sampler( |
| 106 | + # sampler.prior, log_lik, |
| 107 | + # pool=pool, output_dir=sampler.path, |
| 108 | + # dynamic=sampler.dynamic, precondition=sampler.precondition, |
| 109 | + # n_effective=sampler.n_effective, n_active=sampler.n_active, |
| 110 | + # ) |
| 111 | + # sampler.pocomc_sampler.run(sampler.n_total, sampler.n_evidence, save_every=sampler.save_every) |
| 112 | + |
| 113 | + # # with Pool(sampler.num_cpu) as pool: |
| 114 | + # # sampler.pocomc_sampler = pocomc.Sampler( |
| 115 | + # # sampler.prior, log_lik, |
| 116 | + # # pool=pool, output_dir=sampler.path, |
| 117 | + # # dynamic=sampler.dynamic, precondition=sampler.precondition, |
| 118 | + # # n_effective=sampler.n_effective, n_active=sampler.n_active, |
| 119 | + # # ) |
| 120 | + # # sampler.pocomc_sampler.run(sampler.n_total, sampler.n_evidence, save_every=sampler.save_every) |
| 121 | + |
| 122 | + # # sampler.run(vega.log_lik) |
| 123 | + # # mpi_comm.barrier() |
| 124 | + |
| 125 | + # if cpu_rank == 0: |
| 126 | + # sampler.write_chain() |
| 127 | + # # mpi_comm.barrier() |
| 128 | + |
| 129 | + # print(f'CPU #{cpu_rank}: Finished running sampler') |
0 commit comments