Merge "Merge branch 'release-2019' into master"
authorPaul Bauer <paul.bauer.q@gmail.com>
Wed, 20 Feb 2019 18:09:31 +0000 (19:09 +0100)
committerPaul Bauer <paul.bauer.q@gmail.com>
Wed, 20 Feb 2019 18:09:31 +0000 (19:09 +0100)
12 files changed:
src/gromacs/domdec/dlbtiming.cpp
src/gromacs/domdec/dlbtiming.h
src/gromacs/mdlib/force.cpp
src/gromacs/mdlib/force.h
src/gromacs/mdlib/shellfc.cpp
src/gromacs/mdlib/shellfc.h
src/gromacs/mdlib/sim_util.cpp
src/gromacs/mdrun/md.cpp
src/gromacs/mdrun/mimic.cpp
src/gromacs/mdrun/minimize.cpp
src/gromacs/mdrun/rerun.cpp
src/gromacs/mdrun/tpi.cpp

index 19ae2ff8679927ba5e7308c4b47b4dcab2b3ef13..818027869719b92cdc9308c15197684c41c70b2c 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2017,2018, by the GROMACS development team, led by
+ * Copyright (c) 2017,2018,2019, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -83,11 +83,10 @@ static BalanceRegion *getBalanceRegion(const gmx_domdec_t *dd)
     return region;
 }
 
-void ddOpenBalanceRegionCpu(const gmx_domdec_t                    *dd,
-                            DdAllowBalanceRegionReopen gmx_unused  allowReopen)
+void DDBalanceRegionHandler::openRegionCpuImpl(DdAllowBalanceRegionReopen gmx_unused allowReopen) const
 {
-    BalanceRegion *reg = getBalanceRegion(dd);
-    if (dd->comm->bRecordLoad)
+    BalanceRegion *reg = getBalanceRegion(dd_);
+    if (dd_->comm->bRecordLoad)
     {
         GMX_ASSERT(allowReopen == DdAllowBalanceRegionReopen::yes || !reg->isOpen, "Should not open an already opened region");
 
@@ -98,14 +97,12 @@ void ddOpenBalanceRegionCpu(const gmx_domdec_t                    *dd,
     }
 }
 
-void ddOpenBalanceRegionGpu(const gmx_domdec_t *dd)
+void DDBalanceRegionHandler::openRegionGpuImpl() const
 {
-    BalanceRegion *reg = getBalanceRegion(dd);
-    if (reg->isOpen)
-    {
-        GMX_ASSERT(!reg->isOpenOnGpu, "Can not re-open a GPU balance region");
-        reg->isOpenOnGpu = true;
-    }
+    BalanceRegion *reg = getBalanceRegion(dd_);
+    GMX_ASSERT(reg->isOpen, "Can only open a GPU region inside an open CPU region");
+    GMX_ASSERT(!reg->isOpenOnGpu, "Can not re-open a GPU balance region");
+    reg->isOpenOnGpu = true;
 }
 
 void ddReopenBalanceRegionCpu(const gmx_domdec_t *dd)
@@ -118,9 +115,9 @@ void ddReopenBalanceRegionCpu(const gmx_domdec_t *dd)
     }
 }
 
-void ddCloseBalanceRegionCpu(const gmx_domdec_t *dd)
+void DDBalanceRegionHandler::closeRegionCpuImpl() const
 {
-    BalanceRegion *reg = getBalanceRegion(dd);
+    BalanceRegion *reg = getBalanceRegion(dd_);
     if (reg->isOpen && reg->isOpenOnCpu)
     {
         GMX_ASSERT(reg->isOpenOnCpu, "Can only close an open region");
@@ -136,17 +133,16 @@ void ddCloseBalanceRegionCpu(const gmx_domdec_t *dd)
         {
             /* We can close the region */
             float cyclesCpu   = cycles - reg->cyclesOpenCpu;
-            dd_cycles_add(dd, cyclesCpu, ddCyclF);
+            dd_cycles_add(dd_, cyclesCpu, ddCyclF);
             reg->isOpen       = false;
         }
     }
 }
 
