-
Notifications
You must be signed in to change notification settings - Fork 4
/
mccrun.cpp
332 lines (277 loc) · 10 KB
/
mccrun.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* Copyright (c) 2013, Robert Rueger <[email protected]>
*
* This file is part of hVMC.
*
* hVMC is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* hVMC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with hVMC. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mccrun.hpp"
#include <random>
#include <memory>
#include <vector>
#include <functional>
#include <numeric>
#include <boost/mpi/collectives.hpp>
#include "mccrun_prepare.hpp"
#include "modman.hpp"
#include "obs_all.hpp"
#include "msgtags.hpp"
using namespace std;
namespace mpi = boost::mpi;
MCCResults mccrun_master(
const Options& opts, const Eigen::VectorXd& vpar, unsigned int num_bins,
const set<observables_t>& obs, const mpi::communicator& mpicomm,
boost::optional<const Eigen::VectorXi&> pconf_init )
{
cout << "========== NEW MONTE CARLO CYCLE ==========" << endl;
cout << ":: Preparing the simulation" << endl;
ModelManager model = prepare_model( opts, vpar, mpicomm, pconf_init );
if ( pconf_init && opts.count("verbose") ) {
if ( model.check_proposed_pconf_accepted() ) {
cout << " -> Master: proposed initial configuration accepted!" << endl;
} else {
cout << " -> Master: proposed initial configuration rejected!" << endl;
}
}
vector< unique_ptr<Observable> > obscalc = prepare_obscalcs( obs, opts );
ObservableCache obscache;
unsigned int finished_workers = 0;
unsigned int scheduled_bins = 0;
unsigned int completed_bins = 0;
unsigned int enqueued_bins = num_bins;
// define procedure to query the slaves for new work requests
function<void()> mpiquery_work_requests( [&]() {
while ( boost::optional<mpi::status> status
= mpicomm.iprobe( mpi::any_source, MSGTAG_S_M_REQUEST_BINS ) ) {
// receive the request and hand out new bins to the source
mpicomm.recv( status->source(), MSGTAG_S_M_REQUEST_BINS );
if ( enqueued_bins > 0 ) {
mpicomm.send( status->source(), MSGTAG_M_S_DISPATCHED_BINS, 1 );
scheduled_bins += 1;
enqueued_bins -= 1;
} else {
mpicomm.send( status->source(), MSGTAG_M_S_DISPATCHED_BINS, 0 );
++finished_workers;
}
}
} );
// define procedure to query the slaves for finished work
function<void()> mpiquery_finished_work( [&]() {
while ( boost::optional<mpi::status> status
= mpicomm.iprobe( mpi::any_source, 2 ) ) {
mpicomm.recv( status->source(), 2 );
--scheduled_bins;
++completed_bins;
}
} );
cout << ":: Equilibrating the system" << endl;
unsigned int equil_mcs = opts["calc.num-mcs-equil"].as<unsigned int>();
if ( pconf_init && model.check_proposed_pconf_accepted() == true ) {
// the proposed initial configuration was accepted, lets assume that the
// initial state is already a good one and reduce the number of mcs required
// for equilibration!
equil_mcs /= 10;
}
for ( unsigned int mcs = 1; mcs <= equil_mcs; ++mcs ) {
// take care of the slaves
mpiquery_finished_work();
mpiquery_work_requests();
// perform a Monte Carlo step
model.mcs();
// progress indicator
if ( opts.count("verbose") ) {
cout << '\r' << " MCS " << mcs << "/" << equil_mcs << " (on master)";
cout.flush();
}
}
if ( opts.count("verbose") ) {
cout << endl;
}
model.finalize_equilibration();
unsigned int completed_bins_master = 0;
cout << ":: Performing Monte Carlo cycle" << endl;
cout << endl;
cout << " Progress:" << endl;
while ( enqueued_bins > 0 ) {
cout << '\r' << " Bin "
<< completed_bins << "/" << num_bins;
cout.flush();
--enqueued_bins;
++scheduled_bins;
for (
unsigned int mcs = 0;
mcs < opts["calc.num-binmcs"].as<unsigned int>();
++mcs ) {
// take care of the slaves
mpiquery_finished_work();
mpiquery_work_requests();
// perform a Monte Carlo step
model.mcs();
// measure observables
for ( const unique_ptr<Observable>& o : obscalc ) {
o->measure( model, obscache );
}
obscache.clear();
}
// tell the observables that a bin has been completed
for ( const unique_ptr<Observable>& o : obscalc ) {
o->completebin();
}
--scheduled_bins;
++completed_bins_master;
++completed_bins;
}
++finished_workers;
while ( completed_bins != num_bins ||
static_cast<int>( finished_workers ) < mpicomm.size() ) {
if ( boost::optional<mpi::status> status
= mpicomm.iprobe( mpi::any_source, MSGTAG_S_M_FINISHED_BINS ) ) {
mpicomm.recv( status->source(), MSGTAG_S_M_FINISHED_BINS );
--scheduled_bins;
++completed_bins;
cout << '\r' << " Bin " << completed_bins << "/" << num_bins;
cout.flush();
}
if ( boost::optional<mpi::status> status
= mpicomm.iprobe( mpi::any_source, MSGTAG_S_M_REQUEST_BINS ) ) {
// receive the request for more work
mpicomm.recv( status->source(), MSGTAG_S_M_REQUEST_BINS );
// tell him there is no more work
mpicomm.send( status->source(), MSGTAG_M_S_DISPATCHED_BINS, 0 );
++finished_workers;
}
}
assert( enqueued_bins == 0 );
assert( scheduled_bins == 0 );
cout << '\r' << " Bin " << completed_bins << "/" << num_bins << endl;
cout.flush();
// check for floating point precision problems
cout << endl;
cout << " Floating point precision control" << endl;
vector<FPDevStat> W_devstats;
assert( mpicomm.rank() == 0 );
mpi::gather( mpicomm, model.get_W_devstat(), W_devstats, 0 );
FPDevStat W_devstat_combined =
accumulate(
W_devstats.begin(), W_devstats.end(),
FPDevStat( opts["fpctrl.W-deviation-target"].as<double>() )
);
cout << " W: " << W_devstat_combined.recalcs
<< "/" << W_devstat_combined.misses
<< "/" << W_devstat_combined.mag1_misses << endl;
vector<FPDevStat> T_devstats;
assert( mpicomm.rank() == 0 );
mpi::gather( mpicomm, model.get_T_devstat(), T_devstats, 0 );
FPDevStat T_devstat_combined =
accumulate(
T_devstats.begin(), T_devstats.end(),
FPDevStat( opts["fpctrl.T-deviation-target"].as<double>() )
);
cout << " T: " << T_devstat_combined.recalcs
<< "/" << T_devstat_combined.misses
<< "/" << T_devstat_combined.mag1_misses << endl;
if ( W_devstat_combined.mag1_misses > 0 ||
T_devstat_combined.mag1_misses > 0 ) {
cout << " Precision targets missed by more than an order of magnitude!" << endl
<< " WARNING: Your results might be unreliable!!!" << endl << endl;
} else if ( W_devstat_combined.misses > 0 ||
T_devstat_combined.misses > 0 ) {
cout << " Some precision targets were missed, but your results should be fine."
<< endl << endl;
} else {
cout << " No missed precision targets." << endl << endl;
}
// collect results from the slaves and return results the scheduler
MCCResults results;
for ( const unique_ptr<Observable>& o : obscalc ) {
o->collect_and_write_results( mpicomm, results );
}
return results;
}
void mccrun_slave(
const Options& opts, const Eigen::VectorXd& vpar,
const set<observables_t>& obs, const mpi::communicator& mpicomm,
boost::optional<const Eigen::VectorXi&> pconf_init )
{
// prepare the simulation
ModelManager model = prepare_model( opts, vpar, mpicomm, pconf_init );
vector< unique_ptr<Observable> > obscalc = prepare_obscalcs( obs, opts );
ObservableCache obscache;
// equilibrate the system
unsigned int equil_mcs = opts["calc.num-mcs-equil"].as<unsigned int>();
if ( pconf_init && model.check_proposed_pconf_accepted() == true ) {
// same procedure as for master ...
equil_mcs /= 10;
}
for ( unsigned int mcs = 1; mcs <= equil_mcs; ++mcs ) {
model.mcs();
}
model.finalize_equilibration();
// run this slaves part of the Monte Carlo cycle
unsigned int completed_bins_thisslave = 0;
bool master_out_of_work = false;
unsigned int scheduled_bins_thisslave;
mpicomm.send( 0, MSGTAG_S_M_REQUEST_BINS );
mpicomm.recv( 0, MSGTAG_M_S_DISPATCHED_BINS, scheduled_bins_thisslave );
master_out_of_work = ( scheduled_bins_thisslave == 0 );
while ( scheduled_bins_thisslave > 0 ) {
unsigned int new_scheduled_bins_thisslave;
mpi::request master_answer;
if ( !master_out_of_work ) {
// ask the master for more work
mpicomm.send( 0, MSGTAG_S_M_REQUEST_BINS );
master_answer = mpicomm.irecv(
0, MSGTAG_M_S_DISPATCHED_BINS,
new_scheduled_bins_thisslave
);
}
for (
unsigned int mcs = 0;
mcs < opts["calc.num-binmcs"].as<unsigned int>();
++mcs )
{
// perform a Monte Carlo step
model.mcs();
// measure observables
for ( const unique_ptr<Observable>& o : obscalc ) {
o->measure( model, obscache );
}
obscache.clear();
}
// tell the observables that a bin has been completed
for ( const unique_ptr<Observable>& o : obscalc ) {
o->completebin();
}
// report completion of the work
mpicomm.send( 0, 2 );
++completed_bins_thisslave;
--scheduled_bins_thisslave;
if ( !master_out_of_work ) {
// wait for answer from master concerning the next bin
master_answer.wait();
if ( new_scheduled_bins_thisslave == 1 ) {
++scheduled_bins_thisslave;
} else {
master_out_of_work = true;
}
}
}
// send floating point precision control data to master
mpi::gather( mpicomm, model.get_W_devstat(), 0 );
mpi::gather( mpicomm, model.get_T_devstat(), 0 );
// send observables to master
for ( const unique_ptr<Observable>& o : obscalc ) {
o->send_results_to_master( mpicomm );
}
}