Add GpuRegionTimer class
[alexxy/gromacs.git] / src / gromacs / gpu_utils / gpuregiontimer_ocl.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35
36 /*! \libinternal \file
37  *  \brief Implements the GPU region timer for OpenCL.
38  *
39  *  \author Aleksei Iupinov <a.yupinov@gmail.com>
40  *
41  *  \inlibraryapi
42  */
43
44 #ifndef GMX_GPU_UTILS_GPUREGIONTIMER_OCL_H
45 #define GMX_GPU_UTILS_GPUREGIONTIMER_OCL_H
46
47 #include <array>
48
49 #include "gromacs/gpu_utils/oclutils.h"
50
51 #include "gpuregiontimer.h"
52
53 template <> struct GpuTraits<GpuFramework::OpenCL>
54 {
55     using CommandStream = cl_command_queue;
56     using CommandEvent  = cl_event;
57 };
58
59 //! Short-hand for external use
60 using GpuRegionTimer = GpuRegionTimerWrapper<GpuFramework::OpenCL>;
61
62 // cppcheck-suppress noConstructor
63 template <> class GpuRegionTimerImpl<GpuFramework::OpenCL>
64 {
65     //! Short-hands
66     using CommandStream = typename GpuTraits<GpuFramework::OpenCL>::CommandStream;
67     using CommandEvent  = typename GpuTraits<GpuFramework::OpenCL>::CommandEvent;
68
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.
72      */
73     std::array<cl_event, 10> events_ = {{nullptr}};
74     //! Index of the active event
75     size_t                   currentEvent_ = 0;
76
77     public:
78         GpuRegionTimerImpl()  = default;
79         ~GpuRegionTimerImpl() = default;
80         inline void openTimingRegion(CommandStream){}
81         inline void closeTimingRegion(CommandStream){}
82
83         inline double getLastRangeTime()
84         {
85             double milliseconds = 0.0;
86             for (size_t i = 0; i < currentEvent_; i++)
87             {
88                 if (events_[i]) // This conditional is ugly, but is required to make some tests (e.g. empty domain) pass
89                 {
90                     cl_ulong          start_ns, end_ns;
91                     cl_int gmx_unused cl_error;
92
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;
100                 }
101             }
102             reset();
103             return milliseconds;
104         }
105
106         inline void reset()
107         {
108             for (size_t i = 0; i < currentEvent_; i++)
109             {
110                 if (events_[i]) // This conditional is ugly, but is required to make some tests (e.g. empty domain) pass
111                 {
112                     GMX_ASSERT(CL_SUCCESS == clReleaseEvent(events_[i]), "OpenCL event release failure");
113                 }
114             }
115             currentEvent_ = 0;
116             // As long as we're doing nullptr checks, we might want to be extra cautious.
117             events_.fill(nullptr);
118         }
119
120         inline CommandEvent *fetchNextEvent()
121         {
122             GMX_ASSERT(currentEvent_ < events_.size(), "Increase c_maxEventNumber_ if needed");
123             cl_event *result = &events_[currentEvent_];
124             currentEvent_++;
125             return result;
126         }
127 };
128
129 #endif