2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2017,2018,2019, 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 * Implements common routines for PME tests.
39 * \author Aleksei Iupinov <a.yupinov@gmail.com>
40 * \ingroup module_ewald
44 #include "pmetestcommon.h"
50 #include "gromacs/domdec/domdec.h"
51 #include "gromacs/ewald/pme_gather.h"
52 #include "gromacs/ewald/pme_gpu_internal.h"
53 #include "gromacs/ewald/pme_grid.h"
54 #include "gromacs/ewald/pme_internal.h"
55 #include "gromacs/ewald/pme_redistribute.h"
56 #include "gromacs/ewald/pme_solve.h"
57 #include "gromacs/ewald/pme_spread.h"
58 #include "gromacs/fft/parallel_3dfft.h"
59 #include "gromacs/gpu_utils/gpu_utils.h"
60 #include "gromacs/math/invertmatrix.h"
61 #include "gromacs/mdtypes/commrec.h"
62 #include "gromacs/pbcutil/pbc.h"
63 #include "gromacs/topology/topology.h"
64 #include "gromacs/utility/exceptions.h"
65 #include "gromacs/utility/gmxassert.h"
66 #include "gromacs/utility/logger.h"
67 #include "gromacs/utility/stringutil.h"
69 #include "testutils/testasserts.h"
76 bool pmeSupportsInputForMode(const gmx_hw_info_t &hwinfo,
77 const t_inputrec *inputRec,
89 implemented = (pme_gpu_supports_build(nullptr) &&
90 pme_gpu_supports_hardware(hwinfo, nullptr) &&
91 pme_gpu_supports_input(*inputRec, mtop, nullptr));
95 GMX_THROW(InternalError("Test not implemented for this mode"));
100 uint64_t getSplineModuliDoublePrecisionUlps(int splineOrder)
102 /* Arbitrary ulp tolerance for sine/cosine implementation. It's
103 * hard to know what to pick without testing lots of
104 * implementations. */
105 const uint64_t sineUlps = 10;
106 return 4 * (splineOrder - 2) + 2 * sineUlps * splineOrder;
109 //! PME initialization
110 PmeSafePointer pmeInitWrapper(const t_inputrec *inputRec,
112 const gmx_device_info_t *gpuInfo,
113 PmeGpuProgramHandle pmeGpuProgram,
114 const Matrix3x3 &box,
115 const real ewaldCoeff_q,
116 const real ewaldCoeff_lj)
118 const MDLogger dummyLogger;
119 const auto runMode = (mode == CodePath::CPU) ? PmeRunMode::CPU : PmeRunMode::Mixed;
120 t_commrec dummyCommrec = {0};
121 NumPmeDomains numPmeDomains = { 1, 1 };
122 gmx_pme_t *pmeDataRaw = gmx_pme_init(&dummyCommrec, numPmeDomains, inputRec, false, false, true,
123 ewaldCoeff_q, ewaldCoeff_lj, 1, runMode, nullptr, gpuInfo, pmeGpuProgram, dummyLogger);
124 PmeSafePointer pme(pmeDataRaw); // taking ownership
126 // TODO get rid of this with proper matrix type
128 for (int i = 0; i < DIM; i++)
130 for (int j = 0; j < DIM; j++)
132 boxTemp[i][j] = box[i * DIM + j];
135 const char *boxError = check_box(-1, boxTemp);
136 GMX_RELEASE_ASSERT(boxError == nullptr, boxError);
141 invertBoxMatrix(boxTemp, pme->recipbox);
145 pme_gpu_set_testing(pme->gpu, true);
146 pme_gpu_update_input_box(pme->gpu, boxTemp);
150 GMX_THROW(InternalError("Test not implemented for this mode"));
156 //! Simple PME initialization based on input, no atom data
157 PmeSafePointer pmeInitEmpty(const t_inputrec *inputRec,
159 const gmx_device_info_t *gpuInfo,
160 PmeGpuProgramHandle pmeGpuProgram,
161 const Matrix3x3 &box,
166 return pmeInitWrapper(inputRec, mode, gpuInfo, pmeGpuProgram, box, ewaldCoeff_q, ewaldCoeff_lj);
167 // hiding the fact that PME actually needs to know the number of atoms in advance
170 //! Make a GPU state-propagator manager
171 std::unique_ptr<StatePropagatorDataGpu>
172 makeStatePropagatorDataGpu(const gmx_pme_t &pme)
174 // TODO: Pin the host buffer and use async memory copies
175 return std::make_unique<StatePropagatorDataGpu>(pme_gpu_get_device_stream(&pme), nullptr, nullptr,
176 pme_gpu_get_device_context(&pme),
177 GpuApiCallBehavior::Sync,
178 pme_gpu_get_padding_size(&pme));
181 //! PME initialization with atom data
182 void pmeInitAtoms(gmx_pme_t *pme,
183 StatePropagatorDataGpu *stateGpu,
185 const CoordinatesVector &coordinates,
186 const ChargesVector &charges)
188 const index atomCount = coordinates.size();
189 GMX_RELEASE_ASSERT(atomCount == charges.ssize(), "Mismatch in atom data");
190 PmeAtomComm *atc = nullptr;
195 atc = &(pme->atc[0]);
196 atc->x = coordinates;
197 atc->coefficient = charges;
198 gmx_pme_reinit_atoms(pme, atomCount, charges.data());
199 /* With decomposition there would be more boilerplate atc code here, e.g. do_redist_pos_coeffs */
203 // TODO: Avoid use of atc in the GPU code path
204 atc = &(pme->atc[0]);
205 // We need to set atc->n for passing the size in the tests
206 atc->setNumAtoms(atomCount);
207 gmx_pme_reinit_atoms(pme, atomCount, charges.data());
209 stateGpu->reinit(atomCount, atomCount);
210 stateGpu->copyCoordinatesToGpu(arrayRefFromArray(coordinates.data(), coordinates.size()), gmx::StatePropagatorDataGpu::AtomLocality::All);
211 pme_gpu_set_kernelparam_coordinates(pme->gpu, stateGpu->getCoordinates());
216 GMX_THROW(InternalError("Test not implemented for this mode"));
220 //! Getting local PME real grid pointer for test I/O
221 static real *pmeGetRealGridInternal(const gmx_pme_t *pme)
223 const size_t gridIndex = 0;
224 return pme->fftgrid[gridIndex];
227 //! Getting local PME real grid dimensions
228 static void pmeGetRealGridSizesInternal(const gmx_pme_t *pme,
230 IVec &gridSize, //NOLINT(google-runtime-references)
231 IVec &paddedGridSize) //NOLINT(google-runtime-references)
233 const size_t gridIndex = 0;
234 IVec gridOffsetUnused;
238 gmx_parallel_3dfft_real_limits(pme->pfft_setup[gridIndex], gridSize, gridOffsetUnused, paddedGridSize);
242 pme_gpu_get_real_grid_sizes(pme->gpu, &gridSize, &paddedGridSize);
246 GMX_THROW(InternalError("Test not implemented for this mode"));
250 //! Getting local PME complex grid pointer for test I/O
251 static t_complex *pmeGetComplexGridInternal(const gmx_pme_t *pme)
253 const size_t gridIndex = 0;
254 return pme->cfftgrid[gridIndex];
257 //! Getting local PME complex grid dimensions
258 static void pmeGetComplexGridSizesInternal(const gmx_pme_t *pme,
259 IVec &gridSize, //NOLINT(google-runtime-references)
260 IVec &paddedGridSize) //NOLINT(google-runtime-references)
262 const size_t gridIndex = 0;
263 IVec gridOffsetUnused, complexOrderUnused;
264 gmx_parallel_3dfft_complex_limits(pme->pfft_setup[gridIndex], complexOrderUnused, gridSize, gridOffsetUnused, paddedGridSize); //TODO: what about YZX ordering?
267 //! Getting the PME grid memory buffer and its sizes - template definition
268 template<typename ValueType> static void pmeGetGridAndSizesInternal(const gmx_pme_t * /*unused*/, CodePath /*unused*/, ValueType * & /*unused*/, IVec & /*unused*/, IVec & /*unused*/) //NOLINT(google-runtime-references)
270 GMX_THROW(InternalError("Deleted function call"));
271 // explicitly deleting general template does not compile in clang/icc, see https://llvm.org/bugs/show_bug.cgi?id=17537
274 //! Getting the PME real grid memory buffer and its sizes
275 template<> void pmeGetGridAndSizesInternal<real>(const gmx_pme_t *pme, CodePath mode, real * &grid, IVec &gridSize, IVec &paddedGridSize)
277 grid = pmeGetRealGridInternal(pme);
278 pmeGetRealGridSizesInternal(pme, mode, gridSize, paddedGridSize);
281 //! Getting the PME complex grid memory buffer and its sizes
282 template<> void pmeGetGridAndSizesInternal<t_complex>(const gmx_pme_t *pme, CodePath /*unused*/, t_complex * &grid, IVec &gridSize, IVec &paddedGridSize)
284 grid = pmeGetComplexGridInternal(pme);
285 pmeGetComplexGridSizesInternal(pme, gridSize, paddedGridSize);
288 //! PME spline calculation and charge spreading
289 void pmePerformSplineAndSpread(gmx_pme_t *pme, CodePath mode, // TODO const qualifiers elsewhere
290 bool computeSplines, bool spreadCharges)
292 GMX_RELEASE_ASSERT(pme != nullptr, "PME data is not initialized");
293 PmeAtomComm *atc = &(pme->atc[0]);
294 const size_t gridIndex = 0;
295 const bool computeSplinesForZeroCharges = true;
296 real *fftgrid = spreadCharges ? pme->fftgrid[gridIndex] : nullptr;
297 real *pmegrid = pme->pmegrid[gridIndex].grid.grid;
302 spread_on_grid(pme, atc, &pme->pmegrid[gridIndex], computeSplines, spreadCharges,
303 fftgrid, computeSplinesForZeroCharges, gridIndex);
304 if (spreadCharges && !pme->bUseThreads)
306 wrap_periodic_pmegrid(pme, pmegrid);
307 copy_pmegrid_to_fftgrid(pme, pmegrid, fftgrid, gridIndex);
312 pme_gpu_spread(pme->gpu, gridIndex, fftgrid, computeSplines, spreadCharges);
316 GMX_THROW(InternalError("Test not implemented for this mode"));
320 //! Getting the internal spline data buffer pointer
321 static real *pmeGetSplineDataInternal(const gmx_pme_t *pme, PmeSplineDataType type, int dimIndex)
323 GMX_ASSERT((0 <= dimIndex) && (dimIndex < DIM), "Invalid dimension index");
324 const PmeAtomComm *atc = &(pme->atc[0]);
325 const size_t threadIndex = 0;
326 real *splineBuffer = nullptr;
329 case PmeSplineDataType::Values:
330 splineBuffer = atc->spline[threadIndex].theta.coefficients[dimIndex];
333 case PmeSplineDataType::Derivatives:
334 splineBuffer = atc->spline[threadIndex].dtheta.coefficients[dimIndex];
338 GMX_THROW(InternalError("Unknown spline data type"));
344 void pmePerformSolve(const gmx_pme_t *pme, CodePath mode,
345 PmeSolveAlgorithm method, real cellVolume,
346 GridOrdering gridOrdering, bool computeEnergyAndVirial)
348 t_complex *h_grid = pmeGetComplexGridInternal(pme);
349 const bool useLorentzBerthelot = false;
350 const size_t threadIndex = 0;
354 if (gridOrdering != GridOrdering::YZX)
356 GMX_THROW(InternalError("Test not implemented for this mode"));
360 case PmeSolveAlgorithm::Coulomb:
361 solve_pme_yzx(pme, h_grid, cellVolume,
362 computeEnergyAndVirial, pme->nthread, threadIndex);
365 case PmeSolveAlgorithm::LennardJones:
366 solve_pme_lj_yzx(pme, &h_grid, useLorentzBerthelot,
367 cellVolume, computeEnergyAndVirial, pme->nthread, threadIndex);
371 GMX_THROW(InternalError("Test not implemented for this mode"));
378 case PmeSolveAlgorithm::Coulomb:
379 pme_gpu_solve(pme->gpu, h_grid, gridOrdering, computeEnergyAndVirial);
383 GMX_THROW(InternalError("Test not implemented for this mode"));
388 GMX_THROW(InternalError("Test not implemented for this mode"));
392 //! PME force gathering
393 void pmePerformGather(gmx_pme_t *pme, CodePath mode,
394 PmeForceOutputHandling inputTreatment, ForcesVector &forces)
396 PmeAtomComm *atc = &(pme->atc[0]);
397 const index atomCount = atc->numAtoms();
398 GMX_RELEASE_ASSERT(forces.ssize() == atomCount, "Invalid force buffer size");
399 const bool forceReductionWithInput = (inputTreatment == PmeForceOutputHandling::ReduceWithInput);
400 const real scale = 1.0;
401 const size_t threadIndex = 0;
402 const size_t gridIndex = 0;
403 real *pmegrid = pme->pmegrid[gridIndex].grid.grid;
404 real *fftgrid = pme->fftgrid[gridIndex];
410 if (atc->nthread == 1)
412 // something which is normally done in serial spline computation (make_thread_local_ind())
413 atc->spline[threadIndex].n = atomCount;
415 copy_fftgrid_to_pmegrid(pme, fftgrid, pmegrid, gridIndex, pme->nthread, threadIndex);
416 unwrap_periodic_pmegrid(pme, pmegrid);
417 gather_f_bsplines(pme, pmegrid, !forceReductionWithInput, atc, &atc->spline[threadIndex], scale);
422 // Variable initialization needs a non-switch scope
423 PmeOutput output = pme_gpu_getOutput(*pme, GMX_PME_CALC_F);
424 GMX_ASSERT(forces.size() == output.forces_.size(), "Size of force buffers did not match");
425 if (forceReductionWithInput)
427 std::copy(std::begin(forces), std::end(forces), std::begin(output.forces_));
429 pme_gpu_gather(pme->gpu, inputTreatment, reinterpret_cast<float *>(fftgrid));
430 std::copy(std::begin(output.forces_), std::end(output.forces_), std::begin(forces));
435 GMX_THROW(InternalError("Test not implemented for this mode"));
439 //! PME test finalization before fetching the outputs
440 void pmeFinalizeTest(const gmx_pme_t *pme, CodePath mode)
448 pme_gpu_synchronize(pme->gpu);
452 GMX_THROW(InternalError("Test not implemented for this mode"));
456 //! Setting atom spline values/derivatives to be used in spread/gather
457 void pmeSetSplineData(const gmx_pme_t *pme, CodePath mode,
458 const SplineParamsDimVector &splineValues, PmeSplineDataType type, int dimIndex)
460 const PmeAtomComm *atc = &(pme->atc[0]);
461 const index atomCount = atc->numAtoms();
462 const index pmeOrder = pme->pme_order;
463 const index dimSize = pmeOrder * atomCount;
464 GMX_RELEASE_ASSERT(dimSize == splineValues.ssize(), "Mismatch in spline data");
465 real *splineBuffer = pmeGetSplineDataInternal(pme, type, dimIndex);
470 std::copy(splineValues.begin(), splineValues.end(), splineBuffer);
474 std::copy(splineValues.begin(), splineValues.end(), splineBuffer);
475 pme_gpu_transform_spline_atom_data(pme->gpu, atc, type, dimIndex, PmeLayoutTransform::HostToGpu);
479 GMX_THROW(InternalError("Test not implemented for this mode"));
483 //! Setting gridline indices to be used in spread/gather
484 void pmeSetGridLineIndices(gmx_pme_t *pme, CodePath mode,
485 const GridLineIndicesVector &gridLineIndices)
487 PmeAtomComm *atc = &(pme->atc[0]);
488 const index atomCount = atc->numAtoms();
489 GMX_RELEASE_ASSERT(atomCount == gridLineIndices.ssize(), "Mismatch in gridline indices size");
491 IVec paddedGridSizeUnused, gridSize(0, 0, 0);
492 pmeGetRealGridSizesInternal(pme, mode, gridSize, paddedGridSizeUnused);
494 for (const auto &index : gridLineIndices)
496 for (int i = 0; i < DIM; i++)
498 GMX_RELEASE_ASSERT((0 <= index[i]) && (index[i] < gridSize[i]), "Invalid gridline index");
505 memcpy(pme->gpu->staging.h_gridlineIndices, gridLineIndices.data(), atomCount * sizeof(gridLineIndices[0]));
509 atc->idx.resize(gridLineIndices.size());
510 std::copy(gridLineIndices.begin(), gridLineIndices.end(), atc->idx.begin());
513 GMX_THROW(InternalError("Test not implemented for this mode"));
517 //! Getting plain index into the complex 3d grid
518 inline size_t pmeGetGridPlainIndexInternal(const IVec &index, const IVec &paddedGridSize, GridOrdering gridOrdering)
521 switch (gridOrdering)
523 case GridOrdering::YZX:
524 result = (index[YY] * paddedGridSize[ZZ] + index[ZZ]) * paddedGridSize[XX] + index[XX];
527 case GridOrdering::XYZ:
528 result = (index[XX] * paddedGridSize[YY] + index[YY]) * paddedGridSize[ZZ] + index[ZZ];
532 GMX_THROW(InternalError("Test not implemented for this mode"));
537 //! Setting real or complex grid
538 template<typename ValueType>
539 static void pmeSetGridInternal(const gmx_pme_t *pme, CodePath mode,
540 GridOrdering gridOrdering,
541 const SparseGridValuesInput<ValueType> &gridValues)
543 IVec gridSize(0, 0, 0), paddedGridSize(0, 0, 0);
545 pmeGetGridAndSizesInternal<ValueType>(pme, mode, grid, gridSize, paddedGridSize);
549 case CodePath::GPU: // intentional absence of break, the grid will be copied from the host buffer in testing mode
551 std::memset(grid, 0, paddedGridSize[XX] * paddedGridSize[YY] * paddedGridSize[ZZ] * sizeof(ValueType));
552 for (const auto &gridValue : gridValues)
554 for (int i = 0; i < DIM; i++)
556 GMX_RELEASE_ASSERT((0 <= gridValue.first[i]) && (gridValue.first[i] < gridSize[i]), "Invalid grid value index");
558 const size_t gridValueIndex = pmeGetGridPlainIndexInternal(gridValue.first, paddedGridSize, gridOrdering);
559 grid[gridValueIndex] = gridValue.second;
564 GMX_THROW(InternalError("Test not implemented for this mode"));
568 //! Setting real grid to be used in gather
569 void pmeSetRealGrid(const gmx_pme_t *pme, CodePath mode,
570 const SparseRealGridValuesInput &gridValues)
572 pmeSetGridInternal<real>(pme, mode, GridOrdering::XYZ, gridValues);
575 //! Setting complex grid to be used in solve
576 void pmeSetComplexGrid(const gmx_pme_t *pme, CodePath mode,
577 GridOrdering gridOrdering,
578 const SparseComplexGridValuesInput &gridValues)
580 pmeSetGridInternal<t_complex>(pme, mode, gridOrdering, gridValues);
583 //! Getting the single dimension's spline values or derivatives
584 SplineParamsDimVector pmeGetSplineData(const gmx_pme_t *pme, CodePath mode,
585 PmeSplineDataType type, int dimIndex)
587 GMX_RELEASE_ASSERT(pme != nullptr, "PME data is not initialized");
588 const PmeAtomComm *atc = &(pme->atc[0]);
589 const size_t atomCount = atc->numAtoms();
590 const size_t pmeOrder = pme->pme_order;
591 const size_t dimSize = pmeOrder * atomCount;
593 real *sourceBuffer = pmeGetSplineDataInternal(pme, type, dimIndex);
594 SplineParamsDimVector result;
598 pme_gpu_transform_spline_atom_data(pme->gpu, atc, type, dimIndex, PmeLayoutTransform::GpuToHost);
599 result = arrayRefFromArray(sourceBuffer, dimSize);
603 result = arrayRefFromArray(sourceBuffer, dimSize);
607 GMX_THROW(InternalError("Test not implemented for this mode"));
612 //! Getting the gridline indices
613 GridLineIndicesVector pmeGetGridlineIndices(const gmx_pme_t *pme, CodePath mode)
615 GMX_RELEASE_ASSERT(pme != nullptr, "PME data is not initialized");
616 const PmeAtomComm *atc = &(pme->atc[0]);
617 const size_t atomCount = atc->numAtoms();
619 GridLineIndicesVector gridLineIndices;
623 gridLineIndices = arrayRefFromArray(reinterpret_cast<IVec *>(pme->gpu->staging.h_gridlineIndices), atomCount);
627 gridLineIndices = atc->idx;
631 GMX_THROW(InternalError("Test not implemented for this mode"));
633 return gridLineIndices;
636 //! Getting real or complex grid - only non zero values
637 template<typename ValueType>
638 static SparseGridValuesOutput<ValueType> pmeGetGridInternal(const gmx_pme_t *pme, CodePath mode, GridOrdering gridOrdering)
640 IVec gridSize(0, 0, 0), paddedGridSize(0, 0, 0);
642 pmeGetGridAndSizesInternal<ValueType>(pme, mode, grid, gridSize, paddedGridSize);
643 SparseGridValuesOutput<ValueType> gridValues;
646 case CodePath::GPU: // intentional absence of break
649 for (int ix = 0; ix < gridSize[XX]; ix++)
651 for (int iy = 0; iy < gridSize[YY]; iy++)
653 for (int iz = 0; iz < gridSize[ZZ]; iz++)
655 IVec temp(ix, iy, iz);
656 const size_t gridValueIndex = pmeGetGridPlainIndexInternal(temp, paddedGridSize, gridOrdering);
657 const ValueType value = grid[gridValueIndex];
658 if (value != ValueType {})
660 auto key = formatString("Cell %d %d %d", ix, iy, iz);
661 gridValues[key] = value;
669 GMX_THROW(InternalError("Test not implemented for this mode"));
674 //! Getting the real grid (spreading output of pmePerformSplineAndSpread())
675 SparseRealGridValuesOutput pmeGetRealGrid(const gmx_pme_t *pme, CodePath mode)
677 return pmeGetGridInternal<real>(pme, mode, GridOrdering::XYZ);
680 //! Getting the complex grid output of pmePerformSolve()
681 SparseComplexGridValuesOutput pmeGetComplexGrid(const gmx_pme_t *pme, CodePath mode,
682 GridOrdering gridOrdering)
684 return pmeGetGridInternal<t_complex>(pme, mode, gridOrdering);
687 //! Getting the reciprocal energy and virial
688 PmeOutput pmeGetReciprocalEnergyAndVirial(const gmx_pme_t *pme, CodePath mode,
689 PmeSolveAlgorithm method)
697 case PmeSolveAlgorithm::Coulomb:
698 get_pme_ener_vir_q(pme->solve_work, pme->nthread, &output);
701 case PmeSolveAlgorithm::LennardJones:
702 get_pme_ener_vir_lj(pme->solve_work, pme->nthread, &output);
706 GMX_THROW(InternalError("Test not implemented for this mode"));
712 case PmeSolveAlgorithm::Coulomb:
713 pme_gpu_getEnergyAndVirial(*pme, &output);
717 GMX_THROW(InternalError("Test not implemented for this mode"));
722 GMX_THROW(InternalError("Test not implemented for this mode"));