PME GPU/CUDA data framework.
[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
79         inline void openTimingRegion(CommandStream){}
80         inline void closeTimingRegion(CommandStream){}
81
82         inline double getLastRangeTime()
83         {
84             double milliseconds = 0.0;
85             for (size_t i = 0; i < currentEvent_; i++)
86             {
87                 if (events_[i]) // This conditional is ugly, but is required to make some tests (e.g. empty domain) pass
88                 {
89                     cl_ulong          start_ns, end_ns;
90                     cl_int gmx_unused cl_error;
91
92                     cl_error = clGetEventProfilingInfo(events_[i], CL_PROFILING_COMMAND_START,
93                                                        sizeof(cl_ulong), &start_ns, nullptr);
94                     GMX_ASSERT(CL_SUCCESS == cl_error, "GPU timing update failure");
95                     cl_error = clGetEventProfilingInfo(events_[i], CL_PROFILING_COMMAND_END,
96                                                        sizeof(cl_ulong), &end_ns, nullptr);
97                     GMX_ASSERT(CL_SUCCESS == cl_error, "GPU timing update failure");
98                     milliseconds += (end_ns - start_ns) / 1000000.0;
99                 }
100             }
101             reset();
102             return milliseconds;
103         }
104
105         inline void reset()
106         {
107             for (size_t i = 0; i < currentEvent_; i++)
108             {
109                 if (events_[i]) // This conditional is ugly, but is required to make some tests (e.g. empty domain) pass
110                 {
111                     GMX_ASSERT(CL_SUCCESS == clReleaseEvent(events_[i]), "OpenCL event release failure");
112                 }
113             }
114             currentEvent_ = 0;
115             // As long as we're doing nullptr checks, we might want to be extra cautious.
116             events_.fill(nullptr);
117         }
118
119         inline CommandEvent *fetchNextEvent()
120         {
121             GMX_ASSERT(currentEvent_ < events_.size(), "Increase c_maxEventNumber_ if needed");
122             cl_event *result = &events_[currentEvent_];
123             currentEvent_++;
124             return result;
125         }
126 };
127
128 #endif