2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2017,2018, 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.
37 * This implements basic PME sanity tests.
38 * It runs the input system with PME for several steps (on CPU and GPU, if available),
39 * and checks the reciprocal and conserved energies.
40 * As part of mdrun-test, this will always run single rank PME simulation.
41 * As part of mdrun-mpi-test, this will run same as above when a single rank is requested,
42 * or a simulation with a single separate PME rank ("-npme 1") when multiple ranks are requested.
43 * \todo Extend and generalize this for more multi-rank tests (-npme 0, -npme 2, etc).
44 * \todo Implement death tests (e.g. for PME GPU decomposition).
46 * \author Aleksei Iupinov <a.yupinov@gmail.com>
47 * \ingroup module_mdrun_integration_tests
57 #include <gtest/gtest-spi.h>
59 #include "gromacs/gpu_utils/gpu_utils.h"
60 #include "gromacs/hardware/gpu_hw_info.h"
61 #include "gromacs/utility/cstringutil.h"
62 #include "gromacs/utility/gmxmpi.h"
63 #include "gromacs/utility/stringutil.h"
65 #include "testutils/mpitest.h"
66 #include "testutils/refdata.h"
68 #include "energyreader.h"
69 #include "moduletest.h"
78 /*! \brief A basic PME runner
80 * \todo Consider also using GpuTest class. */
81 class PmeTest : public MdrunTestFixture
84 //! Before any test is run, work out whether any compatible GPUs exist.
85 static void SetUpTestCase();
86 //! Store whether any compatible GPUs exist.
87 static bool s_hasCompatibleCudaGpus;
90 bool PmeTest::s_hasCompatibleCudaGpus = false;
92 void PmeTest::SetUpTestCase()
94 gmx_gpu_info_t gpuInfo {};
95 // It would be nicer to do this detection once and have mdrun
96 // re-use it, but this is OK. Note that this also caters for when
97 // there is no GPU support in the build.
99 // TODO report any error messages gracefully.
100 if (GMX_GPU == GMX_GPU_CUDA &&
101 canDetectGpus(nullptr))
104 s_hasCompatibleCudaGpus = (gpuInfo.n_dev_compatible > 0);
106 free_gpu_info(&gpuInfo);
109 TEST_F(PmeTest, ReproducesEnergies)
111 const int nsteps = 20;
112 std::string theMdpFile = formatString("coulombtype = PME\n"
113 "nstcalcenergy = 1\n"
119 runner_.useStringAsMdpFile(theMdpFile);
121 const std::string inputFile = "spc-and-methanol";
122 runner_.useTopGroAndNdxFromDatabase(inputFile.c_str());
124 // With single rank we can and will always test PP+PME as part of mdrun-test.
125 // With multiple ranks we can additionally test a single PME-only rank within mdrun-mpi-test.
126 const bool parallelRun = (getNumberOfTestMpiRanks() > 1);
127 const bool useSeparatePme = parallelRun;
129 EXPECT_EQ(0, runner_.callGrompp());
131 //TODO test all proper/improper combinations in more thorough way?
132 std::map < std::string, std::vector < const char *>> runModes;
133 runModes["PmeOnCpu"] = {"-pme", "cpu"};
134 runModes["PmeAuto"] = {"-pme", "auto"};
135 runModes["PmeOnGpuFftOnCpu"] = {"-pme", "gpu", "-pmefft", "cpu"};
136 runModes["PmeOnGpuFftOnGpu"] = {"-pme", "gpu", "-pmefft", "gpu"};
137 runModes["PmeOnGpuFftAuto"] = {"-pme", "gpu", "-pmefft", "auto"};
138 // same manual modes but marked for PME tuning
139 runModes["PmeOnCpuTune"] = {"-pme", "cpu"};
140 runModes["PmeOnGpuFftOnCpuTune"] = {"-pme", "gpu", "-pmefft", "cpu"};
141 runModes["PmeOnGpuFftOnGpuTune"] = {"-pme", "gpu", "-pmefft", "gpu"};
142 TestReferenceData refData;
143 TestReferenceChecker rootChecker(refData.rootChecker());
144 const bool thisRankChecks = (gmx_node_rank() == 0);
147 EXPECT_NONFATAL_FAILURE(rootChecker.checkUnusedEntries(), ""); // skip checks on other ranks
149 for (const auto &mode : runModes)
151 auto modeTargetsGpus = (mode.first.find("Gpu") != std::string::npos);
152 if (modeTargetsGpus && !s_hasCompatibleCudaGpus)
154 // This run mode will cause a fatal error from mdrun when
155 // it can't find GPUs, which is not something we're trying
160 runner_.edrFileName_ = fileManager_.getTemporaryFilePath(inputFile + "_" + mode.first + ".edr");
162 CommandLine commandLine(mode.second);
164 const bool usePmeTuning = (mode.first.find("Tune") != std::string::npos);
167 commandLine.append("-tunepme");
168 commandLine.addOption("-nstlist", 1); // a new grid every step
172 commandLine.append("-notunepme"); // for reciprocal energy reproducibility
176 commandLine.addOption("-npme", 1);
178 ASSERT_EQ(0, runner_.callMdrun(commandLine));
182 auto energyReader = openEnergyFileToReadFields(runner_.edrFileName_, {"Coul. recip.", "Total Energy", "Kinetic En."});
183 auto conservedChecker = rootChecker.checkCompound("Energy", "Conserved");
184 auto reciprocalChecker = rootChecker.checkCompound("Energy", "Reciprocal");
185 for (int i = 0; i <= nsteps; i++)
187 EnergyFrame frame = energyReader->frame();
188 std::string stepNum = gmx::formatString("%d", i);
189 const real conservedEnergy = frame.at("Total Energy");
190 const real reciprocalEnergy = frame.at("Coul. recip.");
193 // use first step values as references for tolerance
194 const real startingKineticEnergy = frame.at("Kinetic En.");
195 const auto conservedTolerance = relativeToleranceAsFloatingPoint(startingKineticEnergy, 2e-5);
196 const auto reciprocalTolerance = relativeToleranceAsFloatingPoint(reciprocalEnergy, 3e-5);
197 reciprocalChecker.setDefaultTolerance(reciprocalTolerance);
198 conservedChecker.setDefaultTolerance(conservedTolerance);
200 conservedChecker.checkReal(conservedEnergy, stepNum.c_str());
201 if (!usePmeTuning) // with PME tuning come differing grids and differing reciprocal energy
203 reciprocalChecker.checkReal(reciprocalEnergy, stepNum.c_str());