gpufilter
GPU-Efficient Recursive Filtering and Summed-Area Tables
include/timer.h
Go to the documentation of this file.
00001 
00008 #ifndef TIMER_H
00009 #define TIMER_H
00010 
00011 //== INCLUDES =================================================================
00012 
00013 #include <string>
00014 #include <list>
00015 #include <cuda_runtime.h>
00016 
00017 //== NAMESPACES ===============================================================
00018 
00019 namespace gpufilter {
00020 
00021 //== CLASS DEFINITION =========================================================
00022 
00030 class base_timer {
00031 
00032 public:
00033 
00040     base_timer( const char *type_label,
00041                 size_t data_size = 0,
00042                 const std::string &unit = "" );
00043 
00047     void start();
00048 
00052     void stop();
00053 
00058     float elapsed();
00059 
00064     bool is_stopped() const { return !m_started; }
00065 
00070     size_t data_size() const { return m_data_size; }
00071 
00076     const std::string &unit() const { return m_unit; }
00077 
00082     const char *type_label() { return m_type_label; }
00083 
00084 protected:
00085 
00089     virtual void do_start() = 0;
00090 
00094     virtual void do_stop() = 0;
00095 
00100     virtual float do_get_elapsed() const = 0;
00101 
00102 private:
00103 
00109     base_timer( const base_timer& bt );
00110 
00117     base_timer& operator = ( const base_timer& bt );
00118 
00119     const char *m_type_label; 
00120 
00121     float m_elapsed; 
00122 
00123     bool m_started; 
00124 
00125     size_t m_data_size; 
00126 
00127     std::string m_unit; 
00128 
00129 };
00130 
00131 //== CLASS DEFINITION =========================================================
00132 
00140 class gpu_timer : public base_timer {
00141 
00142 public:
00143 
00150     gpu_timer( size_t data_size = 0,
00151                const std::string& unit = "",
00152                bool start = true );
00153 
00157     ~gpu_timer();
00158 
00159 private:
00160 
00164     virtual void do_start();
00165 
00169     virtual void do_stop();
00170 
00175     virtual float do_get_elapsed() const;
00176 
00177     cudaEvent_t m_start; 
00178     cudaEvent_t m_stop; 
00179 
00180 };
00181 
00182 //== CLASS DEFINITION =========================================================
00183 
00191 class cpu_timer : public base_timer {
00192 
00193 public:
00194 
00201     cpu_timer( size_t data_size = 0,
00202                const std::string& unit = "",
00203                bool start = true );
00204 
00208     ~cpu_timer();
00209 
00210 private:
00211 
00215     virtual void do_start();
00216 
00220     virtual void do_stop();
00221 
00226     virtual float do_get_elapsed() const;
00227 
00232     double get_cpu_time() const;
00233 
00234     double m_start_time; 
00235     double m_stop_time; 
00236 
00237 };
00238 
00239 //== CLASS DEFINITION =========================================================
00240 
00249 class scoped_timer_stop {
00250 
00251 public:
00252 
00257     scoped_timer_stop( base_timer& timer );
00258 
00262     ~scoped_timer_stop() { stop(); }
00263 
00267     void stop() { m_timer->stop(); }
00268 
00273     float elapsed() const { return m_timer->elapsed(); }
00274 
00275 private:
00276 
00277     base_timer *m_timer; 
00278 
00279 };
00280 
00281 //== CLASS DEFINITION =========================================================
00282 
00293 class timer_pool {
00294 
00295 public:
00296 
00300     ~timer_pool() { }
00301 
00309     gpu_timer &gpu_add( const std::string& label,
00310                         size_t data_size = 0,
00311                         const std::string& unit = "" );
00312 
00320     cpu_timer &cpu_add( const std::string& label,
00321                         size_t data_size = 0,
00322                         const std::string& unit = "" );
00323 
00327     void flush();
00328 
00329 private:
00330 
00335     struct timer_data {
00336         base_timer *timer; 
00337         std::string label; 
00338         int level; 
00339     };
00340 
00341     typedef std::list<timer_data> timer_list; 
00342 
00343     timer_list m_timers; 
00344 
00345 };
00346 
00347 //== EXTERNS ==================================================================
00348 
00354 extern
00355 timer_pool timers;
00356 
00357 //=============================================================================
00358 } // namespace gpufilter
00359 //=============================================================================
00360 #endif // TIMER_H
00361 //=============================================================================
00362 //vi: ai sw=4 ts=4