Removed smalloc.h from many DD files
authorBerk Hess <hess@kth.se>
Thu, 28 Jun 2018 20:40:41 +0000 (22:40 +0200)
committerMark Abraham <mark.j.abraham@gmail.com>
Sun, 1 Jul 2018 19:31:25 +0000 (21:31 +0200)
Many files in the domdec module included smalloc.h unnecessarily.
Replaced use of two pointers by std::vector in domdec_setup.cpp.

Change-Id: Ib2644330113660fedd53af9e6f87454224ec6fc5

src/gromacs/domdec/collect.cpp
src/gromacs/domdec/distribute.cpp
src/gromacs/domdec/domdec_setup.cpp
src/gromacs/domdec/redistribute.cpp
src/gromacs/domdec/utility.cpp
src/gromacs/domdec/utility.h

index d5564e18c89ab8ca96e0f26e7f36ef602ace7e6b..f7085ce596710af9be30b5705751b0e7bb711009 100644 (file)
@@ -50,7 +50,6 @@
 #include "gromacs/math/vec.h"
 #include "gromacs/mdtypes/state.h"
 #include "gromacs/utility/fatalerror.h"
-#include "gromacs/utility/smalloc.h"
 
 #include "atomdistribution.h"
 #include "distribute.h"
index 3072799e84349112ee92243f3738c8e45c00ac92..f517fadb8c91fc660270414e1c06c741d2120b17 100644 (file)
@@ -54,7 +54,6 @@
 #include "gromacs/mdtypes/df_history.h"
 #include "gromacs/mdtypes/state.h"
 #include "gromacs/utility/fatalerror.h"
-#include "gromacs/utility/smalloc.h"
 
 #include "atomdistribution.h"
 #include "cellsizes.h"
index ee5cee93cde319e48a7e0157d4ef3bf06ebfd8bd..dd4aabc99259479f583c3bb93126ea3d297b49f8 100644 (file)
@@ -62,7 +62,6 @@
 #include "gromacs/pbcutil/pbc.h"
 #include "gromacs/topology/topology.h"
 #include "gromacs/utility/fatalerror.h"
-#include "gromacs/utility/smalloc.h"
 
 /*! \brief Margin for setting up the DD grid */
 #define DD_GRID_MARGIN_PRES_SCALE 1.05
 /*! \brief Factorize \p n.
  *
  * \param[in]    n     Value to factorize
- * \param[out]   fac   Pointer to array of factors (to be allocated in this function)
- * \param[out]   mfac  Pointer to array of the number of times each factor repeats in the factorization (to be allocated in this function)
- * \return             The number of unique factors
+ * \param[out]   fac   Vector of factors (to be allocated in this function)
+ * \param[out]   mfac  Vector with the number of times each factor repeats in the factorization (to be allocated in this function)
  */
-static int factorize(int n, int **fac, int **mfac)
+static void factorize(int               n,
+                      std::vector<int> *fac,
+                      std::vector<int> *mfac)
 {
-    int d, ndiv;
-
     if (n <= 0)
     {
         gmx_fatal(FARGS, "Can only factorize positive integers.");
     }
 
     /* Decompose n in factors */
-    snew(*fac, n/2);
-    snew(*mfac, n/2);
-    d    = 2;
-    ndiv = 0;
+    fac->clear();
+    mfac->clear();
+    int d = 2;
     while (n > 1)
     {
         while (n % d == 0)
         {
-            if (ndiv == 0 || (*fac)[ndiv-1] != d)
+            if (fac->empty() || fac->back() != d)
+            {
+                fac->push_back(d);
+                mfac->push_back(1);
+            }
+            else
             {
-                ndiv++;
-                (*fac)[ndiv-1] = d;
+                mfac->back()++;
             }
-            (*mfac)[ndiv-1]++;
             n /= d;
         }
         d++;
     }
-
-    return ndiv;
 }
 
 /*! \brief Find largest divisor of \p n smaller than \p n*/
 static gmx_bool largest_divisor(int n)
 {
-    int ndiv, *div, *mdiv, ldiv;
-
-    ndiv = factorize(n, &div, &mdiv);
-    ldiv = div[ndiv-1];
-    sfree(div);
-    sfree(mdiv);
+    std::vector<int> div;
+    std::vector<int> mdiv;
+    factorize(n, &div, &mdiv);
 
-    return ldiv;
+    return div.back();
 }
 
 /*! \brief Compute largest common divisor of \p n1 and \b n2 */
