Introduce DeviceStreamManager
[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/utility/enumerationhelpers.h"
55
56 #include "gputest.h"
57
58 namespace gmx
59 {
60
61 namespace test
62 {
63
64 namespace
65 {
66
67 //! GPU device stream names for outputs.
68 const EnumerationArray<DeviceStreamType, std::string> c_deviceStreamNames = {
69     { "non-bonded local", "non-bonded non-local", "PME", "PME-PP transfer", "update" }
70 };
71
72 //! Test fixture
73 class DeviceStreamManagerTest : public GpuTest
74 {
75 public:
76     //! Helper function to implement readable testing
77     void expectValidStreams(DeviceStreamManager* manager, std::initializer_list<DeviceStreamType> types)
78     {
79         if (canExpectValidStreams_)
80         {
81             for (const DeviceStreamType type : types)
82             {
83                 SCOPED_TRACE("Testing " + c_deviceStreamNames[type] + " stream.");
84                 EXPECT_TRUE(manager->streamIsValid(type));
85             }
86         }
87     }
88     //! Helper function to implement readable testing
89     void expectInvalidStreams(DeviceStreamManager* manager, std::initializer_list<DeviceStreamType> types)
90     {
91         for (const DeviceStreamType type : types)
92         {
93             SCOPED_TRACE("Testing " + c_deviceStreamNames[type] + " stream.");
94             EXPECT_FALSE(manager->streamIsValid(type));
95         }
96     }
97
98     /*! \brief Non-GPU builds return nullptr instead of streams,
99      * so we have to expect that in such build configurations. */
100     const bool canExpectValidStreams_ = (GMX_GPU != GMX_GPU_NONE);
101 };
102
103 TEST_F(DeviceStreamManagerTest, CorrectStreamsAreReturnedOnNonbondedDevice)
104 {
105     // It would be nice to test that the priority is high when it can
106     // be, but that requires calling the same API calls we're testing
107     // that we've called, so it is not very useful.
108     const bool useTiming = false;
109
110     // TODO Is it enough to only test one device?
111     for (const auto* deviceInfo : getDeviceInfos())
112     {
113         EXPECT_FALSE(deviceInfo == nullptr)
114                 << "Device information should be provided for the GPU builds.";
115         // Test all the different cases successively.
116
117         {
118             SCOPED_TRACE("No DD, no PME rank, no GPU update");
119             bool                useGpuForPme              = false;
120             bool                havePpDomainDecomposition = false;
121             bool                doGpuPmePpTransfer        = false;
122             bool                useGpuForUpdate           = false;
123             DeviceStreamManager manager(*deviceInfo, useGpuForPme, havePpDomainDecomposition,
124                                         doGpuPmePpTransfer, useGpuForUpdate, useTiming);
125
126             expectValidStreams(&manager, { DeviceStreamType::NonBondedLocal });
127             expectInvalidStreams(&manager, { DeviceStreamType::NonBondedNonLocal,
128                                              DeviceStreamType::Pme, DeviceStreamType::PmePpTransfer,
129                                              DeviceStreamType::UpdateAndConstraints });
130         }
131
132         {
133             SCOPED_TRACE("With DD, no PME rank, no GPU update");
134             bool                useGpuForPme              = false;
135             bool                havePpDomainDecomposition = true;
136             bool                doGpuPmePpTransfer        = false;
137             bool                useGpuForUpdate           = false;
138             DeviceStreamManager manager(*deviceInfo, useGpuForPme, havePpDomainDecomposition,
139                                         doGpuPmePpTransfer, useGpuForUpdate, useTiming);
140
141             expectValidStreams(&manager, { DeviceStreamType::NonBondedLocal,
142                                            DeviceStreamType::NonBondedNonLocal });
143             expectInvalidStreams(&manager, { DeviceStreamType::Pme, DeviceStreamType::PmePpTransfer,
144                                              DeviceStreamType::UpdateAndConstraints });
145         }
146
147         {
148             SCOPED_TRACE("No DD, with PME rank, no GPU update");
149             bool                useGpuForPme              = true;
150             bool                havePpDomainDecomposition = false;
151             bool                doGpuPmePpTransfer        = true;
152             bool                useGpuForUpdate           = false;
153             DeviceStreamManager manager(*deviceInfo, useGpuForPme, havePpDomainDecomposition,
154                                         doGpuPmePpTransfer, useGpuForUpdate, useTiming);
155
156             expectValidStreams(&manager, { DeviceStreamType::Pme, DeviceStreamType::NonBondedLocal,
157                                            DeviceStreamType::PmePpTransfer,
158                                            DeviceStreamType::UpdateAndConstraints });
159             expectInvalidStreams(&manager, { DeviceStreamType::NonBondedNonLocal });
160         }
161
162         {
163             SCOPED_TRACE("With DD, with PME rank, no GPU update");
164             bool                useGpuForPme              = true;
165             bool                havePpDomainDecomposition = true;
166             bool                doGpuPmePpTransfer        = true;
167             bool                useGpuForUpdate           = false;
168             DeviceStreamManager manager(*deviceInfo, useGpuForPme, havePpDomainDecomposition,
169                                         doGpuPmePpTransfer, useGpuForUpdate, useTiming);
170
171             expectValidStreams(&manager, { DeviceStreamType::Pme, DeviceStreamType::NonBondedLocal,
172                                            DeviceStreamType::NonBondedNonLocal, DeviceStreamType::PmePpTransfer,
173                                            DeviceStreamType::UpdateAndConstraints });
174             expectInvalidStreams(&manager, {});
175         }
176
177         {
178             SCOPED_TRACE("No DD, no PME rank, with GPU update");
179             bool                useGpuForPme              = false;
180             bool                havePpDomainDecomposition = false;
181             bool                doGpuPmePpTransfer        = false;
182             bool                useGpuForUpdate           = true;
183             DeviceStreamManager manager(*deviceInfo, useGpuForPme, havePpDomainDecomposition,
184                                         doGpuPmePpTransfer, useGpuForUpdate, useTiming);
185
186             expectValidStreams(&manager, { DeviceStreamType::NonBondedLocal,
187                                            DeviceStreamType::UpdateAndConstraints });
188             expectInvalidStreams(&manager, { DeviceStreamType::NonBondedNonLocal,
189                                              DeviceStreamType::Pme, DeviceStreamType::PmePpTransfer });
190         }
191
192         {
193             SCOPED_TRACE("With DD, no PME rank, with GPU update");
194             bool                useGpuForPme              = false;
195             bool                havePpDomainDecomposition = true;
196             bool                doGpuPmePpTransfer        = false;
197             bool                useGpuForUpdate           = true;
198             DeviceStreamManager manager(*deviceInfo, useGpuForPme, havePpDomainDecomposition,
199                                         doGpuPmePpTransfer, useGpuForUpdate, useTiming);
200
201             expectValidStreams(&manager, { DeviceStreamType::NonBondedLocal, DeviceStreamType::NonBondedNonLocal,
202                                            DeviceStreamType::UpdateAndConstraints });
203             expectInvalidStreams(&manager, { DeviceStreamType::Pme, DeviceStreamType::PmePpTransfer });
204         }
205
206         {
207             SCOPED_TRACE("No DD, with PME rank, with GPU update");
208             bool                useGpuForPme              = true;
209             bool                havePpDomainDecomposition = false;
210             bool                doGpuPmePpTransfer        = true;
211             bool                useGpuForUpdate           = true;
212             DeviceStreamManager manager(*deviceInfo, useGpuForPme, havePpDomainDecomposition,
213                                         doGpuPmePpTransfer, useGpuForUpdate, useTiming);
214
215             expectValidStreams(&manager, { DeviceStreamType::Pme, DeviceStreamType::NonBondedLocal,
216                                            DeviceStreamType::PmePpTransfer,
217                                            DeviceStreamType::UpdateAndConstraints });
218             expectInvalidStreams(&manager, { DeviceStreamType::NonBondedNonLocal });
219         }
220
221         {
222             SCOPED_TRACE("With DD, with PME rank, with GPU update");
223             bool                useGpuForPme              = true;
224             bool                havePpDomainDecomposition = true;
225             bool                doGpuPmePpTransfer        = true;
226             bool                useGpuForUpdate           = true;
227             DeviceStreamManager manager(*deviceInfo, useGpuForPme, havePpDomainDecomposition,
228                                         doGpuPmePpTransfer, useGpuForUpdate, useTiming);
229
230             expectValidStreams(&manager, { DeviceStreamType::Pme, DeviceStreamType::NonBondedLocal,
231                                            DeviceStreamType::NonBondedNonLocal, DeviceStreamType::PmePpTransfer,
232                                            DeviceStreamType::UpdateAndConstraints });
233         }
234     }
235 }
236
237 } // namespace
238 } // namespace test
239 } // namespace gmx