Cleaned up mdrunner_start_threads
[gromacs/AngularHB.git] / src / programs / mdrun / tests / moduletest.cpp
bloba1be7b677521dac08f5ff3b6c062033fe373f10a
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2013,2014,2015,2016,2017, 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.
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.
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.
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.
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.
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.
35 /*! \internal \file
36 * \brief
37 * Implements classes in moduletest.h.
39 * \author Mark Abraham <mark.j.abraham@gmail.com>
40 * \ingroup module_mdrun_integration_tests
42 #include "gmxpre.h"
44 #include "moduletest.h"
46 #include "config.h"
48 #include <cstdio>
50 #include "gromacs/gmxpreprocess/grompp.h"
51 #include "gromacs/hardware/detecthardware.h"
52 #include "gromacs/options/basicoptions.h"
53 #include "gromacs/options/ioptionscontainer.h"
54 #include "gromacs/utility/basedefinitions.h"
55 #include "gromacs/utility/basenetwork.h"
56 #include "gromacs/utility/gmxmpi.h"
57 #include "gromacs/utility/textwriter.h"
58 #include "programs/mdrun/mdrun_main.h"
60 #include "testutils/cmdlinetest.h"
61 #include "testutils/mpitest.h"
62 #include "testutils/testfilemanager.h"
63 #include "testutils/testoptions.h"
65 namespace gmx
67 namespace test
70 /********************************************************************
71 * MdrunTestFixture
74 namespace
77 #if GMX_OPENMP || defined(DOXYGEN)
78 //! Number of OpenMP threads for child mdrun call.
79 int g_numOpenMPThreads = 1;
80 #endif
81 //! \cond
82 GMX_TEST_OPTIONS(MdrunTestOptions, options)
84 GMX_UNUSED_VALUE(options);
85 #if GMX_OPENMP
86 options->addOption(IntegerOption("nt_omp").store(&g_numOpenMPThreads)
87 .description("Number of OpenMP threads for child mdrun calls"));
88 #endif
90 //! \endcond
94 SimulationRunner::SimulationRunner(TestFileManager *fileManager) :
95 topFileName_(),
96 groFileName_(),
97 fullPrecisionTrajectoryFileName_(),
98 ndxFileName_(),
99 mdpInputFileName_(fileManager->getTemporaryFilePath("input.mdp")),
100 mdpOutputFileName_(fileManager->getTemporaryFilePath("output.mdp")),
101 tprFileName_(fileManager->getTemporaryFilePath(".tpr")),
102 logFileName_(fileManager->getTemporaryFilePath(".log")),
103 edrFileName_(fileManager->getTemporaryFilePath(".edr")),
104 nsteps_(-2),
105 fileManager_(*fileManager)
107 #if GMX_LIB_MPI
108 GMX_RELEASE_ASSERT(gmx_mpi_initialized(), "MPI system not initialized for mdrun tests");
109 #endif
112 // TODO The combination of defaulting to Verlet cut-off scheme, NVE,
113 // and verlet-buffer-tolerance = -1 gives a grompp error. If we keep
114 // things that way, this function should be renamed. For now,
115 // force the use of the group scheme.
116 // TODO There is possible outstanding unexplained behaviour of mdp
117 // input parsing e.g. Redmine 2074, so this particular set of mdp
118 // contents is also tested with GetIrTest in gmxpreprocess-test.
119 void
120 SimulationRunner::useEmptyMdpFile()
122 // TODO When removing the group scheme, update actual and potential users of useEmptyMdpFile
123 useStringAsMdpFile("cutoff-scheme = Group\n");
126 void
127 SimulationRunner::useStringAsMdpFile(const char *mdpString)
129 useStringAsMdpFile(std::string(mdpString));
132 void
133 SimulationRunner::useStringAsMdpFile(const std::string &mdpString)
135 gmx::TextWriter::writeFileFromString(mdpInputFileName_, mdpString);
138 void
139 SimulationRunner::useStringAsNdxFile(const char *ndxString)
141 gmx::TextWriter::writeFileFromString(ndxFileName_, ndxString);
144 void
145 SimulationRunner::useTopGroAndNdxFromDatabase(const char *name)
147 topFileName_ = fileManager_.getInputFilePath((std::string(name) + ".top").c_str());
148 groFileName_ = fileManager_.getInputFilePath((std::string(name) + ".gro").c_str());
149 ndxFileName_ = fileManager_.getInputFilePath((std::string(name) + ".ndx").c_str());
152 void
153 SimulationRunner::useGroFromDatabase(const char *name)
155 groFileName_ = fileManager_.getInputFilePath((std::string(name) + ".gro").c_str());
159 SimulationRunner::callGromppOnThisRank(const CommandLine &callerRef)
161 CommandLine caller;
162 caller.append("grompp");
163 caller.merge(callerRef);
164 caller.addOption("-f", mdpInputFileName_);
165 caller.addOption("-n", ndxFileName_);
166 caller.addOption("-p", topFileName_);
167 caller.addOption("-c", groFileName_);
168 caller.addOption("-r", groFileName_);
170 caller.addOption("-po", mdpOutputFileName_);
171 caller.addOption("-o", tprFileName_);
173 return gmx_grompp(caller.argc(), caller.argv());
177 SimulationRunner::callGromppOnThisRank()
179 return callGromppOnThisRank(CommandLine());
183 SimulationRunner::callGrompp(const CommandLine &callerRef)
185 int returnValue = 0;
186 #if GMX_LIB_MPI
187 // When compiled with external MPI, we're trying to run mdrun with
188 // MPI, but we need to make sure that we only do grompp on one
189 // rank
190 if (0 == gmx_node_rank())
191 #endif
193 returnValue = callGromppOnThisRank(callerRef);
195 #if GMX_LIB_MPI
196 // Make sure rank zero has written the .tpr file before other
197 // ranks try to read it. Thread-MPI and serial do this just fine
198 // on their own.
199 MPI_Barrier(MPI_COMM_WORLD);
200 #endif
201 return returnValue;
205 SimulationRunner::callGrompp()
207 return callGrompp(CommandLine());
211 SimulationRunner::callMdrun(const CommandLine &callerRef)
213 /* Conforming to style guide by not passing a non-const reference
214 to this function. Passing a non-const reference might make it
215 easier to write code that incorrectly re-uses callerRef after
216 the call to this function. */
218 CommandLine caller;
219 caller.append("mdrun");
220 caller.merge(callerRef);
221 caller.addOption("-s", tprFileName_);
223 caller.addOption("-g", logFileName_);
224 caller.addOption("-e", edrFileName_);
225 caller.addOption("-o", fullPrecisionTrajectoryFileName_);
226 caller.addOption("-x", reducedPrecisionTrajectoryFileName_);
228 caller.addOption("-deffnm", fileManager_.getTemporaryFilePath("state"));
230 if (nsteps_ > -2)
232 caller.addOption("-nsteps", nsteps_);
235 #if GMX_THREAD_MPI
236 caller.addOption("-ntmpi", getNumberOfTestMpiRanks());
237 #endif
239 #if GMX_OPENMP
240 caller.addOption("-ntomp", g_numOpenMPThreads);
241 #endif
243 #if GMX_GPU != GMX_GPU_NONE
244 /* TODO Ideally, with real MPI, we could call
245 * gmx_collect_hardware_mpi() here and find out how many nodes
246 * mdrun will run on. For now, we assume that we're running on one
247 * node regardless of the number of ranks, because that's true in
248 * Jenkins and for most developers running the tests. */
249 int numberOfNodes = 1;
250 int numberOfRanks = getNumberOfTestMpiRanks();
251 if (numberOfRanks > numberOfNodes && !gmx_multiple_gpu_per_node_supported())
253 if (gmx_node_rank() == 0)
255 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);
257 caller.addOption("-nb", "cpu");
259 #endif
260 return gmx_mdrun(caller.argc(), caller.argv());
264 SimulationRunner::callMdrun()
266 return callMdrun(CommandLine());
269 // ====
271 MdrunTestFixtureBase::MdrunTestFixtureBase()
273 #if GMX_LIB_MPI
274 GMX_RELEASE_ASSERT(gmx_mpi_initialized(), "MPI system not initialized for mdrun tests");
275 #endif
278 MdrunTestFixtureBase::~MdrunTestFixtureBase()
282 // ====
284 MdrunTestFixture::MdrunTestFixture() : runner_(&fileManager_)
288 MdrunTestFixture::~MdrunTestFixture()
292 } // namespace test
293 } // namespace gmx