-void ddCloseBalanceRegionGpu(const gmx_domdec_t          *dd,
-                             float                        waitGpuCyclesInCpuRegion,
-                             DdBalanceRegionWaitedForGpu  waitedForGpu)
+void DDBalanceRegionHandler::closeRegionGpuImpl(float                       waitGpuCyclesInCpuRegion,
+                                                DdBalanceRegionWaitedForGpu waitedForGpu) const
 {
-    BalanceRegion *reg = getBalanceRegion(dd);
+    BalanceRegion *reg = getBalanceRegion(dd_);
     if (reg->isOpen)
     {
         GMX_ASSERT(reg->isOpenOnGpu, "Can not close a non-open GPU balance region");
@@ -163,10 +159,10 @@ void ddCloseBalanceRegionGpu(const gmx_domdec_t          *dd,
         }
 
         float cyclesCpu = reg->cyclesLastCpu - reg->cyclesOpenCpu;
-        dd_cycles_add(dd, cyclesCpu + waitGpuCyclesEstimate, ddCyclF);
+        dd_cycles_add(dd_, cyclesCpu + waitGpuCyclesEstimate, ddCyclF);
 
         /* Register the total GPU wait time, to redistribute with GPU sharing */
-        dd_cycles_add(dd, waitGpuCyclesInCpuRegion + waitGpuCyclesEstimate, ddCyclWaitGPU);
+        dd_cycles_add(dd_, waitGpuCyclesInCpuRegion + waitGpuCyclesEstimate, ddCyclWaitGPU);
 
         /* Close the region */
         reg->isOpenOnGpu = false;
index e2554e346c0898630dc5858fd922c3dbaacb2a87..67e909f245ec0d10f07ea85bbe36cdbe78626111 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2017,2018, by the GROMACS development team, led by
+ * Copyright (c) 2017,2018,2019, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
 #ifndef GMX_DOMDEC_DLBTIMING_H
 #define GMX_DOMDEC_DLBTIMING_H
 
+#include "gromacs/mdtypes/commrec.h"
+
 struct BalanceRegion;
 struct gmx_domdec_t;
 struct t_nrnb;
 
-/*! \brief Tells if we should open the balancing region */
-enum class DdOpenBalanceRegionBeforeForceComputation
-{
-    no,  //!< Do not open a balancing region
-    yes  //!< Open the balancing region before update or after pair-search
-};
-
-/*! \brief Tells if we should close the balancing region after the force computation has completed */
-enum class DdCloseBalanceRegionAfterForceComputation
-{
-    no,  //!< Do not close a balancing region
-    yes  //!< Close the balancing region after computation completed
-};
-
 /*! \brief Tells if we should open the balancing region */
 enum class DdAllowBalanceRegionReopen
 {
@@ -77,35 +65,6 @@ enum class DdBalanceRegionWaitedForGpu
     yes  //!< We had to wait for the GPU to finish computation
 };
 
-/*! \brief Returns a pointer to a constructed \p BalanceRegion struct
- *
- * Should be replaced by a proper constructor once BalanceRegion is a proper
- * class (requires restructering in domdec.cpp).
- */
-BalanceRegion *ddBalanceRegionAllocate();
-
-/*! \brief Open the load balance timing region on the CPU
- *
- * Opens the balancing region for timing how much time it takes to perform
- * the (balancable part of) the MD step. This should be called right after
- * the last communication during the previous step to maximize the region.
- * In practice this means right after the force communication finished
- * or just before neighbor search at search steps.
- * It is assumed that computation done in the region either scales along
- * with the domain size or takes constant time.
- *
- * \param[in,out] dd           The domain decomposition struct
- * \param[in]     allowReopen  Allows calling with a potentially already opened region
- */
-void ddOpenBalanceRegionCpu(const gmx_domdec_t         *dd,
-                            DdAllowBalanceRegionReopen  allowReopen);
-
-/*! \brief Open the load balance timing region for the CPU
- *
- * This can only be called within a region that is open on the CPU side.
- */
-void ddOpenBalanceRegionGpu(const gmx_domdec_t *dd);
-
 /*! \brief Re-open the, already opened, load balance timing region
  *
  * This function should be called after every MPI communication that occurs
@@ -119,26 +78,143 @@ void ddOpenBalanceRegionGpu(const gmx_domdec_t *dd);
  */
 void ddReopenBalanceRegionCpu(const gmx_domdec_t *dd);
 
-/*! \brief Close the load balance timing region on the CPU side
- *
- * \param[in,out] dd  The domain decomposition struct
+/*! \libinternal
+ * \brief Manager for starting and stopping the dynamic load balancing region
  */
-void ddCloseBalanceRegionCpu(const gmx_domdec_t *dd);
+class DDBalanceRegionHandler
+{
+    public:
+        //! Constructor, pass a pointer to t_commrec or nullptr when not using domain decomposition
+        DDBalanceRegionHandler(const t_commrec *cr) :
+            dd_(cr != nullptr ? cr->dd : nullptr)
+        {
+            useBalancingRegion_ = (cr != nullptr &&
+                                   cr->dd != nullptr &&
+                                   cr->nnodes - cr->npmenodes > 1);
+        }
+
+        /*! \brief Returns whether were are actually using the balancing region
+         */
+        bool useBalancingRegion() const
+        {
+            return useBalancingRegion_;
+        }
+
+        /*! \brief Open the load balance timing region on the CPU
+         *
+         * Opens the balancing region for timing how much time it takes to perform
+         * the (balancable part of) the MD step. This should be called right after
+         * the last communication during the previous step to maximize the region.
+         * In practice this means right after the force communication finished
+         * or just before neighbor search at search steps.
+         * It is assumed that computation done in the region either scales along
+         * with the domain size or takes constant time.
+         *
+         * \param[in] allowReopen  Allows calling with a potentially already opened region
+         */
+        void openBeforeForceComputationCpu(DdAllowBalanceRegionReopen allowReopen) const
+        {
+            if (useBalancingRegion_)
+            {
+                openRegionCpuImpl(allowReopen);
+            }
+        }
+
+        /*! \brief Open the load balance timing region for the CPU
+         *
+         * This can only be called within a region that is open on the CPU side.
+         */
+        void openBeforeForceComputationGpu() const
+        {
+            if (useBalancingRegion_)
+            {
+                openRegionGpuImpl();
+            }
+        }
+
+        /*! \brief Re-open the, already opened, load balance timing region
+         *
+         * This function should be called after every MPI communication that occurs
+         * in the main MD loop.
+         * Note that the current setup assumes that all MPI communication acts like
+         * a global barrier. But if some ranks don't participate in communication
+         * or if some ranks communicate faster with neighbors than others,
+         * the obtained timings might not accurately reflect the computation time.
+         */
+        void reopenRegionCpu() const
+        {
+            if (useBalancingRegion_)
+            {
+                ddReopenBalanceRegionCpu(dd_);
+            }
+        }
+
+        /*! \brief Close the load balance timing region on the CPU side
+         */
+        void closeAfterForceComputationCpu() const
+        {
+            if (useBalancingRegion_)
+            {
+                closeRegionCpuImpl();
+            }
+        }
+
+        /*! \brief Close the load balance timing region on the GPU side
+         *
+         * This should be called after the CPU receives the last (local) results
+         * from the GPU. The wait time for these results is estimated, depending
+         * on the \p waitedForGpu parameter.
+         * If called on an already closed region, this call does nothing.
+         *
+         * \param[in] waitCyclesGpuInCpuRegion  The time we waited for the GPU earlier, overlapping completely with the open CPU region
+         * \param[in] waitedForGpu              Tells if we waited for the GPU to finish now
+         */
+        void closeAfterForceComputationGpu(float                       waitCyclesGpuInCpuRegion,
+                                           DdBalanceRegionWaitedForGpu waitedForGpu) const
+        {
+            if (useBalancingRegion_)
+            {
+                closeRegionGpuImpl(waitCyclesGpuInCpuRegion, waitedForGpu);
+            }
+        }
+
+    private:
+        /*! \brief Open the load balance timing region on the CPU
+         *
+         * \param[in] allowReopen  Allows calling with a potentially already opened region
+         */
+        void openRegionCpuImpl(DdAllowBalanceRegionReopen allowReopen) const;
+
+        /*! \brief Open the load balance timing region for the GPU
+         *
+         * This can only be called within a region that is open on the CPU side.
+         */
+        void openRegionGpuImpl() const;
+
+        /*! \brief Close the load balance timing region on the CPU side
+         */
+        void closeRegionCpuImpl() const;
+
+        /*! \brief Close the load balance timing region on the GPU side
+         *
+         * \param[in] waitCyclesGpuInCpuRegion  The time we waited for the GPU earlier, overlapping completely with the open CPU region
+         * \param[in] waitedForGpu              Tells if we waited for the GPU to finish now
+         */
+        void closeRegionGpuImpl(float                       waitCyclesGpuInCpuRegion,
+                                DdBalanceRegionWaitedForGpu waitedForGpu) const;
+
+        //! Tells whether the balancing region should be active
+        bool          useBalancingRegion_;
+        //! A pointer to the DD struct, only valid with useBalancingRegion_=true
+        gmx_domdec_t *dd_;
+};
 
-/*! \brief Close the load balance timing region on the GPU side
- *
- * This should be called after the CPU receives the last (local) results
- * from the GPU. The wait time for these results is estimated, depending
- * on the \p waitedForGpu parameter.
- * If called on an already closed region, this call does nothing.
+/*! \brief Returns a pointer to a constructed \p BalanceRegion struct
  *
- * \param[in,out] dd                        The domain decomposition struct
- * \param[in]     waitCyclesGpuInCpuRegion  The time we waited for the GPU earlier, overlapping completely with the open CPU region
- * \param[in]     waitedForGpu              Tells if we waited for the GPU to finish now
+ * Should be replaced by a proper constructor once BalanceRegion is a proper
+ * class (requires restructering in domdec.cpp).
  */
-void ddCloseBalanceRegionGpu(const gmx_domdec_t          *dd,
-                             float                        waitCyclesGpuInCpuRegion,
-                             DdBalanceRegionWaitedForGpu  waitedForGpu);
+BalanceRegion *ddBalanceRegionAllocate();
 
 /*! \brief Start the force flop count */
 void dd_force_flop_start(struct gmx_domdec_t *dd, t_nrnb *nrnb);
index 1a7d648e801d74102a86dae217152757b2aa0335..ef7b727ce0916fe057b02f0cb02bc1912d4de6bc 100644 (file)
@@ -44,6 +44,7 @@
 #include <cmath>
 #include <cstring>
 
+#include "gromacs/domdec/dlbtiming.h"
 #include "gromacs/domdec/domdec.h"
 #include "gromacs/domdec/domdec_struct.h"
 #include "gromacs/ewald/ewald.h"
@@ -138,28 +139,29 @@ static void reduceEwaldThreadOuput(int nthreads, ewald_corr_thread_t *ewc_t)
     }
 }
 
