/*!\file \brief Implementation of functions for timing and performance measurement. */ #include #include #include #include #include #undef ZERO_ONLY #if TARGET == BGL #define ZERO_ONLY #endif #if TARGET == BGP #define ZERO_ONLY #endif #ifdef UNIFORM_SEED_TESTING const int if_print = 0; #else const int if_print = 1; #endif CPS_START_NAMESPACE /*! \return The current time in seconds (accurate to the microsecond), */ Float dclock(bool stop){ if(stop) Barrier(); struct timeval tp; gettimeofday(&tp, NULL); return((Float) tp.tv_sec + (Float) tp.tv_usec * 1e-6); } /*! The user counts the operations and calls \a gettimeofday before they start and after they finish to get the elapsed time. \param n_flops The number of floating point operations performed \param start The initial result of calling \a gettimeofday \param end The final result of calling \a gettimeofday \return The FLOPS rate. */ Float print_flops(double nflops, struct timeval *start, struct timeval *end){ if (0) printf("nflops=%u start=%p end=%p\n",nflops,start,end); int sec = end->tv_sec - start->tv_sec; int usec = end->tv_usec - start->tv_usec; if (0) printf("src=%d usec=%d\n",sec,usec); Float time = sec + 1.e-6*usec; if(!UniqueID() && if_print) printf("%e flops /%e seconds = %e MFlops\n",(Float)nflops,time,(Float)nflops/(time*1.e6)); return nflops/time; } /*! The user counts the operations and calls \a gettimeofday before they start and after they finish to get the elapsed time. The class name and method name are printed with the FLOPS rate. \param cname The class name. \param fname The method name. \param n_flops The number of floating point operations performed \param start The initial result of calling \a gettimeofday \param end The final result of calling \a gettimeofday \return The FLOPS rate. */ Float print_flops(const char cname[], const char fname[], double nflops, struct timeval *start, struct timeval *end){ if(!UniqueID() && if_print) printf("%s:%s: ",cname,fname); return print_flops(nflops,start,end); } /*! The user counts the operations and calls dclock before they start and after they finish to get the elapsed time. \param n_flops The number of floating point operations performed \param time The elapsed time in seconds. \return The FLOPS rate. */ Float print_time(const char *cname, const char *fname, Float time){ if (!UniqueID() && if_print) printf("%s::%s: %e seconds\n",cname,fname,time); return time; } Float print_flops(double nflops, Float time){ if(!UniqueID() && if_print) printf("%e flops /%e seconds = %e MFlops\n",(Float)nflops,time,(Float)nflops/(time*1.e6)); return nflops/time; } /*! The user counts the operations and calls dclock before they start and after they finish to get the elapsed time. The class name and method name are printed with the FLOPS rate. \param cname The class name. \param fname The method name. \param n_flops The number of floating point operations performed. \param time The elapsed time in seconds. \return The FLOPS rate. */ Float print_flops(const char cname[], const char fname[], double nflops, Float time){ if(!UniqueID() && if_print) printf("Node 0: %s:%s: ",cname,fname); return print_flops(nflops,time); } void print_asctime_(){ time_t timer; struct tm *date; char str[256]; timer = time(NULL); date = localtime(&timer); printf(" : %s\n", asctime(date)); } void TimeStamp::set_file(const char *filename){ stream = Fopen(ZERO_ONLY,filename,"w"); reset(); } void TimeStamp::reset(){ cur_depth = 0; start = dclock(); enabled = true; } void TimeStamp::close_file(){ Fclose(stream); enabled=false; } void TimeStamp::vstamp(const char *format, va_list args){ char into[1024]; vsprintf(into,format,args); Float time = dclock()-start; Float rem = time; int hours = int(floor(rem/3600)); rem -= hours*3600; int mins = int(floor(rem/60)); rem -= mins*60; Fprintf(ZERO_ONLY,stream,"TimeStamp depth %d : %e secs (%d h %d m %.4f s): %s\n",cur_depth,time,hours,mins,rem,into); } void TimeStamp::stamp(const char *format,...){ if(!enabled) return; va_list args; va_start(args,format); vstamp(format,args); va_end(args); } void TimeStamp::stamp_incr(const char *format,...){ if(!enabled) return; va_list args; va_start(args,format); vstamp(format,args); va_end(args); incr_depth(); } void TimeStamp::stamp_decr(const char *format,...){ if(!enabled) return; va_list args; va_start(args,format); vstamp(format,args); va_end(args); decr_depth(); } void TimeStamp::incr_stamp(const char *format,...){ if(!enabled) return; incr_depth(); va_list args; va_start(args,format); vstamp(format,args); va_end(args); } void TimeStamp::decr_stamp(const char *format,...){ if(!enabled) return; decr_depth(); va_list args; va_start(args,format); vstamp(format,args); va_end(args); } void TimeStamp::start_func(const char* cls, const char *fnc){ stamp("Starting %s::%s",cls,fnc); } void TimeStamp::end_func(const char* cls, const char *fnc){ stamp("Finished %s::%s",cls,fnc); } void TimeStamp::incr_depth(){ cur_depth++; } void TimeStamp::decr_depth(){ cur_depth--; } int TimeStamp::cur_depth(0); FILE *TimeStamp::stream(NULL); bool TimeStamp::enabled(false); Float TimeStamp::start(0.0); Elapsed::Elapsed(const Float &delta){ hours=delta/3600.0; mins=(delta-3600.0*hours)/60.0; secs=delta-3600.0*hours-60.0*mins; } CPS_END_NAMESPACE #include CPS_START_NAMESPACE #if 1 Float Timer::dtime_begin(dclock()); Float Timer::dtime_last(dclock()); double Timer::autodisplay_interval=60; double Timer::show_stop=1.; void Timer::reset(){ dtime_begin = dclock(); dtime_last = dtime_begin; } //Time since last reset Elapsed Timer::elapsed_time(){ return Elapsed(dclock()-dtime_begin); } //Time since last call to this function Elapsed Timer::relative_time(){ Float now = dclock(); Float delta = now - dtime_last; dtime_last = now; return Elapsed(delta); } #endif CPS_END_NAMESPACE