2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2017,2018,2019,2020 by the GROMACS development team.
5 * Copyright (c) 2021, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
37 /*! \libinternal \file
38 * \brief Defines the GPU region timer implementation/wrapper classes.
39 * The implementations live in gpuregiontimer.cuh for CUDA and gpuregiontimer_ocl.h for OpenCL.
41 * \author Aleksei Iupinov <a.yupinov@gmail.com>
44 #ifndef GMX_GPU_UTILS_GPUREGIONTIMER_H
45 #define GMX_GPU_UTILS_GPUREGIONTIMER_H
49 #include "gromacs/utility/gmxassert.h"
51 //! Debug GPU timers in debug builds only
53 static const bool c_debugTimerState = false;
55 static const bool c_debugTimerState = true;
58 /*! \libinternal \brief
59 * This is a GPU region timing wrapper class.
60 * It allows for host-side tracking of the accumulated execution timespans in GPU code
61 * (measuring kernel or transfers duration).
62 * It also partially tracks the correctness of the timer state transitions,
63 * as far as current implementation allows (see TODO in getLastRangeTime() for a disabled check).
64 * Internally it uses GpuRegionTimerImpl for measuring regions.
66 template<typename GpuRegionTimerImpl>
67 class GpuRegionTimerWrapper
69 //! The timer state used for debug-only assertions
75 } debugState_ = TimerState::Idle;
77 //! The number of times the timespan has been measured
78 unsigned int callCount_ = 0;
79 //! The accumulated duration of the timespans measured (milliseconds)
80 double totalMilliseconds_ = 0.0;
81 //! The underlying region timer implementation
82 GpuRegionTimerImpl impl_;
86 * To be called before the region start.
88 * \param[in] deviceStream The GPU command stream where the event being measured takes place.
90 void openTimingRegion(const DeviceStream& deviceStream)
92 if (c_debugTimerState)
94 std::string error = "GPU timer should be idle, but is "
95 + std::string((debugState_ == TimerState::Stopped) ? "stopped" : "recording")
97 GMX_ASSERT(debugState_ == TimerState::Idle, error.c_str());
98 debugState_ = TimerState::Recording;
100 impl_.openTimingRegion(deviceStream);
103 * To be called after the region end.
105 * \param[in] deviceStream The GPU command stream where the event being measured takes place.
107 void closeTimingRegion(const DeviceStream& deviceStream)
109 if (c_debugTimerState)
111 std::string error = "GPU timer should be recording, but is "
112 + std::string((debugState_ == TimerState::Idle) ? "idle" : "stopped")
114 GMX_ASSERT(debugState_ == TimerState::Recording, error.c_str());
115 debugState_ = TimerState::Stopped;
118 impl_.closeTimingRegion(deviceStream);
121 * Accumulates the last timespan of all the events used into the total duration,
122 * and resets the internal timer state.
123 * To be called after closeTimingRegion() and the command stream of the event having been
125 * \returns The last timespan (in milliseconds).
127 double getLastRangeTime()
129 if (c_debugTimerState)
131 /* The assertion below is commented because it is currently triggered on a special case:
132 * the early return before the local non-bonded kernel launch if there is nothing to do.
133 * This can be reproduced in OpenCL build by running
134 * mdrun-mpi-test -ntmpi 2 --gtest_filter=*Empty*
135 * Similarly, the GpuRegionTimerImpl suffers from non-nullptr
136 * cl_event conditionals which ideally should only be asserts.
137 * TODO: improve internal task scheduling, re-enable the assert, turn conditionals into asserts
140 std::string error = "GPU timer should be stopped, but is " + std::string((debugState_ == TimerState::Idle) ? "idle" : "recording") + ".";
141 GMX_ASSERT(debugState_ == TimerState::Stopped, error.c_str());
143 debugState_ = TimerState::Idle;
145 double milliseconds = impl_.getLastRangeTime();
146 totalMilliseconds_ += milliseconds;
149 /*! \brief Resets the implementation and total time/call count to zeroes. */
152 if (c_debugTimerState)
154 debugState_ = TimerState::Idle;
156 totalMilliseconds_ = 0.0;
160 /*! \brief Gets total time recorded (in milliseconds). */
161 double getTotalTime() const { return totalMilliseconds_; }
162 /*! \brief Gets total call count recorded. */
163 unsigned int getCallCount() const { return callCount_; }
165 * Gets a pointer to a new timing event for passing into individual GPU API calls
166 * within the region if they require it (e.g. on OpenCL).
167 * \returns The pointer to the underlying single command timing event.
169 CommandEvent* fetchNextEvent()
171 if (c_debugTimerState)
173 std::string error = "GPU timer should be recording, but is "
174 + std::string((debugState_ == TimerState::Idle) ? "idle" : "stopped")
176 GMX_ASSERT(debugState_ == TimerState::Recording, error.c_str());
178 return impl_.fetchNextEvent();