2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2017,2018, 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 Implements the GPU region timer for OpenCL.
39 * \author Aleksei Iupinov <a.yupinov@gmail.com>
44 #ifndef GMX_GPU_UTILS_GPUREGIONTIMER_OCL_H
45 #define GMX_GPU_UTILS_GPUREGIONTIMER_OCL_H
49 #include "gromacs/gpu_utils/gputraits_ocl.h"
50 #include "gromacs/gpu_utils/oclutils.h"
52 #include "gpuregiontimer.h"
54 /*! \libinternal \brief
55 * The OpenCL implementation of the GPU code region timing.
56 * With OpenCL, one has to use cl_event handle for each API call that has to be timed, and
57 * accumulate the timing afterwards. As we would like to avoid overhead on API calls,
58 * we only query and accumulate cl_event timing at the end of time steps, not after the API calls.
59 * Thus, this implementation does not reuse a single cl_event for multiple calls, but instead
60 * maintains an array of cl_events to be used within any single code region.
61 * The array size is fixed at a small but sufficiently large value for the number of cl_events
62 * that might contribute to a timer region, currently 10.
64 class GpuRegionTimerImpl
66 /*! \brief The underlying individual timing events array.
67 * The maximum size is chosen arbitrarily to work with current code, and can be changed.
68 * There is simply no need for run-time resizing, and it's unlikely we'll ever need more than 10.
70 std::array<cl_event, 10> events_ = {{nullptr}};
71 //! Index of the active event
72 size_t currentEvent_ = 0;
76 GpuRegionTimerImpl() = default;
77 ~GpuRegionTimerImpl() = default;
79 GpuRegionTimerImpl(const GpuRegionTimerImpl &) = delete;
81 GpuRegionTimerImpl &operator=(GpuRegionTimerImpl &&) = delete;
82 //! Moving is disabled but can be considered in the future if needed
83 GpuRegionTimerImpl(GpuRegionTimerImpl &&) = delete;
85 /*! \brief Should be called before the region start. */
86 inline void openTimingRegion(CommandStream /*unused*/){}
87 /*! \brief Should be called after the region end. */
88 inline void closeTimingRegion(CommandStream /*unused*/){}
89 /*! \brief Returns the last measured region timespan (in milliseconds) and calls reset(). */
90 inline double getLastRangeTime()
92 double milliseconds = 0.0;
93 for (size_t i = 0; i < currentEvent_; i++)
95 if (events_[i]) // This conditional is ugly, but is required to make some tests (e.g. empty domain) pass
97 cl_ulong start_ns, end_ns;
98 cl_int gmx_unused cl_error;
100 cl_error = clGetEventProfilingInfo(events_[i], CL_PROFILING_COMMAND_START,
101 sizeof(cl_ulong), &start_ns, nullptr);
102 GMX_ASSERT(CL_SUCCESS == cl_error, "GPU timing update failure");
103 cl_error = clGetEventProfilingInfo(events_[i], CL_PROFILING_COMMAND_END,
104 sizeof(cl_ulong), &end_ns, nullptr);
105 GMX_ASSERT(CL_SUCCESS == cl_error, "GPU timing update failure");
106 milliseconds += (end_ns - start_ns) / 1000000.0;
112 /*! \brief Resets the internal state, releasing the used cl_events. */
115 for (size_t i = 0; i < currentEvent_; i++)
117 if (events_[i]) // This conditional is ugly, but is required to make some tests (e.g. empty domain) pass
119 cl_int gmx_unused cl_error = clReleaseEvent(events_[i]);
120 GMX_ASSERT(CL_SUCCESS == cl_error, "OpenCL event release failure");
124 // As long as we're doing nullptr checks, we might want to be extra cautious.
125 events_.fill(nullptr);
127 /*! \brief Returns a new raw timing event
128 * for passing into individual GPU API calls
129 * within the region if the API requires it (e.g. on OpenCL).
131 inline CommandEvent *fetchNextEvent()
133 GMX_ASSERT(currentEvent_ < events_.size(), "Increase c_maxEventNumber_ if needed");
134 cl_event *result = &events_[currentEvent_];
140 //! Short-hand for external use
141 using GpuRegionTimer = GpuRegionTimerWrapper<GpuRegionTimerImpl>;