Change the behavior of the GPU update UI
[alexxy/gromacs.git] / src / gromacs / ewald / pme_load_balancing.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015,2016,2017,2018,2019, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \internal \file
36  *
37  * \brief This file contains function definitions necessary for
38  * managing automatic load balance of PME calculations (Coulomb and
39  * LJ).
40  *
41  * \author Berk Hess <hess@kth.se>
42  * \ingroup module_ewald
43  */
44 #include "gmxpre.h"
45
46 #include "pme_load_balancing.h"
47
48 #include <cassert>
49 #include <cmath>
50
51 #include <algorithm>
52
53 #include "gromacs/domdec/dlb.h"
54 #include "gromacs/domdec/domdec.h"
55 #include "gromacs/domdec/domdec_network.h"
56 #include "gromacs/domdec/domdec_struct.h"
57 #include "gromacs/domdec/partition.h"
58 #include "gromacs/ewald/ewald_utils.h"
59 #include "gromacs/ewald/pme.h"
60 #include "gromacs/fft/calcgrid.h"
61 #include "gromacs/gmxlib/network.h"
62 #include "gromacs/math/functions.h"
63 #include "gromacs/math/vec.h"
64 #include "gromacs/mdlib/dispersioncorrection.h"
65 #include "gromacs/mdlib/forcerec.h"
66 #include "gromacs/mdtypes/commrec.h"
67 #include "gromacs/mdtypes/inputrec.h"
68 #include "gromacs/mdtypes/md_enums.h"
69 #include "gromacs/mdtypes/state.h"
70 #include "gromacs/nbnxm/gpu_data_mgmt.h"
71 #include "gromacs/nbnxm/nbnxm.h"
72 #include "gromacs/pbcutil/pbc.h"
73 #include "gromacs/timing/wallcycle.h"
74 #include "gromacs/utility/cstringutil.h"
75 #include "gromacs/utility/fatalerror.h"
76 #include "gromacs/utility/gmxassert.h"
77 #include "gromacs/utility/logger.h"
78 #include "gromacs/utility/smalloc.h"
79 #include "gromacs/utility/strconvert.h"
80
81 #include "pme_internal.h"
82
83 /*! \brief Parameters and settings for one PP-PME setup */
84 struct pme_setup_t
85 {
86     real rcut_coulomb;         /**< Coulomb cut-off                              */
87     real rlistOuter;           /**< cut-off for the outer pair-list              */
88     real rlistInner;           /**< cut-off for the inner pair-list              */
89     real spacing;              /**< (largest) PME grid spacing                   */
90     ivec grid;                 /**< the PME grid dimensions                      */
91     real grid_efficiency;      /**< ineffiency factor for non-uniform grids <= 1 */
92     real ewaldcoeff_q;         /**< Electrostatic Ewald coefficient            */
93     real ewaldcoeff_lj;        /**< LJ Ewald coefficient, only for the call to send_switchgrid */
94     struct gmx_pme_t* pmedata; /**< the data structure used in the PME code      */
95     int               count;   /**< number of times this setup has been timed    */
96     double            cycles;  /**< the fastest time for this setup in cycles    */
97 };
98
99 /*! \brief After 50 nstlist periods of not observing imbalance: never tune PME */
100 const int PMETunePeriod = 50;
101 /*! \brief Trigger PME load balancing at more than 5% PME overload */
102 const real loadBalanceTriggerFactor = 1.05;
103 /*! \brief Scale the grid by a most at factor 1.7.
104  *
105  * This still leaves room for about 4-4.5x decrease in grid spacing while limiting the cases where
106  * large imbalance leads to extreme cutoff scaling for marginal benefits.
107  *
108  * This should help to avoid:
109  *   - large increase in power consumption for little performance gain
110  *   - increasing communication volume
111  *   - limiting DLB
112  */
113 const real c_maxSpacingScaling = 1.7;
114 /*! \brief In the initial scan, step by grids that are at least a factor 0.8 coarser */
115 const real gridpointsScaleFactor = 0.8;
116 /*! \brief In the initial scan, try to skip grids with uneven x/y/z spacing,
117  * checking if the "efficiency" is more than 5% worse than the previous grid.
118  */
119 const real relativeEfficiencyFactor = 1.05;
120 /*! \brief Rerun until a run is 12% slower setups than the fastest run so far */
121 const real maxRelativeSlowdownAccepted = 1.12;
122 /*! \brief If setups get more than 2% faster, do another round to avoid
123  * choosing a slower setup due to acceleration or fluctuations.
124  */
125 const real maxFluctuationAccepted = 1.02;
126
127 //! \brief Number of nstlist long tuning intervals to skip before starting
128 //         load-balancing at the beginning of the run.
129 const int c_numFirstTuningIntervalSkip = 5;
130 //! \brief Number of nstlist long tuning intervals to skip before starting
131 //         load-balancing at the beginning of the run with separate PME ranks. */
132 const int c_numFirstTuningIntervalSkipWithSepPme = 3;
133 //! \brief Number of nstlist long tuning intervals to skip after switching to a new setting
134 //         during balancing.
135 const int c_numPostSwitchTuningIntervalSkip = 1;
136 //! \brief Number of seconds to delay the tuning at startup to allow processors clocks to ramp up.
137 const double c_startupTimeDelay = 5.0;
138
139 /*! \brief Enumeration whose values describe the effect limiting the load balancing */
140 enum epmelb
141 {
142     epmelblimNO,
143     epmelblimBOX,
144     epmelblimDD,
145     epmelblimPMEGRID,
146     epmelblimMAXSCALING,
147     epmelblimNR
148 };
149
150 /*! \brief Descriptive strings matching ::epmelb */
151 static const char* pmelblim_str[epmelblimNR] = { "no", "box size", "domain decompostion",
152                                                  "PME grid restriction",
153                                                  "maximum allowed grid scaling" };
154
155 struct pme_load_balancing_t
156 {
157     gmx_bool bSepPMERanks;  /**< do we have separate PME ranks? */
158     gmx_bool bActive;       /**< is PME tuning active? */
159     int64_t  step_rel_stop; /**< stop the tuning after this value of step_rel */
160     gmx_bool bTriggerOnDLB; /**< trigger balancing only on DD DLB */
161     gmx_bool bBalance;      /**< are we in the balancing phase, i.e. trying different setups? */
162     int      nstage;        /**< the current maximum number of stages */
163
164     real                     cut_spacing;        /**< the minimum cutoff / PME grid spacing ratio */
165     real                     rcut_vdw;           /**< Vdw cutoff (does not change) */
166     real                     rcut_coulomb_start; /**< Initial electrostatics cutoff */
167     real                     rbufOuter_coulomb;  /**< the outer pairlist buffer size */
168     real                     rbufOuter_vdw;      /**< the outer pairlist buffer size */
169     real                     rbufInner_coulomb;  /**< the inner pairlist buffer size */
170     real                     rbufInner_vdw;      /**< the inner pairlist buffer size */
171     matrix                   box_start;          /**< the initial simulation box */
172     std::vector<pme_setup_t> setup;              /**< the PME+cutoff setups */
173     int                      cur;                /**< the index (in setup) of the current setup */
174     int                      fastest;            /**< index of the fastest setup up till now */
175     int                      lower_limit;        /**< don't go below this setup index */
176     int                      start;    /**< start of setup index range to consider in stage>0 */
177     int                      end;      /**< end   of setup index range to consider in stage>0 */
178     int                      elimited; /**< was the balancing limited, uses enum above */
179     int                      cutoff_scheme; /**< Verlet or group cut-offs */
180
181     int stage; /**< the current stage */
182
183     int    cycles_n; /**< step cycle counter cumulative count */
184     double cycles_c; /**< step cycle counter cumulative cycles */
185     double startTime; /**< time stamp when the balancing was started (relative to the UNIX epoch start).*/
186 };
187
188 /* TODO The code in this file should call this getter, rather than
189  * read bActive anywhere */
190 bool pme_loadbal_is_active(const pme_load_balancing_t* pme_lb)
191 {
192     return pme_lb != nullptr && pme_lb->bActive;
193 }
194
195 // TODO Return a unique_ptr to pme_load_balancing_t
196 void pme_loadbal_init(pme_load_balancing_t**     pme_lb_p,
197                       t_commrec*                 cr,
198                       const gmx::MDLogger&       mdlog,
199                       const t_inputrec&          ir,
200                       const matrix               box,
201                       const interaction_const_t& ic,
202                       const nonbonded_verlet_t&  nbv,
203                       gmx_pme_t*                 pmedata,
204                       gmx_bool                   bUseGPU)
205 {
206
207     pme_load_balancing_t* pme_lb;
208     real                  spm, sp;
209     int                   d;
210
211     // Note that we don't (yet) support PME load balancing with LJ-PME only.
212     GMX_RELEASE_ASSERT(EEL_PME(ir.coulombtype),
213                        "pme_loadbal_init called without PME electrostatics");
214     // To avoid complexity, we require a single cut-off with PME for q+LJ.
215     // This is checked by grompp, but it doesn't hurt to check again.
216     GMX_RELEASE_ASSERT(!(EEL_PME(ir.coulombtype) && EVDW_PME(ir.vdwtype) && ir.rcoulomb != ir.rvdw),
217                        "With Coulomb and LJ PME, rcoulomb should be equal to rvdw");
218
219     pme_lb = new pme_load_balancing_t;
220
221     pme_lb->bSepPMERanks = !thisRankHasDuty(cr, DUTY_PME);
222
223     /* Initially we turn on balancing directly on based on PP/PME imbalance */
224     pme_lb->bTriggerOnDLB = FALSE;
225
226     /* Any number of stages >= 2 is supported */
227     pme_lb->nstage = 2;
228
229     pme_lb->cutoff_scheme = ir.cutoff_scheme;
230
231     pme_lb->rbufOuter_coulomb = nbv.pairlistOuterRadius() - ic.rcoulomb;
232     pme_lb->rbufOuter_vdw     = nbv.pairlistOuterRadius() - ic.rvdw;
233     pme_lb->rbufInner_coulomb = nbv.pairlistInnerRadius() - ic.rcoulomb;
234     pme_lb->rbufInner_vdw     = nbv.pairlistInnerRadius() - ic.rvdw;
235
236     /* Scale box with Ewald wall factor; note that we pmedata->boxScaler
237      * can't always usedd as it's not available with separate PME ranks.
238      */
239     EwaldBoxZScaler boxScaler(ir);
240     boxScaler.scaleBox(box, pme_lb->box_start);
241
242     pme_lb->setup.resize(1);
243
244     pme_lb->rcut_vdw           = ic.rvdw;
245     pme_lb->rcut_coulomb_start = ir.rcoulomb;
246
247     pme_lb->cur                    = 0;
248     pme_lb->setup[0].rcut_coulomb  = ic.rcoulomb;
249     pme_lb->setup[0].rlistOuter    = nbv.pairlistOuterRadius();
250     pme_lb->setup[0].rlistInner    = nbv.pairlistInnerRadius();
251     pme_lb->setup[0].grid[XX]      = ir.nkx;
252     pme_lb->setup[0].grid[YY]      = ir.nky;
253     pme_lb->setup[0].grid[ZZ]      = ir.nkz;
254     pme_lb->setup[0].ewaldcoeff_q  = ic.ewaldcoeff_q;
255     pme_lb->setup[0].ewaldcoeff_lj = ic.ewaldcoeff_lj;
256
257     if (!pme_lb->bSepPMERanks)
258     {
259         GMX_RELEASE_ASSERT(pmedata,
260                            "On ranks doing both PP and PME we need a valid pmedata object");
261         pme_lb->setup[0].pmedata = pmedata;
262     }
263
264     spm = 0;
265     for (d = 0; d < DIM; d++)
266     {
267         sp = norm(pme_lb->box_start[d]) / pme_lb->setup[0].grid[d];
268         if (sp > spm)
269         {
270             spm = sp;
271         }
272     }
273     pme_lb->setup[0].spacing = spm;
274
275     if (ir.fourier_spacing > 0)
276     {
277         pme_lb->cut_spacing = ir.rcoulomb / ir.fourier_spacing;
278     }
279     else
280     {
281         pme_lb->cut_spacing = ir.rcoulomb / pme_lb->setup[0].spacing;
282     }
283
284     pme_lb->stage = 0;
285
286     pme_lb->fastest     = 0;
287     pme_lb->lower_limit = 0;
288     pme_lb->start       = 0;
289     pme_lb->end         = 0;
290     pme_lb->elimited    = epmelblimNO;
291
292     pme_lb->cycles_n  = 0;
293     pme_lb->cycles_c  = 0;
294     pme_lb->startTime = gmx_gettime();
295
296     if (!wallcycle_have_counter())
297     {
298         GMX_LOG(mdlog.warning)
299                 .asParagraph()
300                 .appendText(
301                         "NOTE: Cycle counters unsupported or not enabled in kernel. Cannot use "
302                         "PME-PP balancing.");
303     }
304
305     /* Tune with GPUs and/or separate PME ranks.
306      * When running only on a CPU without PME ranks, PME tuning will only help
307      * with small numbers of atoms in the cut-off sphere.
308      */
309     pme_lb->bActive = (wallcycle_have_counter() && (bUseGPU || pme_lb->bSepPMERanks));
310
311     /* With GPUs and no separate PME ranks we can't measure the PP/PME
312      * imbalance, so we start balancing right away.
313      * Otherwise we only start balancing after we observe imbalance.
314      */
315     pme_lb->bBalance = (pme_lb->bActive && (bUseGPU && !pme_lb->bSepPMERanks));
316
317     pme_lb->step_rel_stop = PMETunePeriod * ir.nstlist;
318
319     /* Delay DD load balancing when GPUs are used */
320     if (pme_lb->bActive && DOMAINDECOMP(cr) && cr->dd->nnodes > 1 && bUseGPU)
321     {
322         /* Lock DLB=auto to off (does nothing when DLB=yes/no.
323          * With GPUs and separate PME nodes, we want to first
324          * do PME tuning without DLB, since DLB might limit
325          * the cut-off, which never improves performance.
326          * We allow for DLB + PME tuning after a first round of tuning.
327          */
328         dd_dlb_lock(cr->dd);
329         if (dd_dlb_is_locked(cr->dd))
330         {
331             GMX_LOG(mdlog.warning)
332                     .asParagraph()
333                     .appendText("NOTE: DLB will not turn on during the first phase of PME tuning");
334         }
335     }
336
337     *pme_lb_p = pme_lb;
338 }
339
340 /*! \brief Try to increase the cutoff during load balancing */
341 static gmx_bool pme_loadbal_increase_cutoff(pme_load_balancing_t* pme_lb, int pme_order, const gmx_domdec_t* dd)
342 {
343     real fac, sp;
344     real tmpr_coulomb, tmpr_vdw;
345     int  d;
346     bool grid_ok;
347
348     /* Try to add a new setup with next larger cut-off to the list */
349     pme_setup_t set;
350
351     set.pmedata = nullptr;
352
353     NumPmeDomains numPmeDomains = getNumPmeDomains(dd);
354
355     fac = 1;
356     do
357     {
358         /* Avoid infinite while loop, which can occur at the minimum grid size.
359          * Note that in practice load balancing will stop before this point.
360          * The factor 2.1 allows for the extreme case in which only grids
361          * of powers of 2 are allowed (the current code supports more grids).
362          */
363         if (fac > 2.1)
364         {
365             return FALSE;
366         }
367
368         fac *= 1.01;
369         clear_ivec(set.grid);
370         sp = calcFftGrid(nullptr, pme_lb->box_start, fac * pme_lb->setup[pme_lb->cur].spacing,
371                          minimalPmeGridSize(pme_order), &set.grid[XX], &set.grid[YY], &set.grid[ZZ]);
372
373         /* As here we can't easily check if one of the PME ranks
374          * uses threading, we do a conservative grid check.
375          * This means we can't use pme_order or less grid lines
376          * per PME rank along x, which is not a strong restriction.
377          */
378         grid_ok = gmx_pme_check_restrictions(pme_order, set.grid[XX], set.grid[YY], set.grid[ZZ],
379                                              numPmeDomains.x, true, false);
380     } while (sp <= 1.001 * pme_lb->setup[pme_lb->cur].spacing || !grid_ok);
381
382     set.rcut_coulomb = pme_lb->cut_spacing * sp;
383     if (set.rcut_coulomb < pme_lb->rcut_coulomb_start)
384     {
385         /* This is unlikely, but can happen when e.g. continuing from
386          * a checkpoint after equilibration where the box shrank a lot.
387          * We want to avoid rcoulomb getting smaller than rvdw
388          * and there might be more issues with decreasing rcoulomb.
389          */
390         set.rcut_coulomb = pme_lb->rcut_coulomb_start;
391     }
392
393     if (pme_lb->cutoff_scheme == ecutsVERLET)
394     {
395         /* Never decrease the Coulomb and VdW list buffers */
396         set.rlistOuter = std::max(set.rcut_coulomb + pme_lb->rbufOuter_coulomb,
397                                   pme_lb->rcut_vdw + pme_lb->rbufOuter_vdw);
398         set.rlistInner = std::max(set.rcut_coulomb + pme_lb->rbufInner_coulomb,
399                                   pme_lb->rcut_vdw + pme_lb->rbufInner_vdw);
400     }
401     else
402     {
403         /* TODO Remove these lines and pme_lb->cutoff_scheme */
404         tmpr_coulomb = set.rcut_coulomb + pme_lb->rbufOuter_coulomb;
405         tmpr_vdw     = pme_lb->rcut_vdw + pme_lb->rbufOuter_vdw;
406         /* Two (known) bugs with cutoff-scheme=group here:
407          * - This modification of rlist results in incorrect DD comunication.
408          * - We should set fr->bTwinRange = (fr->rlistlong > fr->rlist).
409          */
410         set.rlistOuter = std::min(tmpr_coulomb, tmpr_vdw);
411         set.rlistInner = set.rlistOuter;
412     }
413
414     set.spacing = sp;
415     /* The grid efficiency is the size wrt a grid with uniform x/y/z spacing */
416     set.grid_efficiency = 1;
417     for (d = 0; d < DIM; d++)
418     {
419         set.grid_efficiency *= (set.grid[d] * sp) / norm(pme_lb->box_start[d]);
420     }
421     /* The Ewald coefficient is inversly proportional to the cut-off */
422     set.ewaldcoeff_q = pme_lb->setup[0].ewaldcoeff_q * pme_lb->setup[0].rcut_coulomb / set.rcut_coulomb;
423     /* We set ewaldcoeff_lj in set, even when LJ-PME is not used */
424     set.ewaldcoeff_lj = pme_lb->setup[0].ewaldcoeff_lj * pme_lb->setup[0].rcut_coulomb / set.rcut_coulomb;
425
426     set.count  = 0;
427     set.cycles = 0;
428
429     if (debug)
430     {
431         fprintf(debug, "PME loadbal: grid %d %d %d, coulomb cutoff %f\n", set.grid[XX],
432                 set.grid[YY], set.grid[ZZ], set.rcut_coulomb);
433     }
434     pme_lb->setup.push_back(set);
435     return TRUE;
436 }
437
438 /*! \brief Print the PME grid */
439 static void print_grid(FILE* fp_err, FILE* fp_log, const char* pre, const char* desc, const pme_setup_t* set, double cycles)
440 {
441     auto buf = gmx::formatString("%-11s%10s pme grid %d %d %d, coulomb cutoff %.3f", pre, desc,
442                                  set->grid[XX], set->grid[YY], set->grid[ZZ], set->rcut_coulomb);
443     if (cycles >= 0)
444     {
445         buf += gmx::formatString(": %.1f M-cycles", cycles * 1e-6);
446     }
447     if (fp_err != nullptr)
448     {
449         fprintf(fp_err, "\r%s\n", buf.c_str());
450         fflush(fp_err);
451     }
452     if (fp_log != nullptr)
453     {
454         fprintf(fp_log, "%s\n", buf.c_str());
455     }
456 }
457
458 /*! \brief Return the index of the last setup used in PME load balancing */
459 static int pme_loadbal_end(pme_load_balancing_t* pme_lb)
460 {
461     /* In the initial stage only n is set; end is not set yet */
462     if (pme_lb->end > 0)
463     {
464         return pme_lb->end;
465     }
466     else
467     {
468         return pme_lb->setup.size();
469     }
470 }
471
472 /*! \brief Print descriptive string about what limits PME load balancing */
473 static void print_loadbal_limited(FILE* fp_err, FILE* fp_log, int64_t step, pme_load_balancing_t* pme_lb)
474 {
475     auto buf = gmx::formatString(
476             "step %4s: the %s limits the PME load balancing to a coulomb cut-off of %.3f",
477             gmx::int64ToString(step).c_str(), pmelblim_str[pme_lb->elimited],
478             pme_lb->setup[pme_loadbal_end(pme_lb) - 1].rcut_coulomb);
479     if (fp_err != nullptr)
480     {
481         fprintf(fp_err, "\r%s\n", buf.c_str());
482         fflush(fp_err);
483     }
484     if (fp_log != nullptr)
485     {
486         fprintf(fp_log, "%s\n", buf.c_str());
487     }
488 }
489
490 /*! \brief Switch load balancing to stage 1
491  *
492  * In this stage, only reasonably fast setups are run again. */
493 static void switch_to_stage1(pme_load_balancing_t* pme_lb)
494 {
495     /* Increase start until we find a setup that is not slower than
496      * maxRelativeSlowdownAccepted times the fastest setup.
497      */
498     pme_lb->start = pme_lb->lower_limit;
499     while (pme_lb->start + 1 < gmx::ssize(pme_lb->setup)
500            && (pme_lb->setup[pme_lb->start].count == 0
501                || pme_lb->setup[pme_lb->start].cycles
502                           > pme_lb->setup[pme_lb->fastest].cycles * maxRelativeSlowdownAccepted))
503     {
504         pme_lb->start++;
505     }
506     /* While increasing start, we might have skipped setups that we did not
507      * time during stage 0. We want to extend the range for stage 1 to include
508      * any skipped setups that lie between setups that were measured to be
509      * acceptably fast and too slow.
510      */
511     while (pme_lb->start > pme_lb->lower_limit && pme_lb->setup[pme_lb->start - 1].count == 0)
512     {
513         pme_lb->start--;
514     }
515
516     /* Decrease end only with setups that we timed and that are slow. */
517     pme_lb->end = pme_lb->setup.size();
518     if (pme_lb->setup[pme_lb->end - 1].count > 0
519         && pme_lb->setup[pme_lb->end - 1].cycles
520                    > pme_lb->setup[pme_lb->fastest].cycles * maxRelativeSlowdownAccepted)
521     {
522         pme_lb->end--;
523     }
524
525     pme_lb->stage = 1;
526
527     /* Next we want to choose setup pme_lb->end-1, but as we will decrease
528      * pme_lb->cur by one right after returning, we set cur to end.
529      */
530     pme_lb->cur = pme_lb->end;
531 }
532
533 /*! \brief Process the timings and try to adjust the PME grid and Coulomb cut-off
534  *
535  * The adjustment is done to generate a different non-bonded PP and PME load.
536  * With separate PME ranks (PP and PME on different processes) or with
537  * a GPU (PP on GPU, PME on CPU), PP and PME run on different resources
538  * and changing the load will affect the load balance and performance.
539  * The total time for a set of integration steps is monitored and a range
540  * of grid/cut-off setups is scanned. After calling pme_load_balance many
541  * times and acquiring enough statistics, the best performing setup is chosen.
542  * Here we try to take into account fluctuations and changes due to external
543  * factors as well as DD load balancing.
544  */
545 static void pme_load_balance(pme_load_balancing_t*          pme_lb,
546                              t_commrec*                     cr,
547                              FILE*                          fp_err,
548                              FILE*                          fp_log,
549                              const gmx::MDLogger&           mdlog,
550                              const t_inputrec&              ir,
551                              const matrix                   box,
552                              gmx::ArrayRef<const gmx::RVec> x,
553                              double                         cycles,
554                              interaction_const_t*           ic,
555                              struct nonbonded_verlet_t*     nbv,
556                              struct gmx_pme_t**             pmedata,
557                              int64_t                        step)
558 {
559     gmx_bool     OK;
560     pme_setup_t* set;
561     double       cycles_fast;
562     char         buf[STRLEN], sbuf[22];
563
564     if (PAR(cr))
565     {
566         gmx_sumd(1, &cycles, cr);
567         cycles /= cr->nnodes;
568     }
569
570     set = &pme_lb->setup[pme_lb->cur];
571     set->count++;
572
573     /* Skip the first c_numPostSwitchTuningIntervalSkip cycles because the first step
574      * after a switch is much slower due to allocation and/or caching effects.
575      */
576     if (set->count % (c_numPostSwitchTuningIntervalSkip + 1) != 0)
577     {
578         return;
579     }
580
581     sprintf(buf, "step %4s: ", gmx_step_str(step, sbuf));
582     print_grid(fp_err, fp_log, buf, "timed with", set, cycles);
583
584     GMX_RELEASE_ASSERT(set->count > c_numPostSwitchTuningIntervalSkip, "We should skip cycles");
585     if (set->count == (c_numPostSwitchTuningIntervalSkip + 1))
586     {
587         set->cycles = cycles;
588     }
589     else
590     {
591         if (cycles * maxFluctuationAccepted < set->cycles && pme_lb->stage == pme_lb->nstage - 1)
592         {
593             /* The performance went up a lot (due to e.g. DD load balancing).
594              * Add a stage, keep the minima, but rescan all setups.
595              */
596             pme_lb->nstage++;
597
598             if (debug)
599             {
600                 fprintf(debug,
601                         "The performance for grid %d %d %d went from %.3f to %.1f M-cycles, this "
602                         "is more than %f\n"
603                         "Increased the number stages to %d"
604                         " and ignoring the previous performance\n",
605                         set->grid[XX], set->grid[YY], set->grid[ZZ], set->cycles * 1e-6,
606                         cycles * 1e-6, maxFluctuationAccepted, pme_lb->nstage);
607             }
608         }
609         set->cycles = std::min(set->cycles, cycles);
610     }
611
612     if (set->cycles < pme_lb->setup[pme_lb->fastest].cycles)
613     {
614         pme_lb->fastest = pme_lb->cur;
615
616         if (DOMAINDECOMP(cr))
617         {
618             /* We found a new fastest setting, ensure that with subsequent
619              * shorter cut-off's the dynamic load balancing does not make
620              * the use of the current cut-off impossible. This solution is
621              * a trade-off, as the PME load balancing and DD domain size
622              * load balancing can interact in complex ways.
623              * With the Verlet kernels, DD load imbalance will usually be
624              * mainly due to bonded interaction imbalance, which will often
625              * quickly push the domain boundaries beyond the limit for the
626              * optimal, PME load balanced, cut-off. But it could be that
627              * better overal performance can be obtained with a slightly
628              * shorter cut-off and better DD load balancing.
629              */
630             set_dd_dlb_max_cutoff(cr, pme_lb->setup[pme_lb->fastest].rlistOuter);
631         }
632     }
633     cycles_fast = pme_lb->setup[pme_lb->fastest].cycles;
634
635     /* Check in stage 0 if we should stop scanning grids.
636      * Stop when the time is more than maxRelativeSlowDownAccepted longer than the fastest.
637      */
638     if (pme_lb->stage == 0 && pme_lb->cur > 0
639         && cycles > pme_lb->setup[pme_lb->fastest].cycles * maxRelativeSlowdownAccepted)
640     {
641         pme_lb->setup.resize(pme_lb->cur + 1);
642         /* Done with scanning, go to stage 1 */
643         switch_to_stage1(pme_lb);
644     }
645
646     if (pme_lb->stage == 0)
647     {
648         int gridsize_start;
649
650         gridsize_start = set->grid[XX] * set->grid[YY] * set->grid[ZZ];
651
652         do
653         {
654             if (pme_lb->cur + 1 < gmx::ssize(pme_lb->setup))
655             {
656                 /* We had already generated the next setup */
657                 OK = TRUE;
658             }
659             else
660             {
661                 /* Find the next setup */
662                 OK = pme_loadbal_increase_cutoff(pme_lb, ir.pme_order, cr->dd);
663
664                 if (!OK)
665                 {
666                     pme_lb->elimited = epmelblimPMEGRID;
667                 }
668             }
669
670             if (OK
671                 && pme_lb->setup[pme_lb->cur + 1].spacing > c_maxSpacingScaling * pme_lb->setup[0].spacing)
672             {
673                 OK               = FALSE;
674                 pme_lb->elimited = epmelblimMAXSCALING;
675             }
676
677             if (OK && ir.ePBC != epbcNONE)
678             {
679                 OK = (gmx::square(pme_lb->setup[pme_lb->cur + 1].rlistOuter) <= max_cutoff2(ir.ePBC, box));
680                 if (!OK)
681                 {
682                     pme_lb->elimited = epmelblimBOX;
683                 }
684             }
685
686             if (OK)
687             {
688                 pme_lb->cur++;
689
690                 if (DOMAINDECOMP(cr))
691                 {
692                     OK = change_dd_cutoff(cr, box, x, pme_lb->setup[pme_lb->cur].rlistOuter);
693                     if (!OK)
694                     {
695                         /* Failed: do not use this setup */
696                         pme_lb->cur--;
697                         pme_lb->elimited = epmelblimDD;
698                     }
699                 }
700             }
701             if (!OK)
702             {
703                 /* We hit the upper limit for the cut-off,
704                  * the setup should not go further than cur.
705                  */
706                 pme_lb->setup.resize(pme_lb->cur + 1);
707                 print_loadbal_limited(fp_err, fp_log, step, pme_lb);
708                 /* Switch to the next stage */
709                 switch_to_stage1(pme_lb);
710             }
711         } while (OK
712                  && !(pme_lb->setup[pme_lb->cur].grid[XX] * pme_lb->setup[pme_lb->cur].grid[YY]
713                                       * pme_lb->setup[pme_lb->cur].grid[ZZ]
714                               < gridsize_start * gridpointsScaleFactor
715                       && pme_lb->setup[pme_lb->cur].grid_efficiency
716                                  < pme_lb->setup[pme_lb->cur - 1].grid_efficiency * relativeEfficiencyFactor));
717     }
718
719     if (pme_lb->stage > 0 && pme_lb->end == 1)
720     {
721         pme_lb->cur   = pme_lb->lower_limit;
722         pme_lb->stage = pme_lb->nstage;
723     }
724     else if (pme_lb->stage > 0 && pme_lb->end > 1)
725     {
726         /* If stage = nstage-1:
727          *   scan over all setups, rerunning only those setups
728          *   which are not much slower than the fastest
729          * else:
730          *   use the next setup
731          * Note that we loop backward to minimize the risk of the cut-off
732          * getting limited by DD DLB, since the DLB cut-off limit is set
733          * to the fastest PME setup.
734          */
735         do
736         {
737             if (pme_lb->cur > pme_lb->start)
738             {
739                 pme_lb->cur--;
740             }
741             else
742             {
743                 pme_lb->stage++;
744
745                 pme_lb->cur = pme_lb->end - 1;
746             }
747         } while (pme_lb->stage == pme_lb->nstage - 1 && pme_lb->setup[pme_lb->cur].count > 0
748                  && pme_lb->setup[pme_lb->cur].cycles > cycles_fast * maxRelativeSlowdownAccepted);
749
750         if (pme_lb->stage == pme_lb->nstage)
751         {
752             /* We are done optimizing, use the fastest setup we found */
753             pme_lb->cur = pme_lb->fastest;
754         }
755     }
756
757     if (DOMAINDECOMP(cr) && pme_lb->stage > 0)
758     {
759         OK = change_dd_cutoff(cr, box, x, pme_lb->setup[pme_lb->cur].rlistOuter);
760         if (!OK)
761         {
762             /* For some reason the chosen cut-off is incompatible with DD.
763              * We should continue scanning a more limited range of cut-off's.
764              */
765             if (pme_lb->cur > 1 && pme_lb->stage == pme_lb->nstage)
766             {
767                 /* stage=nstage says we're finished, but we should continue
768                  * balancing, so we set back stage which was just incremented.
769                  */
770                 pme_lb->stage--;
771             }
772             if (pme_lb->cur <= pme_lb->fastest)
773             {
774                 /* This should not happen, as we set limits on the DLB bounds.
775                  * But we implement a complete failsafe solution anyhow.
776                  */
777                 GMX_LOG(mdlog.warning)
778                         .asParagraph()
779                         .appendTextFormatted(
780                                 "The fastest PP/PME load balancing setting (cutoff %.3d nm) is no "
781                                 "longer available due to DD DLB or box size limitations",
782                                 pme_lb->fastest);
783                 pme_lb->fastest = pme_lb->lower_limit;
784                 pme_lb->start   = pme_lb->lower_limit;
785             }
786             /* Limit the range to below the current cut-off, scan from start */
787             pme_lb->end      = pme_lb->cur;
788             pme_lb->cur      = pme_lb->start;
789             pme_lb->elimited = epmelblimDD;
790             print_loadbal_limited(fp_err, fp_log, step, pme_lb);
791         }
792     }
793
794     /* Change the Coulomb cut-off and the PME grid */
795
796     set = &pme_lb->setup[pme_lb->cur];
797
798     ic->rcoulomb = set->rcut_coulomb;
799     nbv->changePairlistRadii(set->rlistOuter, set->rlistInner);
800     ic->ewaldcoeff_q = set->ewaldcoeff_q;
801     /* TODO: centralize the code that sets the potentials shifts */
802     if (ic->coulomb_modifier == eintmodPOTSHIFT)
803     {
804         GMX_RELEASE_ASSERT(ic->rcoulomb != 0, "Cutoff radius cannot be zero");
805         ic->sh_ewald = std::erfc(ic->ewaldcoeff_q * ic->rcoulomb) / ic->rcoulomb;
806     }
807     if (EVDW_PME(ic->vdwtype))
808     {
809         /* We have PME for both Coulomb and VdW, set rvdw equal to rcoulomb */
810         ic->rvdw          = set->rcut_coulomb;
811         ic->ewaldcoeff_lj = set->ewaldcoeff_lj;
812         if (ic->vdw_modifier == eintmodPOTSHIFT)
813         {
814             real crc2;
815
816             ic->dispersion_shift.cpot = -1.0 / gmx::power6(static_cast<double>(ic->rvdw));
817             ic->repulsion_shift.cpot  = -1.0 / gmx::power12(static_cast<double>(ic->rvdw));
818             crc2                      = gmx::square(ic->ewaldcoeff_lj * ic->rvdw);
819             ic->sh_lj_ewald =
820                     (std::exp(-crc2) * (1 + crc2 + 0.5 * crc2 * crc2) - 1) / gmx::power6(ic->rvdw);
821         }
822     }
823
824     /* We always re-initialize the tables whether they are used or not */
825     init_interaction_const_tables(nullptr, ic);
826
827     Nbnxm::gpu_pme_loadbal_update_param(nbv, ic);
828
829     if (!pme_lb->bSepPMERanks)
830     {
831         /* FIXME:
832          * CPU PME keeps a list of allocated pmedata's, that's why pme_lb->setup[pme_lb->cur].pmedata is not always nullptr.
833          * GPU PME, however, currently needs the gmx_pme_reinit always called on load balancing
834          * (pme_gpu_reinit might be not sufficiently decoupled from gmx_pme_init).
835          * This can lead to a lot of reallocations for PME GPU.
836          * Would be nicer if the allocated grid list was hidden within a single pmedata structure.
837          */
838         if ((pme_lb->setup[pme_lb->cur].pmedata == nullptr)
839             || pme_gpu_task_enabled(pme_lb->setup[pme_lb->cur].pmedata))
840         {
841             /* Generate a new PME data structure,
842              * copying part of the old pointers.
843              */
844             gmx_pme_reinit(&set->pmedata, cr, pme_lb->setup[0].pmedata, &ir, set->grid,
845                            set->ewaldcoeff_q, set->ewaldcoeff_lj);
846         }
847         *pmedata = set->pmedata;
848     }
849     else
850     {
851         /* Tell our PME-only rank to switch grid */
852         gmx_pme_send_switchgrid(cr, set->grid, set->ewaldcoeff_q, set->ewaldcoeff_lj);
853     }
854
855     if (debug)
856     {
857         print_grid(nullptr, debug, "", "switched to", set, -1);
858     }
859
860     if (pme_lb->stage == pme_lb->nstage)
861     {
862         print_grid(fp_err, fp_log, "", "optimal", set, -1);
863     }
864 }
865
866 /*! \brief Prepare for another round of PME load balancing
867  *
868  * \param[in,out] pme_lb       Pointer to PME load balancing struct
869  * \param[in]     bDlbUnlocked TRUE is DLB was locked and is now unlocked
870  *
871  * If the conditions (e.g. DLB off/on, CPU/GPU throttling etc.) changed,
872  * the PP/PME balance might change and re-balancing can improve performance.
873  * This function adds 2 stages and adjusts the considered setup range.
874  */
875 static void continue_pme_loadbal(pme_load_balancing_t* pme_lb, gmx_bool bDlbUnlocked)
876 {
877     /* Add 2 tuning stages, keep the detected end of the setup range */
878     pme_lb->nstage += 2;
879     if (bDlbUnlocked && pme_lb->bSepPMERanks)
880     {
881         /* With separate PME ranks, DLB should always lower the PP load and
882          * can only increase the PME load (more communication and imbalance),
883          * so we only need to scan longer cut-off's.
884          */
885         pme_lb->lower_limit = pme_lb->cur;
886     }
887     pme_lb->start = pme_lb->lower_limit;
888 }
889
890 void pme_loadbal_do(pme_load_balancing_t*          pme_lb,
891                     t_commrec*                     cr,
892                     FILE*                          fp_err,
893                     FILE*                          fp_log,
894                     const gmx::MDLogger&           mdlog,
895                     const t_inputrec&              ir,
896                     t_forcerec*                    fr,
897                     const matrix                   box,
898                     gmx::ArrayRef<const gmx::RVec> x,
899                     gmx_wallcycle_t                wcycle,
900                     int64_t                        step,
901                     int64_t                        step_rel,
902                     gmx_bool*                      bPrinting,
903                     bool                           useGpuPmePpCommunication)
904 {
905     int    n_prev;
906     double cycles_prev;
907
908     assert(pme_lb != nullptr);
909
910     if (!pme_lb->bActive)
911     {
912         return;
913     }
914
915     n_prev      = pme_lb->cycles_n;
916     cycles_prev = pme_lb->cycles_c;
917     wallcycle_get(wcycle, ewcSTEP, &pme_lb->cycles_n, &pme_lb->cycles_c);
918
919     /* Before the first step we haven't done any steps yet.
920      * Also handle cases where ir.init_step % ir.nstlist != 0.
921      * We also want to skip a number of steps and seconds while
922      * the CPU and GPU, when used, performance stabilizes.
923      */
924     if (pme_lb->cycles_n == 0 || step_rel < c_numFirstTuningIntervalSkip * ir.nstlist
925         || gmx_gettime() - pme_lb->startTime < c_startupTimeDelay)
926     {
927         *bPrinting = FALSE;
928
929         return;
930     }
931     /* Sanity check, we expect nstlist cycle counts */
932     if (pme_lb->cycles_n - n_prev != ir.nstlist)
933     {
934         /* We could return here, but it's safer to issue an error and quit */
935         gmx_incons("pme_loadbal_do called at an interval != nstlist");
936     }
937
938     /* PME grid + cut-off optimization with GPUs or PME ranks */
939     if (!pme_lb->bBalance && pme_lb->bSepPMERanks)
940     {
941         if (pme_lb->bTriggerOnDLB)
942         {
943             pme_lb->bBalance = dd_dlb_is_on(cr->dd);
944         }
945         /* We should ignore the first timing to avoid timing allocation
946          * overhead. And since the PME load balancing is called just
947          * before DD repartitioning, the ratio returned by dd_pme_f_ratio
948          * is not over the last nstlist steps, but the nstlist steps before
949          * that. So the first useful ratio is available at step_rel=3*nstlist.
950          */
951         else if (step_rel >= c_numFirstTuningIntervalSkipWithSepPme * ir.nstlist)
952         {
953             if (DDMASTER(cr->dd))
954             {
955                 /* If PME rank load is too high, start tuning. If
956                    PME-PP direct GPU communication is active,
957                    unconditionally start tuning since ratio will be
958                    unreliable due to CPU-GPU asynchronicity in codepath */
959                 pme_lb->bBalance = useGpuPmePpCommunication
960                                            ? true
961                                            : (dd_pme_f_ratio(cr->dd) >= loadBalanceTriggerFactor);
962             }
963             dd_bcast(cr->dd, sizeof(gmx_bool), &pme_lb->bBalance);
964         }
965
966         pme_lb->bActive = (pme_lb->bBalance || step_rel <= pme_lb->step_rel_stop);
967     }
968
969     /* The location in the code of this balancing termination is strange.
970      * You would expect to have it after the call to pme_load_balance()
971      * below, since there pme_lb->stage is updated.
972      * But when terminating directly after deciding on and selecting the
973      * optimal setup, DLB will turn on right away if it was locked before.
974      * This might be due to PME reinitialization. So we check stage here
975      * to allow for another nstlist steps with DLB locked to stabilize
976      * the performance.
977      */
978     if (pme_lb->bBalance && pme_lb->stage == pme_lb->nstage)
979     {
980         pme_lb->bBalance = FALSE;
981
982         if (DOMAINDECOMP(cr) && dd_dlb_is_locked(cr->dd))
983         {
984             /* Unlock the DLB=auto, DLB is allowed to activate */
985             dd_dlb_unlock(cr->dd);
986             GMX_LOG(mdlog.warning)
987                     .asParagraph()
988                     .appendText("NOTE: DLB can now turn on, when beneficial");
989
990             /* We don't deactivate the tuning yet, since we will balance again
991              * after DLB gets turned on, if it does within PMETune_period.
992              */
993             continue_pme_loadbal(pme_lb, TRUE);
994             pme_lb->bTriggerOnDLB = TRUE;
995             pme_lb->step_rel_stop = step_rel + PMETunePeriod * ir.nstlist;
996         }
997         else
998         {
999             /* We're completely done with PME tuning */
1000             pme_lb->bActive = FALSE;
1001         }
1002
1003         if (DOMAINDECOMP(cr))
1004         {
1005             /* Set the cut-off limit to the final selected cut-off,
1006              * so we don't have artificial DLB limits.
1007              * This also ensures that we won't disable the currently
1008              * optimal setting during a second round of PME balancing.
1009              */
1010             set_dd_dlb_max_cutoff(cr, fr->nbv->pairlistOuterRadius());
1011         }
1012     }
1013
1014     if (pme_lb->bBalance)
1015     {
1016         /* We might not have collected nstlist steps in cycles yet,
1017          * since init_step might not be a multiple of nstlist,
1018          * but the first data collected is skipped anyhow.
1019          */
1020         pme_load_balance(pme_lb, cr, fp_err, fp_log, mdlog, ir, box, x,
1021                          pme_lb->cycles_c - cycles_prev, fr->ic, fr->nbv.get(), &fr->pmedata, step);
1022
1023         /* Update deprecated rlist in forcerec to stay in sync with fr->nbv */
1024         fr->rlist = fr->nbv->pairlistOuterRadius();
1025
1026         if (ir.eDispCorr != edispcNO)
1027         {
1028             fr->dispersionCorrection->setParameters(*fr->ic);
1029         }
1030     }
1031
1032     if (!pme_lb->bBalance && (!pme_lb->bSepPMERanks || step_rel > pme_lb->step_rel_stop))
1033     {
1034         /* We have just deactivated the balancing and we're not measuring PP/PME
1035          * imbalance during the first steps of the run: deactivate the tuning.
1036          */
1037         pme_lb->bActive = FALSE;
1038     }
1039
1040     if (!(pme_lb->bActive) && DOMAINDECOMP(cr) && dd_dlb_is_locked(cr->dd))
1041     {
1042         /* Make sure DLB is allowed when we deactivate PME tuning */
1043         dd_dlb_unlock(cr->dd);
1044         GMX_LOG(mdlog.warning)
1045                 .asParagraph()
1046                 .appendText("NOTE: DLB can now turn on, when beneficial");
1047     }
1048
1049     *bPrinting = pme_lb->bBalance;
1050 }
1051
1052 /*! \brief Return product of the number of PME grid points in each dimension */
1053 static int pme_grid_points(const pme_setup_t* setup)
1054 {
1055     return setup->grid[XX] * setup->grid[YY] * setup->grid[ZZ];
1056 }
1057
1058 /*! \brief Print one load-balancing setting */
1059 static void print_pme_loadbal_setting(FILE* fplog, const char* name, const pme_setup_t* setup)
1060 {
1061     fprintf(fplog, "   %-7s %6.3f nm %6.3f nm     %3d %3d %3d   %5.3f nm  %5.3f nm\n", name,
1062             setup->rcut_coulomb, setup->rlistInner, setup->grid[XX], setup->grid[YY],
1063             setup->grid[ZZ], setup->spacing, 1 / setup->ewaldcoeff_q);
1064 }
1065
1066 /*! \brief Print all load-balancing settings */
1067 static void print_pme_loadbal_settings(pme_load_balancing_t* pme_lb,
1068                                        FILE*                 fplog,
1069                                        const gmx::MDLogger&  mdlog,
1070                                        gmx_bool              bNonBondedOnGPU)
1071 {
1072     double pp_ratio, grid_ratio;
1073     real   pp_ratio_temporary;
1074
1075     pp_ratio_temporary = pme_lb->setup[pme_lb->cur].rlistInner / pme_lb->setup[0].rlistInner;
1076     pp_ratio           = gmx::power3(pp_ratio_temporary);
1077     grid_ratio         = pme_grid_points(&pme_lb->setup[pme_lb->cur])
1078                  / static_cast<double>(pme_grid_points(&pme_lb->setup[0]));
1079
1080     fprintf(fplog, "\n");
1081     fprintf(fplog, "       P P   -   P M E   L O A D   B A L A N C I N G\n");
1082     fprintf(fplog, "\n");
1083     /* Here we only warn when the optimal setting is the last one */
1084     if (pme_lb->elimited != epmelblimNO && pme_lb->cur == pme_loadbal_end(pme_lb) - 1)
1085     {
1086         fprintf(fplog, " NOTE: The PP/PME load balancing was limited by the %s,\n",
1087                 pmelblim_str[pme_lb->elimited]);
1088         fprintf(fplog, "       you might not have reached a good load balance.\n");
1089         if (pme_lb->elimited == epmelblimDD)
1090         {
1091             fprintf(fplog, "       Try different mdrun -dd settings or lower the -dds value.\n");
1092         }
1093         fprintf(fplog, "\n");
1094     }
1095     fprintf(fplog, " PP/PME load balancing changed the cut-off and PME settings:\n");
1096     fprintf(fplog, "           particle-particle                    PME\n");
1097     fprintf(fplog, "            rcoulomb  rlist            grid      spacing   1/beta\n");
1098     print_pme_loadbal_setting(fplog, "initial", &pme_lb->setup[0]);
1099     print_pme_loadbal_setting(fplog, "final", &pme_lb->setup[pme_lb->cur]);
1100     fprintf(fplog, " cost-ratio           %4.2f             %4.2f\n", pp_ratio, grid_ratio);
1101     fprintf(fplog, " (note that these numbers concern only part of the total PP and PME load)\n");
1102
1103     if (pp_ratio > 1.5 && !bNonBondedOnGPU)
1104     {
1105         GMX_LOG(mdlog.warning)
1106                 .asParagraph()
1107                 .appendText(
1108                         "NOTE: PME load balancing increased the non-bonded workload by more than "
1109                         "50%.\n"
1110                         "      For better performance, use (more) PME ranks (mdrun -npme),\n"
1111                         "      or if you are beyond the scaling limit, use fewer total ranks (or "
1112                         "nodes).");
1113     }
1114     else
1115     {
1116         fprintf(fplog, "\n");
1117     }
1118 }
1119
1120 void pme_loadbal_done(pme_load_balancing_t* pme_lb, FILE* fplog, const gmx::MDLogger& mdlog, gmx_bool bNonBondedOnGPU)
1121 {
1122     if (fplog != nullptr && (pme_lb->cur > 0 || pme_lb->elimited != epmelblimNO))
1123     {
1124         print_pme_loadbal_settings(pme_lb, fplog, mdlog, bNonBondedOnGPU);
1125     }
1126
1127     delete pme_lb;
1128 }