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
333
334
335
336
337
338
339
340
/* ----------------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
http://lammps.sandia.gov, Sandia National Laboratories
Steve Plimpton, sjplimp@sandia.gov
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#include "fix_balance.h"
#include <cstring>
#include "balance.h"
#include "update.h"
#include "atom.h"
#include "comm.h"
#include "domain.h"
#include "neighbor.h"
#include "irregular.h"
#include "force.h"
#include "kspace.h"
#include "modify.h"
#include "fix_store.h"
#include "rcb.h"
#include "error.h"
using namespace LAMMPS_NS;
using namespace FixConst;
enum{SHIFT,BISECTION};
/* ---------------------------------------------------------------------- */
FixBalance::FixBalance(LAMMPS *lmp, int narg, char **arg) :
Fix(lmp, narg, arg), balance(NULL), irregular(NULL)
{
if (narg < 6) error->all(FLERR,"Illegal fix balance command");
box_change_domain = 1;
scalar_flag = 1;
extscalar = 0;
vector_flag = 1;
size_vector = 3;
extvector = 0;
global_freq = 1;
// parse required arguments
int dimension = domain->dimension;
nevery = force->inumeric(FLERR,arg[3]);
if (nevery < 0) error->all(FLERR,"Illegal fix balance command");
thresh = force->numeric(FLERR,arg[4]);
if (strcmp(arg[5],"shift") == 0) lbstyle = SHIFT;
else if (strcmp(arg[5],"rcb") == 0) lbstyle = BISECTION;
else error->all(FLERR,"Illegal fix balance command");
int iarg = 5;
if (lbstyle == SHIFT) {
if (iarg+4 > narg) error->all(FLERR,"Illegal fix balance command");
if (strlen(arg[iarg+1]) > 3)
error->all(FLERR,"Illegal fix balance command");
strcpy(bstr,arg[iarg+1]);
nitermax = force->inumeric(FLERR,arg[iarg+2]);
if (nitermax <= 0) error->all(FLERR,"Illegal fix balance command");
stopthresh = force->numeric(FLERR,arg[iarg+3]);
if (stopthresh < 1.0) error->all(FLERR,"Illegal fix balance command");
iarg += 4;
} else if (lbstyle == BISECTION) {
iarg++;
}
// error checks
if (lbstyle == SHIFT) {
int blen = strlen(bstr);
for (int i = 0; i < blen; i++) {
if (bstr[i] != 'x' && bstr[i] != 'y' && bstr[i] != 'z')
error->all(FLERR,"Fix balance shift string is invalid");
if (bstr[i] == 'z' && dimension == 2)
error->all(FLERR,"Fix balance shift string is invalid");
for (int j = i+1; j < blen; j++)
if (bstr[i] == bstr[j])
error->all(FLERR,"Fix balance shift string is invalid");
}
}
if (lbstyle == BISECTION && comm->style == 0)
error->all(FLERR,"Fix balance rcb cannot be used with comm_style brick");
// create instance of Balance class
// if SHIFT, initialize it with params
// process remaining optional args via Balance
balance = new Balance(lmp);
if (lbstyle == SHIFT) balance->shift_setup(bstr,nitermax,thresh);
balance->options(iarg,narg,arg);
wtflag = balance->wtflag;
if (balance->varflag && nevery == 0)
error->all(FLERR,"Fix balance nevery = 0 cannot be used with weight var");
// create instance of Irregular class
irregular = new Irregular(lmp);
// only force reneighboring if nevery > 0
if (nevery) force_reneighbor = 1;
lastbalance = -1;
next_reneighbor = -1;
// compute initial outputs
itercount = 0;
pending = 0;
imbfinal = imbprev = maxloadperproc = 0.0;
}
/* ---------------------------------------------------------------------- */
FixBalance::~FixBalance()
{
delete balance;
delete irregular;
}
/* ---------------------------------------------------------------------- */
int FixBalance::setmask()
{
int mask = 0;
mask |= PRE_EXCHANGE;
mask |= PRE_NEIGHBOR;
return mask;
}
/* ---------------------------------------------------------------------- */
void FixBalance::post_constructor()
{
if (wtflag) balance->weight_storage(id);
}
/* ---------------------------------------------------------------------- */
void FixBalance::init()
{
if (force->kspace) kspace_flag = 1;
else kspace_flag = 0;
balance->init_imbalance(1);
}
/* ---------------------------------------------------------------------- */
void FixBalance::setup(int /*vflag*/)
{
// compute final imbalance factor if setup_pre_exchange() invoked balancer
// this is called at end of run setup, before output
pre_neighbor();
}
/* ---------------------------------------------------------------------- */
void FixBalance::setup_pre_exchange()
{
// do not allow rebalancing twice on same timestep
// even if wanted to, can mess up elapsed time in ImbalanceTime
if (update->ntimestep == lastbalance) return;
lastbalance = update->ntimestep;
// insure atoms are in current box & update box via shrink-wrap
// has to be be done before rebalance() invokes Irregular::migrate_atoms()
// since it requires atoms be inside simulation box
// even though pbc() will be done again in Verlet::run()
// no exchange() since doesn't matter if atoms are assigned to correct procs
if (domain->triclinic) domain->x2lamda(atom->nlocal);
domain->pbc();
domain->reset_box();
if (domain->triclinic) domain->lamda2x(atom->nlocal);
// perform a rebalance if threshold exceeded
balance->set_weights();
imbnow = balance->imbalance_factor(maxloadperproc);
if (imbnow > thresh) rebalance();
// next timestep to rebalance
if (nevery) next_reneighbor = (update->ntimestep/nevery)*nevery + nevery;
}
/* ----------------------------------------------------------------------
perform dynamic load balancing
------------------------------------------------------------------------- */
void FixBalance::pre_exchange()
{
// return if not a rebalance timestep
if (nevery && update->ntimestep < next_reneighbor) return;
// do not allow rebalancing twice on same timestep
// even if wanted to, can mess up elapsed time in ImbalanceTime
if (update->ntimestep == lastbalance) return;
lastbalance = update->ntimestep;
// insure atoms are in current box & update box via shrink-wrap
// no exchange() since doesn't matter if atoms are assigned to correct procs
if (domain->triclinic) domain->x2lamda(atom->nlocal);
domain->pbc();
domain->reset_box();
if (domain->triclinic) domain->lamda2x(atom->nlocal);
// perform a rebalance if threshold exceeded
// if weight variable is used, wrap weight setting in clear/add compute
if (balance->varflag) modify->clearstep_compute();
balance->set_weights();
if (balance->varflag) modify->addstep_compute(update->ntimestep + nevery);
imbnow = balance->imbalance_factor(maxloadperproc);
if (imbnow > thresh) rebalance();
// next timestep to rebalance
if (nevery) next_reneighbor = (update->ntimestep/nevery)*nevery + nevery;
}
/* ----------------------------------------------------------------------
compute final imbalance factor based on nlocal after comm->exchange()
only do this if rebalancing just occured
------------------------------------------------------------------------- */
void FixBalance::pre_neighbor()
{
if (!pending) return;
imbfinal = balance->imbalance_factor(maxloadperproc);
pending = 0;
// set disable = 1, so weights no longer migrate with atoms
if (wtflag) balance->fixstore->disable = 1;
}
/* ----------------------------------------------------------------------
perform dynamic load balancing
------------------------------------------------------------------------- */
void FixBalance::rebalance()
{
imbprev = imbnow;
// invoke balancer and reset comm->uniform flag
int *sendproc;
if (lbstyle == SHIFT) {
itercount = balance->shift();
comm->layout = Comm::LAYOUT_NONUNIFORM;
} else if (lbstyle == BISECTION) {
sendproc = balance->bisection();
comm->layout = Comm::LAYOUT_TILED;
}
// reset proc sub-domains
// check and warn if any proc's subbox is smaller than neigh skin
// since may lead to lost atoms in comm->exchange()
if (domain->triclinic) domain->set_lamda_box();
domain->set_local_box();
domain->subbox_too_small_check(neighbor->skin);
// output of new decomposition
if (balance->outflag) balance->dumpout(update->ntimestep);
// move atoms to new processors via irregular()
// for non-RCB only needed if migrate_check() says an atom moves too far
// else allow caller's comm->exchange() to do it
// set disable = 0, so weights migrate with atoms
// important to delay disable = 1 until after pre_neighbor imbfinal calc
// b/c atoms may migrate again in comm->exchange()
// NOTE: for reproducible debug runs, set 1st arg of migrate_atoms() to 1
if (domain->triclinic) domain->x2lamda(atom->nlocal);
if (wtflag) balance->fixstore->disable = 0;
if (lbstyle == BISECTION) irregular->migrate_atoms(0,1,sendproc);
else if (irregular->migrate_check()) irregular->migrate_atoms();
if (domain->triclinic) domain->lamda2x(atom->nlocal);
// invoke KSpace setup_grid() to adjust to new proc sub-domains
if (kspace_flag) force->kspace->setup_grid();
// pending triggers pre_neighbor() to compute final imbalance factor
// can only be done after atoms migrate in comm->exchange()
pending = 1;
}
/* ----------------------------------------------------------------------
return imbalance factor after last rebalance
------------------------------------------------------------------------- */
double FixBalance::compute_scalar()
{
return imbfinal;
}
/* ----------------------------------------------------------------------
return stats for last rebalance
------------------------------------------------------------------------- */
double FixBalance::compute_vector(int i)
{
if (i == 0) return maxloadperproc;
if (i == 1) return (double) itercount;
return imbprev;
}
/* ----------------------------------------------------------------------
return # of bytes of allocated memory
------------------------------------------------------------------------- */
double FixBalance::memory_usage()
{
double bytes = irregular->memory_usage();
if (balance->rcb) bytes += balance->rcb->memory_usage();
return bytes;
}