PME GPU/CUDA data framework.
[gromacs.git] / src / gromacs / gpu_utils / gpuregiontimer_ocl.h
blobfc64d04e96e18752607130dcad23cee6df3656ce
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:
79 inline void openTimingRegion(CommandStream){}
80 inline void closeTimingRegion(CommandStream){}
82 inline double getLastRangeTime()
84 double milliseconds = 0.0;
85 for (size_t i = 0; i < currentEvent_; i++)
87 if (events_[i]) // This conditional is ugly, but is required to make some tests (e.g. empty domain) pass
89 cl_ulong start_ns, end_ns;
90 cl_int gmx_unused cl_error;
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;
101 reset();
102 return milliseconds;
105 inline void reset()
107 for (size_t i = 0; i < currentEvent_; i++)
109 if (events_[i]) // This conditional is ugly, but is required to make some tests (e.g. empty domain) pass
111 GMX_ASSERT(CL_SUCCESS == clReleaseEvent(events_[i]), "OpenCL event release failure");
114 currentEvent_ = 0;
115 // As long as we're doing nullptr checks, we might want to be extra cautious.
116 events_.fill(nullptr);
119 inline CommandEvent *fetchNextEvent()
121 GMX_ASSERT(currentEvent_ < events_.size(), "Increase c_maxEventNumber_ if needed");
122 cl_event *result = &events_[currentEvent_];
123 currentEvent_++;
124 return result;
128 #endif