Add initialization to t_nrnb
authorBerk Hess <hess@kth.se>
Fri, 26 Jul 2019 13:19:32 +0000 (15:19 +0200)
committerEric Irrgang <ericirrgang@gmail.com>
Mon, 29 Jul 2019 22:53:09 +0000 (00:53 +0200)
Also fixed a memory leak.

Change-Id: I9c4bd3ed1919c82cf26d1181ea98d4c2dcc37b0b

src/gromacs/ewald/pme_only.cpp
src/gromacs/gmxana/gmx_disre.cpp
src/gromacs/gmxlib/nrnb.cpp
src/gromacs/gmxlib/nrnb.h
src/gromacs/mdlib/resethandler.cpp
src/gromacs/mdrun/md.cpp
src/gromacs/mdrun/mimic.cpp
src/gromacs/mdrun/minimize.cpp
src/gromacs/mdrun/rerun.cpp
src/gromacs/mdrun/runner.cpp

index 3d6c5bb3b00fd557748efc4c105e5e815b93a158..6f8ebe1723e80eb7cca84ddbfeada3a23a7d9064 100644 (file)
@@ -164,7 +164,7 @@ static void reset_pmeonly_counters(gmx_wallcycle_t           wcycle,
     /* Reset all the counters related to performance over the run */
     wallcycle_stop(wcycle, ewcRUN);
     wallcycle_reset_all(wcycle);
-    init_nrnb(nrnb);
+    *nrnb = { 0 };
     wallcycle_start(wcycle, ewcRUN);
     walltime_accounting_reset_time(walltime_accounting, step);
 
@@ -552,7 +552,7 @@ int gmx_pmeonly(struct gmx_pme_t *pme,
         changePinningPolicy(&pme_pp->x, pme_get_pinning_policy());
     }
 
-    init_nrnb(mynrnb);
+    clear_nrnb(mynrnb);
 
     count = 0;
     do /****** this is a quasi-loop over time steps! */
index d4d2941a239f04642636e327e2d63249f32c525c..9c6fe6564fbaf32d275c7f4d015389c5466a302e 100644 (file)
@@ -52,7 +52,6 @@
 #include "gromacs/fileio/xvgr.h"
 #include "gromacs/gmxana/gmx_ana.h"
 #include "gromacs/gmxana/gstat.h"
-#include "gromacs/gmxlib/nrnb.h"
 #include "gromacs/listed_forces/disre.h"
 #include "gromacs/math/do_fit.h"
 #include "gromacs/math/functions.h"
@@ -660,7 +659,6 @@ int gmx_disre(int argc, char *argv[])
     FILE             *out = nullptr, *aver = nullptr, *numv = nullptr, *maxxv = nullptr, *xvg = nullptr;
     gmx_localtop_t    top;
     t_fcdata          fcd;
-    t_nrnb            nrnb;
     t_graph          *g;
     int               i, j, kkk;
     t_trxstatus      *status;
@@ -808,7 +806,6 @@ int gmx_disre(int argc, char *argv[])
     auto mdAtoms = gmx::makeMDAtoms(fplog, *topInfo.mtop(), *ir, false);
     atoms2md(topInfo.mtop(), ir, -1, nullptr, ntopatoms, mdAtoms.get());
     update_mdatoms(mdAtoms->mdatoms(), ir->fepvals->init_lambda);
-    init_nrnb(&nrnb);
     if (ir->ePBC != epbcNONE)
     {
         gpbc = gmx_rmpbc_init(&top.idef, ir->ePBC, natoms);
index 2755e8f23892fae77d9e31d471a5748e00dbd8c8..08de8f3af1e8d09f66c79559340f31f7e9fcd81d 100644 (file)
 #include <cstring>
 
 #include <algorithm>
+#include <vector>
 
 #include "gromacs/mdtypes/commrec.h"
 #include "gromacs/mdtypes/md_enums.h"
 #include "gromacs/utility/arraysize.h"
-#include "gromacs/utility/smalloc.h"
 
 typedef struct {
     const char *name;
@@ -254,7 +254,7 @@ static void pr_difftime(FILE *out, double dt)
     fprintf(out, "\n");
 }
 
-void init_nrnb(t_nrnb *nrnb)
+void clear_nrnb(t_nrnb *nrnb)
 {
     int i;
 
@@ -264,16 +264,6 @@ void init_nrnb(t_nrnb *nrnb)
     }
 }
 
-void cp_nrnb(t_nrnb *dest, t_nrnb *src)
-{
-    int i;
-
-    for (i = 0; (i < eNRNB); i++)
-    {
-        dest->n[i] = src->n[i];
-    }
-}
-
 void add_nrnb(t_nrnb *dest, t_nrnb *s1, t_nrnb *s2)
 {
     int i;
@@ -551,63 +541,59 @@ static double pr_av(FILE *log, t_commrec *cr,
 
 void pr_load(FILE *log, t_commrec *cr, t_nrnb nrnb[])
 {
-    int     i, j, perc;
-    double  dperc, unb, uf, us;
-    double *ftot, fav;
-    double *stot, sav;
-    t_nrnb *av;
-
-    snew(av, 1);
-    snew(ftot, cr->nnodes);
-    snew(stot, cr->nnodes);
-    init_nrnb(av);
-    for (i = 0; (i < cr->nnodes); i++)
+    t_nrnb              av;
+
+    std::vector<double> ftot(cr->nnodes);
+    std::vector<double> stot(cr->nnodes);
+    for (int i = 0; (i < cr->nnodes); i++)
     {
-        add_nrnb(av, av, &(nrnb[i]));
+        add_nrnb(&av, &av, &(nrnb[i]));
         /* Cost due to forces */
-        for (j = 0; (j < eNR_NBKERNEL_TOTAL_NR); j++)
+        for (int j = 0; (j < eNR_NBKERNEL_TOTAL_NR); j++)
         {
             ftot[i] += nrnb[i].n[j]*cost_nrnb(j);
         }
-        for (j = 0; (j < NFORCE_INDEX); j++)
+        for (int j = 0; (j < NFORCE_INDEX); j++)
         {
             ftot[i] += nrnb[i].n[force_index[j]]*cost_nrnb(force_index[j]);
         }
         /* Due to shake */
-        for (j = 0; (j < NCONSTR_INDEX); j++)
+        for (int j = 0; (j < NCONSTR_INDEX); j++)
         {
             stot[i] += nrnb[i].n[constr_index[j]]*cost_nrnb(constr_index[j]);
         }
     }
-    for (j = 0; (j < eNRNB); j++)
+    for (int j = 0; (j < eNRNB); j++)
     {
-        av->n[j] = av->n[j]/static_cast<double>(cr->nnodes - cr->npmenodes);
+        av.n[j] = av.n[j]/static_cast<double>(cr->nnodes - cr->npmenodes);
     }
 
     fprintf(log, "\nDetailed load balancing info in percentage of average\n");
 
     fprintf(log, " Type                 RANK:");
-    for (i = 0; (i < cr->nnodes); i++)
+    for (int i = 0; (i < cr->nnodes); i++)
     {
         fprintf(log, "%3d ", i);
     }
     fprintf(log, "Scaling\n");
     fprintf(log, "---------------------------");
-    for (i = 0; (i < cr->nnodes); i++)
+    for (int i = 0; (i < cr->nnodes); i++)
     {
         fprintf(log, "----");
     }
     fprintf(log, "-------\n");
 
-    for (j = 0; (j < eNRNB); j++)
+    for (int j = 0; (j < eNRNB); j++)
     {
-        unb = 100.0;
-        if (av->n[j] > 0)
+        double unb = 100.0;
+        if (av.n[j] > 0)
         {
+            double dperc;
+            int    perc;
             fprintf(log, " %-26s", nrnb_str(j));
-            for (i = 0; (i < cr->nnodes); i++)
+            for (int i = 0; (i < cr->nnodes); i++)
             {
-                dperc = (100.0*nrnb[i].n[j])/av->n[j];
+                dperc = (100.0*nrnb[i].n[j])/av.n[j];
                 unb   = std::max(unb, dperc);
                 perc  = static_cast<int>(dperc);
                 fprintf(log, "%3d ", perc);
@@ -623,16 +609,17 @@ void pr_load(FILE *log, t_commrec *cr, t_nrnb nrnb[])
             }
         }
     }
-    fav = sav = 0;
-    for (i = 0; (i < cr->nnodes); i++)
+    double fav = 0;
+    double sav = 0;
+    for (int i = 0; (i < cr->nnodes); i++)
     {
         fav += ftot[i];
         sav += stot[i];
     }
-    uf = pr_av(log, cr, fav, ftot, "Total Force");
-    us = pr_av(log, cr, sav, stot, "Total Constr.");
+    double uf = pr_av(log, cr, fav, ftot.data(), "Total Force");
+    double us = pr_av(log, cr, sav, stot.data(), "Total Constr.");
 
-    unb = (uf*fav+us*sav)/(fav+sav);
+    double unb = (uf*fav+us*sav)/(fav+sav);
     if (unb > 0)
     {
         unb = 10000.0/unb;
index dbdcd202ce9679bd8519adab7f21d91caf69b5e5..66decbebcaa39ae0e60ecf8a3fd543e6b83e97b8 100644 (file)
@@ -117,17 +117,14 @@ enum
 };
 
 
-typedef struct t_nrnb
+struct t_nrnb
 {
-    double n[eNRNB];
-}
-t_nrnb;
-struct t_commrec;
-struct t_nrnb;
+    double n[eNRNB] = { 0 };
+};
 
-void init_nrnb(t_nrnb *nrnb);
+struct t_commrec;
 
-void cp_nrnb(t_nrnb *dest, t_nrnb *src);
+void clear_nrnb(t_nrnb *nrnb);
 
 void add_nrnb(t_nrnb *dest, t_nrnb *s1, t_nrnb *s2);
 
index 6c2cc15b0e4c3fbacfaaef6ac48fe7f0471b82be..7d1f7ed8b9757f56b32d7ef3d2dd3eb14b81da02 100644 (file)
@@ -195,7 +195,7 @@ bool ResetHandler::resetCountersImpl(
         {
             reset_dd_statistics_counters(cr->dd);
         }
-        init_nrnb(nrnb);
+        clear_nrnb(nrnb);
         wallcycle_start(wcycle, ewcRUN);
         walltime_accounting_reset_time(walltime_accounting, step);
         print_date_and_time(fplog, cr->nodeid, "Restarted time", gmx_gettime());
index d2e44dbc6090b2ffe0222a32213813cc0aca23e6..9c8c4344c3b0dc167e3c0b855c9024232f1040be 100644 (file)
@@ -256,7 +256,6 @@ void gmx::Simulator::do_md()
     {
         pleaseCiteCouplingAlgorithms(fplog, *ir);
     }
-    init_nrnb(nrnb);
     gmx_mdoutf       *outf = init_mdoutf(fplog, nfile, fnm, mdrunOptions, cr, outputProvider, ir, top_global, oenv, wcycle,
                                          StartingBehavior::NewSimulation);
     gmx::EnergyOutput energyOutput(mdoutf_get_fp_ene(outf), top_global, ir, pull_work, mdoutf_get_fp_dhdl(outf), false);
index 26477d2e28dcd7dac84a21b8abf325e809f3424d..6f1ede62d6a1e6255ad41e8f0d8ff7fc9da44c6f 100644 (file)
@@ -225,7 +225,6 @@ void gmx::Simulator::do_mimic()
     top_global->intermolecularExclusionGroup = genQmmmIndices(*top_global);
 
     initialize_lambdas(fplog, *ir, MASTER(cr), &state_global->fep_state, state_global->lambda, lam0);
-    init_nrnb(nrnb);
 
     gmx_mdoutf        *outf = init_mdoutf(fplog, nfile, fnm, mdrunOptions, cr, outputProvider, ir, top_global, oenv, wcycle,
                                           StartingBehavior::NewSimulation);
index 5058bf7493f6a9d9102944527127bf2f762c6b79..b0417c518456efc93c98f3bebc848f53e150f42b 100644 (file)
@@ -376,8 +376,6 @@ static void init_em(FILE *fplog,
     }
     initialize_lambdas(fplog, *ir, MASTER(cr), &(state_global->fep_state), state_global->lambda, nullptr);
 
-    init_nrnb(nrnb);
-
     if (ir->eI == eiNM)
     {
         GMX_ASSERT(shellfc != nullptr, "With NM we always support shells");
@@ -2756,9 +2754,6 @@ Simulator::do_nm()
         snew(full_matrix, sz*sz);
     }
 
-    init_nrnb(nrnb);
-
-
     /* Write start time and temperature */
     print_em_start(fplog, cr, walltime_accounting, wcycle, NM);
 
index 5694b71524a6fb90272779f3255833ad33712766..d28eb93c2f29fea59d81e2b9c4930b659187e456 100644 (file)
@@ -298,7 +298,6 @@ void gmx::Simulator::do_rerun()
     }
 
     initialize_lambdas(fplog, *ir, MASTER(cr), &state_global->fep_state, state_global->lambda, lam0);
-    init_nrnb(nrnb);
     gmx_mdoutf       *outf = init_mdoutf(fplog, nfile, fnm, mdrunOptions, cr, outputProvider, ir, top_global, oenv, wcycle,
                                          StartingBehavior::NewSimulation);
     gmx::EnergyOutput energyOutput(mdoutf_get_fp_ene(outf), top_global, ir, pull_work, mdoutf_get_fp_dhdl(outf), true);
index 70c3ce97784f630785a01802897612c295917eec..162ea9ea7eca2e0832e2d410bac250b48787ac12 100644 (file)
@@ -437,7 +437,6 @@ static void finish_run(FILE *fplog,
                        const gmx_pme_t *pme,
                        gmx_bool bWriteStat)
 {
-    t_nrnb *nrnb_tot = nullptr;
     double  delta_t  = 0;
     double  nbfs     = 0, mflop = 0;
     double  elapsed_time,
@@ -465,9 +464,12 @@ static void finish_run(FILE *fplog,
         printReport = false;
     }
 
+    t_nrnb                  *nrnb_tot;
+    std::unique_ptr<t_nrnb>  nrnbTotalStorage;
     if (cr->nnodes > 1)
     {
-        snew(nrnb_tot, 1);
+        nrnbTotalStorage = std::make_unique<t_nrnb>();
+        nrnb_tot         = nrnbTotalStorage.get();
 #if GMX_MPI
         MPI_Allreduce(nrnb->n, nrnb_tot->n, eNRNB, MPI_DOUBLE, MPI_SUM,
                       cr->mpi_comm_mysim);
@@ -507,10 +509,6 @@ static void finish_run(FILE *fplog,
     {
         print_flop(fplog, nrnb_tot, &nbfs, &mflop);
     }
-    if (cr->nnodes > 1)
-    {
-        sfree(nrnb_tot);
-    }
 
     if (thisRankHasDuty(cr, DUTY_PP) && DOMAINDECOMP(cr))
     {
@@ -566,7 +564,6 @@ static void finish_run(FILE *fplog,
 int Mdrunner::mdrunner()
 {
     matrix                    box;
-    t_nrnb                   *nrnb;
     t_forcerec               *fr               = nullptr;
     t_fcdata                 *fcd              = nullptr;
     real                      ewaldcoeff_q     = 0;
@@ -1237,7 +1234,7 @@ int Mdrunner::mdrunner()
     std::unique_ptr<MDAtoms>     mdAtoms;
     std::unique_ptr<gmx_vsite_t> vsite;
 
-    snew(nrnb, 1);
+    t_nrnb nrnb;
     if (thisRankHasDuty(cr, DUTY_PP))
     {
         /* Initiate forcerecord */
@@ -1422,7 +1419,7 @@ int Mdrunner::mdrunner()
                                     || observablesHistory.edsamHistory);
         auto constr              = makeConstraints(mtop, *inputrec, pull_work, doEssentialDynamics,
                                                    fplog, *mdAtoms->mdatoms(),
-                                                   cr, ms, nrnb, wcycle, fr->bMolPBC);
+                                                   cr, ms, &nrnb, wcycle, fr->bMolPBC);
 
         /* Energy terms and groups */
         gmx_enerdata_t enerd(mtop.groups.groups[SimulationAtomGroupType::EnergyOutput].size(), inputrec->fepvals->n_lambda);
@@ -1466,7 +1463,7 @@ int Mdrunner::mdrunner()
             fcd,
             globalState.get(),
             &observablesHistory,
-            mdAtoms.get(), nrnb, wcycle, fr,
+            mdAtoms.get(), &nrnb, wcycle, fr,
             &enerd,
             &ppForceWorkload,
             replExParams,
@@ -1487,7 +1484,7 @@ int Mdrunner::mdrunner()
         GMX_RELEASE_ASSERT(pmedata, "pmedata was NULL while cr->duty was not DUTY_PP");
         /* do PME only */
         walltime_accounting = walltime_accounting_init(gmx_omp_nthreads_get(emntPME));
-        gmx_pmeonly(pmedata, cr, nrnb, wcycle, walltime_accounting, inputrec, pmeRunMode);
+        gmx_pmeonly(pmedata, cr, &nrnb, wcycle, walltime_accounting, inputrec, pmeRunMode);
     }
 
     wallcycle_stop(wcycle, ewcRUN);
@@ -1496,7 +1493,7 @@ int Mdrunner::mdrunner()
      * if rerunMD, don't write last frame again
      */
     finish_run(fplog, mdlog, cr,
-               inputrec, nrnb, wcycle, walltime_accounting,
+               inputrec, &nrnb, wcycle, walltime_accounting,
                fr ? fr->nbv.get() : nullptr,
                pmedata,
                EI_DYNAMICS(inputrec->eI) && !isMultiSim(ms));
@@ -1534,7 +1531,6 @@ int Mdrunner::mdrunner()
     /* Does what it says */
     print_date_and_time(fplog, cr->nodeid, "Finished mdrun", gmx_gettime());
     walltime_accounting_destroy(walltime_accounting);
-    sfree(nrnb);
 
     // Ensure log file content is written
     if (logFileHandle)