-void do_force_lowlevel(t_forcerec           *fr,
-                       const t_inputrec     *ir,
-                       const t_idef         *idef,
-                       const t_commrec      *cr,
-                       const gmx_multisim_t *ms,
-                       t_nrnb               *nrnb,
-                       gmx_wallcycle_t       wcycle,
-                       const t_mdatoms      *md,
-                       rvec                  x[],
-                       history_t            *hist,
-                       rvec                 *forceForUseWithShiftForces,
-                       gmx::ForceWithVirial *forceWithVirial,
-                       gmx_enerdata_t       *enerd,
-                       t_fcdata             *fcd,
-                       matrix                box,
-                       t_lambda             *fepvals,
-                       real                 *lambda,
-                       const t_graph        *graph,
-                       const t_blocka       *excl,
-                       rvec                  mu_tot[],
-                       int                   flags,
-                       float                *cycles_pme)
+void do_force_lowlevel(t_forcerec                   *fr,
+                       const t_inputrec             *ir,
+                       const t_idef                 *idef,
+                       const t_commrec              *cr,
+                       const gmx_multisim_t         *ms,
+                       t_nrnb                       *nrnb,
+                       gmx_wallcycle_t               wcycle,
+                       const t_mdatoms              *md,
+                       rvec                          x[],
+                       history_t                    *hist,
+                       rvec                         *forceForUseWithShiftForces,
+                       gmx::ForceWithVirial         *forceWithVirial,
+                       gmx_enerdata_t               *enerd,
+                       t_fcdata                     *fcd,
+                       matrix                        box,
+                       t_lambda                     *fepvals,
+                       real                         *lambda,
+                       const t_graph                *graph,
+                       const t_blocka               *excl,
+                       rvec                          mu_tot[],
+                       int                           flags,
+                       float                        *cycles_pme,
+                       const DDBalanceRegionHandler &ddBalanceRegionHandler)
 {
     int         i, j;
     int         donb_flags;
@@ -460,10 +462,7 @@ void do_force_lowlevel(t_forcerec           *fr,
                      * balancing region here, because PME does global
                      * communication that acts as a global barrier.
                      */
-                    if (DOMAINDECOMP(cr))
-                    {
-                        ddCloseBalanceRegionCpu(cr->dd);
-                    }
+                    ddBalanceRegionHandler.closeAfterForceComputationCpu();
 
                     wallcycle_start(wcycle, ewcPMEMESH);
                     status = gmx_pme_do(fr->pmedata,
index 99cbd502eaa5755b93943a12d248f87cc3a6615f..a4cc28e9c88cc032c648e6f2bb797b7bd8799059 100644 (file)
 #ifndef GMX_MDLIB_FORCE_H
 #define GMX_MDLIB_FORCE_H
 
-#include "gromacs/domdec/dlbtiming.h"
 #include "gromacs/math/arrayrefwithpadding.h"
 #include "gromacs/math/vectypes.h"
 #include "gromacs/utility/arrayref.h"
 
+class DDBalanceRegionHandler;
 struct gmx_edsam;
 struct gmx_enerdata_t;
 struct gmx_enfrot;
@@ -120,8 +120,7 @@ void do_force(FILE                                     *log,
               double                                    t,
               gmx_edsam                                *ed,
               int                                       flags,
-              DdOpenBalanceRegionBeforeForceComputation ddOpenBalanceRegion,
-              DdCloseBalanceRegionAfterForceComputation ddCloseBalanceRegion);
+              const DDBalanceRegionHandler             &ddBalanceRegionHandler);
 
 /* Communicate coordinates (if parallel).
  * Do neighbor searching (if necessary).
@@ -164,7 +163,8 @@ void do_force_lowlevel(t_forcerec   *fr,
                        const t_blocka *excl,
                        rvec         mu_tot[2],
                        int          flags,
-                       float        *cycles_pme);
+                       float        *cycles_pme,
+                       const DDBalanceRegionHandler &ddBalanceRegionHandler);
 /* Call all the force routines */
 
 #endif
index 2f99a517483135c1ac0e9462b28807d575787b6d..ff3aa7a123b3862d6fb7c88012bf4b5aa34f3369 100644 (file)
@@ -991,8 +991,7 @@ void relax_shell_flexcon(FILE                                     *fplog,
                          double                                    t,
                          rvec                                      mu_tot,
                          const gmx_vsite_t                        *vsite,
-                         DdOpenBalanceRegionBeforeForceComputation ddOpenBalanceRegion,
-                         DdCloseBalanceRegionAfterForceComputation ddCloseBalanceRegion)
+                         const DDBalanceRegionHandler             &ddBalanceRegionHandler)
 {
     int           nshell;
     t_shell      *shell;
@@ -1132,7 +1131,7 @@ void relax_shell_flexcon(FILE                                     *fplog,
              state->lambda, graph,
              fr, ppForceWorkload, vsite, mu_tot, t, nullptr,
              (bDoNS ? GMX_FORCE_NS : 0) | shellfc_flags,
-             ddOpenBalanceRegion, ddCloseBalanceRegion);
+             ddBalanceRegionHandler);
 
     sf_dir = 0;
     if (nflexcon)
@@ -1242,7 +1241,7 @@ void relax_shell_flexcon(FILE                                     *fplog,
                  md, enerd, fcd, state->lambda, graph,
                  fr, ppForceWorkload, vsite, mu_tot, t, nullptr,
                  shellfc_flags,
-                 ddOpenBalanceRegion, ddCloseBalanceRegion);
+                 ddBalanceRegionHandler);
         sum_epot(&(enerd->grpp), enerd->term);
         if (gmx_debug_at)
         {
index f7401426edd947ddd1b1ab79c915076f6a997d08..913ee5efa83b40c6cede9cd84547b65d59a465bb 100644 (file)
 
 #include <cstdio>
 
-#include "gromacs/domdec/dlbtiming.h"
 #include "gromacs/math/arrayrefwithpadding.h"
 #include "gromacs/mdlib/vsite.h"
 #include "gromacs/timing/wallcycle.h"
 
+class DDBalanceRegionHandler;
 struct gmx_enerdata_t;
 struct gmx_enfrot;
 struct gmx_groups_t;
@@ -102,8 +102,7 @@ void relax_shell_flexcon(FILE                                     *log,
                          double                                    t,
                          rvec                                      mu_tot,
                          const gmx_vsite_t                        *vsite,
-                         DdOpenBalanceRegionBeforeForceComputation ddOpenBalanceRegion,
-                         DdCloseBalanceRegionAfterForceComputation ddCloseBalanceRegion);
+                         const DDBalanceRegionHandler             &ddBalanceRegionHandler);
 
 /* Print some final output */
 void done_shellfc(FILE *fplog, gmx_shellfc_t *shellfc, int64_t numSteps);
