SkePU 0.7
include_starpu/skepu/src/timer_linux.h
Go to the documentation of this file.
00001 
00005 #ifndef TIMER_LINUX_H
00006 #define TIMER_LINUX_H
00007 
00008 #include <sys/time.h>
00009 #include <iostream>
00010 #include <vector>
00011 
00012 namespace skepu
00013 {
00014 
00031 class TimerLinux_GTOD
00032 {
00033 
00034 private:
00035     timeval timerStart;
00036     timeval timerEnd;
00037 
00038 
00039     
00040     std::vector<double> multi_time; // used for estimating multi backends.
00041     std::vector<double> time;
00042     bool record_multi;
00043     
00044     
00045     void addMultiMaxTime() // used to estimate when using multi-Backend
00046     {
00047     double max = 0.0f; 
00048     //std::cout<<"AddMultiMaxTime call before"<<time.size()<<"\n";
00049     if(multi_time.empty())
00050       return;
00051     for(std::vector<double>::iterator it = multi_time.begin(); it != multi_time.end(); ++it)
00052         {
00053       if (max < *it) 
00054           max = *it;
00055     }
00056         time.push_back( max );
00057     //std::cout<<"MAXXX:"<<max<<" ";
00058     
00059     multi_time.clear();  // clear it in both cases.
00060     //std::cout<<"AddMultiMaxTime call after reset"<<time.size()<<"\n";
00061     }
00062     
00063     void addMultiMinTime() // used to estimate when using multi-Backend
00064     {
00065     double min = 0.0f; 
00066     if(multi_time.empty())
00067       return;
00068     for(std::vector<double>::iterator it = multi_time.begin(); it != multi_time.end(); ++it)
00069         {
00070       if (min > *it) 
00071           min = *it;
00072     }
00073         time.push_back( min );
00074     
00075     multi_time.clear();  // clear it in both cases.
00076     }
00077     
00078 public:
00079 
00080       
00081      void start_record_multi()
00082      {
00083        record_multi=true;
00084        multi_time.clear();  // clear it in both cases.
00085      }
00086      
00087      void stop_record_multi()
00088      {
00089        addMultiMaxTime();
00090        record_multi=false;
00091      }
00092   
00093     TimerLinux_GTOD() {record_multi=false;}
00094 
00098     void start()
00099     {
00100         gettimeofday(&timerStart, NULL);
00101     //std::cout<<"start_\n";
00102     }
00103 
00107     void stop()
00108     {
00109         gettimeofday(&timerEnd, NULL);
00110     if(record_multi)
00111       multi_time.push_back( (timerEnd.tv_sec - timerStart.tv_sec + (timerEnd.tv_usec - timerStart.tv_usec) / 1000000.0) * 1000 );
00112     else 
00113       time.push_back( (timerEnd.tv_sec - timerStart.tv_sec + (timerEnd.tv_usec - timerStart.tv_usec) / 1000000.0) * 1000 );
00114     //std::cout<<"stop_\n";
00115     }
00116 
00120     void reset()
00121     {
00122       if(!record_multi)
00123       {
00124 //  std::cout<<"Time reset:\n";
00125         time.clear();
00126       }
00127       
00128       multi_time.clear();  // clear it in both cases.
00129       
00130     }
00131     
00132     
00133     
00134 
00140     double getTime(int run = 0)
00141     {
00142         return time.at(run);
00143     }
00144 
00148     double getTotalTime()
00149     {
00150         double totalTime = 0.0f;
00151 
00152         for(std::vector<double>::iterator it = time.begin(); it != time.end(); ++it)
00153         {
00154             totalTime += *it;
00155         }
00156 
00157         return totalTime;
00158     }
00159 
00163     double getAverageTime()
00164     {
00165         double totalTime = 0.0f;
00166 
00167         for(std::vector<double>::iterator it = time.begin(); it != time.end(); ++it)
00168         {
00169             totalTime += *it;
00170         }
00171 
00172         return (double)(totalTime/time.size());
00173     }
00174     
00175     double getMaxTime()
00176     {
00177         double max = 0.0f; 
00178     for(std::vector<double>::iterator it = time.begin(); it != time.end(); ++it)
00179         {
00180       if (max < *it) 
00181           max = *it;
00182     }
00183     return max;
00184     }
00185     
00186     double getMinTime()
00187     {
00188         double min = 0.0f; 
00189     for(std::vector<double>::iterator it = time.begin(); it != time.end(); ++it)
00190         {
00191       if (min > *it) 
00192           min = *it;
00193     }
00194     
00195     return min;
00196     }
00197 
00201     double getResolutionUs()
00202     {
00203         double result = 0.0f;
00204         timeval tStart;
00205         timeval tEnd;
00206         gettimeofday(&tStart, NULL);
00207         gettimeofday(&tEnd, NULL);
00208         int delay = 0;
00209 
00210         do
00211         {
00212             delay++;
00213             gettimeofday(&tStart, NULL);
00214             for(int i = 0; i < delay; ++i) ;
00215             gettimeofday(&tEnd, NULL);
00216 
00217             result = ((((double)tEnd.tv_sec)*1000000.0) + ((double)tEnd.tv_usec)) - ((((double)tStart.tv_sec)*1000000.0) + ((double)tStart.tv_usec));
00218 
00219         } while(result == 0);
00220 
00221         return result;
00222     }
00223 
00227     int getNumTimings()
00228     {
00229         return time.size();
00230     }
00231 };
00232 
00233 }
00234 
00235 #endif
00236 
 All Classes Namespaces Files Functions Enumerations Friends Defines