@@ -145,22 +140,18 @@ static gmx_bool fits_pme_ratio(int nrank_tot, int nrank_pme, float ratio)
 /*! \brief Returns TRUE when npme out of ntot ranks doing PME is expected to give reasonable performance */
 static gmx_bool fits_pp_pme_perf(int ntot, int npme, float ratio)
 {
-    int ndiv, *div, *mdiv, ldiv;
-    int npp_root3, npme_root2;
+    std::vector<int> div;
+    std::vector<int> mdiv;
+    factorize(ntot - npme, &div, &mdiv);
 
-    ndiv = factorize(ntot - npme, &div, &mdiv);
-    ldiv = div[ndiv-1];
-    sfree(div);
-    sfree(mdiv);
-
-    npp_root3  = static_cast<int>(std::cbrt(ntot - npme) + 0.5);
-    npme_root2 = static_cast<int>(std::sqrt(static_cast<double>(npme)) + 0.5);
+    int npp_root3  = static_cast<int>(std::cbrt(ntot - npme) + 0.5);
+    int npme_root2 = static_cast<int>(std::sqrt(static_cast<double>(npme)) + 0.5);
 
     /* The check below gives a reasonable division:
      * factor 5 allowed at 5 or more PP ranks,
      * factor 7 allowed at 49 or more PP ranks.
      */
-    if (ldiv > 3 + npp_root3)
+    if (div.back() > 3 + npp_root3)
     {
         return FALSE;
     }
@@ -559,7 +550,8 @@ static void assign_factors(const gmx_domdec_t *dd,
                            const matrix box, const gmx_ddbox_t *ddbox,
                            int natoms, const t_inputrec *ir,
                            float pbcdxr, int npme,
-                           int ndiv, int *div, int *mdiv, ivec ir_try, ivec opt)
+                           int ndiv, const int *div, const int *mdiv,
+                           ivec ir_try, ivec opt)
 {
     int   x, y, i;
     float ce;
@@ -628,7 +620,7 @@ static real optimize_ncells(FILE *fplog,
                             gmx_bool bInterCGBondeds,
                             ivec nc)
 {
-    int      npp, npme, ndiv, *div, *mdiv, d, nmax;
+    int      npp, npme, d, nmax;
     double   pbcdxr;
     real     limit;
     ivec     itry;
@@ -720,17 +712,16 @@ static real optimize_ncells(FILE *fplog,
     }
 
     /* Decompose npp in factors */
-    ndiv = factorize(npp, &div, &mdiv);
+    std::vector<int> div;
+    std::vector<int> mdiv;
+    factorize(npp, &div, &mdiv);
 
     itry[XX] = 1;
     itry[YY] = 1;
     itry[ZZ] = 1;
     clear_ivec(nc);
     assign_factors(dd, limit, cutoff, box, ddbox, mtop->natoms, ir, pbcdxr,
-                   npme, ndiv, div, mdiv, itry, nc);
-
-    sfree(div);
-    sfree(mdiv);
+                   npme, div.size(), div.data(), mdiv.data(), itry, nc);
 
     return limit;
 }
index a29e79111ecded0208499ec3fcb9b090cc890c23..8afc95d8f9376fae1fab88a76e648d4e1cd77f2d 100644 (file)
@@ -57,7 +57,6 @@
 #include "gromacs/mdtypes/state.h"
 #include "gromacs/utility/cstringutil.h"
 #include "gromacs/utility/fatalerror.h"
-#include "gromacs/utility/smalloc.h"
 
 #include "domdec_internal.h"
 #include "utility.h"
index 311a13c3553c9496cd15751bec2e632ab3c2c468..978d4ba46709e80d8a7387132d177edb669923d2 100644 (file)
@@ -46,6 +46,7 @@
 
 #include "gromacs/mdtypes/state.h"
 #include "gromacs/utility/fatalerror.h"
+#include "gromacs/utility/smalloc.h"
 
 #include "domdec_internal.h"
 
index d52de93f5408e7af6f33a439c7cb0965187bdd13..c38553998ce6143dc9731d33ec9c52245c891dc8 100644 (file)
@@ -44,7 +44,6 @@
 
 #include "gromacs/math/paddedvector.h"
 #include "gromacs/mdtypes/forcerec.h"
-#include "gromacs/utility/smalloc.h"
 
 #include "domdec_internal.h"