Kokkos Core Kernels Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Kokkos_Timer.hpp
1 //@HEADER
2 // ************************************************************************
3 //
4 // Kokkos v. 4.0
5 // Copyright (2022) National Technology & Engineering
6 // Solutions of Sandia, LLC (NTESS).
7 //
8 // Under the terms of Contract DE-NA0003525 with NTESS,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12 // See https://kokkos.org/LICENSE for license information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //@HEADER
16 
17 #ifndef KOKKOS_TIMER_HPP
18 #define KOKKOS_TIMER_HPP
19 #ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
20 #define KOKKOS_IMPL_PUBLIC_INCLUDE
21 #define KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_TIMER
22 #endif
23 
24 #include <Kokkos_Macros.hpp>
25 // gcc 10.3.0 with CUDA doesn't support std::chrono,
26 // see https://github.com/kokkos/kokkos/issues/4334
27 #if defined(KOKKOS_COMPILER_GNU) && (KOKKOS_COMPILER_GNU == 1030) && \
28  defined(KOKKOS_COMPILER_NVCC)
29 #include <sys/time.h>
30 #else
31 #include <chrono>
32 #endif
33 
34 namespace Kokkos {
35 
38 #if defined(KOKKOS_COMPILER_GNU) && (KOKKOS_COMPILER_GNU == 1030) && \
39  defined(KOKKOS_COMPILER_NVCC)
40 class Timer {
41  private:
42  struct timeval m_old;
43 
44  public:
45  inline void reset() { gettimeofday(&m_old, nullptr); }
46 
47  inline ~Timer() = default;
48 
49  inline Timer() { reset(); }
50 
51  Timer(const Timer&) = delete;
52  Timer& operator=(const Timer&) = delete;
53 
54  inline double seconds() const {
55  struct timeval m_new;
56 
57  gettimeofday(&m_new, nullptr);
58 
59  return ((double)(m_new.tv_sec - m_old.tv_sec)) +
60  ((double)(m_new.tv_usec - m_old.tv_usec) * 1.0e-6);
61  }
62 };
63 #else
64 class Timer {
65  private:
66  std::chrono::high_resolution_clock::time_point m_old;
67 
68  public:
69  inline void reset() { m_old = std::chrono::high_resolution_clock::now(); }
70 
71  inline ~Timer() = default;
72 
73  inline Timer() { reset(); }
74 
75  Timer(const Timer&);
76  Timer& operator=(const Timer&);
77 
78  inline double seconds() const {
79  std::chrono::high_resolution_clock::time_point m_new =
80  std::chrono::high_resolution_clock::now();
81  return std::chrono::duration_cast<std::chrono::duration<double> >(m_new -
82  m_old)
83  .count();
84  }
85 };
86 #endif
87 
88 } // namespace Kokkos
89 
90 #ifdef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_TIMER
91 #undef KOKKOS_IMPL_PUBLIC_INCLUDE
92 #undef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_TIMER
93 #endif
94 #endif /* #ifndef KOKKOS_TIMER_HPP */
Time since construction.