Merge branch 'origin/release-2020' into master
[alexxy/gromacs.git] / src / gromacs / gpu_utils / device_stream_manager.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2019,2020, 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 /*! \internal \file
36  *
37  * \brief Implements GPU stream manager.
38  *
39  * \author Mark Abraham <mark.j.abraham@gmail.com>
40  * \author Artem Zhmurov <zhmurov@gmail.com>
41  *
42  * \ingroup module_gpu_utils
43  */
44 #include "gmxpre.h"
45
46 #include "device_stream_manager.h"
47
48 #include "gromacs/gpu_utils/device_context.h"
49 #include "gromacs/gpu_utils/device_stream.h"
50 #include "gromacs/gpu_utils/gputraits.h"
51 #include "gromacs/utility/enumerationhelpers.h"
52 #include "gromacs/utility/exceptions.h"
53 #include "gromacs/utility/gmxassert.h"
54 #include "gromacs/utility/stringutil.h"
55
56 namespace gmx
57 {
58
59 /*! \libinternal
60  * \brief Impl class to manages the lifetime of the GPU streams.
61  *
62  * If supported by the GPU API, the available runtime and the
63  * indicated device, some streams will be configured at high
64  * priority. Otherwise, all streams will share the default priority
65  * appropriate to the situation.
66  */
67 class DeviceStreamManager::Impl
68 {
69 public:
70     /*! \brief Constructor.
71      *
72      * \throws InternalError  If any of the required resources could not be initialized.
73      */
74     Impl(const DeviceInformation& deviceInfo,
75          bool                     useGpuForPme,
76          bool                     havePpDomainDecomposition,
77          bool                     doGpuPmePpTransfer,
78          bool                     useGpuForUpdate,
79          bool                     useTiming);
80     ~Impl();
81
82     //! Device context.
83     DeviceContext context_;
84     //! GPU command streams.
85     EnumerationArray<DeviceStreamType, DeviceStream> streams_;
86 };
87
88 // DeviceStreamManager::Impl
89 DeviceStreamManager::Impl::Impl(const DeviceInformation& deviceInfo,
90                                 const bool               useGpuForPme,
91                                 const bool               havePpDomainDecomposition,
92                                 const bool               doGpuPmePpTransfer,
93                                 const bool               useGpuForUpdate,
94                                 const bool               useTiming) :
95     context_(deviceInfo)
96 {
97     try
98     {
99         streams_[DeviceStreamType::NonBondedLocal].init(context_, DeviceStreamPriority::Normal, useTiming);
100
101         if (useGpuForPme)
102         {
103             /* Creating a PME GPU stream:
104              * - default high priority with CUDA
105              * - no priorities implemented yet with OpenCL; see #2532
106              */
107             streams_[DeviceStreamType::Pme].init(context_, DeviceStreamPriority::High, useTiming);
108         }
109
110         if (havePpDomainDecomposition)
111         {
112             streams_[DeviceStreamType::NonBondedNonLocal].init(context_, DeviceStreamPriority::High,
113                                                                useTiming);
114         }
115         // Update stream is used both for coordinates transfers and for GPU update/constraints
116         if (useGpuForPme || useGpuForUpdate)
117         {
118             streams_[DeviceStreamType::UpdateAndConstraints].init(
119                     context_, DeviceStreamPriority::Normal, useTiming);
120         }
121         if (doGpuPmePpTransfer)
122         {
123             streams_[DeviceStreamType::PmePpTransfer].init(context_, DeviceStreamPriority::Normal, useTiming);
124         }
125     }
126     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
127 }
128
129 DeviceStreamManager::Impl::~Impl() = default;
130
131 // DeviceStreamManager
132 DeviceStreamManager::DeviceStreamManager(const DeviceInformation& deviceInfo,
133                                          const bool               useGpuForPme,
134                                          const bool               havePpDomainDecomposition,
135                                          const bool               doGpuPmePpTransfer,
136                                          const bool               useGpuForUpdate,
137                                          const bool               useTiming) :
138     impl_(new Impl(deviceInfo, useGpuForPme, havePpDomainDecomposition, doGpuPmePpTransfer, useGpuForUpdate, useTiming))
139 {
140 }
141
142 DeviceStreamManager::~DeviceStreamManager() = default;
143
144 const DeviceInformation& DeviceStreamManager::deviceInfo() const
145 {
146     return impl_->context_.deviceInfo();
147 }
148
149 const DeviceContext& DeviceStreamManager::context() const
150 {
151     return impl_->context_;
152 }
153
154 const DeviceStream& DeviceStreamManager::stream(DeviceStreamType streamToGet) const
155 {
156     return impl_->streams_[streamToGet];
157 }
158
159 const DeviceStream& DeviceStreamManager::bondedStream(bool hasPPDomainDecomposition) const
160 {
161     if (hasPPDomainDecomposition)
162     {
163         GMX_RELEASE_ASSERT(stream(DeviceStreamType::NonBondedNonLocal).isValid(),
164                            "GPU non-bonded non-local stream should be valid in order to use GPU "
165                            "version of bonded forces with domain decomposition.");
166         return stream(DeviceStreamType::NonBondedNonLocal);
167     }
168     else
169     {
170         GMX_RELEASE_ASSERT(stream(DeviceStreamType::NonBondedLocal).isValid(),
171                            "GPU non-bonded local stream should be valid in order to use GPU "
172                            "version of bonded forces without domain decomposition.");
173         return stream(DeviceStreamType::NonBondedLocal);
174     }
175 }
176
177 bool DeviceStreamManager::streamIsValid(DeviceStreamType streamToCheck) const
178 {
179     return impl_->streams_[streamToCheck].isValid();
180 }
181
182 } // namespace gmx