index 3d5578a664b07f0849877da60a1a25f0b68985c1..eeacc953451d9b75f46e9e0b86536151f9a2ff7d 100644 (file)
@@ -946,8 +946,7 @@ static void do_force_cutsVERLET(FILE *fplog,
                                 double t,
                                 gmx_edsam *ed,
                                 const int flags,
-                                DdOpenBalanceRegionBeforeForceComputation ddOpenBalanceRegion,
-                                DdCloseBalanceRegionAfterForceComputation ddCloseBalanceRegion)
+                                const DDBalanceRegionHandler &ddBalanceRegionHandler)
 {
     int                 cg1, i, j;
     double              mu[2*DIM];
@@ -978,10 +977,9 @@ static void do_force_cutsVERLET(FILE *fplog,
      * somewhere early inside the step after communication during domain
      * decomposition (and not during the previous step as usual).
      */
-    if (bNS &&
-        ddOpenBalanceRegion == DdOpenBalanceRegionBeforeForceComputation::yes)
+    if (bNS)
     {
-        ddOpenBalanceRegionCpu(cr->dd, DdAllowBalanceRegionReopen::yes);
+        ddBalanceRegionHandler.openBeforeForceComputationCpu(DdAllowBalanceRegionReopen::yes);
     }
 
     cycles_wait_gpu = 0;
@@ -1181,10 +1179,7 @@ static void do_force_cutsVERLET(FILE *fplog,
 
     if (bUseGPU)
     {
-        if (DOMAINDECOMP(cr))
-        {
-            ddOpenBalanceRegionGpu(cr->dd);
-        }
+        ddBalanceRegionHandler.openBeforeForceComputationGpu();
 
         wallcycle_start(wcycle, ewcLAUNCH_GPU);
 
@@ -1315,7 +1310,8 @@ static void do_force_cutsVERLET(FILE *fplog,
         if (PAR(cr))
         {
             gmx_sumd(2*DIM, mu, cr);
-            ddReopenBalanceRegionCpu(cr->dd);
+
+            ddBalanceRegionHandler.reopenRegionCpu();
         }
 
         for (i = 0; i < 2; i++)
@@ -1475,7 +1471,8 @@ static void do_force_cutsVERLET(FILE *fplog,
                       cr, ms, nrnb, wcycle, mdatoms,
                       as_rvec_array(x.unpaddedArrayRef().data()), hist, f, &forceWithVirial, enerd, fcd,
                       box, inputrec->fepvals, lambda, graph, &(top->excls), fr->mu_tot,
-                      flags, &cycles_pme);
+                      flags,
+                      &cycles_pme, ddBalanceRegionHandler);
 
     wallcycle_stop(wcycle, ewcFORCE);
 
@@ -1524,10 +1521,8 @@ static void do_force_cutsVERLET(FILE *fplog,
          * If we use a GPU this will overlap with GPU work, so in that case
          * we do not close the DD force balancing region here.
          */
-        if (ddCloseBalanceRegion == DdCloseBalanceRegionAfterForceComputation::yes)
-        {
-            ddCloseBalanceRegionCpu(cr->dd);
-        }
+        ddBalanceRegionHandler.closeAfterForceComputationCpu();
+
         if (bDoForces)
         {
             dd_move_f(cr->dd, force.unpaddedArrayRef(), fr->fshift, wcycle);
@@ -1564,7 +1559,7 @@ static void do_force_cutsVERLET(FILE *fplog,
                                     fr->fshift);
         float cycles_tmp = wallcycle_stop(wcycle, ewcWAIT_GPU_NB_L);
 
-        if (ddCloseBalanceRegion == DdCloseBalanceRegionAfterForceComputation::yes)
+        if (ddBalanceRegionHandler.useBalancingRegion())
         {
             DdBalanceRegionWaitedForGpu waitedForGpu = DdBalanceRegionWaitedForGpu::yes;
             if (bDoForces && cycles_tmp <= gpuWaitApiOverheadMargin)
@@ -1577,7 +1572,7 @@ static void do_force_cutsVERLET(FILE *fplog,
                  */
                 waitedForGpu = DdBalanceRegionWaitedForGpu::no;
             }
-            ddCloseBalanceRegionGpu(cr->dd, cycles_wait_gpu, waitedForGpu);
+            ddBalanceRegionHandler.closeAfterForceComputationGpu(cycles_wait_gpu, waitedForGpu);
         }
     }
 
@@ -1714,8 +1709,7 @@ static void do_force_cutsGROUP(FILE *fplog,
                                double t,
                                gmx_edsam *ed,
                                int flags,
-                               DdOpenBalanceRegionBeforeForceComputation ddOpenBalanceRegion,
-                               DdCloseBalanceRegionAfterForceComputation ddCloseBalanceRegion)
+                               const DDBalanceRegionHandler &ddBalanceRegionHandler)
 {
     int        cg0, cg1, i, j;
     double     mu[2*DIM];
@@ -1818,10 +1812,7 @@ static void do_force_cutsGROUP(FILE *fplog,
         dd_move_x(cr->dd, box, x.unpaddedArrayRef(), wcycle);
 
         /* No GPU support, no move_x overlap, so reopen the balance region here */
-        if (ddOpenBalanceRegion == DdOpenBalanceRegionBeforeForceComputation::yes)
-        {
-            ddReopenBalanceRegionCpu(cr->dd);
-        }
+        ddBalanceRegionHandler.reopenRegionCpu();
     }
 
     if (inputrecNeedMutot(inputrec))
@@ -1831,7 +1822,8 @@ static void do_force_cutsGROUP(FILE *fplog,
             if (PAR(cr))
             {
                 gmx_sumd(2*DIM, mu, cr);
-                ddReopenBalanceRegionCpu(cr->dd);
+
+                ddBalanceRegionHandler.reopenRegionCpu();
             }
             for (i = 0; i < 2; i++)
             {
@@ -1937,7 +1929,7 @@ static void do_force_cutsGROUP(FILE *fplog,
                       box, inputrec->fepvals, lambda,
                       graph, &(top->excls), fr->mu_tot,
                       flags,
-                      &cycles_pme);
+                      &cycles_pme, ddBalanceRegionHandler);
 
     wallcycle_stop(wcycle, ewcFORCE);
 
@@ -1945,10 +1937,7 @@ static void do_force_cutsGROUP(FILE *fplog,
     {
         dd_force_flop_stop(cr->dd, nrnb);
 
-        if (ddCloseBalanceRegion == DdCloseBalanceRegionAfterForceComputation::yes)
-        {
-            ddCloseBalanceRegionCpu(cr->dd);
-        }
+        ddBalanceRegionHandler.closeAfterForceComputationCpu();
     }
 
     computeSpecialForces(fplog, cr, inputrec, awh, enforcedRotation,
@@ -2051,8 +2040,7 @@ void do_force(FILE                                     *fplog,
               double                                    t,
               gmx_edsam                                *ed,
               int                                       flags,
-              DdOpenBalanceRegionBeforeForceComputation ddOpenBalanceRegion,
-              DdCloseBalanceRegionAfterForceComputation ddCloseBalanceRegion)
+              const DDBalanceRegionHandler             &ddBalanceRegionHandler)
 {
     /* modify force flag if not doing nonbonded */
     if (!fr->bNonbonded)
@@ -2078,8 +2066,7 @@ void do_force(FILE                                     *fplog,
                                 vsite, mu_tot,
                                 t, ed,
                                 flags,
-                                ddOpenBalanceRegion,
-                                ddCloseBalanceRegion);
+                                ddBalanceRegionHandler);
             break;
         case ecutsGROUP:
             do_force_cutsGROUP(fplog, cr, ms, inputrec,
@@ -2094,8 +2081,7 @@ void do_force(FILE                                     *fplog,
                                fr, vsite, mu_tot,
                                t, ed,
                                flags,
-                               ddOpenBalanceRegion,
-                               ddCloseBalanceRegion);
+                               ddBalanceRegionHandler);
             break;
         default:
             gmx_incons("Invalid cut-off scheme passed!");
@@ -2107,10 +2093,7 @@ void do_force(FILE                                     *fplog,
      * virial calculation and COM pulling, is not thus not included in
      * the balance timing, which is ok as most tasks do communication.
      */
-    if (ddOpenBalanceRegion == DdOpenBalanceRegionBeforeForceComputation::yes)
-    {
-        ddOpenBalanceRegionCpu(cr->dd, DdAllowBalanceRegionReopen::no);
-    }
+    ddBalanceRegionHandler.openBeforeForceComputationCpu(DdAllowBalanceRegionReopen::no);
 }
 
 
index 3139bbdd117a5a3f94220667e8b3fe5676172a11..09dd5ace38f83fcea3aa485e669298d93f20c67e 100644 (file)
@@ -54,6 +54,7 @@
 #include "gromacs/awh/awh.h"
 #include "gromacs/commandline/filenm.h"
 #include "gromacs/domdec/collect.h"
+#include "gromacs/domdec/dlbtiming.h"
 #include "gromacs/domdec/domdec.h"
 #include "gromacs/domdec/domdec_network.h"
 #include "gromacs/domdec/domdec_struct.h"
@@ -659,8 +660,7 @@ void gmx::Integrator::do_md()
                 ir->nsteps, MASTER(cr), mdrunOptions.timingOptions.resetHalfway,
                 mdrunOptions.maximumHoursToRun, mdlog, wcycle, walltime_accounting);
 
-    DdOpenBalanceRegionBeforeForceComputation ddOpenBalanceRegion   = (DOMAINDECOMP(cr) ? DdOpenBalanceRegionBeforeForceComputation::yes : DdOpenBalanceRegionBeforeForceComputation::no);
-    DdCloseBalanceRegionAfterForceComputation ddCloseBalanceRegion  = (DOMAINDECOMP(cr) ? DdCloseBalanceRegionAfterForceComputation::yes : DdCloseBalanceRegionAfterForceComputation::no);
+    const DDBalanceRegionHandler ddBalanceRegionHandler(cr);
 
     step     = ir->init_step;
     step_rel = 0;
@@ -854,7 +854,7 @@ void gmx::Integrator::do_md()
                                 nrnb, wcycle, graph, groups,
                                 shellfc, fr, ppForceWorkload, t, mu_tot,
                                 vsite,
-                                ddOpenBalanceRegion, ddCloseBalanceRegion);
+                                ddBalanceRegionHandler);
         }
         else
         {
@@ -883,7 +883,7 @@ void gmx::Integrator::do_md()
                      state->lambda, graph,
                      fr, ppForceWorkload, vsite, mu_tot, t, ed ? ed->getLegacyED() : nullptr,
                      (bNS ? GMX_FORCE_NS : 0) | force_flags,
-                     ddOpenBalanceRegion, ddCloseBalanceRegion);
+                     ddBalanceRegionHandler);
         }
 
         if (EI_VV(ir->eI) && !startingFromCheckpoint)
index e221640482ba286a419a1edf115ebdd3d5b494e4..30f826217bd960e178f065a9bf3a49a341a8a908 100644 (file)
@@ -53,6 +53,7 @@
 #include "gromacs/awh/awh.h"
 #include "gromacs/commandline/filenm.h"
 #include "gromacs/domdec/collect.h"
+#include "gromacs/domdec/dlbtiming.h"
 #include "gromacs/domdec/domdec.h"
 #include "gromacs/domdec/domdec_network.h"
 #include "gromacs/domdec/domdec_struct.h"
@@ -365,14 +366,7 @@ void gmx::Integrator::do_mimic()
     // we don't do counter resetting in rerun - finish will always be valid
     walltime_accounting_set_valid_finish(walltime_accounting);
 
-    DdOpenBalanceRegionBeforeForceComputation ddOpenBalanceRegion   =
-        (DOMAINDECOMP(cr) ?
-         DdOpenBalanceRegionBeforeForceComputation::yes :
-         DdOpenBalanceRegionBeforeForceComputation::no);
-    DdCloseBalanceRegionAfterForceComputation ddCloseBalanceRegion  =
-        (DOMAINDECOMP(cr) ?
-         DdCloseBalanceRegionAfterForceComputation::yes :
-         DdCloseBalanceRegionAfterForceComputation::no);
+    const DDBalanceRegionHandler ddBalanceRegionHandler(cr);
 
     step     = ir->init_step;
     step_rel = 0;
@@ -448,7 +442,7 @@ void gmx::Integrator::do_mimic()
                                 nrnb, wcycle, graph, groups,
                                 shellfc, fr, ppForceWorkload, t, mu_tot,
                                 vsite,
-                                ddOpenBalanceRegion, ddCloseBalanceRegion);
+                                ddBalanceRegionHandler);
         }
         else
         {
@@ -466,7 +460,7 @@ void gmx::Integrator::do_mimic()
                      state->lambda, graph,
                      fr, ppForceWorkload, vsite, mu_tot, t, ed,
                      GMX_FORCE_NS | force_flags,
-                     ddOpenBalanceRegion, ddCloseBalanceRegion);
+                     ddBalanceRegionHandler);
         }
 
         /* Now we have the energies and forces corresponding to the
index f3ba13faae6aa710d0efb08f9b2265d1125e9dac..932e63ef73aa112bb58f7cc4dde88ac09f349e14 100644 (file)
@@ -55,6 +55,7 @@
 
 #include "gromacs/commandline/filenm.h"
 #include "gromacs/domdec/collect.h"
+#include "gromacs/domdec/dlbtiming.h"
 #include "gromacs/domdec/domdec.h"
 #include "gromacs/domdec/domdec_struct.h"
 #include "gromacs/domdec/partition.h"
@@ -852,12 +853,7 @@ EnergyEvaluator::run(em_state_t *ems, rvec mu_tot,
              GMX_FORCE_STATECHANGED | GMX_FORCE_ALLFORCES |
              GMX_FORCE_VIRIAL | GMX_FORCE_ENERGY |
              (bNS ? GMX_FORCE_NS : 0),
-             DOMAINDECOMP(cr) ?
-             DdOpenBalanceRegionBeforeForceComputation::yes :
-             DdOpenBalanceRegionBeforeForceComputation::no,
-             DOMAINDECOMP(cr) ?
-             DdCloseBalanceRegionAfterForceComputation::yes :
-             DdCloseBalanceRegionAfterForceComputation::no);
+             DDBalanceRegionHandler(cr));
 
     /* Clear the unused shake virial and pressure */
     clear_mat(shake_vir);
