timer.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012 Inria
  4. * Copyright (C) 2012,2017 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. /*! \file timer_linux.h
  18. * \brief Contains timer class that can be used by Linux systems.
  19. */
  20. #ifndef TIMER_LINUX_H
  21. #define TIMER_LINUX_H
  22. #include <sys/time.h>
  23. #include <iostream>
  24. #include <vector>
  25. class Timer
  26. {
  27. private:
  28. timeval timerStart;
  29. timeval timerEnd;
  30. std::vector<double> multi_time; // used for estimating multi backends.
  31. std::vector<double> time;
  32. bool record_multi;
  33. void addMultiMaxTime() // used to estimate when using multi-Backend
  34. {
  35. double max = 0.0f;
  36. //std::cout<<"AddMultiMaxTime call before"<<time.size()<<"\n";
  37. if(multi_time.empty())
  38. return;
  39. for(std::vector<double>::iterator it = multi_time.begin(); it != multi_time.end(); ++it)
  40. {
  41. if (max < *it)
  42. max = *it;
  43. }
  44. time.push_back( max );
  45. //std::cout<<"MAXXX:"<<max<<" ";
  46. multi_time.clear(); // clear it in both cases.
  47. //std::cout<<"AddMultiMaxTime call after reset"<<time.size()<<"\n";
  48. }
  49. void addMultiMinTime() // used to estimate when using multi-Backend
  50. {
  51. double min = 0.0f;
  52. if(multi_time.empty())
  53. return;
  54. for(std::vector<double>::iterator it = multi_time.begin(); it != multi_time.end(); ++it)
  55. {
  56. if (min > *it)
  57. min = *it;
  58. }
  59. time.push_back( min );
  60. multi_time.clear(); // clear it in both cases.
  61. }
  62. public:
  63. void start_record_multi()
  64. {
  65. record_multi=true;
  66. multi_time.clear(); // clear it in both cases.
  67. }
  68. void stop_record_multi()
  69. {
  70. addMultiMaxTime();
  71. record_multi=false;
  72. }
  73. Timer() {record_multi=false;}
  74. /*!
  75. * Starts the timimg.
  76. */
  77. void start()
  78. {
  79. gettimeofday(&timerStart, NULL);
  80. //std::cout<<"start_\n";
  81. }
  82. /*!
  83. * Stops the timimg and stores time in a vector.
  84. */
  85. void stop()
  86. {
  87. gettimeofday(&timerEnd, NULL);
  88. if(record_multi)
  89. multi_time.push_back( (timerEnd.tv_sec - timerStart.tv_sec + (timerEnd.tv_usec - timerStart.tv_usec) / 1000000.0) * 1000 );
  90. else
  91. time.push_back( (timerEnd.tv_sec - timerStart.tv_sec + (timerEnd.tv_usec - timerStart.tv_usec) / 1000000.0) * 1000 );
  92. //std::cout<<"stop_\n";
  93. }
  94. /*!
  95. * Clears all timings taken.
  96. */
  97. void reset()
  98. {
  99. if(!record_multi)
  100. {
  101. // std::cout<<"Time reset:\n";
  102. time.clear();
  103. }
  104. multi_time.clear(); // clear it in both cases.
  105. }
  106. /*!
  107. * \param run The run to get timing of.
  108. *
  109. * \return Time for a certain run.
  110. */
  111. double getTime(int run = 0)
  112. {
  113. return time.at(run);
  114. }
  115. /*!
  116. * \return Total time of all stored runs.
  117. */
  118. double getTotalTime()
  119. {
  120. double totalTime = 0.0f;
  121. for(std::vector<double>::iterator it = time.begin(); it != time.end(); ++it)
  122. {
  123. totalTime += *it;
  124. }
  125. return totalTime;
  126. }
  127. /*!
  128. * \return Average time of all stored runs.
  129. */
  130. double getAverageTime()
  131. {
  132. double totalTime = 0.0f;
  133. for(std::vector<double>::iterator it = time.begin(); it != time.end(); ++it)
  134. {
  135. totalTime += *it;
  136. }
  137. return (double)(totalTime/time.size());
  138. }
  139. double getMaxTime()
  140. {
  141. double max = 0.0f;
  142. for(std::vector<double>::iterator it = time.begin(); it != time.end(); ++it)
  143. {
  144. if (max < *it)
  145. max = *it;
  146. }
  147. return max;
  148. }
  149. double getMinTime()
  150. {
  151. double min = 0.0f;
  152. for(std::vector<double>::iterator it = time.begin(); it != time.end(); ++it)
  153. {
  154. if (min > *it)
  155. min = *it;
  156. }
  157. return min;
  158. }
  159. /*!
  160. * \return The resolution of the timer in micro seconds.
  161. */
  162. double getResolutionUs()
  163. {
  164. double result = 0.0f;
  165. timeval tStart;
  166. timeval tEnd;
  167. gettimeofday(&tStart, NULL);
  168. gettimeofday(&tEnd, NULL);
  169. int delay = 0;
  170. do
  171. {
  172. delay++;
  173. gettimeofday(&tStart, NULL);
  174. for(int i = 0; i < delay; ++i) ;
  175. gettimeofday(&tEnd, NULL);
  176. result = ((((double)tEnd.tv_sec)*1000000.0) + ((double)tEnd.tv_usec)) - ((((double)tStart.tv_sec)*1000000.0) + ((double)tStart.tv_usec));
  177. } while(result == 0);
  178. return result;
  179. }
  180. /*!
  181. * \return Number of runs stored in timer.
  182. */
  183. int getNumTimings()
  184. {
  185. return time.size();
  186. }
  187. };
  188. #endif