Apply re-formatting to C++ in src/ tree.
[alexxy/gromacs.git] / src / gromacs / gpu_utils / tests / device_stream_manager.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2017,2018,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  * \brief Tests GPU stream manager
37  *
38  * \author Mark Abraham <mark.j.abraham@gmail.com>
39  * \author Artem Zhmurov <zhmurov@gmail.com>
40  *
41  * \ingroup module_gpu_utils
42  */
43 #include "gmxpre.h"
44
45 #include "gromacs/gpu_utils/device_stream_manager.h"
46
47 #include "config.h"
48
49 #include <initializer_list>
50 #include <vector>
51
52 #include <gtest/gtest.h>
53
54 #include "gromacs/hardware/device_management.h"
55 #include "gromacs/mdtypes/simulation_workload.h"
56 #include "gromacs/utility/enumerationhelpers.h"
57
58 #include "testutils/test_hardware_environment.h"
59
60 namespace gmx
61 {
62
63 namespace test
64 {
65
66 namespace
67 {
68
69 //! GPU device stream names for outputs.
70 const EnumerationArray<DeviceStreamType, std::string> c_deviceStreamNames = {
71     { "non-bonded local", "non-bonded non-local", "PME", "PME-PP transfer", "update" }
72 };
73
74 /*! \brief Non-GPU builds return nullptr instead of streams,
75  * so we have to expect that in such build configurations. */
76 constexpr bool c_canExpectValidStreams = (GMX_GPU != 0);
77
78 //! Helper function to implement readable testing
79 void expectValidStreams(DeviceStreamManager* manager, std::initializer_list<DeviceStreamType> types)
80 {
81     if (c_canExpectValidStreams)
82     {
83         for (const DeviceStreamType type : types)
84         {
85             SCOPED_TRACE("Testing " + c_deviceStreamNames[type] + " stream.");
86             EXPECT_TRUE(manager->streamIsValid(type));
87         }
88     }
89 }
90 //! Helper function to implement readable testing
91 void expectInvalidStreams(DeviceStreamManager* manager, std::initializer_list<DeviceStreamType> types)
92 {
93     for (const DeviceStreamType type : types)
94     {
95         SCOPED_TRACE("Testing " + c_deviceStreamNames[type] + " stream.");
96         EXPECT_FALSE(manager->streamIsValid(type));
97     }
98 }
99
100 //! Test fixture
101 class DeviceStreamManagerTest : public ::testing::Test
102 {
103 public:
104 };
105
106 TEST_F(DeviceStreamManagerTest, CorrectStreamsAreReturnedOnNonbondedDevice)
107 {
108     // It would be nice to test that the priority is high when it can
109     // be, but that requires calling the same API calls we're testing
110     // that we've called, so it is not very useful.
111     const bool useTiming = false;
112
113     const auto& testDeviceList = getTestHardwareEnvironment()->getTestDeviceList();
114     for (const auto& testDevice : testDeviceList)
115     {
116         const DeviceInformation& deviceInfo = testDevice->deviceInfo();
117         setActiveDevice(deviceInfo);
118
119         {
120             SCOPED_TRACE("No DD, no PME rank, no GPU update");
121             SimulationWorkload simulationWork;
122             simulationWork.useGpuPme                      = false;
123             simulationWork.useGpuPmePpCommunication       = false;
124             simulationWork.useGpuUpdate                   = false;
125             bool                havePpDomainDecomposition = false;
126             DeviceStreamManager manager(deviceInfo, havePpDomainDecomposition, simulationWork, useTiming);
127
128             expectValidStreams(&manager, { DeviceStreamType::NonBondedLocal });
129             expectInvalidStreams(&manager,
130                                  { DeviceStreamType::NonBondedNonLocal,
131                                    DeviceStreamType::Pme,
132                                    DeviceStreamType::PmePpTransfer,
133                                    DeviceStreamType::UpdateAndConstraints });
134         }
135
136         {
137             SCOPED_TRACE("With DD, no PME rank, no GPU update");
138             SimulationWorkload simulationWork;
139             simulationWork.useGpuPme                      = false;
140             simulationWork.useGpuPmePpCommunication       = false;
141             simulationWork.useGpuUpdate                   = false;
142             bool                havePpDomainDecomposition = true;
143             DeviceStreamManager manager(deviceInfo, havePpDomainDecomposition, simulationWork, useTiming);
144
145             expectValidStreams(
146                     &manager, { DeviceStreamType::NonBondedLocal, DeviceStreamType::NonBondedNonLocal });
147             expectInvalidStreams(&manager,
148                                  { DeviceStreamType::Pme,
149                                    DeviceStreamType::PmePpTransfer,
150                                    DeviceStreamType::UpdateAndConstraints });
151         }
152
153         {
154             SCOPED_TRACE("No DD, with PME rank, no GPU update");
155             SimulationWorkload simulationWork;
156             simulationWork.useGpuPme                      = true;
157             simulationWork.useGpuPmePpCommunication       = true;
158             simulationWork.useGpuUpdate                   = false;
159             bool                havePpDomainDecomposition = false;
160             DeviceStreamManager manager(deviceInfo, havePpDomainDecomposition, simulationWork, useTiming);
161
162             expectValidStreams(&manager,
163                                { DeviceStreamType::Pme,
164                                  DeviceStreamType::NonBondedLocal,
165                                  DeviceStreamType::PmePpTransfer,
166                                  DeviceStreamType::UpdateAndConstraints });
167             expectInvalidStreams(&manager, { DeviceStreamType::NonBondedNonLocal });
168         }
169
170         {
171             SCOPED_TRACE("With DD, with PME rank, no GPU update");
172             SimulationWorkload simulationWork;
173             simulationWork.useGpuPme                      = true;
174             simulationWork.useGpuPmePpCommunication       = true;
175             simulationWork.useGpuUpdate                   = false;
176             bool                havePpDomainDecomposition = true;
177             DeviceStreamManager manager(deviceInfo, havePpDomainDecomposition, simulationWork, useTiming);
178
179             expectValidStreams(&manager,
180                                { DeviceStreamType::Pme,
181                                  DeviceStreamType::NonBondedLocal,
182                                  DeviceStreamType::NonBondedNonLocal,
183                                  DeviceStreamType::PmePpTransfer,
184                                  DeviceStreamType::UpdateAndConstraints });
185         }
186
187         {
188             SCOPED_TRACE("No DD, no PME rank, with GPU update");
189             SimulationWorkload simulationWork;
190             simulationWork.useGpuPme                      = false;
191             simulationWork.useGpuPmePpCommunication       = false;
192             simulationWork.useGpuUpdate                   = true;
193             bool                havePpDomainDecomposition = false;
194             DeviceStreamManager manager(deviceInfo, havePpDomainDecomposition, simulationWork, useTiming);
195
196             expectValidStreams(
197                     &manager, { DeviceStreamType::NonBondedLocal, DeviceStreamType::UpdateAndConstraints });
198             expectInvalidStreams(&manager,
199                                  { DeviceStreamType::NonBondedNonLocal,
200                                    DeviceStreamType::Pme,
201                                    DeviceStreamType::PmePpTransfer });
202         }
203
204         {
205             SCOPED_TRACE("With DD, no PME rank, with GPU update");
206             SimulationWorkload simulationWork;
207             simulationWork.useGpuPme                      = false;
208             simulationWork.useGpuPmePpCommunication       = false;
209             simulationWork.useGpuUpdate                   = true;
210             bool                havePpDomainDecomposition = true;
211             DeviceStreamManager manager(deviceInfo, havePpDomainDecomposition, simulationWork, useTiming);
212
213             expectValidStreams(&manager,
214                                { DeviceStreamType::NonBondedLocal,
215                                  DeviceStreamType::NonBondedNonLocal,
216                                  DeviceStreamType::UpdateAndConstraints });
217             expectInvalidStreams(&manager, { DeviceStreamType::Pme, DeviceStreamType::PmePpTransfer });
218         }
219
220         {
221             SCOPED_TRACE("No DD, with PME rank, with GPU update");
222             SimulationWorkload simulationWork;
223             simulationWork.useGpuPme                      = true;
224             simulationWork.useGpuPmePpCommunication       = true;
225             simulationWork.useGpuUpdate                   = true;
226             bool                havePpDomainDecomposition = false;
227             DeviceStreamManager manager(deviceInfo, havePpDomainDecomposition, simulationWork, useTiming);
228
229             expectValidStreams(&manager,
230                                { DeviceStreamType::Pme,
231                                  DeviceStreamType::NonBondedLocal,
232                                  DeviceStreamType::PmePpTransfer,
233                                  DeviceStreamType::UpdateAndConstraints });
234             expectInvalidStreams(&manager, { DeviceStreamType::NonBondedNonLocal });
235         }
236
237         {
238             SCOPED_TRACE("With DD, with PME rank, with GPU update");
239             SimulationWorkload simulationWork;
240             simulationWork.useGpuPme                      = true;
241             simulationWork.useGpuPmePpCommunication       = true;
242             simulationWork.useGpuUpdate                   = true;
243             bool                havePpDomainDecomposition = true;
244             DeviceStreamManager manager(deviceInfo, havePpDomainDecomposition, simulationWork, useTiming);
245
246             expectValidStreams(&manager,
247                                { DeviceStreamType::Pme,
248                                  DeviceStreamType::NonBondedLocal,
249                                  DeviceStreamType::NonBondedNonLocal,
250                                  DeviceStreamType::PmePpTransfer,
251                                  DeviceStreamType::UpdateAndConstraints });
252         }
253     }
254 }
255
256 } // namespace
257 } // namespace test
258 } // namespace gmx