@@ -2880,8 +2876,7 @@ Integrator::do_nm()
                                         t,
                                         mu_tot,
                                         vsite,
-                                        DdOpenBalanceRegionBeforeForceComputation::no,
-                                        DdCloseBalanceRegionAfterForceComputation::no);
+                                        DDBalanceRegionHandler(nullptr));
                     bNS = false;
                     step++;
                 }
index fc6d53e60c2759d20ad3bc842a33aa4521d80e4d..53002609e79eaa13066523d8be3c6f014941684f 100644 (file)
@@ -54,6 +54,7 @@
 #include "gromacs/awh/awh.h"
 #include "gromacs/commandline/filenm.h"
 #include "gromacs/domdec/collect.h"
+#include "gromacs/domdec/dlbtiming.h"
 #include "gromacs/domdec/domdec.h"
 #include "gromacs/domdec/domdec_network.h"
 #include "gromacs/domdec/domdec_struct.h"
@@ -477,8 +478,7 @@ void gmx::Integrator::do_rerun()
     // we don't do counter resetting in rerun - finish will always be valid
     walltime_accounting_set_valid_finish(walltime_accounting);
 
-    DdOpenBalanceRegionBeforeForceComputation ddOpenBalanceRegion   = (DOMAINDECOMP(cr) ? DdOpenBalanceRegionBeforeForceComputation::yes : DdOpenBalanceRegionBeforeForceComputation::no);
-    DdCloseBalanceRegionAfterForceComputation ddCloseBalanceRegion  = (DOMAINDECOMP(cr) ? DdCloseBalanceRegionAfterForceComputation::yes : DdCloseBalanceRegionAfterForceComputation::no);
+    const DDBalanceRegionHandler ddBalanceRegionHandler(cr);
 
     step     = ir->init_step;
     step_rel = 0;
