Ginkgo  Generated from pipelines/1330831941 branch based on master. Ginkgo version 1.8.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 
12 #include <ginkgo/core/base/executor.hpp>
13 
14 
15 namespace gko {
16 
17 
21 class time_point {
22 public:
23  ~time_point();
24 
26 
27  time_point& operator=(time_point&&);
28 
29  time_point(const time_point&) = delete;
30 
31  time_point& operator=(const time_point&) = delete;
32 
33 private:
34  time_point();
35 
36  friend class Timer;
37  friend class CpuTimer;
38  friend class CudaTimer;
39  friend class HipTimer;
40  friend class DpcppTimer;
41 
43  enum class type {
45  cpu,
47  cuda,
49  hip,
51  dpcpp,
52  };
53 
54  type type_;
55  union data_union {
56  CUevent_st* cuda_event;
57  GKO_HIP_EVENT_STRUCT* hip_event;
58  sycl::event* dpcpp_event;
59  std::chrono::steady_clock::time_point chrono;
60 
61  data_union();
62  } data_;
63 };
64 
65 
81 class Timer {
82 public:
83  virtual ~Timer() = default;
84 
90 
94  virtual void record(time_point& time) = 0;
95 
100  virtual void wait(time_point& time) = 0;
101 
110  std::chrono::nanoseconds difference(time_point& start, time_point& stop);
111 
124  virtual std::chrono::nanoseconds difference_async(
125  const time_point& start, const time_point& stop) = 0;
126 
137  static std::unique_ptr<Timer> create_for_executor(
138  std::shared_ptr<const Executor> exec);
139 
140 protected:
142  virtual void init_time_point(time_point& time) = 0;
143 };
144 
145 
147 class CpuTimer : public Timer {
148 public:
149  void record(time_point& time) override;
150 
151  void wait(time_point& time) override;
152 
153  std::chrono::nanoseconds difference_async(const time_point& start,
154  const time_point& stop) override;
155 
156 protected:
157  void init_time_point(time_point& time) override;
158 };
159 
160 
167 class CudaTimer : public Timer {
168 public:
169  void record(time_point& time) override;
170 
171  void wait(time_point& time) override;
172 
173  std::chrono::nanoseconds difference_async(const time_point& start,
174  const time_point& stop) override;
175 
176  CudaTimer(std::shared_ptr<const CudaExecutor> exec);
177 
178 protected:
179  void init_time_point(time_point& time) override;
180 
181 private:
182  int device_id_;
183  CUstream_st* stream_;
184 };
185 
186 
193 class HipTimer : public Timer {
194 public:
195  void record(time_point& time) override;
196 
197  void wait(time_point& time) override;
198 
199  std::chrono::nanoseconds difference_async(const time_point& start,
200  const time_point& stop) override;
201 
202  HipTimer(std::shared_ptr<const HipExecutor> exec);
203 
204 protected:
205  void init_time_point(time_point& time) override;
206 
207 private:
208  int device_id_;
209  GKO_HIP_STREAM_STRUCT* stream_;
210 };
211 
212 
214 class DpcppTimer : public Timer {
215 public:
216  void record(time_point& time) override;
217 
218  void wait(time_point& time) override;
219 
220  std::chrono::nanoseconds difference_async(const time_point& start,
221  const time_point& stop) override;
222 
223  DpcppTimer(std::shared_ptr<const DpcppExecutor> exec);
224 
225 protected:
226  void init_time_point(time_point& time) override;
227 
228 private:
229  sycl::queue* queue_;
230 };
231 
232 
233 } // namespace gko
234 
235 
236 #endif // GKO_PUBLIC_CORE_BASE_TIMER_HPP_
gko::HipTimer
A timer using events for timing on a HipExecutor.
Definition: timer.hpp:193
gko::CpuTimer
A timer using std::chrono::steady_clock for timing.
Definition: timer.hpp:147
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:21
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:214
gko::Timer
Represents a generic timer that can be used to record time points and measure time differences on hos...
Definition: timer.hpp:81
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:167
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.