/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/ck_tile/host/timer.hpp Source File

/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/ck_tile/host/timer.hpp Source File#

Composable Kernel: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/ck_tile/host/timer.hpp Source File
timer.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: MIT
2 // Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
3 
4 #pragma once
5 
8 #include <hip/hip_runtime.h>
9 #include <cstddef>
10 #include <chrono>
11 
12 namespace ck_tile {
13 
14 struct gpu_timer
15 {
17  {
18  HIP_CHECK_ERROR(hipEventCreate(&start_evt));
19  HIP_CHECK_ERROR(hipEventCreate(&stop_evt));
20  }
21 
22  CK_TILE_HOST ~gpu_timer() noexcept(false)
23  {
24  HIP_CHECK_ERROR(hipEventDestroy(start_evt));
25  HIP_CHECK_ERROR(hipEventDestroy(stop_evt));
26  }
27 
28  CK_TILE_HOST void start(const hipStream_t& s)
29  {
30  HIP_CHECK_ERROR(hipStreamSynchronize(s));
31  HIP_CHECK_ERROR(hipEventRecord(start_evt, s));
32  }
33 
34  CK_TILE_HOST void stop(const hipStream_t& s)
35  {
36  HIP_CHECK_ERROR(hipEventRecord(stop_evt, s));
37  HIP_CHECK_ERROR(hipEventSynchronize(stop_evt));
38  }
39  // return in ms
40  CK_TILE_HOST float duration() const
41  {
42  float ms = 0;
43  HIP_CHECK_ERROR(hipEventElapsedTime(&ms, start_evt, stop_evt));
44  return ms;
45  }
46 
47  private:
48  hipEvent_t start_evt, stop_evt;
49 };
50 
51 struct cpu_timer
52 {
53  // torch.utils.benchmark.Timer(), there is a sync inside each timer callback
54  CK_TILE_HOST void start(const hipStream_t& s)
55  {
56  HIP_CHECK_ERROR(hipStreamSynchronize(s));
57  start_tick = std::chrono::high_resolution_clock::now();
58  }
59  // torch.utils.benchmark.Timer(), there is a sync inside each timer callback
60  CK_TILE_HOST void stop(const hipStream_t& s)
61  {
62  HIP_CHECK_ERROR(hipStreamSynchronize(s));
63  stop_tick = std::chrono::high_resolution_clock::now();
64  }
65  // return in ms
66  CK_TILE_HOST float duration() const
67  {
68  double sec =
69  std::chrono::duration_cast<std::chrono::duration<double>>(stop_tick - start_tick)
70  .count();
71  return static_cast<float>(sec * 1e3);
72  }
73 
74  private:
75  std::chrono::time_point<std::chrono::high_resolution_clock> start_tick;
76  std::chrono::time_point<std::chrono::high_resolution_clock> stop_tick;
77 };
78 
79 } // namespace ck_tile
#define CK_TILE_HOST
Definition: config.hpp:40
#define HIP_CHECK_ERROR(retval_or_funcall)
Definition: hip_check_error.hpp:21
Definition: cluster_descriptor.hpp:13
Definition: timer.hpp:52
CK_TILE_HOST void stop(const hipStream_t &s)
Definition: timer.hpp:60
CK_TILE_HOST void start(const hipStream_t &s)
Definition: timer.hpp:54
CK_TILE_HOST float duration() const
Definition: timer.hpp:66
Definition: timer.hpp:15
CK_TILE_HOST void start(const hipStream_t &s)
Definition: timer.hpp:28
CK_TILE_HOST ~gpu_timer() noexcept(false)
Definition: timer.hpp:22
CK_TILE_HOST void stop(const hipStream_t &s)
Definition: timer.hpp:34
CK_TILE_HOST float duration() const
Definition: timer.hpp:40
CK_TILE_HOST gpu_timer()
Definition: timer.hpp:16