Move force buffer resizing to mdsetup
authorBerk Hess <hess@kth.se>
Mon, 6 Apr 2020 11:27:31 +0000 (11:27 +0000)
committerMark Abraham <mark.j.abraham@gmail.com>
Mon, 6 Apr 2020 11:27:31 +0000 (11:27 +0000)
Consolidate resizing of the force buffer passed to do_force() from
many places in the code to mdAlgorithmsSetupAtomData().
Now only the modular simulation does it's own resizing when not using
domain decomposition. Some refactoring is needed to avoid this.

Change-Id: I1320dffb7b42150f65c5c6a260a7e94e2f6806b5

19 files changed:
src/gromacs/domdec/distribute.cpp
src/gromacs/domdec/distribute.h
src/gromacs/domdec/mdsetup.cpp
src/gromacs/domdec/mdsetup.h
src/gromacs/domdec/partition.cpp
src/gromacs/domdec/redistribute.cpp
src/gromacs/domdec/redistribute.h
src/gromacs/domdec/utility.cpp
src/gromacs/domdec/utility.h
src/gromacs/mdlib/forcerec.cpp
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/shellfc.cpp
src/gromacs/mdtypes/forcerec.h
src/gromacs/modularsimulator/topologyholder.cpp
src/gromacs/modularsimulator/topologyholder.h

index a681378a0514147c1271370e75683dbd7a4e3f32..062acc6b570c712254536c922220ad6dcb1a1adc 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2018,2019, by the GROMACS development team, led by
+ * Copyright (c) 2018,2019,2020, 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.
@@ -187,10 +187,7 @@ static void dd_distribute_dfhist(gmx_domdec_t* dd, df_history_t* dfhist)
     }
 }
 
