git-svn-id: svn://svn.icms.temple.edu/lammps-ro/trunk@16053 f3b2605a-c512-4ea7-a41b...
[lammps.git] / doc / src / balance.txt
blobb9d36452a66b51abc3487536c8ea93ecdff7f47d
1 "LAMMPS WWW Site"_lws - "LAMMPS Documentation"_ld - "LAMMPS Commands"_lc :c
3 :link(lws,http://lammps.sandia.gov)
4 :link(ld,Manual.html)
5 :link(lc,Section_commands.html#comm)
7 :line
9 balance command :h3
11 [Syntax:]
13 balance thresh style args ... keyword args ... :pre
15 thresh = imbalance threshhold that must be exceeded to perform a re-balance :ulb,l
16 one style/arg pair can be used (or multiple for {x},{y},{z}) :l
17 style = {x} or {y} or {z} or {shift} or {rcb} :l
18   {x} args = {uniform} or Px-1 numbers between 0 and 1
19     {uniform} = evenly spaced cuts between processors in x dimension
20     numbers = Px-1 ascending values between 0 and 1, Px - # of processors in x dimension
21     {x} can be specified together with {y} or {z}
22   {y} args = {uniform} or Py-1 numbers between 0 and 1
23     {uniform} = evenly spaced cuts between processors in y dimension
24     numbers = Py-1 ascending values between 0 and 1, Py - # of processors in y dimension
25     {y} can be specified together with {x} or {z}
26   {z} args = {uniform} or Pz-1 numbers between 0 and 1
27     {uniform} = evenly spaced cuts between processors in z dimension
28     numbers = Pz-1 ascending values between 0 and 1, Pz - # of processors in z dimension
29     {z} can be specified together with {x} or {y}
30   {shift} args = dimstr Niter stopthresh
31     dimstr = sequence of letters containing "x" or "y" or "z", each not more than once
32     Niter = # of times to iterate within each dimension of dimstr sequence
33     stopthresh = stop balancing when this imbalance threshhold is reached
34   {rcb} args = none :pre
35 zero or more keyword/arg pairs may be appended :l
36 keyword = {weight} or {out} :l
37   {weight} style args = use weighted particle counts for the balancing
38     {style} = {group} or {neigh} or {time} or {var} or {store}
39       {group} args = Ngroup group1 weight1 group2 weight2 ...
40         Ngroup = number of groups with assigned weights
41         group1, group2, ... = group IDs
42         weight1, weight2, ...   = corresponding weight factors
43       {neigh} factor = compute weight based on number of neighbors
44         factor = scaling factor (> 0)
45       {time} factor = compute weight based on time spend computing
46         factor = scaling factor (> 0)
47       {var} name = take weight from atom-style variable
48         name = name of the atom-style variable
49       {store} name = store weight in custom atom property defined by "fix property/atom"_fix_property_atom.html command
50         name = atom property name (without d_ prefix)
51   {out} arg = filename
52     filename = write each processor's sub-domain to a file :pre
53 :ule
55 [Examples:]
57 balance 0.9 x uniform y 0.4 0.5 0.6
58 balance 1.2 shift xz 5 1.1
59 balance 1.0 shift xz 5 1.1
60 balance 1.1 rcb
61 balance 1.0 shift x 10 1.1 weight group 2 fast 0.5 slow 2.0
62 balance 1.0 shift x 10 1.1 weight time 0.8 weight neigh 0.5 weight store balance
63 balance 1.0 shift x 20 1.0 out tmp.balance :pre
65 [Description:]
67 This command adjusts the size and shape of processor sub-domains
68 within the simulation box, to attempt to balance the number of atoms
69 or particles and thus indirectly the computational cost (load) more
70 evenly across processors.  The load balancing is "static" in the sense
71 that this command performs the balancing once, before or between
72 simulations.  The processor sub-domains will then remain static during
73 the subsequent run.  To perform "dynamic" balancing, see the "fix
74 balance"_fix_balance.html command, which can adjust processor
75 sub-domain sizes and shapes on-the-fly during a "run"_run.html.
77 Load-balancing is typically most useful if the particles in the
78 simulation box have a spatially-varying density distribution or when
79 the computational cost varies signficantly between different
80 particles.  E.g. a model of a vapor/liquid interface, or a solid with
81 an irregular-shaped geometry containing void regions, or "hybrid pair
82 style simulations"_pair_hybrid.html which combine pair styles with
83 different computational cost.  In these cases, the LAMMPS default of
84 dividing the simulation box volume into a regular-spaced grid of 3d
85 bricks, with one equal-volume sub-domain per procesor, may assign
86 numbers of particles per processor in a way that the computational
87 effort varies significantly.  This can lead to poor performance when
88 the simulation is run in parallel.
90 The balancing can be performed with or without per-particle weighting.
91 With no weighting, the balancing attempts to assign an equal number of
92 particles to each processor.  With weighting, the balancing attempts
93 to assign an equal aggregate computational weight to each processor,
94 which typically inducces a diffrent number of atoms assigned to each
95 processor.  Details on the various weighting options and examples for
96 how they can be used are "given below"_#weighted_balance.
98 Note that the "processors"_processors.html command allows some control
99 over how the box volume is split across processors.  Specifically, for
100 a Px by Py by Pz grid of processors, it allows choice of Px, Py, and
101 Pz, subject to the constraint that Px * Py * Pz = P, the total number
102 of processors.  This is sufficient to achieve good load-balance for
103 some problems on some processor counts.  However, all the processor
104 sub-domains will still have the same shape and same volume.
106 The requested load-balancing operation is only performed if the
107 current "imbalance factor" in particles owned by each processor
108 exceeds the specified {thresh} parameter.  The imbalance factor is
109 defined as the maximum number of particles (or weight) owned by any
110 processor, divided by the average number of particles (or weight) per
111 processor.  Thus an imbalance factor of 1.0 is perfect balance.
113 As an example, for 10000 particles running on 10 processors, if the
114 most heavily loaded processor has 1200 particles, then the factor is
115 1.2, meaning there is a 20% imbalance.  Note that a re-balance can be
116 forced even if the current balance is perfect (1.0) be specifying a
117 {thresh} < 1.0.
119 NOTE: Balancing is performed even if the imbalance factor does not
120 exceed the {thresh} parameter if a "grid" style is specified when the
121 current partitioning is "tiled".  The meaning of "grid" vs "tiled" is
122 explained below.  This is to allow forcing of the partitioning to
123 "grid" so that the "comm_style brick"_comm_style.html command can then
124 be used to replace a current "comm_style tiled"_comm_style.html
125 setting.
127 When the balance command completes, it prints statistics about the
128 result, including the change in the imbalance factor and the change in
129 the maximum number of particles on any processor.  For "grid" methods
130 (defined below) that create a logical 3d grid of processors, the
131 positions of all cutting planes in each of the 3 dimensions (as
132 fractions of the box length) are also printed.
134 NOTE: This command attempts to minimize the imbalance factor, as
135 defined above.  But depending on the method a perfect balance (1.0)
136 may not be achieved.  For example, "grid" methods (defined below) that
137 create a logical 3d grid cannot achieve perfect balance for many
138 irregular distributions of particles.  Likewise, if a portion of the
139 system is a perfect lattice, e.g. the initial system is generated by
140 the "create_atoms"_create_atoms.html command, then "grid" methods may
141 be unable to achieve exact balance.  This is because entire lattice
142 planes will be owned or not owned by a single processor.
144 NOTE: The imbalance factor is also an estimate of the maximum speed-up
145 you can hope to achieve by running a perfectly balanced simulation
146 versus an imbalanced one.  In the example above, the 10000 particle
147 simulation could run up to 20% faster if it were perfectly balanced,
148 versus when imbalanced.  However, computational cost is not strictly
149 proportional to particle count, and changing the relative size and
150 shape of processor sub-domains may lead to additional computational
151 and communication overheads, e.g. in the PPPM solver used via the
152 "kspace_style"_kspace_style.html command.  Thus you should benchmark
153 the run times of a simulation before and after balancing.
155 :line
157 The method used to perform a load balance is specified by one of the
158 listed styles (or more in the case of {x},{y},{z}), which are
159 described in detail below.  There are 2 kinds of styles.
161 The {x}, {y}, {z}, and {shift} styles are "grid" methods which produce
162 a logical 3d grid of processors.  They operate by changing the cutting
163 planes (or lines) between processors in 3d (or 2d), to adjust the
164 volume (area in 2d) assigned to each processor, as in the following 2d
165 diagram where processor sub-domains are shown and particles are
166 colored by the processor that owns them.  The leftmost diagram is the
167 default partitioning of the simulation box across processors (one
168 sub-box for each of 16 processors); the middle diagram is after a
169 "grid" method has been applied.
171 :image(JPG/balance_uniform_small.jpg,JPG/balance_uniform.jpg),image(JPG/balance_nonuniform_small.jpg,JPG/balance_nonuniform.jpg),image(JPG/balance_rcb_small.jpg,JPG/balance_rcb.jpg)
174 The {rcb} style is a "tiling" method which does not produce a logical
175 3d grid of processors.  Rather it tiles the simulation domain with
176 rectangular sub-boxes of varying size and shape in an irregular
177 fashion so as to have equal numbers of particles (or weight) in each
178 sub-box, as in the rightmost diagram above.
180 The "grid" methods can be used with either of the
181 "comm_style"_comm_style.html command options, {brick} or {tiled}.  The
182 "tiling" methods can only be used with "comm_style
183 tiled"_comm_style.html.  Note that it can be useful to use a "grid"
184 method with "comm_style tiled"_comm_style.html to return the domain
185 partitioning to a logical 3d grid of processors so that "comm_style
186 brick" can afterwords be specified for subsequent "run"_run.html
187 commands.
189 When a "grid" method is specified, the current domain partitioning can
190 be either a logical 3d grid or a tiled partitioning.  In the former
191 case, the current logical 3d grid is used as a starting point and
192 changes are made to improve the imbalance factor.  In the latter case,
193 the tiled partitioning is discarded and a logical 3d grid is created
194 with uniform spacing in all dimensions.  This becomes the starting
195 point for the balancing operation.
197 When a "tiling" method is specified, the current domain partitioning
198 ("grid" or "tiled") is ignored, and a new partitioning is computed
199 from scratch.
201 :line
203 The {x}, {y}, and {z} styles invoke a "grid" method for balancing, as
204 described above.  Note that any or all of these 3 styles can be
205 specified together, one after the other, but they cannot be used with
206 any other style.  This style adjusts the position of cutting planes
207 between processor sub-domains in specific dimensions.  Only the
208 specified dimensions are altered.
210 The {uniform} argument spaces the planes evenly, as in the left
211 diagrams above.  The {numeric} argument requires listing Ps-1 numbers
212 that specify the position of the cutting planes.  This requires
213 knowing Ps = Px or Py or Pz = the number of processors assigned by
214 LAMMPS to the relevant dimension.  This assignment is made (and the
215 Px, Py, Pz values printed out) when the simulation box is created by
216 the "create_box" or "read_data" or "read_restart" command and is
217 influenced by the settings of the "processors"_processors.html
218 command.
220 Each of the numeric values must be between 0 and 1, and they must be
221 listed in ascending order.  They represent the fractional position of
222 the cutting place.  The left (or lower) edge of the box is 0.0, and
223 the right (or upper) edge is 1.0.  Neither of these values is
224 specified.  Only the interior Ps-1 positions are specified.  Thus is
225 there are 2 procesors in the x dimension, you specify a single value
226 such as 0.75, which would make the left processor's sub-domain 3x
227 larger than the right processor's sub-domain.
229 :line
231 The {shift} style invokes a "grid" method for balancing, as
232 described above.  It changes the positions of cutting planes between
233 processors in an iterative fashion, seeking to reduce the imbalance
234 factor, similar to how the "fix balance shift"_fix_balance.html
235 command operates.
237 The {dimstr} argument is a string of characters, each of which must be
238 an "x" or "y" or "z".  Eacn character can appear zero or one time,
239 since there is no advantage to balancing on a dimension more than
240 once.  You should normally only list dimensions where you expect there
241 to be a density variation in the particles.
243 Balancing proceeds by adjusting the cutting planes in each of the
244 dimensions listed in {dimstr}, one dimension at a time.  For a single
245 dimension, the balancing operation (described below) is iterated on up
246 to {Niter} times.  After each dimension finishes, the imbalance factor
247 is re-computed, and the balancing operation halts if the {stopthresh}
248 criterion is met.
250 A rebalance operation in a single dimension is performed using a
251 recursive multisectioning algorithm, where the position of each
252 cutting plane (line in 2d) in the dimension is adjusted independently.
253 This is similar to a recursive bisectioning for a single value, except
254 that the bounds used for each bisectioning take advantage of
255 information from neighboring cuts if possible.  At each iteration, the
256 count of particles on either side of each plane is tallied.  If the
257 counts do not match the target value for the plane, the position of
258 the cut is adjusted to be halfway between a low and high bound.  The
259 low and high bounds are adjusted on each iteration, using new count
260 information, so that they become closer together over time.  Thus as
261 the recursion progresses, the count of particles on either side of the
262 plane gets closer to the target value.
264 Once the rebalancing is complete and final processor sub-domains
265 assigned, particles are migrated to their new owning processor, and
266 the balance procedure ends.
268 NOTE: At each rebalance operation, the bisectioning for each cutting
269 plane (line in 2d) typcially starts with low and high bounds separated
270 by the extent of a processor's sub-domain in one dimension.  The size
271 of this bracketing region shrinks by 1/2 every iteration.  Thus if
272 {Niter} is specified as 10, the cutting plane will typically be
273 positioned to 1 part in 1000 accuracy (relative to the perfect target
274 position).  For {Niter} = 20, it will be accurate to 1 part in a
275 million.  Thus there is no need ot set {Niter} to a large value.
276 LAMMPS will check if the threshold accuracy is reached (in a
277 dimension) is less iterations than {Niter} and exit early.  However,
278 {Niter} should also not be set too small, since it will take roughly
279 the same number of iterations to converge even if the cutting plane is
280 initially close to the target value.
282 :line
284 The {rcb} style invokes a "tiled" method for balancing, as described
285 above.  It performs a recursive coordinate bisectioning (RCB) of the
286 simulation domain. The basic idea is as follows.
288 The simulation domain is cut into 2 boxes by an axis-aligned cut in
289 the longest dimension, leaving one new box on either side of the cut.
290 All the processors are also partitioned into 2 groups, half assigned
291 to the box on the lower side of the cut, and half to the box on the
292 upper side.  (If the processor count is odd, one side gets an extra
293 processor.)  The cut is positioned so that the number of particles in
294 the lower box is exactly the number that the processors assigned to
295 that box should own for load balance to be perfect.  This also makes
296 load balance for the upper box perfect.  The positioning is done
297 iteratively, by a bisectioning method.  Note that counting particles
298 on either side of the cut requires communication between all
299 processors at each iteration.
301 That is the procedure for the first cut.  Subsequent cuts are made
302 recursively, in exactly the same manner.  The subset of processors
303 assigned to each box make a new cut in the longest dimension of that
304 box, splitting the box, the subset of processsors, and the particles
305 in the box in two.  The recursion continues until every processor is
306 assigned a sub-box of the entire simulation domain, and owns the
307 particles in that sub-box.
309 :line
311 This sub-section describes how to perform weighted load balancing
312 using the {weight} keyword. :link(weighted_balance)
314 By default, all particles have a weight of 1.0, which means each
315 particle is assumed to require the same amount of computation during a
316 timestep.  There are, however, scenarios where this is not a good
317 assumption.  Measuring the computational cost for each particle
318 accurately would be impractical and slow down the computation.
319 Instead the {weight} keyword implements several ways to influence the
320 per-particle weights empirically by properties readily available or
321 using the user's knowledge of the system.  Note that the absolute
322 value of the weights are not important; only their relative ratios
323 affect which particle is assigned to which processor.  A particle with
324 a weight of 2.5 is assumed to require 5x more computational than a
325 particle with a weight of 0.5.  For all the options below the weight
326 assigned to a particle must be a positive value; an error will be be
327 generated if a weight is <= 0.0.
329 Below is a list of possible weight options with a short description of
330 their usage and some example scenarios where they might be applicable.
331 It is possible to apply multiple weight flags and the weightings they
332 induce will be combined through multiplication.  Most of the time,
333 however, it is sufficient to use just one method.
335 The {group} weight style assigns weight factors to specified
336 "groups"_group.html of particles.  The {group} style keyword is
337 followed by the number of groups, then pairs of group IDs and the
338 corresponding weight factor.  If a particle belongs to none of the
339 specified groups, its weight is not changed.  If it belongs to
340 multiple groups, its weight is the product of the weight factors.
342 This weight style is useful in combination with pair style
343 "hybrid"_pair_hybrid.html, e.g. when combining a more costly manybody
344 potential with a fast pair-wise potential.  It is also useful when
345 using "run_style respa"_run_style.html where some portions of the
346 system have many bonded interactions and others none.  It assumes that
347 the computational cost for each group remains constant over time.
348 This is a purely empirical weighting, so a series test runs to tune
349 the assigned weight factors for optimal performance is recommended.
351 The {neigh} weight style assigns the same weight to each particle
352 owned by a processor based on the total count of neighbors in the
353 neighbor list owned by that processor.  The motivation is that more
354 neighbors means a higher computational cost.  The style does not use
355 neighbors per atom to assign a unique weight to each atom, because
356 that value can vary depending on how the neighbor list is built.
358 The {factor} setting is applied as an overall scale factor to the
359 {neigh} weights which allows adjustment of their impact on the
360 balancing operation.  The specified {factor} value must be positive.
361 A value > 1.0 will increase the weights so that the ratio of max
362 weight to min weight increases by {factor}.  A value < 1.0 will
363 decrease the weights so that the ratio of max weight to min weight
364 decreases by {factor}.  In both cases the intermediate weight values
365 increase/decrease proportionally as well.  A value = 1.0 has no effect
366 on the {neigh} weights.  As a rule of thumb, we have found a {factor}
367 of about 0.8 often results in the best performance, since the number
368 of neighbors is likely to overestimate the ideal weight.
370 This weight style is useful for systems where there are different
371 cutoffs used for different pairs of interations, or the density
372 fluctuates, or a large number of particles are in the vicinity of a
373 wall, or a combination of these effects.  If a simulation uses
374 multiple neighbor lists, this weight style will use the first suitable
375 neighbor list it finds.  It will not request or compute a new list.  A
376 warning will be issued if there is no suitable neighbor list available
377 or if it is not current, e.g. if the balance command is used before a
378 "run"_run.html or "minimize"_minimize.html command is used, in which
379 case the neighbor list may not yet have been built.  In this case no
380 weights are computed.  Inserting a "run 0 post no"_run.html command
381 before issuing the {balance} command, may be a workaround for this
382 case, as it will induce the neighbor list to be built.
384 The {time} weight style uses "timer data"_timer.html to estimate
385 weights.  It assigns the same weight to each particle owned by a
386 processor based on the total computational time spent by that
387 processor.  See details below on what time window is used.  It uses
388 the same timing information as is used for the "MPI task timing
389 breakdown"_Section_start.html#start_8, namely, for sections {Pair},
390 {Bond}, {Kspace}, and {Neigh}.  The time spent in those portions of
391 the timestep are measured for each MPI rank, summed, then divided by
392 the number of particles owned by that processor.  I.e. the weight is
393 an effective CPU time/particle averaged over the particles on that
394 processor.
396 The {factor} setting is applied as an overall scale factor to the
397 {time} weights which allows adjustment of their impact on the
398 balancing operation.  The specified {factor} value must be positive.
399 A value > 1.0 will increase the weights so that the ratio of max
400 weight to min weight increases by {factor}.  A value < 1.0 will
401 decrease the weights so that the ratio of max weight to min weight
402 decreases by {factor}.  In both cases the intermediate weight values
403 increase/decrease proportionally as well.  A value = 1.0 has no effect
404 on the {time} weights.  As a rule of thumb, effective values to use
405 are typicall between 0.5 and 1.2.  Note that the timer quantities
406 mentioned above can be affected by communication which occurs in the
407 middle of the operations, e.g. pair styles with intermediate exchange
408 of data witin the force computation, and likewise for KSpace solves.
410 When using the {time} weight style with the {balance} command, the
411 timing data is taken from the preceding run command, i.e. the timings
412 are for the entire previous run.  For the {fix balance} command the
413 timing data is for only the timesteps since the last balancing
414 operation was performed.  If timing information for the required
415 sections is not available, e.g. at the beginning of a run, or when the
416 "timer"_timer.html command is set to either {loop} or {off}, a warning
417 is issued.  In this case no weights are computed.
419 NOTE: The {time} weight style is the most generic option, and should
420 be tried first, unless the {group} style is easily applicable.
421 However, since the computed cost function is averaged over all
422 particles on a processor, the weights may not be highly accurate.
423 This style can also be effective as a secondary weight in combination
424 with either {group} or {neigh} to offset some of inaccuracies in
425 either of those heuristics.
427 The {var} weight style assigns per-particle weights by evaluating an
428 "atom-style variable"_variable.html specified by {name}.  This is
429 provided as a more flexible alternative to the {group} weight style,
430 allowing definition of a more complex heuristics based on information
431 (global and per atom) available inside of LAMMPS.  For example,
432 atom-style variables can reference the position of a particle, its
433 velocity, the volume of its Voronoi cell, etc.
435 The {store} weight style does not compute a weight factor.  Instead it
436 stores the current accumulated weights in a custom per-atom property
437 specified by {name}.  This must be a property defined as {d_name} via
438 the "fix property/atom"_fix_property_atom.html command.  Note that
439 these custom per-atom properties can be output in a "dump"_dump.html
440 file, so this is a way to examine, debug, or visualize the
441 per-particle weights computed during the load-balancing operation.
443 :line
445 The {out} keyword writes a text file to the specified {filename} with
446 the results of the balancing operation.  The file contains the bounds
447 of the sub-domain for each processor after the balancing operation
448 completes.  The format of the file is compatible with the
449 "Pizza.py"_pizza {mdump} tool which has support for manipulating and
450 visualizing mesh files.  An example is shown here for a balancing by 4
451 processors for a 2d problem:
453 ITEM: TIMESTEP
455 ITEM: NUMBER OF NODES
457 ITEM: BOX BOUNDS
458 0 10
459 0 10
460 0 10
461 ITEM: NODES
462 1 1 0 0 0
463 2 1 5 0 0
464 3 1 5 5 0
465 4 1 0 5 0
466 5 1 5 0 0
467 6 1 10 0 0
468 7 1 10 5 0
469 8 1 5 5 0
470 9 1 0 5 0
471 10 1 5 5 0
472 11 1 5 10 0
473 12 1 10 5 0
474 13 1 5 5 0
475 14 1 10 5 0
476 15 1 10 10 0
477 16 1 5 10 0
478 ITEM: TIMESTEP
480 ITEM: NUMBER OF SQUARES
482 ITEM: SQUARES
483 1 1 1 2 3 4
484 2 1 5 6 7 8
485 3 1 9 10 11 12
486 4 1 13 14 15 16 :pre
488 The coordinates of all the vertices are listed in the NODES section, 5
489 per processor.  Note that the 4 sub-domains share vertices, so there
490 will be duplicate nodes in the list.
492 The "SQUARES" section lists the node IDs of the 4 vertices in a
493 rectangle for each processor (1 to 4).
495 For a 3d problem, the syntax is similar with 8 vertices listed for
496 each processor, instead of 4, and "SQUARES" replaced by "CUBES".
498 :line
500 [Restrictions:]
502 For 2d simulations, the {z} style cannot be used.  Nor can a "z"
503 appear in {dimstr} for the {shift} style.
505 [Related commands:]
507 "group"_group.html, "processors"_processors.html,
508 "fix balance"_fix_balance.html
510 [Default:] none