Advertisement
stiansjogren

time_meas.h

Jun 11th, 2016
1,546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifdef OMP
  2. #include <omp.h>
  3. #endif
  4.  
  5. typedef struct {
  6.  
  7.   long long in;
  8.   long long diff;
  9.   long long max;
  10.   int trials;
  11. } time_stats_t;
  12.  
  13. static inline void start_meas(time_stats_t *ts) __attribute__((always_inline));
  14. static inline void stop_meas(time_stats_t *ts) __attribute__((always_inline));
  15.  
  16. #if defined(__i386__)
  17. static inline unsigned long long rdtsc_oai(void) __attribute__((always_inline));
  18. static inline unsigned long long rdtsc_oai(void) {
  19.     unsigned long long int x;
  20.     __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
  21.     return x;
  22. }
  23. #elif defined(__x86_64__)
  24. static inline unsigned long long rdtsc_oai() __attribute__((always_inline));
  25. static inline unsigned long long rdtsc_oai() {
  26.   unsigned long long a, d;
  27.   __asm__ volatile ("rdtsc" : "=a" (a), "=d" (d));
  28.   return (d<<32) | a;
  29. }
  30. #endif
  31.  
  32. static inline void start_meas(time_stats_t *ts) {
  33.  
  34. #ifdef OMP
  35.   int tid;
  36.  
  37.   tid = omp_get_thread_num();
  38.   if (tid==0)
  39. #endif
  40.     {
  41.       ts->trials++;
  42.       ts->in = rdtsc_oai();
  43.     }
  44. }
  45.  
  46. static inline void stop_meas(time_stats_t *ts) {
  47.  
  48.   long long out = rdtsc_oai();
  49.  
  50. #ifdef OMP
  51.   int tid;
  52.   tid = omp_get_thread_num();
  53.   if (tid==0)
  54. #endif
  55.     {
  56.       ts->diff += (out-ts->in);
  57.       if ((out-ts->in) > ts->max)
  58.     ts->max = out-ts->in;
  59.      
  60.     }
  61. }
  62.  
  63. static inline void reset_meas(time_stats_t *ts) {
  64.  
  65.   ts->trials=0;
  66.   ts->diff=0;
  67.   ts->max=0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement