timer.h 5.0 KB

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