Timer.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #ifndef __TIMER_H__
  2. #define __TIMER_H__
  3. #include <cuda_runtime.h>
  4. #include <chrono>
  5. #include "cuda.h"
  6. class GPUTimer {
  7. cudaEvent_t start, stop;
  8. public:
  9. GPUTimer()
  10. {
  11. cudaEventCreate(&start);
  12. cudaEventCreate(&stop);
  13. }
  14. ~GPUTimer()
  15. {
  16. cudaEventDestroy(start);
  17. cudaEventDestroy(stop);
  18. }
  19. inline void Record() { cudaEventRecord(start); }
  20. inline void Elapsed(float& time_elapsed)
  21. {
  22. cudaEventRecord(stop);
  23. cudaEventSynchronize(stop);
  24. cudaEventElapsedTime(&time_elapsed, start, stop);
  25. }
  26. };
  27. class CPUTimer {
  28. std::chrono::high_resolution_clock::time_point start;
  29. public:
  30. CPUTimer() : start(std::chrono::high_resolution_clock::now()) {}
  31. inline void Reset() { start = std::chrono::high_resolution_clock::now(); }
  32. inline float Elapsed()
  33. {
  34. auto temp = start;
  35. start = std::chrono::high_resolution_clock::now();
  36. return (float)(std::chrono::duration_cast<std::chrono::microseconds>(start - temp).count() /
  37. 1e3);
  38. }
  39. };
  40. #endif