Remove PrivateImplPointer in favour of std::unique_ptr
[alexxy/gromacs.git] / src / gromacs / gpu_utils / device_stream_manager.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2019,2020,2021, 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 /*! \libinternal \file
36  *
37  * \brief This file declares a manager of GPU context and streams needed for
38  * running workloads on GPUs.
39  *
40  * \author Mark Abraham <mark.j.abraham@gmail.com>
41  * \author Artem Zhmurov <zhmurov@gmail.com>
42  *
43  * \inlibraryapi
44  * \ingroup module_gpu_utils
45  */
46 #ifndef GMX_GPU_UTILS_GPUSTREAMMANAGER_H
47 #define GMX_GPU_UTILS_GPUSTREAMMANAGER_H
48
49 #include <memory>
50 #include <string>
51
52 class DeviceContext;
53 struct DeviceInformation;
54 class DeviceStream;
55
56 namespace gmx
57 {
58
59 class SimulationWorkload;
60
61 /*! \brief Class enum to describe the different logical streams used
62  * for GPU work.
63  *
64  * Whether the actual streams differ is an implementation detail of
65  * the manager class.
66  */
67 enum class DeviceStreamType : int
68 {
69     //! Stream primarily for short-ranged local nonbonded work.
70     NonBondedLocal,
71     //! Stream primarily for short-ranged nonlocal nonbonded work.
72     NonBondedNonLocal,
73     //! Stream primarily for PME work.
74     Pme,
75     //! Stream primarily for data exchange between PME and PP ranks.
76     PmePpTransfer,
77     //! Stream primarily for update and constraints.
78     UpdateAndConstraints,
79     //! Conventional termination of the enumeration.
80     Count
81 };
82
83 /*! \libinternal
84  * \brief Device stream and context manager.
85  *
86  * Manages the lifetime of the GPU streams and their association
87  * with context and device information that is needed to use them.
88  *
89  * If supported by the GPU API, the available runtime and the
90  * indicated device, some streams will be configured at high
91  * priority. Otherwise, all streams will share the default priority
92  * appropriate to the situation.
93  */
94 class DeviceStreamManager
95 {
96 public:
97     /*! \brief Constructor.
98      *
99      * \throws InternalError  If any of the required resources could not be initialized.
100      */
101     DeviceStreamManager(const DeviceInformation& deviceInfo,
102                         bool                     havePpDomainDecomposition,
103                         SimulationWorkload       simulationWork,
104                         bool                     useTiming);
105     ~DeviceStreamManager();
106
107     /*! \brief Get the device information object of the associated device.
108      *
109      * \returns reference to device info.
110      */
111     const DeviceInformation& deviceInfo() const;
112
113     /*! \brief Returns a handle to the GPU context.
114      *
115      * \todo This relies on the fact that only one unique device
116      * is described by nonbondedDeviceInfo and pmeDeviceInfo.
117      */
118     const DeviceContext& context() const;
119
120     /*! \brief Returns a handle to the requested GPU stream.
121      *
122      * \param[in] streamToGet Which stream to get.
123      */
124     const DeviceStream& stream(DeviceStreamType streamToGet) const;
125
126     /*! \brief Returns a handle to the GPU stream to compute bonded forces in.
127      *
128      * \param[in] hasPPDomainDecomposition Whether there is a particle-particle domain decomposition.
129      */
130     const DeviceStream& bondedStream(bool hasPPDomainDecomposition) const;
131
132     /*! \brief Return whether the requested GPU stream is valid for use.
133      *
134      * \param[in] streamToCheck Which stream to check.
135      *
136      * \returns Whether the stream was initialized.
137      */
138     bool streamIsValid(DeviceStreamType streamToCheck) const;
139
140 private:
141     class Impl;
142     std::unique_ptr<Impl> impl_;
143 };
144
145 } // namespace gmx
146
147 #endif