Add GpuRegionTimer class
[gromacs.git] / src / gromacs / gpu_utils / gpuregiontimer_ocl.h
blobe0b4b3f87f3c927cce04fa1676a778d6d1c526fc
1 /*
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 Implements the GPU region timer for OpenCL.
39 * \author Aleksei Iupinov <a.yupinov@gmail.com>
41 * \inlibraryapi
44 #ifndef GMX_GPU_UTILS_GPUREGIONTIMER_OCL_H
45 #define GMX_GPU_UTILS_GPUREGIONTIMER_OCL_H
47 #include <array>
49 #include "gromacs/gpu_utils/oclutils.h"
51 #include "gpuregiontimer.h"
53 template <> struct GpuTraits<GpuFramework::OpenCL>
55 using CommandStream = cl_command_queue;
56 using CommandEvent = cl_event;
59 //! Short-hand for external use
60 using GpuRegionTimer = GpuRegionTimerWrapper<GpuFramework::OpenCL>;
62 // cppcheck-suppress noConstructor
63 template <> class GpuRegionTimerImpl<GpuFramework::OpenCL>
65 //! Short-hands
66 using CommandStream = typename GpuTraits<GpuFramework::OpenCL>::CommandStream;
67 using CommandEvent = typename GpuTraits<GpuFramework::OpenCL>::CommandEvent;
69 /*! \brief The underlying individual timing events array.
70 * The maximum size is chosen arbitrarily to work with current code, and can be changed.
71 * There is simply no need for run-time resizing, and it's unlikely we'll ever need more than 10.
73 std::array<cl_event, 10> events_ = {{nullptr}};
74 //! Index of the active event
75 size_t currentEvent_ = 0;
77 public:
78 GpuRegionTimerImpl() = default;
79 ~GpuRegionTimerImpl() = default;
80 inline void openTimingRegion(CommandStream){}
81 inline void closeTimingRegion(CommandStream){}
83 inline double getLastRangeTime()
85 double milliseconds = 0.0;
86 for (size_t i = 0; i < currentEvent_; i++)
88 if (events_[i]) // This conditional is ugly, but is required to make some tests (e.g. empty domain) pass
90 cl_ulong start_ns, end_ns;
91 cl_int gmx_unused cl_error;
93 cl_error = clGetEventProfilingInfo(events_[i], CL_PROFILING_COMMAND_START,
94 sizeof(cl_ulong), &start_ns, nullptr);
95 GMX_ASSERT(CL_SUCCESS == cl_error, "GPU timing update failure");
96 cl_error = clGetEventProfilingInfo(events_[i], CL_PROFILING_COMMAND_END,
97 sizeof(cl_ulong), &end_ns, nullptr);
98 GMX_ASSERT(CL_SUCCESS == cl_error, "GPU timing update failure");
99 milliseconds += (end_ns - start_ns) / 1000000.0;
102 reset();
103 return milliseconds;
106 inline void reset()
108 for (size_t i = 0; i < currentEvent_; i++)
110 if (events_[i]) // This conditional is ugly, but is required to make some tests (e.g. empty domain) pass
112 GMX_ASSERT(CL_SUCCESS == clReleaseEvent(events_[i]), "OpenCL event release failure");
115 currentEvent_ = 0;
116 // As long as we're doing nullptr checks, we might want to be extra cautious.
117 events_.fill(nullptr);
120 inline CommandEvent *fetchNextEvent()
122 GMX_ASSERT(currentEvent_ < events_.size(), "Increase c_maxEventNumber_ if needed");
123 cl_event *result = &events_[currentEvent_];
124 currentEvent_++;
125 return result;
129 #endif