Ginkgo Generated from branch based on master. Ginkgo version 1.7.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
timer.hpp
1/*******************************<GINKGO LICENSE>******************************
2Copyright (c) 2017-2023, the Ginkgo authors
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
91. Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright
13notice, this list of conditions and the following disclaimer in the
14documentation and/or other materials provided with the distribution.
15
163. Neither the name of the copyright holder nor the names of its
17contributors may be used to endorse or promote products derived from
18this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31******************************<GINKGO LICENSE>*******************************/
32
33#ifndef GKO_PUBLIC_CORE_BASE_TIMER_HPP_
34#define GKO_PUBLIC_CORE_BASE_TIMER_HPP_
35
36
37#include <chrono>
38
39
40#include <ginkgo/core/base/executor.hpp>
41
42
43namespace gko {
44
45
50public:
52
54
55 time_point& operator=(time_point&&);
56
57 time_point(const time_point&) = delete;
58
59 time_point& operator=(const time_point&) = delete;
60
61private:
62 time_point();
63
64 friend class Timer;
65 friend class CpuTimer;
66 friend class CudaTimer;
67 friend class HipTimer;
68 friend class DpcppTimer;
69
71 enum class type {
73 cpu,
75 cuda,
77 hip,
79 dpcpp,
80 };
81
82 type type_;
83 union data_union {
84 CUevent_st* cuda_event;
85 GKO_HIP_EVENT_STRUCT* hip_event;
86 sycl::event* dpcpp_event;
87 std::chrono::steady_clock::time_point chrono;
88
89 data_union();
90 } data_;
91};
92
93
109class Timer {
110public:
111 virtual ~Timer() = default;
112
118
122 virtual void record(time_point& time) = 0;
123
128 virtual void wait(time_point& time) = 0;
129
138 std::chrono::nanoseconds difference(time_point& start, time_point& stop);
139
152 virtual std::chrono::nanoseconds difference_async(
153 const time_point& start, const time_point& stop) = 0;
154
165 static std::unique_ptr<Timer> create_for_executor(
166 std::shared_ptr<const Executor> exec);
167
168protected:
170 virtual void init_time_point(time_point& time) = 0;
171};
172
173
175class CpuTimer : public Timer {
176public:
177 void record(time_point& time) override;
178
179 void wait(time_point& time) override;
180
181 std::chrono::nanoseconds difference_async(const time_point& start,
182 const time_point& stop) override;
183
184protected:
185 void init_time_point(time_point& time) override;
186};
187
188
190class CudaTimer : public Timer {
191public:
192 void record(time_point& time) override;
193
194 void wait(time_point& time) override;
195
196 std::chrono::nanoseconds difference_async(const time_point& start,
197 const time_point& stop) override;
198
199 CudaTimer(std::shared_ptr<const CudaExecutor> exec);
200
201protected:
202 void init_time_point(time_point& time) override;
203
204private:
205 std::shared_ptr<const CudaExecutor> exec_;
206};
207
208
210class HipTimer : public Timer {
211public:
212 void record(time_point& time) override;
213
214 void wait(time_point& time) override;
215
216 std::chrono::nanoseconds difference_async(const time_point& start,
217 const time_point& stop) override;
218
219 HipTimer(std::shared_ptr<const HipExecutor> exec);
220
221protected:
222 void init_time_point(time_point& time) override;
223
224private:
225 std::shared_ptr<const HipExecutor> exec_;
226};
227
228
230class DpcppTimer : public Timer {
231public:
232 void record(time_point& time) override;
233
234 void wait(time_point& time) override;
235
236 std::chrono::nanoseconds difference_async(const time_point& start,
237 const time_point& stop) override;
238
239 DpcppTimer(std::shared_ptr<const DpcppExecutor> exec);
240
241protected:
242 void init_time_point(time_point& time) override;
243
244private:
245 std::shared_ptr<const DpcppExecutor> exec_;
246};
247
248
249} // namespace gko
250
251
252#endif // GKO_PUBLIC_CORE_BASE_TIMER_HPP_
A timer using std::chrono::steady_clock for timing.
Definition timer.hpp:175
std::chrono::nanoseconds difference_async(const time_point &start, const time_point &stop) override
Computes the difference between the two time points in nanoseconds.
void wait(time_point &time) override
Waits until all kernels in-process when recording the time point are finished.
void record(time_point &time) override
Records a time point at the current time.
A timer using events for timing on a CudaExecutor.
Definition timer.hpp:190
void wait(time_point &time) override
Waits until all kernels in-process when recording the time point are finished.
std::chrono::nanoseconds difference_async(const time_point &start, const time_point &stop) override
Computes the difference between the two time points in nanoseconds.
void record(time_point &time) override
Records a time point at the current time.
A timer using kernels for timing on a DpcppExecutor in profiling mode.
Definition timer.hpp:230
void record(time_point &time) override
Records a time point at the current time.
std::chrono::nanoseconds difference_async(const time_point &start, const time_point &stop) override
Computes the difference between the two time points in nanoseconds.
void wait(time_point &time) override
Waits until all kernels in-process when recording the time point are finished.
A timer using events for timing on a HipExecutor.
Definition timer.hpp:210
void wait(time_point &time) override
Waits until all kernels in-process when recording the time point are finished.
std::chrono::nanoseconds difference_async(const time_point &start, const time_point &stop) override
Computes the difference between the two time points in nanoseconds.
void record(time_point &time) override
Records a time point at the current time.
Represents a generic timer that can be used to record time points and measure time differences on hos...
Definition timer.hpp:109
std::chrono::nanoseconds difference(time_point &start, time_point &stop)
Computes the difference between the two time points in nanoseconds.
time_point create_time_point()
Returns a newly created time point.
virtual void wait(time_point &time)=0
Waits until all kernels in-process when recording the time point are finished.
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.
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.
virtual void record(time_point &time)=0
Records a time point at the current time.
An opaque wrapper for a time point generated by a timer.
Definition timer.hpp:49
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803