Timer.h 1.2 KB

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