Fix random typos
[alexxy/gromacs.git] / src / gromacs / gpu_utils / gpuregiontimer.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
9  *
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.
14  *
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.
19  *
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.
24  *
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.
32  *
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.
35  */
36
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.
40  *
41  *  \author Aleksei Iupinov <a.yupinov@gmail.com>
42  */
43
44 #ifndef GMX_GPU_UTILS_GPUREGIONTIMER_H
45 #define GMX_GPU_UTILS_GPUREGIONTIMER_H
46
47 #include <string>
48
49 #include "gromacs/utility/gmxassert.h"
50
51 //! Debug GPU timers in debug builds only
52 #if defined(NDEBUG)
53 static const bool c_debugTimerState = false;
54 #else
55 static const bool c_debugTimerState = true;
56 #endif
57
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.
65  */
66 template<typename GpuRegionTimerImpl>
67 class GpuRegionTimerWrapper
68 {
69     //! The timer state used for debug-only assertions
70     enum class TimerState
71     {
72         Idle,
73         Recording,
74         Stopped
75     } debugState_ = TimerState::Idle;
76
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_;
83
84 public:
85     /*! \brief
86      * To be called before the region start.
87      *
88      * \param[in] deviceStream   The GPU command stream where the event being measured takes place.
89      */
90     void openTimingRegion(const DeviceStream& deviceStream)
91     {
92         if (c_debugTimerState)
93         {
94             std::string error = "GPU timer should be idle, but is "
95                                 + std::string((debugState_ == TimerState::Stopped) ? "stopped" : "recording")
96                                 + ".";
97             GMX_ASSERT(debugState_ == TimerState::Idle, error.c_str());
98             debugState_ = TimerState::Recording;
99         }
100         impl_.openTimingRegion(deviceStream);
101     }
102     /*! \brief
103      * To be called after the region end.
104      *
105      * \param[in] deviceStream   The GPU command stream where the event being measured takes place.
106      */
107     void closeTimingRegion(const DeviceStream& deviceStream)
108     {
109         if (c_debugTimerState)
110         {
111             std::string error = "GPU timer should be recording, but is "
112                                 + std::string((debugState_ == TimerState::Idle) ? "idle" : "stopped")
113                                 + ".";
114             GMX_ASSERT(debugState_ == TimerState::Recording, error.c_str());
115             debugState_ = TimerState::Stopped;
116         }
117         callCount_++;
118         impl_.closeTimingRegion(deviceStream);
119     }
120     /*! \brief
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
124      * synchronized.
125      * \returns The last timespan (in milliseconds).
126      */
127     double getLastRangeTime()
128     {
129         if (c_debugTimerState)
130         {
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
138              */
139             /*
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());
142              */
143             debugState_ = TimerState::Idle;
144         }
145         double milliseconds = impl_.getLastRangeTime();
146         totalMilliseconds_ += milliseconds;
147         return milliseconds;
148     }
149     /*! \brief Resets the implementation and total time/call count to zeroes. */
150     void reset()
151     {
152         if (c_debugTimerState)
153         {
154             debugState_ = TimerState::Idle;
155         }
156         totalMilliseconds_ = 0.0;
157         callCount_         = 0;
158         impl_.reset();
159     }
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_; }
164     /*! \brief
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.
168      */
169     CommandEvent* fetchNextEvent()
170     {
171         if (c_debugTimerState)
172         {
173             std::string error = "GPU timer should be recording, but is "
174                                 + std::string((debugState_ == TimerState::Idle) ? "idle" : "stopped")
175                                 + ".";
176             GMX_ASSERT(debugState_ == TimerState::Recording, error.c_str());
177         }
178         return impl_.fetchNextEvent();
179     }
180 };
181
182 #endif