Fix use of _POSIX_THREAD*
authorMark Abraham <mark.j.abraham@gmail.com>
Wed, 1 Jun 2016 08:48:07 +0000 (10:48 +0200)
committerTeemu Murtola <teemu.murtola@gmail.com>
Fri, 1 Jul 2016 18:57:33 +0000 (20:57 +0200)
This fixes a couple of aspects of behaviour. Formerly, if
_POSIX_THREADS was defined and equal to zero, we might have used
clock_gettime and got some kind of error (compiling/linking/runtime
behaviour). Similarly, if _POSIX_THREADS was undefined, C99 defines
such preprocessor symbols as zero, so we again used clock_gettime
inappropriately.

Now we avoid compiler warnings if the symbol is undefined, and when it
is defined we use clock_gettime only when _POSIX_THREADS_ has a value
such that it is supposed to work.

Adapted this an the BG/Q fix also for
gmx_gettime_per_thread(). Expanded the documentation of why the code
is the way it is. Noted future TODO to consider std::chrono.

Fixes #1980

Change-Id: Ib3e40903e2344354074c5328d40e8467f264b51f

src/gromacs/timing/walltime_accounting.cpp

index ee102f7305d420b2d14f3b83b3c300f6b3516bcc..bfcbf1d294cd28349c9483d850168e5a1b8911d7 100644 (file)
@@ -59,7 +59,9 @@
  * If/when any kind of task parallelism is implemented (even OpenMP
  * regions simultaneously assigned to different tasks), consider
  * whether this data structure (and/or cycle counters) should be
- * maintained on a per-OpenMP-thread basis. */
+ * maintained on a per-OpenMP-thread basis.
+ *
+ * Consider also replacing this with std::chrono. */
 
 /*! \brief Manages caching wall-clock time measurements for
  * simulations */
@@ -193,12 +195,13 @@ walltime_accounting_set_nsteps_done(gmx_walltime_accounting_t   walltime_account
 double
 gmx_gettime()
 {
-#if HAVE_CLOCK_GETTIME && _POSIX_TIMERS >= 0 && !(defined __bgq__ && defined __clang__)
-    /* Mac and Windows do not support this. For added fun, Windows
-     * defines _POSIX_TIMERS without actually providing the
-     * implementation. The BlueGene/Q CNK only supports gettimeofday,
-     * and bgclang doesn't provide a fully functional implementation
-     * for clock_gettime (unlike xlc). */
+    /* Use clock_gettime only if we know linking the C run-time
+       library will work (which is not trivial on e.g. Crays), and its
+       headers claim sufficient support for POSIX (ie not Mac and
+       Windows), and it isn't BG/Q (whose compute node kernel only
+       supports gettimeofday, and bgclang doesn't provide a fully
+       functional implementation clock_gettime, unlike xlc). */
+#if HAVE_CLOCK_GETTIME && defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 && !(defined __bgq__ && defined __clang__)
     struct timespec t;
     double          seconds;
 
@@ -229,7 +232,13 @@ gmx_gettime()
 static double
 gmx_gettime_per_thread()
 {
-#if HAVE_CLOCK_GETTIME && _POSIX_THREAD_CPUTIME >= 0
+    /* Use clock_gettime only if we know linking the C run-time
+       library will work (which is not trivial on e.g. Crays), and its
+       headers claim sufficient support for POSIX (ie not Mac and
+       Windows), and it isn't BG/Q (whose compute node kernel only
+       supports gettimeofday, and bgclang doesn't provide a fully
+       functional implementation clock_gettime, unlike xlc). */
+#if HAVE_CLOCK_GETTIME && defined(_POSIX_THREAD_CPUTIME) && _POSIX_THREAD_CPUTIME > 0 && !(defined __bgq__ && defined __clang__)
     struct timespec t;
     double          seconds;