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

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

Composable Kernel: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/3643/include/ck_tile/host/high_res_cpu_clock.hpp Source File
high_res_cpu_clock.hpp
Go to the documentation of this file.
1 // Copyright (c) Advanced Micro Devices, Inc., or its affiliates.
2 // SPDX-License-Identifier: MIT
3 
4 #pragma once
5 
6 #include <stdint.h>
7 
8 namespace ck_tile {
9 
10 // Time structure to hold nanoseconds since epoch or arbitrary start point
12 {
14 };
15 
16 // Platform-specific includes and implementation
17 #if defined(_WIN32) || defined(_WIN64)
18 // Windows
19 #include <windows.h>
20 
21 static inline timepoint_t high_res_now()
22 {
23  // Cache the performance counter frequency; it is constant for the system lifetime.
24  static LARGE_INTEGER frequency = []() {
25  LARGE_INTEGER f;
26  QueryPerformanceFrequency(&f);
27  return f;
28  }();
29 
30  LARGE_INTEGER counter;
31  timepoint_t tp;
32  QueryPerformanceCounter(&counter);
33 
34  // Convert to nanoseconds using floating-point to avoid 64-bit integer overflow
35  tp.nanoseconds =
36  static_cast<int64_t>((static_cast<long double>(counter.QuadPart) * 1000000000.0L) /
37  static_cast<long double>(frequency.QuadPart));
38 
39  return tp;
40 }
41 
42 #elif defined(__linux__) || defined(__unix__) || defined(_POSIX_VERSION)
43 // Linux/Unix/POSIX
44 #include <time.h>
45 
46 static inline timepoint_t high_res_now()
47 {
48  struct timespec ts;
49  timepoint_t tp;
50 
51  // Use CLOCK_MONOTONIC for consistent timing unaffected by system time changes
52  // Use CLOCK_REALTIME if you need wall-clock time
53  clock_gettime(CLOCK_MONOTONIC, &ts);
54 
55  tp.nanoseconds = static_cast<int64_t>(ts.tv_sec * 1000000000LL + ts.tv_nsec);
56 
57  return tp;
58 }
59 
60 #else
61 // Fallback for other platforms
62 #include <time.h>
63 
64 static inline timepoint_t high_res_now()
65 {
66  timepoint_t tp;
67  time_t t = time(NULL);
68  tp.nanoseconds = static_cast<int64_t>(t * 1000000000LL);
69  return tp;
70 }
71 
72 #endif
73 
74 // Duration calculation functions
75 static inline int64_t duration_ns(timepoint_t start, timepoint_t end)
76 {
77  return end.nanoseconds - start.nanoseconds;
78 }
79 
80 static inline int64_t duration_us(timepoint_t start, timepoint_t end)
81 {
82  return (end.nanoseconds - start.nanoseconds) / 1000LL;
83 }
84 
85 static inline int64_t duration_ms(timepoint_t start, timepoint_t end)
86 {
87  return (end.nanoseconds - start.nanoseconds) / 1000000LL;
88 }
89 
90 static inline double duration_sec(timepoint_t start, timepoint_t end)
91 {
92  return static_cast<double>(end.nanoseconds - start.nanoseconds) / 1000000000.0;
93 }
94 
95 } // namespace ck_tile
Definition: cluster_descriptor.hpp:13
signed __int64 int64_t
Definition: stdint.h:135
Definition: high_res_cpu_clock.hpp:12
int64_t nanoseconds
Definition: high_res_cpu_clock.hpp:13