| 1 | // |
| 2 | // Timer.h |
| 3 | // |
| 4 | // |
| 5 | |
| 6 | #ifndef TIMER_HPP |
| 7 | #define TIMER_HPP |
| 8 | |
| 9 | #include <time.h> |
| 10 | #include <sys/time.h> |
| 11 | |
| 12 | #ifdef __MACH__ |
| 13 | #include <mach/clock.h> |
| 14 | #include <mach/mach.h> |
| 15 | #endif |
| 16 | |
| 17 | /*! \brief Class for cpu time benchmarking |
| 18 | * |
| 19 | * Usage: |
| 20 | * |
| 21 | * \snippet timer_util_test.hpp timer usage and behavior |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | class timer |
| 26 | { |
| 27 | //! Flag that indicate if the timer is running or not |
| 28 | bool running; |
| 29 | |
| 30 | //! starting time |
| 31 | struct timespec tsstart; |
| 32 | |
| 33 | //! start time from epoch |
| 34 | clock_t cstart; |
| 35 | |
| 36 | // stop time |
| 37 | struct timespec tsstop; |
| 38 | |
| 39 | //! stop time from epoch |
| 40 | clock_t cstop; |
| 41 | |
| 42 | // Fill the stop point |
| 43 | void check() |
| 44 | { |
| 45 | #if defined(SYNC_BEFORE_TAKE_TIME) && defined(__NVCC__) |
| 46 | cudaDeviceSynchronize(); |
| 47 | #endif |
| 48 | |
| 49 | #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time |
| 50 | clock_serv_t cclock; |
| 51 | mach_timespec_t mts; |
| 52 | host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); |
| 53 | clock_get_time(cclock, &mts); |
| 54 | mach_port_deallocate(mach_task_self(), cclock); |
| 55 | tsstop.tv_sec = mts.tv_sec; |
| 56 | tsstop.tv_nsec = mts.tv_nsec; |
| 57 | #else |
| 58 | clock_gettime(CLOCK_REALTIME, &tsstop); |
| 59 | #endif |
| 60 | cstop = clock(); |
| 61 | } |
| 62 | |
| 63 | public: |
| 64 | |
| 65 | //! Default constructor |
| 66 | timer() |
| 67 | :running(false),cstart(0),cstop(0) |
| 68 | { |
| 69 | tsstart = timespec(); |
| 70 | tsstop = timespec(); |
| 71 | } |
| 72 | |
| 73 | /*! \brief Start the timer |
| 74 | * |
| 75 | */ |
| 76 | void start() |
| 77 | { |
| 78 | // time is running |
| 79 | running = true; |
| 80 | |
| 81 | #if defined(SYNC_BEFORE_TAKE_TIME) && defined(__NVCC__) |
| 82 | cudaDeviceSynchronize(); |
| 83 | #endif |
| 84 | |
| 85 | #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time |
| 86 | clock_serv_t cclock; |
| 87 | mach_timespec_t mts; |
| 88 | host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); |
| 89 | clock_get_time(cclock, &mts); |
| 90 | mach_port_deallocate(mach_task_self(), cclock); |
| 91 | tsstart.tv_sec = mts.tv_sec; |
| 92 | tsstart.tv_nsec = mts.tv_nsec; |
| 93 | #else |
| 94 | clock_gettime(CLOCK_REALTIME, &tsstart); |
| 95 | #endif |
| 96 | |
| 97 | cstart = clock(); |
| 98 | |
| 99 | } |
| 100 | |
| 101 | /*! \brief Stop the timer |
| 102 | * |
| 103 | * |
| 104 | */ |
| 105 | void stop() |
| 106 | { |
| 107 | if (running == false) return; |
| 108 | running = false; |
| 109 | check(); |
| 110 | } |
| 111 | |
| 112 | /*! \brief Return the elapsed real time |
| 113 | * |
| 114 | * |
| 115 | */ |
| 116 | double getwct() |
| 117 | { |
| 118 | if (running == true) |
| 119 | check(); |
| 120 | |
| 121 | return ((double)(tsstop.tv_sec - tsstart.tv_sec)) + ((1e-9) * ((double)(tsstop.tv_nsec - tsstart.tv_nsec))); |
| 122 | } |
| 123 | |
| 124 | /*! \brief Return the cpu time |
| 125 | * |
| 126 | * |
| 127 | */ |
| 128 | double getcputime() |
| 129 | { |
| 130 | if (running == true) |
| 131 | check(); |
| 132 | |
| 133 | return (((double)(cstop - cstart)) / CLOCKS_PER_SEC); |
| 134 | } |
| 135 | |
| 136 | /*! \brief Reset the timer |
| 137 | * |
| 138 | * |
| 139 | */ |
| 140 | void reset() |
| 141 | { |
| 142 | tsstart = tsstop; |
| 143 | cstart = cstop; |
| 144 | } |
| 145 | }; |
| 146 | |
| 147 | #endif |
| 148 | |
| 149 | |
| 150 | |