Moved timing source to C++
authorErik Lindahl <erik@kth.se>
Thu, 16 Jul 2015 17:06:15 +0000 (19:06 +0200)
committerGerrit Code Review <gerrit@gerrit.gromacs.org>
Tue, 28 Jul 2015 08:21:30 +0000 (10:21 +0200)
Change-Id: I175cf79b68330767b90e0780829f67aa9ca50af5

src/gromacs/timing/CMakeLists.txt
src/gromacs/timing/cyclecounter.cpp [moved from src/gromacs/timing/cyclecounter.c with 93% similarity]
src/gromacs/timing/wallcycle.cpp [moved from src/gromacs/timing/wallcycle.c with 94% similarity]
src/gromacs/timing/walltime_accounting.cpp [moved from src/gromacs/timing/walltime_accounting.c with 96% similarity]

index f0a5af0a1b4e7a58d693ec80898d4e1b95dcd558..627e7e0b6caeb8f52c30b519a6ac24d812092c9c 100644 (file)
@@ -1,7 +1,7 @@
 #
 # This file is part of the GROMACS molecular simulation package.
 #
-# Copyright (c) 2013,2014, by the GROMACS development team, led by
+# Copyright (c) 2013,2014,2015, 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.
@@ -32,7 +32,7 @@
 # To help us fund GROMACS development, we humbly ask that you cite
 # the research papers on the package. Check out http://www.gromacs.org.
 
-file(GLOB TIMING_SOURCES *.cpp *.c)
+file(GLOB TIMING_SOURCES *.cpp)
 set(LIBGROMACS_SOURCES ${LIBGROMACS_SOURCES} ${TIMING_SOURCES} PARENT_SCOPE)
 
 gmx_install_headers(
similarity index 93%
rename from src/gromacs/timing/cyclecounter.c
rename to src/gromacs/timing/cyclecounter.cpp
index 780587e11477cca335e5de29aa0139d04aa0088a..09204ae145efabb5af0e3ac5ba094b9a7b9700d1 100644 (file)
@@ -2,7 +2,7 @@
  * This file is part of the GROMACS molecular simulation package.
  *
  * Copyright (c) 1991-2006 David van der Spoel, Erik Lindahl, Berk Hess, University of Groningen.
- * Copyright (c) 2013,2014, by the GROMACS development team, led by
+ * Copyright (c) 2013,2014,2015, 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.
@@ -39,7 +39,7 @@
 
 #include "config.h"
 
-#include <time.h>
+#include <ctime>
 
 #ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>
@@ -72,7 +72,7 @@ gmx_cycles_calibrate(double sampletime)
 
     QueryPerformanceFrequency(&i);
 
-    return 1.0/((double) i.QuadPart);
+    return 1.0/static_cast<double>(i.QuadPart);
     /* end of MS Windows implementation */
 
 #elif (defined HAVE_GETTIMEOFDAY)
@@ -82,7 +82,6 @@ gmx_cycles_calibrate(double sampletime)
     gmx_cycles_t   c1, c2;
     double         timediff, cyclediff;
     double         d = 0.1; /* Dummy variable so we don't optimize away delay loop */
-    int            i;
 
     if (!gmx_cycles_have_counter())
     {
@@ -116,15 +115,14 @@ gmx_cycles_calibrate(double sampletime)
          * that will underflow to zero in most cases. By conditionally adding it
          * to a result at the end it cannot be removed. n=10000 is arbitrary...
          */
-        for (i = 0; i < 10000; i++)
+        for (int i = 0; i < 10000; i++)
         {
-            d = d/(1.0+(double)i);
+            d = d/(1.0+static_cast<double>(i));
         }
         /* Read the time again */
         gettimeofday(&t2, NULL);
         c2       = gmx_cycles_read();
-        timediff = (double)(t2.tv_sec-t1.tv_sec)+
-            (double)(t2.tv_usec-t1.tv_usec)*1e-6;
+        timediff = static_cast<double>(t2.tv_sec-t1.tv_sec)+(t2.tv_usec-t1.tv_usec)*1e-6;
     }
     while (timediff < sampletime);
 
similarity index 94%
rename from src/gromacs/timing/wallcycle.c
rename to src/gromacs/timing/wallcycle.cpp
index 4961ce58a3303529327e44df7fd005a9c644f14a..050d6eee8873cd630f9bbafa33fd36b683e96834 100644 (file)
 
 #include "config.h"
 
-#include <assert.h>
-#include <stdlib.h>
+#include <cstdlib>
 
 #include "gromacs/legacyheaders/md_logging.h"
 #include "gromacs/legacyheaders/types/commrec.h"
 #include "gromacs/timing/cyclecounter.h"
 #include "gromacs/timing/gpu_timing.h"
 #include "gromacs/utility/cstringutil.h"
+#include "gromacs/utility/gmxassert.h"
 #include "gromacs/utility/gmxmpi.h"
 #include "gromacs/utility/smalloc.h"
 #include "gromacs/utility/snprintf.h"
@@ -110,6 +110,7 @@ static const char *wcn[ewcNR] =
     "Enforced rotation", "Add rot. forces", "Coordinate swapping", "IMD", "Test"
 };
 
+#ifdef GMX_CYCLE_SUBCOUNTERS
 static const char *wcsn[ewcsNR] =
 {
     "DD redist.", "DD NS grid + sort", "DD setup comm.",
@@ -124,6 +125,7 @@ static const char *wcsn[ewcsNR] =
     "NB X buffer ops.",
     "NB F buffer ops.",
 };
+#endif
 
 gmx_bool wallcycle_have_counter(void)
 {
@@ -344,7 +346,7 @@ double wallcycle_stop(gmx_wallcycle_t wc, int ewc)
 void wallcycle_get(gmx_wallcycle_t wc, int ewc, int *n, double *c)
 {
     *n = wc->wcc[ewc].n;
-    *c = (double)wc->wcc[ewc].c;
+    *c = static_cast<double>(wc->wcc[ewc].c);
 }
 
 void wallcycle_reset_all(gmx_wallcycle_t wc)
@@ -393,7 +395,7 @@ static void subtract_cycles(wallcc_t *wcc, int ewc_main, int ewc_sub)
 {
     if (wcc[ewc_sub].n > 0)
     {
-        assert(wcc[ewc_main].c >= wcc[ewc_sub].c);
+        GMX_ASSERT(wcc[ewc_main].c >= wcc[ewc_sub].c, "Subcounter cannot have more ticks than parent");
 
         wcc[ewc_main].c -= wcc[ewc_sub].c;
     }
@@ -403,7 +405,11 @@ void wallcycle_sum(t_commrec *cr, gmx_wallcycle_t wc)
 {
     wallcc_t *wcc;
     double    cycles[ewcNR+ewcsNR];
-    double    cycles_n[ewcNR+ewcsNR], buf[ewcNR+ewcsNR], *cyc_all, *buf_all;
+#ifdef GMX_MPI
+    double    cycles_n[ewcNR+ewcsNR];
+    double    buf[ewcNR+ewcsNR];
+    double   *buf_all, *cyc_all;
+#endif
     int       i, j;
     int       nsum;
 
@@ -466,7 +472,7 @@ void wallcycle_sum(t_commrec *cr, gmx_wallcycle_t wc)
         if (wcc[ewcPMEMESH].n > 0)
         {
             /* This must be a PME only node, calculate the Wait + Comm. time */
-            assert(wcc[ewcRUN].c >= wcc[ewcPMEMESH].c);
+            GMX_ASSERT(wcc[ewcRUN].c >= wcc[ewcPMEMESH].c, "Total run ticks must be greater than PME-only ticks");
             wcc[ewcPMEWAITCOMM].c = wcc[ewcRUN].c - wcc[ewcPMEMESH].c;
         }
     }
@@ -474,16 +480,20 @@ void wallcycle_sum(t_commrec *cr, gmx_wallcycle_t wc)
     /* Store the cycles in a double buffer for summing */
     for (i = 0; i < ewcNR; i++)
     {
-        cycles_n[i] = (double)wcc[i].n;
-        cycles[i]   = (double)wcc[i].c;
+#ifdef GMX_MPI
+        cycles_n[i] = static_cast<double>(wcc[i].n);
+#endif
+        cycles[i]   = static_cast<double>(wcc[i].c);
     }
     nsum = ewcNR;
 #ifdef GMX_CYCLE_SUBCOUNTERS
     for (i = 0; i < ewcsNR; i++)
     {
         wc->wcsc[i].c    *= wc->nthreads_pp;
-        cycles_n[ewcNR+i] = (double)wc->wcsc[i].n;
-        cycles[ewcNR+i]   = (double)wc->wcsc[i].c;
+#ifdef GMX_MPI
+        cycles_n[ewcNR+i] = static_cast<double>(wc->wcsc[i].n);
+#endif
+        cycles[ewcNR+i]   = static_cast<double>(wc->wcsc[i].c);
     }
     nsum += ewcsNR;
 #endif
@@ -495,12 +505,12 @@ void wallcycle_sum(t_commrec *cr, gmx_wallcycle_t wc)
                       cr->mpi_comm_mysim);
         for (i = 0; i < ewcNR; i++)
         {
-            wcc[i].n = (int)(buf[i] + 0.5);
+            wcc[i].n = static_cast<int>(buf[i] + 0.5);
         }
 #ifdef GMX_CYCLE_SUBCOUNTERS
         for (i = 0; i < ewcsNR; i++)
         {
-            wc->wcsc[i].n = (int)(buf[ewcNR+i] + 0.5);
+            wc->wcsc[i].n = static_cast<int>(buf[ewcNR+i] + 0.5);
         }
 #endif
 
@@ -519,7 +529,7 @@ void wallcycle_sum(t_commrec *cr, gmx_wallcycle_t wc)
                           cr->mpi_comm_mysim);
             for (i = 0; i < ewcNR*ewcNR; i++)
             {
-                wc->wcc_all[i].c = buf_all[i];
+                wc->wcc_all[i].c = static_cast<gmx_cycles_t>(buf_all[i]);
             }
             sfree(buf_all);
             sfree(cyc_all);
