2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2017, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
36 /*! \libinternal \file
37 * \brief Defines the GPU region timer implementation/wrapper classes.
38 * The implementations live in gpuregiontimer.cuh for CUDA and gpuregiontimer_ocl.h for OpenCL.
40 * \author Aleksei Iupinov <a.yupinov@gmail.com>
43 #ifndef GMX_GPU_UTILS_GPUREGIONTIMER_H
44 #define GMX_GPU_UTILS_GPUREGIONTIMER_H
48 #include "gromacs/utility/gmxassert.h"
50 //! Debug GPU timers in debug builds only
52 static const bool c_debugTimerState = false;
54 static const bool c_debugTimerState = true;
57 /*! \libinternal \brief
58 * Enumeration of possible GPU build-paths.
59 * \todo Move somewhere general?
61 enum class GpuFramework
67 /*! \libinternal \brief
68 * GPU build-path traits such as types.
69 * \todo Move somewhere general?
71 template <GpuFramework> struct GpuTraits
73 using CommandStream = void; //!< GPU command stream
74 using CommandEvent = void; //!< Single command call timing event - used in OpenCL
77 /*! \libinternal \brief
78 * This is a GPU region timing implementation interface.
79 * It should provide methods for measuring the last timespan.
81 template <GpuFramework framework> class GpuRegionTimerImpl
84 using CommandStream = typename GpuTraits<framework>::CommandStream;
85 using CommandEvent = typename GpuTraits<framework>::CommandEvent;
88 GpuRegionTimerImpl() = default;
89 ~GpuRegionTimerImpl() = default;
91 /*! \brief Will be called before the region start. */
92 inline void openTimingRegion(CommandStream) = 0;
93 /*! \brief Will be called after the region end. */
94 inline void closeTimingRegion(CommandStream) = 0;
95 /*! \brief Resets any internal state if needed */
96 inline void reset() = 0;
97 /*! \brief Returns the last measured region timespan (in milliseconds) and calls reset() */
98 inline double getLastRangeTime() = 0;
99 /*! \brief Returns a new raw timing event
100 * for passing into individual GPU API calls
101 * within the region if the API requires it (e.g. on OpenCL). */
102 inline CommandEvent *fetchNextEvent() = 0;
105 /*! \libinternal \brief
106 * This is a GPU region timing wrapper class.
107 * It allows for host-side tracking of the accumulated execution timespans in GPU code
108 * (measuring kernel or transfers duration).
109 * It also partially tracks the correctness of the timer state transitions,
110 * as far as current implementation allows (see TODO in getLastRangeTime() for a disabled check).
111 * Internally it uses GpuRegionTimerImpl for measuring regions.
113 template <GpuFramework framework> class GpuRegionTimerWrapper
116 using CommandStream = typename GpuTraits<framework>::CommandStream;
117 using CommandEvent = typename GpuTraits<framework>::CommandEvent;
118 //! The timer state used for debug-only assertions
119 enum class TimerState
126 //! The number of times the timespan has been measured
127 unsigned int callCount_;
128 //! The accumulated duration of the timespans measured (milliseconds)
129 double totalMilliseconds_;
130 //! The underlying region timer implementation
131 GpuRegionTimerImpl<framework> impl_;
134 GpuRegionTimerWrapper() : impl_()
138 ~GpuRegionTimerWrapper() = default;
140 * To be called before the region start.
142 * \param[in] s The GPU command stream where the event being measured takes place.
144 void openTimingRegion(CommandStream s)
146 if (c_debugTimerState)
148 std::string error = "GPU timer should be idle, but is " + std::string((debugState_ == TimerState::Stopped) ? "stopped" : "recording") + ".";
149 GMX_ASSERT(debugState_ == TimerState::Idle, error.c_str());
150 debugState_ = TimerState::Recording;
152 impl_.openTimingRegion(s);
155 * To be called after the region end.
157 * \param[in] s The GPU command stream where the event being measured takes place.
159 void closeTimingRegion(CommandStream s)
161 if (c_debugTimerState)
163 std::string error = "GPU timer should be recording, but is " + std::string((debugState_ == TimerState::Idle) ? "idle" : "stopped") + ".";
164 GMX_ASSERT(debugState_ == TimerState::Recording, error.c_str());
165 debugState_ = TimerState::Stopped;
168 impl_.closeTimingRegion(s);
171 * Accumulates the last timespan of all the events used into the the total duration,
172 * and resets the internal timer state.
173 * To be called after closeTimingRegion() and the command stream of the event having been synchronized.
174 * \returns The last timespan (in milliseconds).
176 double getLastRangeTime()
178 if (c_debugTimerState)
180 /* The assertion below is commented because it is currently triggered on a special case:
181 * the early return before the local non-bonded kernel launch if there is nothing to do.
182 * This can be reproduced in OpenCL build by running
183 * mdrun-mpi-test -ntmpi 2 --gtest_filter=*Empty*
184 * Similarly, the GpuRegionTimerImpl suffers from non-nullptr
185 * cl_event conditionals which ideally should only be asserts.
186 * TODO: improve internal task scheduling, re-enable the assert, turn conditionals into asserts
189 std::string error = "GPU timer should be stopped, but is " + std::string((debugState_ == TimerState::Idle) ? "idle" : "recording") + ".";
190 GMX_ASSERT(debugState_ == TimerState::Stopped, error.c_str());
192 debugState_ = TimerState::Idle;
194 double milliseconds = impl_.getLastRangeTime();
195 totalMilliseconds_ += milliseconds;
198 /*! \brief Resets the implementation and total time/call count to zeroes. */
201 if (c_debugTimerState)
203 debugState_ = TimerState::Idle;
205 totalMilliseconds_ = 0.0;
209 /*! \brief Gets total time recorded (in milliseconds). */
210 double getTotalTime() const
212 return totalMilliseconds_;
214 /*! \brief Gets total call count recorded. */
215 unsigned int getCallCount() const
220 * Gets a pointer to a new timing event for passing into individual GPU API calls
221 * within the region if they require it (e.g. on OpenCL).
222 * \returns The pointer to the underlying single command timing event.
224 CommandEvent *fetchNextEvent()
226 if (c_debugTimerState)
228 std::string error = "GPU timer should be recording, but is " + std::string((debugState_ == TimerState::Idle) ? "idle" : "stopped") + ".";
229 GMX_ASSERT(debugState_ == TimerState::Recording, error.c_str());
231 return impl_.fetchNextEvent();