-static void dd_distribute_state(gmx_domdec_t*                dd,
-                                const t_state*               state,
-                                t_state*                     state_local,
-                                PaddedHostVector<gmx::RVec>* f)
+static void dd_distribute_state(gmx_domdec_t* dd, const t_state* state, t_state* state_local)
 {
     int nh = state_local->nhchainlength;
 
@@ -252,7 +249,7 @@ static void dd_distribute_state(gmx_domdec_t*                dd,
     /* communicate df_history -- required for restarting from checkpoint */
     dd_distribute_dfhist(dd, state_local->dfhist);
 
-    dd_resize_state(state_local, f, dd->comm->atomRanges.numHomeAtoms());
+    state_change_natoms(state_local, dd->comm->atomRanges.numHomeAtoms());
 
     if (state_local->flags & (1 << estX))
     {
@@ -552,17 +549,16 @@ static void distributeAtomGroups(const gmx::MDLogger& mdlog,
     }
 }
 
-void distributeState(const gmx::MDLogger&         mdlog,
-                     gmx_domdec_t*                dd,
-                     const gmx_mtop_t&            mtop,
-                     t_state*                     state_global,
-                     const gmx_ddbox_t&           ddbox,
-                     t_state*                     state_local,
-                     PaddedHostVector<gmx::RVec>* f)
+void distributeState(const gmx::MDLogger& mdlog,
+                     gmx_domdec_t*        dd,
+                     const gmx_mtop_t&    mtop,
+                     t_state*             state_global,
+                     const gmx_ddbox_t&   ddbox,
+                     t_state*             state_local)
 {
     rvec* xGlobal = (DDMASTER(dd) ? state_global->x.rvec_array() : nullptr);
 
     distributeAtomGroups(mdlog, dd, mtop, DDMASTER(dd) ? state_global->box : nullptr, &ddbox, xGlobal);
 
-    dd_distribute_state(dd, state_global, state_local, f);
+    dd_distribute_state(dd, state_global, state_local);
 }
index 7dc1a7b53d3b6d29a6018eb6e3791ac2999898ae..7012f5bd600020e9543ea75b37864809c45e2b3d 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2018,2019, by the GROMACS development team, led by
+ * Copyright (c) 2018,2019,2020, 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.
@@ -42,7 +42,6 @@
 #ifndef GMX_DOMDEC_DOMDEC_DISTRIBUTE_H
 #define GMX_DOMDEC_DOMDEC_DISTRIBUTE_H
 
-#include "gromacs/gpu_utils/hostallocator.h"
 #include "gromacs/utility/basedefinitions.h"
 
 struct gmx_ddbox_t;
@@ -57,12 +56,11 @@ class MDLogger;
 }
 
 /*! \brief Distributes the state from the master rank to all DD ranks */
-void distributeState(const gmx::MDLogger&              mdlog,
-                     gmx_domdec_t*                     dd,
-                     const gmx_mtop_t&                 mtop,
-                     t_state*                          state_global,
-                     const gmx_ddbox_t&                ddbox,
-                     t_state*                          state_local,
-                     gmx::PaddedHostVector<gmx::RVec>* f);
+void distributeState(const gmx::MDLogger& mdlog,
+                     gmx_domdec_t*        dd,
+                     const gmx_mtop_t&    mtop,
+                     t_state*             state_global,
+                     const gmx_ddbox_t&   ddbox,
+                     t_state*             state_local);
 
 #endif
index bbe0cc67cf91f05fa762c78608cd050a182c4536..218a4e559de8683548c3ebb57de3bdb6970ee011 100644 (file)
 #include "gromacs/utility/gmxassert.h"
 #include "gromacs/utility/smalloc.h"
 
+namespace gmx
+{
+
 /* TODO: Add a routine that collects the initial setup of the algorithms.
  *
  * The final solution should be an MD algorithm base class with methods
  * for initialization and atom-data setup.
  */
-void mdAlgorithmsSetupAtomData(const t_commrec*  cr,
-                               const t_inputrec* ir,
-                               const gmx_mtop_t& top_global,
-                               gmx_localtop_t*   top,
-                               t_forcerec*       fr,
-                               gmx::MDAtoms*     mdAtoms,
-                               gmx::Constraints* constr,
-                               gmx_vsite_t*      vsite,
-                               gmx_shellfc_t*    shellfc)
+void mdAlgorithmsSetupAtomData(const t_commrec*        cr,
+                               const t_inputrec*       ir,
+                               const gmx_mtop_t&       top_global,
+                               gmx_localtop_t*         top,
+                               t_forcerec*             fr,
+                               PaddedHostVector<RVec>* force,
+                               MDAtoms*                mdAtoms,
+                               Constraints*            constr,
+                               gmx_vsite_t*            vsite,
+                               gmx_shellfc_t*          shellfc)
 {
     bool usingDomDec = DOMAINDECOMP(cr);
 
-    int  numAtomIndex, numHomeAtoms;
+    int  numAtomIndex;
     int* atomIndex;
+    int  numHomeAtoms;
+    int  numTotalAtoms;
 
     if (usingDomDec)
     {
-        numAtomIndex = dd_natoms_mdatoms(cr->dd);
-        atomIndex    = cr->dd->globalAtomIndices.data();
-        numHomeAtoms = dd_numHomeAtoms(*cr->dd);
+        numAtomIndex  = dd_natoms_mdatoms(cr->dd);
+        atomIndex     = cr->dd->globalAtomIndices.data();
+        numHomeAtoms  = dd_numHomeAtoms(*cr->dd);
+        numTotalAtoms = dd_natoms_mdatoms(cr->dd);
     }
     else
     {
-        numAtomIndex = -1;
-        atomIndex    = nullptr;
-        numHomeAtoms = top_global.natoms;
+        numAtomIndex  = -1;
+        atomIndex     = nullptr;
+        numHomeAtoms  = top_global.natoms;
+        numTotalAtoms = top_global.natoms;
+    }
+
+    if (force != nullptr)
+    {
+        /* We need to allocate one element extra, since we might use
+         * (unaligned) 4-wide SIMD loads to access rvec entries.
+         */
+        force->resizeWithPadding(numTotalAtoms);
     }
+
     atoms2md(&top_global, ir, numAtomIndex, atomIndex, numHomeAtoms, mdAtoms);
 
     auto mdatoms = mdAtoms->mdatoms();
@@ -140,3 +157,5 @@ void mdAlgorithmsSetupAtomData(const t_commrec*  cr,
         constr->setConstraints(top, *mdatoms);
     }
 }
+
+} // namespace gmx
index 2daa293526c87283c2c22fbbd1322d49001d64ec..369bf64d710a3343fd561e96f4a5686aeabdac8f 100644 (file)
@@ -43,6 +43,9 @@
 #ifndef GMX_DOMDEC_MDSETUP_H
 #define GMX_DOMDEC_MDSETUP_H
 
+#include "gromacs/gpu_utils/hostallocator.h"
+#include "gromacs/math/vectypes.h"
+
 struct bonded_threading_t;
 struct gmx_localtop_t;
 struct gmx_mtop_t;
@@ -57,7 +60,6 @@ namespace gmx
 {
 class Constraints;
 class MDAtoms;
-} // namespace gmx
 
 /*! \brief Gets the local shell with domain decomposition
  *
@@ -79,19 +81,23 @@ void make_local_shells(const t_commrec* cr, const t_mdatoms* md, gmx_shellfc_t*
  * \param[in]     top_global The global topology
  * \param[in,out] top        The local topology
  * \param[in,out] fr         The force calculation parameter/data record
+ * \param[out]    force      The force buffer
  * \param[out]    mdAtoms    The MD atom data
  * \param[in,out] constr     The constraints handler, can be NULL
  * \param[in,out] vsite      The virtual site data, can be NULL
  * \param[in,out] shellfc    The shell/flexible-constraint data, can be NULL
  */
-void mdAlgorithmsSetupAtomData(const t_commrec*  cr,
-                               const t_inputrec* ir,
-                               const gmx_mtop_t& top_global,
-                               gmx_localtop_t*   top,
-                               t_forcerec*       fr,
-                               gmx::MDAtoms*     mdAtoms,
-                               gmx::Constraints* constr,
-                               gmx_vsite_t*      vsite,
-                               gmx_shellfc_t*    shellfc);
+void mdAlgorithmsSetupAtomData(const t_commrec*        cr,
+                               const t_inputrec*       ir,
+                               const gmx_mtop_t&       top_global,
+                               gmx_localtop_t*         top,
+                               t_forcerec*             fr,
+                               PaddedHostVector<RVec>* force,
+                               MDAtoms*                mdAtoms,
+                               Constraints*            constr,
+                               gmx_vsite_t*            vsite,
+                               gmx_shellfc_t*          shellfc);
+
+} // namespace gmx
 
 #endif
