#ifndef NVD_TIMER_H
#define NVD_TIMER_H
#include "nvd_macros.h"
#include "nvd_device.h"
namespace ucl_cudadr {
class UCL_Timer {
public:
inline UCL_Timer() : _total_time(0.0f), _initialized(false) { }
inline UCL_Timer(UCL_Device &dev) : _total_time(0.0f), _initialized(false)
{ init(dev); }
inline ~UCL_Timer() { clear(); }
inline void clear() {
if (_initialized) {
CU_DESTRUCT_CALL(cuEventDestroy(start_event));
CU_DESTRUCT_CALL(cuEventDestroy(stop_event));
_initialized=false;
_total_time=0.0;
}
}
inline void init(UCL_Device &dev) { init(dev, dev.cq()); }
inline void init(UCL_Device &dev, command_queue &cq) {
clear();
_cq=cq;
_initialized=true;
CU_SAFE_CALL( cuEventCreate(&start_event,0) );
CU_SAFE_CALL( cuEventCreate(&stop_event,0) );
}
inline void start() { CU_SAFE_CALL(cuEventRecord(start_event,_cq)); }
inline void stop() { CU_SAFE_CALL(cuEventRecord(stop_event,_cq)); }
inline void sync_start()
{ CU_SAFE_CALL(cuEventSynchronize(start_event)); }
inline void sync_stop()
{ CU_SAFE_CALL(cuEventSynchronize(stop_event)); }
inline void zero() {
CU_SAFE_CALL(cuEventRecord(start_event,_cq));
CU_SAFE_CALL(cuEventRecord(stop_event,_cq));
}
inline void zero_total() { _total_time=0.0; }
inline double add_to_total()
{ double t=time(); _total_time+=t; return t/1000.0; }
inline void add_time_to_total(const double t) { _total_time+=t; }
inline double time() {
float timer;
CU_SAFE_CALL(cuEventSynchronize(stop_event));
CU_SAFE_CALL( cuEventElapsedTime(&timer,start_event,stop_event) );
return timer;
}
inline double seconds() { return time()/1000.0; }
inline double total_time() { return _total_time; }
inline double total_seconds() { return _total_time/1000.0; }
private:
CUevent start_event, stop_event;
CUstream _cq;
double _total_time;
bool _initialized;
};
}
#endif