Muster
 All Classes Namespaces Files Functions Variables Typedefs Macros
timing.cpp
Go to the documentation of this file.
1 #include "timing.h"
2 #include "muster-config.h"
3 
4 #include <cmath>
5 using namespace std;
6 
7 
8 #if defined(MUSTER_BLUEGENE_L)
9 // -------------------------------------------------------- //
10 // Timing code for BlueGene/L
11 // -------------------------------------------------------- //
12 #include <rts.h>
13 
14 // this will return number of nanoseconds in a single BGL cycle
15 // use for converting from cycle units returned by rts_gettimebase
16 // to nanoseconds.
17 static double get_ns_per_cycle() {
18  BGLPersonality personality;
19  if (rts_get_personality(&personality, sizeof(personality)) != 0)
20  return 0;
21  return 1.0e9/((double) personality.clockHz);
22 }
23 
24 // returns time in nanoseconds.
26  static double ns_per_cycle = get_ns_per_cycle();
27  return (timing_t)(ns_per_cycle * rts_get_timebase());
28 }
29 
30 
31 #elif defined(MUSTER_BLUEGENE_P)
32 // -------------------------------------------------------- //
33 // Timing code for BlueGene/P
34 // -------------------------------------------------------- //
35 #define SPRN_TBRL 0x10C // Time Base Read Lower Register (user & sup R/O)
36 #define SPRN_TBRU 0x10D // Time Base Read Upper Register (user & sup R/O)
37 #define BGP_NS_PER_CYCLE (1.0/0.85) // Nanoseconds per cycle on BGP (850Mhz clock)
38 
39 #define _bgp_mfspr(SPRN) ({ \
40  unsigned int tmp; \
41  do { \
42  asm volatile ("mfspr %0,%1" : "=&r" (tmp) : "i" (SPRN) : "memory" ); \
43  } while(0); \
44  tmp; \
45 })
46 
47 union bgp_time_reg {
48  unsigned int ul[2];
49  unsigned long long ull;
50 };
51 
52 static inline unsigned long long timebase() {
53  bgp_time_reg reg;
54  unsigned int utmp;
55 
56  do {
57  utmp = _bgp_mfspr(SPRN_TBRU);
58  reg.ul[1] = _bgp_mfspr(SPRN_TBRL);
59  reg.ul[0] = _bgp_mfspr(SPRN_TBRU);
60  }
61  while( utmp != reg.ul[0] );
62 
63  return reg.ull;
64 }
65 
67  return llround(BGP_NS_PER_CYCLE * timebase());
68 }
69 
70 
71 
72 #elif (defined(MUSTER_HAVE_CLOCK_GETTIME))
73 // -------------------------------------------------------- //
74 // Timing code using Linux hires timers.
75 // -------------------------------------------------------- //
76 
77 #include <ctime>
78 #include <sys/time.h>
79 
81  struct timespec ts;
82  clock_gettime(CLOCK_MONOTONIC, &ts);
83  return (ts.tv_sec * 1000000000ll + ts.tv_nsec);
84 }
85 
86 #elif defined(MUSTER_HAVE_MACH_TIME)
87 
88 #include <mach/mach.h>
89 #include <mach/mach_time.h>
90 
92  static mach_timebase_info_data_t timebase_info;
93  if (timebase_info.denom == 0) {
94  mach_timebase_info(&timebase_info);
95  }
96  timing_t machtime = mach_absolute_time();
97  return machtime * timebase_info.numer / timebase_info.denom;
98 }
99 
100 #elif defined(MUSTER_HAVE_GETTIMEOFDAY)
101 // -------------------------------------------------------- //
102 // Generic timing code using gettimeofday.
103 // -------------------------------------------------------- //
104 
105 #include <ctime>
106 #include <sys/time.h>
107 
109  struct timeval tv;
110  gettimeofday(&tv, NULL);
111  return tv.tv_sec * 1000000000ll + tv.tv_usec * 1000ll;
112 }
113 
114 
115 #else // if we get to here, we don't even have gettimeofday.
116 #error "NO SUPPORTED TIMING FUNCTIONS FOUND!"
117 #endif // types of timers
unsigned long long timing_t
Size for timings reported by get_time_ns.
Definition: timing.h:9
timing_t get_time_ns()
This is defined differently depending on compile-time timing options and on the platform we&#39;re using...
Muster. Copyright © 2010, Lawrence Livermore National Laboratory, LLNL-CODE-433662.
Distribution of Muster and its documentation is subject to terms of the Muster LICENSE.
Generated on Thu Sep 1 2016 using Doxygen 1.8.5