Move responsibility for bonded threading decomposition
[alexxy/gromacs.git] / src / gromacs / listed-forces / listed-forces.cpp
index 634f24ddf65bb4ebecd71d7b71c4d77305d64b1f..3258c8021936b38f43e1a6bef7bc44f98c47b42f 100644 (file)
@@ -89,7 +89,7 @@ isPairInteraction(int ftype)
 
 /*! \brief Zero thread-local output buffers */
 static void
-zero_thread_output(struct bonded_threading_t *bt, int thread)
+zero_thread_output(bonded_threading_t *bt, int thread)
 {
     if (!bt->haveBondeds)
     {
@@ -141,7 +141,7 @@ zero_thread_output(struct bonded_threading_t *bt, int thread)
 /*! \brief Reduce thread-local force buffers */
 static void
 reduce_thread_forces(int n, rvec *f,
-                     struct bonded_threading_t *bt,
+                     bonded_threading_t *bt,
                      int nthreads)
 {
     if (nthreads > MAX_BONDED_THREADS)
@@ -199,7 +199,7 @@ reduce_thread_forces(int n, rvec *f,
 static void
 reduce_thread_output(int n, rvec *f, rvec *fshift,
                      real *ener, gmx_grppairener_t *grpp, real *dvdl,
-                     struct bonded_threading_t *bt,
+                     bonded_threading_t *bt,
                      gmx_bool bCalcEnerVir,
                      gmx_bool bDHDL)
 {
@@ -259,6 +259,7 @@ reduce_thread_output(int n, rvec *f, rvec *fshift,
 static real
 calc_one_bond(int thread,
               int ftype, const t_idef *idef,
+              const bonded_threading_t &bondedThreading,
               const rvec x[], rvec4 f[], rvec fshift[],
               const t_forcerec *fr,
               const t_pbc *pbc, const t_graph *g,
@@ -295,10 +296,10 @@ calc_one_bond(int thread,
     nbonds    = idef->il[ftype].nr/nat1;
     iatoms    = idef->il[ftype].iatoms;
 
-    GMX_ASSERT(idef->il_thread_division[ftype*(idef->nthreads + 1) + idef->nthreads] == idef->il[ftype].nr, "The thread division should match the topology");
+    GMX_ASSERT(bondedThreading.il_thread_division[ftype*(bondedThreading.nthreads + 1) + bondedThreading.nthreads] == idef->il[ftype].nr, "The thread division should match the topology");
 
-    nb0 = idef->il_thread_division[ftype*(idef->nthreads+1)+thread];
-    nbn = idef->il_thread_division[ftype*(idef->nthreads+1)+thread+1] - nb0;
+    nb0 = bondedThreading.il_thread_division[ftype*(bondedThreading.nthreads+1)+thread];
+    nbn = bondedThreading.il_thread_division[ftype*(bondedThreading.nthreads+1)+thread+1] - nb0;
 
     if (!isPairInteraction(ftype))
     {
@@ -427,7 +428,7 @@ calcBondedForces(const t_idef     *idef,
                  gmx_bool          bCalcEnerVir,
                  int              *global_atom_index)
 {
-    struct bonded_threading_t *bt = fr->bonded_threading;
+    bonded_threading_t *bt = fr->bondedThreading;
 
 #pragma omp parallel for num_threads(bt->nthreads) schedule(static)
     for (int thread = 0; thread < bt->nthreads; thread++)
@@ -465,7 +466,8 @@ calcBondedForces(const t_idef     *idef,
             {
                 if (idef->il[ftype].nr > 0 && ftype_is_bonded_potential(ftype))
                 {
-                    v = calc_one_bond(thread, ftype, idef, x,
+                    v = calc_one_bond(thread, ftype, idef,
+                                      *fr->bondedThreading, x,
                                       ft, fshift, fr, pbc_null, g, grpp,
                                       nrnb, lambda, dvdlt,
                                       md, fcd, bCalcEnerVir,
@@ -495,13 +497,9 @@ void calc_listed(const t_commrec             *cr,
                  t_fcdata *fcd, int *global_atom_index,
                  int force_flags)
 {
-    struct bonded_threading_t *bt;
     gmx_bool                   bCalcEnerVir;
     const  t_pbc              *pbc_null;
-
-    bt = fr->bonded_threading;
-
-    assert(bt->nthreads == idef->nthreads);
+    bonded_threading_t        *bt  = fr->bondedThreading;
 
     bCalcEnerVir = (force_flags & (GMX_FORCE_VIRIAL | GMX_FORCE_ENERGY));
 
@@ -610,12 +608,13 @@ void calc_listed_lambda(const t_idef *idef,
                         t_fcdata *fcd,
                         int *global_atom_index)
 {
-    real          v;
-    real          dvdl_dum[efptNR] = {0};
-    rvec4        *f;
-    rvec         *fshift;
-    const  t_pbc *pbc_null;
-    t_idef        idef_fe;
+    real               v;
+    real               dvdl_dum[efptNR] = {0};
+    rvec4             *f;
+    rvec              *fshift;
+    const  t_pbc      *pbc_null;
+    t_idef             idef_fe;
+    bonded_threading_t bondedThreading;
 
     if (fr->bMolPBC)
     {
@@ -627,9 +626,9 @@ void calc_listed_lambda(const t_idef *idef,
     }
 
     /* Copy the whole idef, so we can modify the contents locally */
-    idef_fe          = *idef;
-    idef_fe.nthreads = 1;
-    snew(idef_fe.il_thread_division, F_NRE*(idef_fe.nthreads+1));
+    idef_fe                  = *idef;
+    bondedThreading.nthreads = 1;
+    snew(bondedThreading.il_thread_division, F_NRE*(bondedThreading.nthreads+1));
 
     /* We already have the forces, so we use temp buffers here */
     snew(f, fr->natoms_force);
@@ -647,12 +646,12 @@ void calc_listed_lambda(const t_idef *idef,
             ilist_fe.nr_nonperturbed = 0;
             ilist_fe.nr              = ilist.nr - ilist.nr_nonperturbed;
             /* Set the work range of thread 0 to the perturbed bondeds */
-            idef_fe.il_thread_division[ftype*2 + 0] = 0;
-            idef_fe.il_thread_division[ftype*2 + 1] = ilist_fe.nr;
+            bondedThreading.il_thread_division[ftype*2 + 0] = 0;
+            bondedThreading.il_thread_division[ftype*2 + 1] = ilist_fe.nr;
 
             if (ilist_fe.nr > 0)
             {
-                v = calc_one_bond(0, ftype, &idef_fe,
+                v = calc_one_bond(0, ftype, &idef_fe, bondedThreading,
                                   x, f, fshift, fr, pbc_null, g,
                                   grpp, nrnb, lambda, dvdl_dum,
                                   md, fcd, TRUE,
@@ -665,7 +664,7 @@ void calc_listed_lambda(const t_idef *idef,
     sfree(fshift);
     sfree(f);
 
-    sfree(idef_fe.il_thread_division);
+    sfree(bondedThreading.il_thread_division);
 }
 
 void