@@ -658,18 +668,18 @@ void wallcycle_print(FILE *fplog, int nnodes, int npme, double realtime,
     }
 
     nth_pp  = wc->nthreads_pp;
-    assert(nth_pp > 0);
+    GMX_ASSERT(nth_pp > 0, "Number of particle-particle threads must be >0");
 
     nth_pme = wc->nthreads_pme;
-    assert(nth_pme > 0);
+    GMX_ASSERT(nth_pme > 0, "Number of PME threads must be >0");
 
     cyc_sum = wc->cycles_sum;
 
-    assert(nnodes > 0);
-    assert(npme >= 0);
+    GMX_ASSERT(nnodes > 0, "Number of nodes must be >0");
+    GMX_ASSERT(npme >= 0, "Number of PME nodes cannot be negative");
     npp     = nnodes - npme;
     /* npme is the number of PME-only ranks used, and we always do PP work */
-    assert(npp > 0);
+    GMX_ASSERT(npp > 0, "Number of particle-particle nodes must be >0");
 
     nth_tot = npp*nth_pp + npme*nth_pme;
 
@@ -694,10 +704,10 @@ void wallcycle_print(FILE *fplog, int nnodes, int npme, double realtime,
 
     /* Conversion factor from cycles to seconds */
     c2t     = realtime/tot;
-    c2t_pp  = c2t * nth_tot / (double) (npp*nth_pp);
+    c2t_pp  = c2t * nth_tot / static_cast<double>(npp*nth_pp);
     if (npme > 0)
     {
-        c2t_pme = c2t * nth_tot / (double) (npme*nth_pme);
+        c2t_pme = c2t * nth_tot / static_cast<double>(npme*nth_pme);
     }
     else
     {
@@ -752,7 +762,7 @@ void wallcycle_print(FILE *fplog, int nnodes, int npme, double realtime,
             }
         }
     }
-    tot_for_rest = tot * (npp * nth_pp) / (double) nth_tot;
+    tot_for_rest = tot * npp * nth_pp / static_cast<double>(nth_tot);
     print_cycles(fplog, c2t_pp, "Rest",
                  npp, nth_pp,
                  -1, tot_for_rest - tot_for_pp, tot);
similarity index 96%
rename from src/gromacs/timing/walltime_accounting.c
rename to src/gromacs/timing/walltime_accounting.cpp
index ec0cba0b1e35fc988e9029e78013005e2449eea0..5eeed23c80f8e4f07b9e78166c41684d0499e3f4 100644 (file)
@@ -2,7 +2,7 @@
  * This file is part of the GROMACS molecular simulation package.
  *
  * Copyright (c) 2013, The GROMACS development team.
- * Copyright (c) 2013,2014, by the GROMACS development team, led by
+ * Copyright (c) 2013,2014,2015, 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.
@@ -39,7 +39,7 @@
 
 #include "config.h"
 
-#include <time.h>
+#include <ctime>
 
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
@@ -201,7 +201,7 @@ gmx_gettime()
     double          seconds;
 
     clock_gettime(CLOCK_REALTIME, &t);
-    seconds = (double) t.tv_sec + 1e-9*(double)t.tv_nsec;
+    seconds = static_cast<double>(t.tv_sec) + 1e-9*t.tv_nsec;
 
     return seconds;
 #elif defined HAVE_GETTIMEOFDAY
@@ -211,7 +211,7 @@ gmx_gettime()
     double         seconds;
 
     gettimeofday(&t, NULL);
-    seconds = (double) t.tv_sec + 1e-6*(double)t.tv_usec;
+    seconds = static_cast<double>(t.tv_sec) + 1e-6*t.tv_usec;
 
     return seconds;
 #else
@@ -231,7 +231,7 @@ gmx_gettime_per_thread()
     double          seconds;
 
     clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t);
-    seconds = (double) t.tv_sec + 1e-9*(double)t.tv_nsec;
+    seconds = static_cast<double>(t.tv_sec) + 1e-9*t.tv_nsec;
 
     return seconds;
 #else