Fix some OpenCL issues
[alexxy/gromacs.git] / src / programs / mdrun / tests / moduletest.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2013,2014,2015, 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
37  * Implements classes in moduletest.h.
38  *
39  * \author Mark Abraham <mark.j.abraham@gmail.com>
40  * \ingroup module_mdrun_integration_tests
41  */
42 #include "gmxpre.h"
43
44 #include "moduletest.h"
45
46 #include "config.h"
47
48 #include <cstdio>
49
50 #include "gromacs/gmxpreprocess/grompp.h"
51 #include "gromacs/legacyheaders/gmx_detect_hardware.h"
52 #include "gromacs/options/basicoptions.h"
53 #include "gromacs/options/options.h"
54 #include "gromacs/utility/basedefinitions.h"
55 #include "gromacs/utility/basenetwork.h"
56 #include "gromacs/utility/file.h"
57 #include "gromacs/utility/gmxmpi.h"
58 #include "programs/mdrun/mdrun_main.h"
59
60 #include "testutils/cmdlinetest.h"
61 #include "testutils/integrationtests.h"
62 #include "testutils/testoptions.h"
63
64 namespace gmx
65 {
66 namespace test
67 {
68
69 /********************************************************************
70  * MdrunTestFixture
71  */
72
73 namespace
74 {
75
76 #if defined(GMX_THREAD_MPI) || defined(DOXYGEN)
77 //! Number of tMPI threads for child mdrun call.
78 int g_numThreads = 1;
79 #endif
80 #if defined(GMX_OPENMP) || defined(DOXYGEN)
81 //! Number of OpenMP threads for child mdrun call.
82 int g_numOpenMPThreads = 1;
83 #endif
84 //! \cond
85 GMX_TEST_OPTIONS(MdrunTestOptions, options)
86 {
87     GMX_UNUSED_VALUE(options);
88 #ifdef GMX_THREAD_MPI
89     options->addOption(IntegerOption("nt").store(&g_numThreads)
90                            .description("Number of thread-MPI threads/ranks for child mdrun calls"));
91 #endif
92 #ifdef GMX_OPENMP
93     options->addOption(IntegerOption("nt_omp").store(&g_numOpenMPThreads)
94                            .description("Number of OpenMP threads for child mdrun calls"));
95 #endif
96 }
97 //! \endcond
98
99 }
100
101 SimulationRunner::SimulationRunner(IntegrationTestFixture *fixture) :
102     fixture_(fixture),
103     topFileName_(),
104     groFileName_(),
105     fullPrecisionTrajectoryFileName_(),
106     ndxFileName_(),
107     mdpInputFileName_(fixture_->fileManager_.getTemporaryFilePath("input.mdp")),
108     mdpOutputFileName_(fixture_->fileManager_.getTemporaryFilePath("output.mdp")),
109     tprFileName_(fixture_->fileManager_.getTemporaryFilePath(".tpr")),
110     logFileName_(fixture_->fileManager_.getTemporaryFilePath(".log")),
111     edrFileName_(fixture_->fileManager_.getTemporaryFilePath(".edr")),
112     nsteps_(-2)
113 {
114 #ifdef GMX_LIB_MPI
115     GMX_RELEASE_ASSERT(gmx_mpi_initialized(), "MPI system not initialized for mdrun tests");
116 #endif
117 }
118
119 // TODO The combination of defaulting to Verlet cut-off scheme, NVE,
120 // and verlet-buffer-tolerance = -1 gives a grompp error. If we keep
121 // things that way, this function should be renamed. For now,
122 // force the use of the group scheme.
123 void
124 SimulationRunner::useEmptyMdpFile()
125 {
126     // TODO When removing the group scheme, update actual and potential users of useEmptyMdpFile
127     useStringAsMdpFile("cutoff-scheme = Group\n");
128 }
129
130 void
131 SimulationRunner::useStringAsMdpFile(const char *mdpString)
132 {
133     useStringAsMdpFile(std::string(mdpString));
134 }
135
136 void
137 SimulationRunner::useStringAsMdpFile(const std::string &mdpString)
138 {
139     gmx::File::writeFileFromString(mdpInputFileName_, mdpString);
140 }
141
142 void
143 SimulationRunner::useStringAsNdxFile(const char *ndxString)
144 {
145     gmx::File::writeFileFromString(ndxFileName_, ndxString);
146 }
147
148 void
149 SimulationRunner::useTopGroAndNdxFromDatabase(const char *name)
150 {
151     topFileName_ = fixture_->fileManager_.getInputFilePath((std::string(name) + ".top").c_str());
152     groFileName_ = fixture_->fileManager_.getInputFilePath((std::string(name) + ".gro").c_str());
153     ndxFileName_ = fixture_->fileManager_.getInputFilePath((std::string(name) + ".ndx").c_str());
154 }
155
156 void
157 SimulationRunner::useGroFromDatabase(const char *name)
158 {
159     groFileName_ = fixture_->fileManager_.getInputFilePath((std::string(name) + ".gro").c_str());
160 }
161
162 int
163 SimulationRunner::callGromppOnThisRank()
164 {
165     CommandLine caller;
166     caller.append("grompp");
167     caller.addOption("-f", mdpInputFileName_);
168     caller.addOption("-n", ndxFileName_);
169     caller.addOption("-p", topFileName_);
170     caller.addOption("-c", groFileName_);
171
172     caller.addOption("-po", mdpOutputFileName_);
173     caller.addOption("-o", tprFileName_);
174
175     return gmx_grompp(caller.argc(), caller.argv());
176 }
177
178 int
179 SimulationRunner::callGrompp()
180 {
181     int returnValue = 0;
182 #ifdef GMX_LIB_MPI
183     // When compiled with external MPI, we're trying to run mdrun with
184     // MPI, but we need to make sure that we only do grompp on one
185     // rank
186     if (0 == gmx_node_rank())
187 #endif
188     {
189         returnValue = callGromppOnThisRank();
190     }
191 #ifdef GMX_LIB_MPI
192     // Make sure rank zero has written the .tpr file before other
193     // ranks try to read it. Thread-MPI and serial do this just fine
194     // on their own.
195     MPI_Barrier(MPI_COMM_WORLD);
196 #endif
197     return returnValue;
198 }
199
200 int
201 SimulationRunner::callMdrun(const CommandLine &callerRef)
202 {
203     /* Conforming to style guide by not passing a non-const reference
204        to this function. Passing a non-const reference might make it
205        easier to write code that incorrectly re-uses callerRef after
206        the call to this function. */
207
208     CommandLine caller(callerRef);
209     caller.addOption("-s", tprFileName_);
210
211     caller.addOption("-g", logFileName_);
212     caller.addOption("-e", edrFileName_);
213     caller.addOption("-o", fullPrecisionTrajectoryFileName_);
214     caller.addOption("-x", reducedPrecisionTrajectoryFileName_);
215
216     caller.addOption("-deffnm", fixture_->fileManager_.getTemporaryFilePath("state"));
217
218     if (nsteps_ > -2)
219     {
220         caller.addOption("-nsteps", nsteps_);
221     }
222
223 #ifdef GMX_THREAD_MPI
224     caller.addOption("-ntmpi", g_numThreads);
225 #endif
226
227 #ifdef GMX_OPENMP
228     caller.addOption("-ntomp", g_numOpenMPThreads);
229 #endif
230
231 #if defined GMX_GPU
232     /* TODO Ideally, with real MPI, we could call
233      * gmx_collect_hardware_mpi() here and find out how many nodes
234      * mdrun will run on. For now, we assume that we're running on one
235      * node regardless of the number of ranks, because that's true in
236      * Jenkins and for most developers running the tests. */
237     int numberOfNodes = 1;
238 #if defined GMX_THREAD_MPI
239     /* Can't use gmx_node_num() because it is only valid after spawn of thread-MPI threads */
240     int numberOfRanks = g_numThreads;
241 #elif defined GMX_LIB_MPI
242     int numberOfRanks = gmx_node_num();
243 #else
244     int numberOfRanks = 1;
245 #endif
246     if (numberOfRanks > numberOfNodes && !gmx_multiple_gpu_per_node_supported())
247     {
248         if (gmx_node_rank() == 0)
249         {
250             fprintf(stderr, "GROMACS in this build configuration cannot run on more than one GPU per node,\n so with %d ranks and %d nodes, this test will disable GPU support", numberOfRanks, numberOfNodes);
251         }
252         caller.addOption("-nb", "cpu");
253     }
254 #endif
255     return gmx_mdrun(caller.argc(), caller.argv());
256 }
257
258 int
259 SimulationRunner::callMdrun()
260 {
261     CommandLine caller;
262     caller.append("mdrun");
263     return callMdrun(caller);
264 }
265
266 // ====
267
268 MdrunTestFixtureBase::MdrunTestFixtureBase()
269 {
270 #ifdef GMX_LIB_MPI
271     GMX_RELEASE_ASSERT(gmx_mpi_initialized(), "MPI system not initialized for mdrun tests");
272 #endif
273 }
274
275 MdrunTestFixtureBase::~MdrunTestFixtureBase()
276 {
277 }
278
279 // ====
280
281 MdrunTestFixture::MdrunTestFixture() : runner_(this)
282 {
283 }
284
285 MdrunTestFixture::~MdrunTestFixture()
286 {
287 }
288
289 } // namespace test
290 } // namespace gmx