Ginkgo  Generated from pipelines/1556235455 branch based on develop. Ginkgo version 1.9.0
A numerical linear algebra library targeting many-core architectures
timer.hpp
1 // SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_BASE_TIMER_HPP_
6 #define GKO_PUBLIC_CORE_BASE_TIMER_HPP_
7 
8 
9 #include <chrono>
10 
11 #include <ginkgo/core/base/executor.hpp>
12 
13 
14 namespace gko {
15 
16 
20 class time_point {
21 public:
22  ~time_point();
23 
25 
26  time_point& operator=(time_point&&);
27 
28  time_point(const time_point&) = delete;
29 
30  time_point& operator=(const time_point&) = delete;
31 
32 private:
33  time_point();
34 
35  friend class Timer;
36  friend class CpuTimer;
37  friend class CudaTimer;
38  friend class HipTimer;
39  friend class DpcppTimer;
40 
42  enum class type {
44  cpu,
46  cuda,
48  hip,
50  dpcpp,
51  };
52 
53  type type_;
54  union data_union {
55  CUevent_st* cuda_event;
56  GKO_HIP_EVENT_STRUCT* hip_event;
57  sycl::event* dpcpp_event;
58  std::chrono::steady_clock::time_point chrono;
59 
60  data_union();
61  } data_;
62 };
63 
64 
80 class Timer {
81 public:
82  virtual ~Timer() = default;
83 
89 
93  virtual void record(time_point& time) = 0;
94 
99  virtual void wait(time_point& time) = 0;
100 
109  std::chrono::nanoseconds difference(time_point& start, time_point& stop);
110 
123  virtual std::chrono::nanoseconds difference_async(
124  const time_point& start, const time_point& stop) = 0;
125 
136  static std::unique_ptr<Timer> create_for_executor(
137  std::shared_ptr<const Executor> exec);
138 
139 protected:
141  virtual void init_time_point(time_point& time) = 0;
142 };
143 
144 
146 class CpuTimer : public Timer {
147 public:
148  void record(time_point& time) override;
149 
150  void wait(time_point& time) override;
151 
152  std::chrono::nanoseconds difference_async(const time_point& start,
153  const time_point& stop) override;
154 
155 protected:
156  void init_time_point(time_point& time) override;
157 };
158 
159 
166 class CudaTimer : public Timer {
167 public:
168  void record(time_point& time) override;
169 
170  void wait(time_point& time) override;
171 
172  std::chrono::nanoseconds difference_async(const time_point& start,
173  const time_point& stop) override;
174 
175  CudaTimer(std::shared_ptr<const CudaExecutor> exec);
176 
177 protected:
178  void init_time_point(time_point& time) override;
179 
180 private:
181  int device_id_;
182  CUstream_st* stream_;
183 };
184 
185 
192 class HipTimer : public Timer {
193 public:
194  void record(time_point& time) override;
195 
196  void wait(time_point& time) override;
197 
198  std::chrono::nanoseconds difference_async(const time_point& start,
199  const time_point& stop) override;
200 
201  HipTimer(std::shared_ptr<const HipExecutor> exec);
202 
203 protected:
204  void init_time_point(time_point& time) override;
205 
206 private:
207  int device_id_;
208  GKO_HIP_STREAM_STRUCT* stream_;
209 };
210 
211 
213 class DpcppTimer : public Timer {
214 public:
215  void record(time_point& time) override;
216 
217  void wait(time_point& time) override;
218 
219  std::chrono::nanoseconds difference_async(const time_point& start,
220  const time_point& stop) override;
221 
222  DpcppTimer(std::shared_ptr<const DpcppExecutor> exec);
223 
224 protected:
225  void init_time_point(time_point& time) override;
226 
227 private:
228  sycl::queue* queue_;
229 };
230 
231 
232 } // namespace gko
233 
234 
235 #endif // GKO_PUBLIC_CORE_BASE_TIMER_HPP_
gko::HipTimer
A timer using events for timing on a HipExecutor.
Definition: timer.hpp:192
gko::CpuTimer
A timer using std::chrono::steady_clock for timing.
Definition: timer.hpp:146
gko::Timer::wait
virtual void wait(time_point &time)=0
Waits until all kernels in-process when recording the time point are finished.
gko::Timer::difference_async
virtual std::chrono::nanoseconds difference_async(const time_point &start, const time_point &stop)=0
Computes the difference between the two time points in nanoseconds.
gko::CpuTimer::record
void record(time_point &time) override
Records a time point at the current time.
gko::CpuTimer::difference_async
std::chrono::nanoseconds difference_async(const time_point &start, const time_point &stop) override
Computes the difference between the two time points in nanoseconds.
gko::time_point
An opaque wrapper for a time point generated by a timer.
Definition: timer.hpp:20
gko::Timer::create_time_point
time_point create_time_point()
Returns a newly created time point.
gko::CudaTimer::record
void record(time_point &time) override
Records a time point at the current time.
gko::Timer::difference
std::chrono::nanoseconds difference(time_point &start, time_point &stop)
Computes the difference between the two time points in nanoseconds.
gko::DpcppTimer
A timer using kernels for timing on a DpcppExecutor in profiling mode.
Definition: timer.hpp:213
gko::Timer
Represents a generic timer that can be used to record time points and measure time differences on hos...
Definition: timer.hpp:80
gko::CpuTimer::wait
void wait(time_point &time) override
Waits until all kernels in-process when recording the time point are finished.
gko::HipTimer::wait
void wait(time_point &time) override
Waits until all kernels in-process when recording the time point are finished.
gko::DpcppTimer::wait
void wait(time_point &time) override
Waits until all kernels in-process when recording the time point are finished.
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::CudaTimer
A timer using events for timing on a CudaExecutor.
Definition: timer.hpp:166
gko::DpcppTimer::difference_async
std::chrono::nanoseconds difference_async(const time_point &start, const time_point &stop) override
Computes the difference between the two time points in nanoseconds.
gko::DpcppTimer::record
void record(time_point &time) override
Records a time point at the current time.
gko::HipTimer::difference_async
std::chrono::nanoseconds difference_async(const time_point &start, const time_point &stop) override
Computes the difference between the two time points in nanoseconds.
gko::Timer::create_for_executor
static std::unique_ptr< Timer > create_for_executor(std::shared_ptr< const Executor > exec)
Creates the timer type most suitable for recording accurate timings of kernels on the given executor.
gko::HipTimer::record
void record(time_point &time) override
Records a time point at the current time.
gko::CudaTimer::wait
void wait(time_point &time) override
Waits until all kernels in-process when recording the time point are finished.
gko::CudaTimer::difference_async
std::chrono::nanoseconds difference_async(const time_point &start, const time_point &stop) override
Computes the difference between the two time points in nanoseconds.
gko::Timer::record
virtual void record(time_point &time)=0
Records a time point at the current time.