timing.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2012, 2014-2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012 CNRS
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu.h>
  18. #include <common/config.h>
  19. #include <starpu_util.h>
  20. #include <profiling/profiling.h>
  21. #include <common/timing.h>
  22. #include <math.h>
  23. #ifdef STARPU_SIMGRID
  24. #include <core/simgrid.h>
  25. #endif
  26. #if defined(_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
  27. #include <windows.h>
  28. #endif
  29. #ifdef STARPU_SIMGRID
  30. void _starpu_timing_init(void)
  31. {
  32. }
  33. void _starpu_clock_gettime(struct timespec *ts)
  34. {
  35. double now = MSG_get_clock();
  36. ts->tv_sec = floor(now);
  37. ts->tv_nsec = floor((now - ts->tv_sec) * 1000000000);
  38. }
  39. #elif defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
  40. #include <time.h>
  41. #ifndef _POSIX_C_SOURCE
  42. /* for clock_gettime */
  43. #define _POSIX_C_SOURCE 199309L
  44. #endif
  45. #ifdef __linux__
  46. #ifndef CLOCK_MONOTONIC_RAW
  47. #define CLOCK_MONOTONIC_RAW 4
  48. #endif
  49. #endif
  50. static struct timespec _starpu_reference_start_time_ts;
  51. /* Modern CPUs' clocks are usually not synchronized so we use a monotonic clock
  52. * to have consistent timing measurements. The CLOCK_MONOTONIC_RAW clock is not
  53. * subject to NTP adjustments, but is not available on all systems (in that
  54. * case we use the CLOCK_MONOTONIC clock instead). */
  55. static void _starpu_clock_readtime(struct timespec *ts)
  56. {
  57. #ifdef CLOCK_MONOTONIC_RAW
  58. static int raw_supported = 0;
  59. switch (raw_supported)
  60. {
  61. case -1:
  62. break;
  63. case 1:
  64. clock_gettime(CLOCK_MONOTONIC_RAW, ts);
  65. return;
  66. case 0:
  67. if (clock_gettime(CLOCK_MONOTONIC_RAW, ts))
  68. {
  69. raw_supported = -1;
  70. break;
  71. }
  72. else
  73. {
  74. raw_supported = 1;
  75. return;
  76. }
  77. }
  78. #endif
  79. clock_gettime(CLOCK_MONOTONIC, ts);
  80. }
  81. void _starpu_timing_init(void)
  82. {
  83. _starpu_clock_gettime(&_starpu_reference_start_time_ts);
  84. }
  85. void _starpu_clock_gettime(struct timespec *ts)
  86. {
  87. struct timespec absolute_ts;
  88. /* Read the current time */
  89. _starpu_clock_readtime(&absolute_ts);
  90. /* Compute the relative time since initialization */
  91. starpu_timespec_sub(&absolute_ts, &_starpu_reference_start_time_ts, ts);
  92. }
  93. #else // !HAVE_CLOCK_GETTIME
  94. #if defined(__i386__) || defined(__pentium__) || defined(__pentiumpro__) || defined(__i586__) || defined(__i686__) || defined(__k6__) || defined(__k7__) || defined(__x86_64__)
  95. union starpu_u_tick
  96. {
  97. uint64_t tick;
  98. struct
  99. {
  100. uint32_t low;
  101. uint32_t high;
  102. }
  103. sub;
  104. };
  105. #define STARPU_GET_TICK(t) __asm__ volatile("rdtsc" : "=a" ((t).sub.low), "=d" ((t).sub.high))
  106. #define STARPU_TICK_RAW_DIFF(t1, t2) ((t2).tick - (t1).tick)
  107. #define STARPU_TICK_DIFF(t1, t2) (STARPU_TICK_RAW_DIFF(t1, t2) - _starpu_residual)
  108. static union starpu_u_tick _starpu_reference_start_tick;
  109. static double _starpu_scale = 0.0;
  110. static unsigned long long _starpu_residual = 0;
  111. static int _starpu_inited = 0;
  112. #if defined(_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
  113. static int mygettimeofday(struct timeval *tv, void *tz)
  114. {
  115. if (tv)
  116. {
  117. FILETIME ft;
  118. unsigned long long res;
  119. GetSystemTimeAsFileTime(&ft);
  120. /* 100-nanosecond intervals since January 1, 1601 */
  121. res = ft.dwHighDateTime;
  122. res <<= 32;
  123. res |= ft.dwLowDateTime;
  124. res /= 10;
  125. /* Now we have microseconds */
  126. res -= (((1970-1601)*365) + 89) * 24ULL * 3600ULL * 1000000ULL;
  127. /* Now we are based on epoch */
  128. tv->tv_sec = res / 1000000ULL;
  129. tv->tv_usec = res % 1000000ULL;
  130. }
  131. }
  132. #else
  133. #define mygettimeofday(tv,tz) gettimeofday(tv,tz)
  134. #endif
  135. void _starpu_timing_init(void)
  136. {
  137. static union starpu_u_tick t1, t2;
  138. int i;
  139. if (_starpu_inited) return;
  140. _starpu_residual = (unsigned long long)1 << 63;
  141. for(i = 0; i < 20; i++)
  142. {
  143. STARPU_GET_TICK(t1);
  144. STARPU_GET_TICK(t2);
  145. _starpu_residual = STARPU_MIN(_starpu_residual, STARPU_TICK_RAW_DIFF(t1, t2));
  146. }
  147. {
  148. struct timeval tv1,tv2;
  149. STARPU_GET_TICK(t1);
  150. mygettimeofday(&tv1,0);
  151. starpu_sleep(0.5);
  152. STARPU_GET_TICK(t2);
  153. mygettimeofday(&tv2,0);
  154. _starpu_scale = ((tv2.tv_sec*1e6 + tv2.tv_usec) -
  155. (tv1.tv_sec*1e6 + tv1.tv_usec)) /
  156. (double)(STARPU_TICK_DIFF(t1, t2));
  157. }
  158. STARPU_GET_TICK(_starpu_reference_start_tick);
  159. _starpu_inited = 1;
  160. }
  161. void _starpu_clock_gettime(struct timespec *ts)
  162. {
  163. union starpu_u_tick tick_now;
  164. STARPU_GET_TICK(tick_now);
  165. uint64_t elapsed_ticks = STARPU_TICK_DIFF(_starpu_reference_start_tick, tick_now);
  166. /* We convert this number into nano-seconds so that we can fill the
  167. * timespec structure. */
  168. uint64_t elapsed_ns = (uint64_t)(((double)elapsed_ticks)*(_starpu_scale*1000.0));
  169. long tv_nsec = (elapsed_ns % 1000000000);
  170. time_t tv_sec = (elapsed_ns / 1000000000);
  171. ts->tv_sec = tv_sec;
  172. ts->tv_nsec = tv_nsec;
  173. }
  174. #else // !HAVE_CLOCK_GETTIME & no rdtsc
  175. #warning StarPU could not find a timer, clock will always return 0
  176. void _starpu_timing_init(void)
  177. {
  178. }
  179. void _starpu_clock_gettime(struct timespec *ts)
  180. {
  181. timerclear(ts);
  182. }
  183. #endif
  184. #endif // HAVE_CLOCK_GETTIME
  185. /* Returns the time elapsed between start and end in microseconds */
  186. double starpu_timing_timespec_delay_us(struct timespec *start, struct timespec *end)
  187. {
  188. struct timespec diff;
  189. starpu_timespec_sub(end, start, &diff);
  190. double us = (diff.tv_sec*1e6) + (diff.tv_nsec*1e-3);
  191. return us;
  192. }
  193. double starpu_timing_timespec_to_us(struct timespec *ts)
  194. {
  195. return (1000000.0*ts->tv_sec) + (0.001*ts->tv_nsec);
  196. }
  197. double starpu_timing_now(void)
  198. {
  199. #ifdef STARPU_SIMGRID
  200. return MSG_get_clock()*1000000;
  201. #else
  202. struct timespec now;
  203. _starpu_clock_gettime(&now);
  204. return starpu_timing_timespec_to_us(&now);
  205. #endif
  206. }