Make use of the DeviceStreamManager
[alexxy/gromacs.git] / src / gromacs / ewald / tests / pmesplinespreadtest.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2016,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
37  * Implements PME spline computation and charge spreading tests.
38  *
39  * \author Aleksei Iupinov <a.yupinov@gmail.com>
40  * \ingroup module_ewald
41  */
42
43 #include "gmxpre.h"
44
45 #include <string>
46
47 #include <gmock/gmock.h>
48
49 #include "gromacs/mdtypes/inputrec.h"
50 #include "gromacs/utility/stringutil.h"
51
52 #include "testutils/refdata.h"
53 #include "testutils/testasserts.h"
54
55 #include "pmetestcommon.h"
56 #include "testhardwarecontexts.h"
57
58 namespace gmx
59 {
60 namespace test
61 {
62 namespace
63 {
64
65 //! PME spline and spread code path being tested
66 enum class PmeSplineAndSpreadOptions
67 {
68     SplineOnly,
69     SpreadOnly,
70     SplineAndSpreadUnified
71 };
72
73 /*! \brief Convenience typedef of input parameters - unit cell box, PME interpolation order, grid
74  * dimensions, particle coordinates, particle charges
75  * TODO: consider inclusion of local grid offsets/sizes or PME nodes counts to test the PME DD
76  */
77 typedef std::tuple<Matrix3x3, int, IVec, CoordinatesVector, ChargesVector> SplineAndSpreadInputParameters;
78
79 /*! \brief Test fixture for testing both atom spline parameter computation and charge spreading.
80  * These 2 stages of PME are tightly coupled in the code.
81  */
82 class PmeSplineAndSpreadTest : public ::testing::TestWithParam<SplineAndSpreadInputParameters>
83 {
84 public:
85     PmeSplineAndSpreadTest() = default;
86     //! The test
87     void runTest()
88     {
89         /* Getting the input */
90         Matrix3x3         box;
91         int               pmeOrder;
92         IVec              gridSize;
93         CoordinatesVector coordinates;
94         ChargesVector     charges;
95
96         std::tie(box, pmeOrder, gridSize, coordinates, charges) = GetParam();
97         const size_t atomCount                                  = coordinates.size();
98
99         /* Storing the input where it's needed */
100         t_inputrec inputRec;
101         inputRec.nkx         = gridSize[XX];
102         inputRec.nky         = gridSize[YY];
103         inputRec.nkz         = gridSize[ZZ];
104         inputRec.pme_order   = pmeOrder;
105         inputRec.coulombtype = eelPME;
106         inputRec.epsilon_r   = 1.0;
107
108         TestReferenceData refData;
109
110         const std::map<PmeSplineAndSpreadOptions, std::string> optionsToTest = {
111             { PmeSplineAndSpreadOptions::SplineAndSpreadUnified,
112               "spline computation and charge spreading (fused)" },
113             { PmeSplineAndSpreadOptions::SplineOnly, "spline computation" },
114             { PmeSplineAndSpreadOptions::SpreadOnly, "charge spreading" }
115         };
116
117         // There is a subtle problem with multiple comparisons against same reference data:
118         // The subsequent (GPU) spreading runs at one point didn't actually copy the output grid
119         // into the proper buffer, but the reference data was already marked as checked
120         // (hasBeenChecked_) by the CPU run, so nothing failed. For now we will manually track that
121         // the count of the grid entries is the same on each run. This is just a hack for a single
122         // specific output though. What would be much better TODO is to split different codepaths
123         // into separate tests, while making them use the same reference files.
124         bool   gridValuesSizeAssigned = false;
125         size_t previousGridValuesSize;
126
127         for (const auto& context : getPmeTestEnv()->getHardwareContexts())
128         {
129             CodePath   codePath = context->codePath();
130             const bool supportedInput =
131                     pmeSupportsInputForMode(*getPmeTestEnv()->hwinfo(), &inputRec, codePath);
132             if (!supportedInput)
133             {
134                 /* Testing the failure for the unsupported input */
135                 EXPECT_THROW_GMX(pmeInitWrapper(&inputRec, codePath, nullptr, nullptr, nullptr, box),
136                                  NotImplementedError);
137                 continue;
138             }
139
140             for (const auto& option : optionsToTest)
141             {
142                 /* Describing the test uniquely in case it fails */
143
144                 SCOPED_TRACE(formatString(
145                         "Testing %s with %s %sfor PME grid size %d %d %d"
146                         ", order %d, %zu atoms",
147                         option.second.c_str(), codePathToString(codePath), context->description().c_str(),
148                         gridSize[XX], gridSize[YY], gridSize[ZZ], pmeOrder, atomCount));
149
150                 /* Running the test */
151
152                 PmeSafePointer pmeSafe =
153                         pmeInitWrapper(&inputRec, codePath, context->deviceContext(),
154                                        context->deviceStream(), context->pmeGpuProgram(), box);
155                 std::unique_ptr<StatePropagatorDataGpu> stateGpu =
156                         (codePath == CodePath::GPU)
157                                 ? makeStatePropagatorDataGpu(*pmeSafe.get(), context->deviceContext(),
158                                                              context->deviceStream())
159                                 : nullptr;
160
161                 pmeInitAtoms(pmeSafe.get(), stateGpu.get(), codePath, coordinates, charges);
162
163                 const bool computeSplines =
164                         (option.first == PmeSplineAndSpreadOptions::SplineOnly)
165                         || (option.first == PmeSplineAndSpreadOptions::SplineAndSpreadUnified);
166                 const bool spreadCharges =
167                         (option.first == PmeSplineAndSpreadOptions::SpreadOnly)
168                         || (option.first == PmeSplineAndSpreadOptions::SplineAndSpreadUnified);
169
170                 if (!computeSplines)
171                 {
172                     // Here we should set up the results of the spline computation so that the spread can run.
173                     // What is lazy and works is running the separate spline so that it will set it up for us:
174                     pmePerformSplineAndSpread(pmeSafe.get(), codePath, true, false);
175                     // We know that it is tested in another iteration.
176                     // TODO: Clean alternative: read and set the reference gridline indices, spline params
177                 }
178
179                 pmePerformSplineAndSpread(pmeSafe.get(), codePath, computeSplines, spreadCharges);
180                 pmeFinalizeTest(pmeSafe.get(), codePath);
181
182                 /* Outputs correctness check */
183                 /* All tolerances were picked empirically for single precision on CPU */
184
185                 TestReferenceChecker rootChecker(refData.rootChecker());
186
187                 const auto maxGridSize = std::max(std::max(gridSize[XX], gridSize[YY]), gridSize[ZZ]);
188                 const auto ulpToleranceSplineValues = 4 * (pmeOrder - 2) * maxGridSize;
189                 /* 4 is a modest estimate for amount of operations; (pmeOrder - 2) is a number of iterations;
190                  * maxGridSize is inverse of the smallest positive fractional coordinate (which are interpolated by the splines).
191                  */
192
193                 if (computeSplines)
194                 {
195                     const char* dimString[] = { "X", "Y", "Z" };
196
197                     /* Spline values */
198                     SCOPED_TRACE(formatString("Testing spline values with tolerance of %d",
199                                               ulpToleranceSplineValues));
200                     TestReferenceChecker splineValuesChecker(
201                             rootChecker.checkCompound("Splines", "Values"));
202                     splineValuesChecker.setDefaultTolerance(
203                             relativeToleranceAsUlp(1.0, ulpToleranceSplineValues));
204                     for (int i = 0; i < DIM; i++)
205                     {
206                         auto splineValuesDim =
207                                 pmeGetSplineData(pmeSafe.get(), codePath, PmeSplineDataType::Values, i);
208                         splineValuesChecker.checkSequence(splineValuesDim.begin(),
209                                                           splineValuesDim.end(), dimString[i]);
210                     }
211
212                     /* Spline derivatives */
213                     const auto ulpToleranceSplineDerivatives = 4 * ulpToleranceSplineValues;
214                     /* 4 is just a wild guess since the derivatives are deltas of neighbor spline values which could differ greatly */
215                     SCOPED_TRACE(formatString("Testing spline derivatives with tolerance of %d",
216                                               ulpToleranceSplineDerivatives));
217                     TestReferenceChecker splineDerivativesChecker(
218                             rootChecker.checkCompound("Splines", "Derivatives"));
219                     splineDerivativesChecker.setDefaultTolerance(
220                             relativeToleranceAsUlp(1.0, ulpToleranceSplineDerivatives));
221                     for (int i = 0; i < DIM; i++)
222                     {
223                         auto splineDerivativesDim = pmeGetSplineData(
224                                 pmeSafe.get(), codePath, PmeSplineDataType::Derivatives, i);
225                         splineDerivativesChecker.checkSequence(
226                                 splineDerivativesDim.begin(), splineDerivativesDim.end(), dimString[i]);
227                     }
228
229                     /* Particle gridline indices */
230                     auto gridLineIndices = pmeGetGridlineIndices(pmeSafe.get(), codePath);
231                     rootChecker.checkSequence(gridLineIndices.begin(), gridLineIndices.end(),
232                                               "Gridline indices");
233                 }
234
235                 if (spreadCharges)
236                 {
237                     /* The wrapped grid */
238                     SparseRealGridValuesOutput nonZeroGridValues = pmeGetRealGrid(pmeSafe.get(), codePath);
239                     TestReferenceChecker gridValuesChecker(
240                             rootChecker.checkCompound("NonZeroGridValues", "RealSpaceGrid"));
241                     const auto ulpToleranceGrid =
242                             2 * ulpToleranceSplineValues
243                             * static_cast<int>(ceil(sqrt(static_cast<real>(atomCount))));
244                     /* 2 is empiric; sqrt(atomCount) assumes all the input charges may spread onto the same cell */
245                     SCOPED_TRACE(formatString("Testing grid values with tolerance of %d", ulpToleranceGrid));
246                     if (!gridValuesSizeAssigned)
247                     {
248                         previousGridValuesSize = nonZeroGridValues.size();
249                         gridValuesSizeAssigned = true;
250                     }
251                     else
252                     {
253                         EXPECT_EQ(previousGridValuesSize, nonZeroGridValues.size());
254                     }
255
256                     gridValuesChecker.setDefaultTolerance(relativeToleranceAsUlp(1.0, ulpToleranceGrid));
257                     for (const auto& point : nonZeroGridValues)
258                     {
259                         gridValuesChecker.checkReal(point.second, point.first.c_str());
260                     }
261                 }
262             }
263         }
264     }
265 };
266
267
268 /*! \brief Test for spline parameter computation and charge spreading. */
269 TEST_P(PmeSplineAndSpreadTest, ReproducesOutputs)
270 {
271     EXPECT_NO_THROW_GMX(runTest());
272 }
273
274 /* Valid input instances */
275
276 //! A couple of valid inputs for boxes.
277 std::vector<Matrix3x3> const c_sampleBoxes{
278     // normal box
279     Matrix3x3{ { 8.0F, 0.0F, 0.0F, 0.0F, 3.4F, 0.0F, 0.0F, 0.0F, 2.0F } },
280     // triclinic box
281     Matrix3x3{ { 7.0F, 0.0F, 0.0F, 0.0F, 4.1F, 0.0F, 3.5F, 2.0F, 12.2F } },
282 };
283
284 //! A couple of valid inputs for grid sizes.
285 std::vector<IVec> const c_sampleGridSizes{ IVec{ 16, 12, 14 }, IVec{ 19, 17, 11 } };
286
287 //! Random charges
288 std::vector<real> const c_sampleChargesFull{ 4.95F, 3.11F, 3.97F, 1.08F, 2.09F, 1.1F,
289                                              4.13F, 3.31F, 2.8F,  5.83F, 5.09F, 6.1F,
290                                              2.86F, 0.24F, 5.76F, 5.19F, 0.72F };
291 //! 1 charge
292 auto const c_sampleCharges1 = ChargesVector(c_sampleChargesFull).subArray(0, 1);
293 //! 2 charges
294 auto const c_sampleCharges2 = ChargesVector(c_sampleChargesFull).subArray(1, 2);
295 //! 13 charges
296 auto const c_sampleCharges13 = ChargesVector(c_sampleChargesFull).subArray(3, 13);
297
298 //! Random coordinate vectors
299 CoordinatesVector const c_sampleCoordinatesFull{ { 5.59F, 1.37F, 0.95F },
300                                                  {
301                                                          16.0F, 1.02F, 0.22F // 2 box lengths in x
302                                                  },
303                                                  { 0.034F, 1.65F, 0.22F },
304                                                  { 0.33F, 0.92F, 1.56F },
305                                                  { 1.16F, 0.75F, 0.39F },
306                                                  { 0.5F, 1.63F, 1.14F },
307                                                  {
308                                                          16.0001F, 1.52F, 1.19F // > 2 box lengths in x
309                                                  },
310                                                  {
311                                                          1.43F, 1.1F, 4.1F // > 2 box lengths in z
312                                                  },
313                                                  {
314                                                          -1.08F, 1.19F, 0.08F // negative x
315                                                  },
316                                                  { 1.6F, 0.93F, 0.53F },
317                                                  {
318                                                          1.32F, -1.48F, 0.16F // negative y
319                                                  },
320                                                  { 0.87F, 0.0F, 0.33F },
321                                                  {
322                                                          0.95F, 7.7F, -0.48F // > 2 box lengths in y, negative z
323                                                  },
324                                                  { 1.23F, 0.91F, 0.68F },
325                                                  { 0.19F, 1.45F, 0.94F },
326                                                  { 1.28F, 0.46F, 0.38F },
327                                                  { 1.21F, 0.23F, 1.0F } };
328 //! 1 coordinate vector
329 CoordinatesVector const c_sampleCoordinates1(c_sampleCoordinatesFull.begin(),
330                                              c_sampleCoordinatesFull.begin() + 1);
331 //! 2 coordinate vectors
332 CoordinatesVector const c_sampleCoordinates2(c_sampleCoordinatesFull.begin() + 1,
333                                              c_sampleCoordinatesFull.begin() + 3);
334 //! 13 coordinate vectors
335 CoordinatesVector const c_sampleCoordinates13(c_sampleCoordinatesFull.begin() + 3,
336                                               c_sampleCoordinatesFull.begin() + 16);
337
338 //! moved out from instantiantions for readability
339 auto c_inputBoxes = ::testing::ValuesIn(c_sampleBoxes);
340 //! moved out from instantiantions for readability
341 auto c_inputPmeOrders = ::testing::Range(3, 5 + 1);
342 //! moved out from instantiantions for readability
343 auto c_inputGridSizes = ::testing::ValuesIn(c_sampleGridSizes);
344
345 /*! \brief Instantiation of the test with valid input and 1 atom */
346 INSTANTIATE_TEST_CASE_P(SaneInput1,
347                         PmeSplineAndSpreadTest,
348                         ::testing::Combine(c_inputBoxes,
349                                            c_inputPmeOrders,
350                                            c_inputGridSizes,
351                                            ::testing::Values(c_sampleCoordinates1),
352                                            ::testing::Values(c_sampleCharges1)));
353
354 /*! \brief Instantiation of the test with valid input and 2 atoms */
355 INSTANTIATE_TEST_CASE_P(SaneInput2,
356                         PmeSplineAndSpreadTest,
357                         ::testing::Combine(c_inputBoxes,
358                                            c_inputPmeOrders,
359                                            c_inputGridSizes,
360                                            ::testing::Values(c_sampleCoordinates2),
361                                            ::testing::Values(c_sampleCharges2)));
362 /*! \brief Instantiation of the test with valid input and 13 atoms */
363 INSTANTIATE_TEST_CASE_P(SaneInput13,
364                         PmeSplineAndSpreadTest,
365                         ::testing::Combine(c_inputBoxes,
366                                            c_inputPmeOrders,
367                                            c_inputGridSizes,
368                                            ::testing::Values(c_sampleCoordinates13),
369                                            ::testing::Values(c_sampleCharges13)));
370 } // namespace
371 } // namespace test
372 } // namespace gmx