timing.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include "timing.h"
  17. #ifdef HAVE_CLOCK_GETTIME
  18. #define TICK_DIFF(t1, t2) ((long long)((t2).ts.tv_sec*1e9 + (t2).ts.tv_nsec) + \
  19. - (long long)((t1).ts.tv_sec*1e9) + (long long)(t1).ts.tv_nsec)
  20. #define TIMING_DELAY(t1, t2) _starpu_tick2usec(TICK_DIFF((t1), (t2)))
  21. void timing_init(void)
  22. {
  23. }
  24. inline double _starpu_tick2usec(long long t)
  25. {
  26. return (double)(t)/1000;
  27. }
  28. inline double timing_delay(tick_t *t1, tick_t *t2)
  29. {
  30. double d1, d2;
  31. d1 = _starpu_tick2usec((t1->ts.tv_sec*1e9) + t1->ts.tv_nsec);
  32. d2 = _starpu_tick2usec((t2->ts.tv_sec*1e9) + t2->ts.tv_nsec);
  33. return (d2 - d1);;
  34. }
  35. /* returns the current time in us */
  36. inline double timing_now(void)
  37. {
  38. tick_t tick_now;
  39. GET_TICK(tick_now);
  40. return _starpu_tick2usec(((tick_now).ts.tv_sec*1e9) + (tick_now).ts.tv_nsec);
  41. }
  42. #else // HAVE_CLOCK_GETTIME
  43. #define TICK_RAW_DIFF(t1, t2) ((t2).tick - (t1).tick)
  44. #define TICK_DIFF(t1, t2) (TICK_RAW_DIFF(t1, t2) - residual)
  45. #define TIMING_DELAY(t1, t2) _starpu_tick2usec(TICK_DIFF(t1, t2))
  46. static double scale = 0.0;
  47. static unsigned long long residual = 0;
  48. static int inited = 0;
  49. void timing_init(void)
  50. {
  51. static tick_t t1, t2;
  52. int i;
  53. if (inited) return;
  54. residual = (unsigned long long)1 << 63;
  55. for(i = 0; i < 20; i++)
  56. {
  57. GET_TICK(t1);
  58. GET_TICK(t2);
  59. residual = STARPU_MIN(residual, TICK_RAW_DIFF(t1, t2));
  60. }
  61. {
  62. struct timeval tv1,tv2;
  63. GET_TICK(t1);
  64. gettimeofday(&tv1,0);
  65. usleep(500000);
  66. GET_TICK(t2);
  67. gettimeofday(&tv2,0);
  68. scale = ((tv2.tv_sec*1e6 + tv2.tv_usec) -
  69. (tv1.tv_sec*1e6 + tv1.tv_usec)) /
  70. (double)(TICK_DIFF(t1, t2));
  71. }
  72. inited = 1;
  73. }
  74. inline double _starpu_tick2usec(long long t)
  75. {
  76. return (double)(t)*scale;
  77. }
  78. inline double timing_delay(tick_t *t1, tick_t *t2)
  79. {
  80. return TIMING_DELAY(*t1, *t2);
  81. }
  82. inline double timing_now(void)
  83. {
  84. tick_t tick_now;
  85. GET_TICK(tick_now);
  86. return _starpu_tick2usec(tick_now.tick);
  87. }
  88. #endif // HAVE_CLOCK_GETTIME