@@ -563,7 +563,7 @@ void gmx::Integrator::do_rerun()
                                 nrnb, wcycle, graph, groups,
                                 shellfc, fr, ppForceWorkload, t, mu_tot,
                                 vsite,
-                                ddOpenBalanceRegion, ddCloseBalanceRegion);
+                                ddBalanceRegionHandler);
         }
         else
         {
@@ -581,7 +581,7 @@ void gmx::Integrator::do_rerun()
                      state->lambda, graph,
                      fr, ppForceWorkload, vsite, mu_tot, t, ed,
                      GMX_FORCE_NS | force_flags,
-                     ddOpenBalanceRegion, ddCloseBalanceRegion);
+                     ddBalanceRegionHandler);
         }
 
         /* Now we have the energies and forces corresponding to the
index da514094d389427cad099348da611fde8f1d12a4..fcaf7722ace65c522494e998a4c809e70a3d4996 100644 (file)
@@ -53,6 +53,7 @@
 #include <cfenv>
 
 #include "gromacs/commandline/filenm.h"
+#include "gromacs/domdec/dlbtiming.h"
 #include "gromacs/domdec/domdec.h"
 #include "gromacs/ewald/pme.h"
 #include "gromacs/fileio/confio.h"
@@ -653,8 +654,7 @@ Integrator::do_tpi()
                      GMX_FORCE_NONBONDED | GMX_FORCE_ENERGY |
                      (bNS ? GMX_FORCE_DYNAMICBOX | GMX_FORCE_NS : 0) |
                      (bStateChanged ? GMX_FORCE_STATECHANGED : 0),
-                     DdOpenBalanceRegionBeforeForceComputation::no,
-                     DdCloseBalanceRegionAfterForceComputation::no);
+                     DDBalanceRegionHandler(nullptr));
             std::feclearexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
             std::feupdateenv(&floatingPointEnvironment);