Modernize t_nrnb
[alexxy/gromacs.git] / src / gromacs / gmxlib / nrnb.cpp
index 59ff411b15c6657ec9014d1daa4f603d25e4f2ab..5239e7506da312931ab2a16a4caa75b43a2fea71 100644 (file)
@@ -4,7 +4,7 @@
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
  * Copyright (c) 2013,2014,2015,2017,2018 by the GROMACS development team.
- * Copyright (c) 2019,2020, by the GROMACS development team, led by
+ * Copyright (c) 2019,2020,2021, 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.
 #include <cstdlib>
 #include <cstring>
 
-#include <algorithm>
-#include <vector>
-
-#include "gromacs/mdtypes/commrec.h"
-#include "gromacs/mdtypes/md_enums.h"
-#include "gromacs/utility/arraysize.h"
-
 typedef struct
 {
     const char* name;
@@ -205,8 +198,8 @@ static void pr_two(FILE* out, int c, int i)
 
 static void pr_difftime(FILE* out, double dt)
 {
-    int      ndays, nhours, nmins, nsecs;
-    gmx_bool bPrint, bPrinted;
+    int  ndays, nhours, nmins, nsecs;
+    bool bPrint, bPrinted;
 
     ndays    = static_cast<int>(dt / (24 * 3600));
     dt       = dt - 24 * 3600 * ndays;
@@ -260,29 +253,15 @@ static void pr_difftime(FILE* out, double dt)
 
 void clear_nrnb(t_nrnb* nrnb)
 {
-    int i;
-
-    for (i = 0; (i < eNRNB); i++)
+    for (int i = 0; (i < eNRNB); i++)
     {
         nrnb->n[i] = 0.0;
     }
 }
 
-void add_nrnb(t_nrnb* dest, t_nrnb* s1, t_nrnb* s2)
-{
-    int i;
-
-    for (i = 0; (i < eNRNB); i++)
-    {
-        dest->n[i] = s1->n[i] + s2->n[i];
-    }
-}
-
 void print_nrnb(FILE* out, t_nrnb* nrnb)
 {
-    int i;
-
-    for (i = 0; (i < eNRNB); i++)
+    for (int i = 0; (i < eNRNB); i++)
     {
         if (nrnb->n[i] > 0)
         {
@@ -291,35 +270,32 @@ void print_nrnb(FILE* out, t_nrnb* nrnb)
     }
 }
 
-void _inc_nrnb(t_nrnb* nrnb, int enr, int inc, char gmx_unused* file, int gmx_unused line)
-{
-    nrnb->n[enr] += inc;
-#ifdef DEBUG_NRNB
-    printf("nrnb %15s(%2d) incremented with %8d from file %s line %d\n", nbdata[enr].name, enr, inc, file, line);
-#endif
-}
-
 /* Returns in enr is the index of a full nbnxn VdW kernel */
-static gmx_bool nrnb_is_nbnxn_vdw_kernel(int enr)
+static bool nrnb_is_nbnxn_vdw_kernel(int enr)
 {
     return (enr >= eNR_NBNXN_LJ_RF && enr <= eNR_NBNXN_LJ_E);
 }
 
 /* Returns in enr is the index of an nbnxn kernel addition (LJ modification) */
-static gmx_bool nrnb_is_nbnxn_kernel_addition(int enr)
+static bool nrnb_is_nbnxn_kernel_addition(int enr)
 {
     return (enr >= eNR_NBNXN_ADD_LJ_FSW && enr <= eNR_NBNXN_ADD_LJ_EWALD_E);
 }
 
+void atomicNrnbIncrement(t_nrnb* nrnb, int index, int increment)
+{
+#pragma omp atomic
+    nrnb->n[index] += increment;
+}
+
 void print_flop(FILE* out, t_nrnb* nrnb, double* nbfs, double* mflop)
 {
-    int         i, j;
     double      mni, frac, tfrac, tflop;
     const char* myline =
             "-----------------------------------------------------------------------------";
 
     *nbfs = 0.0;
-    for (i = 0; (i < eNR_NBKERNEL_TOTAL_NR); i++)
+    for (int i = 0; (i < eNR_NBKERNEL_TOTAL_NR); i++)
     {
         if (std::strstr(nbdata[i].name, "W3-W3") != nullptr)
         {
@@ -343,7 +319,7 @@ void print_flop(FILE* out, t_nrnb* nrnb, double* nbfs, double* mflop)
         }
     }
     tflop = 0;
-    for (i = 0; (i < eNRNB); i++)
+    for (int i = 0; (i < eNRNB); i++)
     {
         tflop += 1e-6 * nrnb->n[i] * nbdata[i].flop;
     }
@@ -370,7 +346,7 @@ void print_flop(FILE* out, t_nrnb* nrnb, double* nbfs, double* mflop)
     }
     *mflop = 0.0;
     tfrac  = 0.0;
-    for (i = 0; (i < eNRNB); i++)
+    for (int i = 0; (i < eNRNB); i++)
     {
         mni = 1e-6 * nrnb->n[i];
         /* Skip empty entries and nbnxn additional flops,
@@ -384,7 +360,7 @@ void print_flop(FILE* out, t_nrnb* nrnb, double* nbfs, double* mflop)
             if (nrnb_is_nbnxn_vdw_kernel(i))
             {
                 /* Possibly add the cost of an LJ switch/Ewald function */
-                for (j = eNR_NBNXN_ADD_LJ_FSW; j <= eNR_NBNXN_ADD_LJ_EWALD; j += 2)
+                for (int j = eNR_NBNXN_ADD_LJ_FSW; j <= eNR_NBNXN_ADD_LJ_EWALD; j += 2)
                 {
                     int e_kernel_add;
 
@@ -512,131 +488,3 @@ const char* nrnb_str(int enr)
 {
     return nbdata[enr].name;
 }
-
-static const int force_index[] = {
-    eNR_BONDS,  eNR_ANGLES, eNR_PROPER, eNR_IMPROPER, eNR_RB,
-    eNR_DISRES, eNR_ORIRES, eNR_POSRES, eNR_FBPOSRES, eNR_NS,
-};
-#define NFORCE_INDEX asize(force_index)
-
-static const int constr_index[] = { eNR_SHAKE,  eNR_SHAKE_RIJ,  eNR_SETTLE,  eNR_UPDATE,
-                                    eNR_PCOUPL, eNR_CONSTR_VIR, eNR_CONSTR_V };
-#define NCONSTR_INDEX asize(constr_index)
-
-static double pr_av(FILE* log, t_commrec* cr, double fav, const double ftot[], const char* title)
-{
-    int    i, perc;
-    double dperc, unb;
-
-    unb = 0;
-    if (fav > 0)
-    {
-        fav /= cr->nnodes - cr->npmenodes;
-        fprintf(log, "\n %-26s", title);
-        for (i = 0; (i < cr->nnodes); i++)
-        {
-            dperc = (100.0 * ftot[i]) / fav;
-            unb   = std::max(unb, dperc);
-            perc  = static_cast<int>(dperc);
-            fprintf(log, "%3d ", perc);
-        }
-        if (unb > 0)
-        {
-            perc = static_cast<int>(10000.0 / unb);
-            fprintf(log, "%6d%%\n\n", perc);
-        }
-        else
-        {
-            fprintf(log, "\n\n");
-        }
-    }
-    return unb;
-}
-
-void pr_load(FILE* log, t_commrec* cr, t_nrnb nrnb[])
-{
-    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]));
-        /* Cost due to forces */
-        for (int j = 0; (j < eNR_NBKERNEL_TOTAL_NR); j++)
-        {
-            ftot[i] += nrnb[i].n[j] * cost_nrnb(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 (int j = 0; (j < NCONSTR_INDEX); j++)
-        {
-            stot[i] += nrnb[i].n[constr_index[j]] * cost_nrnb(constr_index[j]);
-        }
-    }
-    for (int j = 0; (j < eNRNB); j++)
-    {
-        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 (int i = 0; (i < cr->nnodes); i++)
-    {
-        fprintf(log, "%3d ", i);
-    }
-    fprintf(log, "Scaling\n");
-    fprintf(log, "---------------------------");
-    for (int i = 0; (i < cr->nnodes); i++)
-    {
-        fprintf(log, "----");
-    }
-    fprintf(log, "-------\n");
-
-    for (int j = 0; (j < eNRNB); j++)
-    {
-        double unb = 100.0;
-        if (av.n[j] > 0)
-        {
-            double dperc;
-            int    perc;
-            fprintf(log, " %-26s", nrnb_str(j));
-            for (int i = 0; (i < cr->nnodes); i++)
-            {
-                dperc = (100.0 * nrnb[i].n[j]) / av.n[j];
-                unb   = std::max(unb, dperc);
-                perc  = static_cast<int>(dperc);
-                fprintf(log, "%3d ", perc);
-            }
-            if (unb > 0)
-            {
-                perc = static_cast<int>(10000.0 / unb);
-                fprintf(log, "%6d%%\n", perc);
-            }
-            else
-            {
-                fprintf(log, "\n");
-            }
-        }
-    }
-    double fav = 0;
-    double sav = 0;
-    for (int i = 0; (i < cr->nnodes); i++)
-    {
-        fav += ftot[i];
-        sav += stot[i];
-    }
-    double uf = pr_av(log, cr, fav, ftot.data(), "Total Force");
-    double us = pr_av(log, cr, sav, stot.data(), "Total Constr.");
-
-    double unb = (uf * fav + us * sav) / (fav + sav);
-    if (unb > 0)
-    {
-        unb = 10000.0 / unb;
-        fprintf(log, "\nTotal Scaling: %.0f%% of max performance\n\n", unb);
-    }
-}