index 7a36e5ae25024dab9e065fe3abf71f73940d4475..b4fa8901ed22578512b88ae9e28e8050a92844c2 100644 (file)
@@ -1807,12 +1807,7 @@ static void clearCommSetupData(dd_comm_setup_work_t* work)
 }
 
 //! Prepare DD communication.
-static void setup_dd_communication(gmx_domdec_t*                dd,
-                                   matrix                       box,
-                                   gmx_ddbox_t*                 ddbox,
-                                   t_forcerec*                  fr,
-                                   t_state*                     state,
-                                   PaddedHostVector<gmx::RVec>* f)
+static void setup_dd_communication(gmx_domdec_t* dd, matrix box, gmx_ddbox_t* ddbox, t_forcerec* fr, t_state* state)
 {
     int                    dim_ind, dim, dim0, dim1, dim2, dimd, nat_tot;
     int                    nzone, nzone_send, zone, zonei, cg0, cg1;
@@ -2089,7 +2084,7 @@ static void setup_dd_communication(gmx_domdec_t*                dd,
             ddSendrecv<int>(dd, dim_ind, dddirBackward, work.atomGroupBuffer, integerBufferRef);
 
             /* Make space for cg_cm */
-            dd_check_alloc_ncg(fr, state, f, pos_cg + ind->nrecv[nzone]);
+            dd_resize_atominfo_and_state(fr, state, pos_cg + ind->nrecv[nzone]);
 
             /* Communicate the coordinates */
             gmx::ArrayRef<gmx::RVec> rvecBufferRef;
@@ -2865,10 +2860,10 @@ void dd_partition_system(FILE*                        fplog,
 
         set_ddbox(*dd, true, DDMASTER(dd) ? state_global->box : nullptr, true, xGlobal, &ddbox);
 
-        distributeState(mdlog, dd, top_global, state_global, ddbox, state_local, f);
+        distributeState(mdlog, dd, top_global, state_global, ddbox, state_local);
 
         /* Ensure that we have space for the new distribution */
-        dd_check_alloc_ncg(fr, state_local, f, dd->ncg_home);
+        dd_resize_atominfo_and_state(fr, state_local, dd->ncg_home);
 
         inc_nrnb(nrnb, eNR_CGCM, comm->atomRanges.numHomeAtoms());
 
@@ -2961,7 +2956,7 @@ void dd_partition_system(FILE*                        fplog,
         wallcycle_sub_start(wcycle, ewcsDD_REDIST);
 
         ncgindex_set = dd->ncg_home;
-        dd_redistribute_cg(fplog, step, dd, ddbox.tric_dir, state_local, f, fr, nrnb, &ncg_moved);
+        dd_redistribute_cg(fplog, step, dd, ddbox.tric_dir, state_local, fr, nrnb, &ncg_moved);
 
         GMX_RELEASE_ASSERT(bSortCG, "Sorting is required after redistribution");
 
@@ -3015,7 +3010,7 @@ void dd_partition_system(FILE*                        fplog,
         dd_sort_state(dd, fr, state_local);
 
         /* After sorting and compacting we set the correct size */
-        dd_resize_state(state_local, f, comm->atomRanges.numHomeAtoms());
+        state_change_natoms(state_local, comm->atomRanges.numHomeAtoms());
 
         /* Rebuild all the indices */
         dd->ga2la->clear();
@@ -3049,7 +3044,7 @@ void dd_partition_system(FILE*                        fplog,
     make_dd_indices(dd, ncgindex_set);
 
     /* Setup up the communication and communicate the coordinates */
-    setup_dd_communication(dd, state_local->box, &ddbox, fr, state_local, f);
+    setup_dd_communication(dd, state_local->box, &ddbox, fr, state_local);
 
     /* Set the indices for the halo atoms */
     make_dd_indices(dd, dd->ncg_home);
@@ -3120,7 +3115,7 @@ void dd_partition_system(FILE*                        fplog,
      */
     state_local->natoms = comm->atomRanges.numAtomsTotal();
 
-    dd_resize_state(state_local, f, state_local->natoms);
+    state_change_natoms(state_local, state_local->natoms);
 
     if (fr->haveDirectVirialContributions)
     {
@@ -3155,7 +3150,7 @@ void dd_partition_system(FILE*                        fplog,
                         comm->atomRanges.end(DDAtomRanges::Type::Constraints), nat_f_novirsum);
 
     /* Update atom data for mdatoms and several algorithms */
-    mdAlgorithmsSetupAtomData(cr, ir, top_global, top_local, fr, mdAtoms, constr, vsite, nullptr);
+    mdAlgorithmsSetupAtomData(cr, ir, top_global, top_local, fr, f, mdAtoms, constr, vsite, nullptr);
 
     auto mdatoms = mdAtoms->mdatoms();
     if (!thisRankHasDuty(cr, DUTY_PME))
index ba86477b598c5db0b46f25b3fa3c1c2ce7c7877f..6b985c1345aa6d6a24407d1664cbab611a82282e 100644 (file)
@@ -1,8 +1,10 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019, by the.
- * Copyright (c) 2019, by the GROMACS development team, led by
+ * Copyright (c) 2005,2006,2007,2008,2009 by the GROMACS development team.
+ * Copyright (c) 2010,2011,2012,2013,2014 by the GROMACS development team.
+ * Copyright (c) 2015,2016,2017,2018,2019 by the GROMACS development team.
+ * Copyright (c) 2020, 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.
@@ -545,15 +547,14 @@ static void applyPbcAndSetMoveFlags(const gmx::UpdateGroupsCog&     updateGroups
     }
 }
 
-void dd_redistribute_cg(FILE*                        fplog,
-                        int64_t                      step,
-                        gmx_domdec_t*                dd,
-                        ivec                         tric_dir,
-                        t_state*                     state,
-                        PaddedHostVector<gmx::RVec>* f,
-                        t_forcerec*                  fr,
-                        t_nrnb*                      nrnb,
-                        int*                         ncg_moved)
+void dd_redistribute_cg(FILE*         fplog,
+                        int64_t       step,
+                        gmx_domdec_t* dd,
+                        ivec          tric_dir,
+                        t_state*      state,
+                        t_forcerec*   fr,
+                        t_nrnb*       nrnb,
+                        int*          ncg_moved)
 {
     gmx_domdec_comm_t* comm = dd->comm;
 
@@ -777,7 +778,7 @@ void dd_redistribute_cg(FILE*                        fplog,
             nvr += i;
         }
 
-        dd_check_alloc_ncg(fr, state, f, home_pos_cg + ncg_recv);
+        dd_resize_atominfo_and_state(fr, state, home_pos_cg + ncg_recv);
 
         /* Process the received charge or update groups */
         int buf_pos = 0;
index 732094f4d7319800e259af9e790dbe689f10ec45..68d8bd9c6ec924821cf42829fa57458efced6718 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2018,2019, by the GROMACS development team, led by
+ * Copyright (c) 2018,2019,2020, 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.
@@ -44,7 +44,7 @@
 
 #include <cstdio>
 
-#include "gromacs/gpu_utils/hostallocator.h"
+#include "gromacs/math/vectypes.h"
 #include "gromacs/utility/basedefinitions.h"
 
 struct gmx_domdec_t;
@@ -53,14 +53,13 @@ struct t_nrnb;
 class t_state;
 
 /*! \brief Redistribute the atoms to their, new, local domains */
-void dd_redistribute_cg(FILE*                             fplog,
-                        int64_t                           step,
-                        gmx_domdec_t*                     dd,
-                        ivec                              tric_dir,
-                        t_state*                          state,
-                        gmx::PaddedHostVector<gmx::RVec>* f,
-                        t_forcerec*                       fr,
-                        t_nrnb*                           nrnb,
-                        int*                              ncg_moved);
+void dd_redistribute_cg(FILE*         fplog,
+                        int64_t       step,
+                        gmx_domdec_t* dd,
+                        ivec          tric_dir,
+                        t_state*      state,
+                        t_forcerec*   fr,
+                        t_nrnb*       nrnb,
+                        int*          ncg_moved);
 
 #endif
index 01db3ead594b9480910a770afecaaf3b63f27a82..db2833c0c3990a5628b76fc180d7375ac413279c 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2018,2019, by the GROMACS development team, led by
+ * Copyright (c) 2018,2019,2020, 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.
@@ -102,34 +102,11 @@ void check_screw_box(const matrix box)
         gmx_fatal(FARGS, "pbc=screw with non-zero box_zy is not supported");
     }
 }
-/*! \brief Resize the state and f*/
-void dd_resize_state(t_state* state, PaddedHostVector<gmx::RVec>* f, int natoms)
-{
-    if (debug)
-    {
-        fprintf(debug, "Resizing state: currently %d, required %d\n", state->natoms, natoms);
-    }
-
-    state_change_natoms(state, natoms);
-
-    if (f != nullptr)
-    {
-        /* We need to allocate one element extra, since we might use
-         * (unaligned) 4-wide SIMD loads to access rvec entries.
-         */
-        f->resizeWithPadding(natoms);
-    }
-}
 
-/*! \brief Ensure fr, state and f, if != nullptr, can hold numChargeGroups
- *         atoms for the Verlet scheme and charge groups for the group scheme.
- *
- * todo refactor this now that group scheme is removed
- */
-void dd_check_alloc_ncg(t_forcerec* fr, t_state* state, PaddedHostVector<gmx::RVec>* f, int numChargeGroups)
+void dd_resize_atominfo_and_state(t_forcerec* fr, t_state* state, const int numAtoms)
 {
-    fr->cginfo.resize(numChargeGroups);
+    fr->cginfo.resize(numAtoms);
 
     /* We use x during the setup of the atom communication */
-    dd_resize_state(state, f, numChargeGroups);
+    state_change_natoms(state, numAtoms);
 }
index 353dc327b0f39a0c4207c7379a080568b583748a..4e36029e2e40b913591ed2b1a244bcc9e8835af7 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2018,2019, by the GROMACS development team, led by
+ * Copyright (c) 2018,2019,2020, 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.
@@ -42,9 +42,9 @@
 #ifndef GMX_DOMDEC_DOMDEC_UTILITY_H
 #define GMX_DOMDEC_DOMDEC_UTILITY_H
 
-#include "gromacs/gpu_utils/hostallocator.h"
 #include "gromacs/mdtypes/forcerec.h"
 #include "gromacs/utility/arrayref.h"
+#include "gromacs/math/vectypes.h"
 
 #include "domdec_internal.h"
 
@@ -96,22 +96,13 @@ static inline int dd_load_count(const gmx_domdec_comm_t* comm)
     return (comm->ddSettings.eFlop ? comm->flop_n : comm->cycl_n[ddCyclF]);
 }
 
-/*! \brief Resize the state and f, if !=nullptr, to natoms
+/*! \brief Ensure fr and state can hold numAtoms atoms
  *
- * \param[in]  state   Current state
- * \param[in]  f       The vector of forces to be resized
- * \param[out] natoms  New number of atoms to accommodate
+ * \param[in]  fr        Force record
+ * \param[in]  state     Current state
+ * \param[out] numAtoms  Number of atoms
  */
-void dd_resize_state(t_state* state, gmx::PaddedHostVector<gmx::RVec>* f, int natoms);
-
-/*! \brief Enrsure fr, state and f, if != nullptr, can hold numChargeGroups atoms for the Verlet scheme and charge groups for the group scheme
- *
- * \param[in]  fr               Force record
- * \param[in]  state            Current state
- * \param[in]  f                The force buffer
- * \param[out] numChargeGroups  Number of charged groups
- */
-void dd_check_alloc_ncg(t_forcerec* fr, t_state* state, gmx::PaddedHostVector<gmx::RVec>* f, int numChargeGroups);
+void dd_resize_atominfo_and_state(t_forcerec* fr, t_state* state, int numAtoms);
 
 /*! \brief Returns a domain-to-domain cutoff distance given an atom-to-atom cutoff */
 static inline real atomToAtomIntoDomainToDomainCutoff(const DDSystemInfo& systemInfo, real cutoff)
index 0d1828fe49ab8c51e612a2dc802e3d059ef846d6..8d3c5fbf03f8dc78fd246c5630a41f506184f756 100644 (file)
@@ -600,11 +600,6 @@ void forcerec_set_ranges(t_forcerec* fr, int natoms_force, int natoms_force_cons
     fr->natoms_force        = natoms_force;
     fr->natoms_force_constr = natoms_force_constr;
 
-    if (fr->natoms_force_constr > fr->nalloc_force)
-    {
-        fr->nalloc_force = over_alloc_dd(fr->natoms_force_constr);
-    }
-
     if (fr->haveDirectVirialContributions)
     {
         fr->forceBufferForDirectVirialContributions.resize(natoms_f_novirsum);
index c21c809ca2faa56f75581dfc3573608838f5e481..905c5f446babcf09f6b2c1d651178ee88dea13e6 100644 (file)
@@ -960,6 +960,10 @@ void do_force(FILE*                               fplog,
               int                                 legacyFlags,
               const DDBalanceRegionHandler&       ddBalanceRegionHandler)
 {
+    GMX_ASSERT(force.unpaddedArrayRef().ssize() >= fr->natoms_force_constr,
+               "The size of the force buffer should be at least the number of atoms to compute "
+               "forces for");
+
     nonbonded_verlet_t*          nbv      = fr->nbv.get();
     interaction_const_t*         ic       = fr->ic;
     gmx::StatePropagatorDataGpu* stateGpu = fr->stateGpu;
index 1900f4938c14cde1a393a31f2c34abe0468db67c..4147925cdf1118c7c1051329668822606ee374e6 100644 (file)
@@ -336,12 +336,11 @@ void gmx::LegacySimulator::do_md()
     else
     {
         state_change_natoms(state_global, state_global->natoms);
-        f.resizeWithPadding(state_global->natoms);
         /* Copy the pointer to the global state */
         state = state_global;
 
         /* Generate and initialize new topology */
-        mdAlgorithmsSetupAtomData(cr, ir, *top_global, &top, fr, mdAtoms, constr, vsite, shellfc);
+        mdAlgorithmsSetupAtomData(cr, ir, *top_global, &top, fr, &f, mdAtoms, constr, vsite, shellfc);
 
         upd.setNumAtoms(state->natoms);
     }
index 247e25caa608cee1d90a39498aae06365e603d64..bfb2cca0e3b1c92f4e6bd354bf350e1d50256bd9 100644 (file)
@@ -267,14 +267,10 @@ void gmx::LegacySimulator::do_mimic()
     else
     {
         state_change_natoms(state_global, state_global->natoms);
-        /* We need to allocate one element extra, since we might use
-         * (unaligned) 4-wide SIMD loads to access rvec entries.
-         */
-        f.resizeWithPadding(state_global->natoms);
         /* Copy the pointer to the global state */
         state = state_global;
 
-        mdAlgorithmsSetupAtomData(cr, ir, *top_global, &top, fr, mdAtoms, constr, vsite, shellfc);
+        mdAlgorithmsSetupAtomData(cr, ir, *top_global, &top, fr, &f, mdAtoms, constr, vsite, shellfc);
     }
 
     auto mdatoms = mdAtoms->mdatoms();
index afe557f2766d6b538483310db34aca1984808022..ca04f456dfdf9d7e6e5585c95357cfea7bde4fcb 100644 (file)
@@ -428,9 +428,8 @@ static void init_em(FILE*                fplog,
         /* Just copy the state */
         ems->s = *state_global;
         state_change_natoms(&ems->s, ems->s.natoms);
-        ems->f.resizeWithPadding(ems->s.natoms);
 
-        mdAlgorithmsSetupAtomData(cr, ir, *top_global, top, fr, mdAtoms, constr, vsite,
+        mdAlgorithmsSetupAtomData(cr, ir, *top_global, top, fr, &ems->f, mdAtoms, constr, vsite,
                                   shellfc ? *shellfc : nullptr);
 
         if (vsite)
index 3d369223a66f92d7cf130f7a32b6cc7bc3294298..6fe368c30e69c93960052a9fc0c38f7683bcff6c 100644 (file)
@@ -325,14 +325,10 @@ void gmx::LegacySimulator::do_rerun()
     else
     {
         state_change_natoms(state_global, state_global->natoms);
-        /* We need to allocate one element extra, since we might use
-         * (unaligned) 4-wide SIMD loads to access rvec entries.
-         */
-        f.resizeWithPadding(state_global->natoms);
         /* Copy the pointer to the global state */
         state = state_global;
 
-        mdAlgorithmsSetupAtomData(cr, ir, *top_global, &top, fr, mdAtoms, constr, vsite, shellfc);
+        mdAlgorithmsSetupAtomData(cr, ir, *top_global, &top, fr, &f, mdAtoms, constr, vsite, shellfc);
     }
 
     auto mdatoms = mdAtoms->mdatoms();
index 57d2e66ce493ee37095aab9e865fcf39c5aa0cf2..4eeae2fe782d5183efa0e12cabbabd97fba3af6c 100644 (file)
@@ -537,7 +537,7 @@ gmx_shellfc_t* init_shell_flexcon(FILE* fplog, const gmx_mtop_t* mtop, int nflex
     return shfc;
 }
 
-void make_local_shells(const t_commrec* cr, const t_mdatoms* md, gmx_shellfc_t* shfc)
+void gmx::make_local_shells(const t_commrec* cr, const t_mdatoms* md, gmx_shellfc_t* shfc)
 {
     int           a0, a1;
     gmx_domdec_t* dd = nullptr;
index 169cb1c15348833fcb1159768fafd71a3adf0d49..552580a7842eb5a07a9e5b0d0cf36e749018cb79 100644 (file)
@@ -214,11 +214,8 @@ struct t_forcerec
 
     /* The number of atoms participating in do_force_lowlevel */
     int natoms_force = 0;
-    /* The number of atoms participating in force and constraints */
+    /* The number of atoms participating in force calculation and constraints */
     int natoms_force_constr = 0;
-    /* The allocation size of vectors of size natoms_force */
-    int nalloc_force = 0;
-
     /* Forces that should not enter into the coord x force virial summation:
      * PPPM/PME/Ewald/posres/ForceProviders
      */
index d51084a2432f75feee050d06be575feddfa86264..ed4ba05e0782aa8db7c97ececb0d23a575e758d7 100644 (file)
@@ -65,8 +65,11 @@ TopologyHolder::TopologyHolder(const gmx_mtop_t& globalTopology,
         // Generate and initialize new topology
         // Note that most of the data needed for the constructor is used here -
         // this function should probably be simplified sooner or later.
-        mdAlgorithmsSetupAtomData(cr, inputrec, globalTopology, localTopology_.get(), fr, mdAtoms,
-                                  constr, vsite, nullptr);
+        // Note: Legacy mdrun resizes the force buffer in mdAlgorithmsSetupAtomData()
+        //       TopologyHolder has no access to the forces, so we are passing a nullptr
+        //       TODO: Find a unique approach to resizing the forces in modular simulator (#3461)
+        mdAlgorithmsSetupAtomData(cr, inputrec, globalTopology, localTopology_.get(), fr, nullptr,
+                                  mdAtoms, constr, vsite, nullptr);
     }
 }
 
index 37170225b988ef967fcdd07662789a9533e71937..53f03e6599f39bff0eb8994deca412c188f6d7eb 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2019, by the GROMACS development team, led by
+ * Copyright (c) 2019,2020, 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.