2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 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.
38 * \brief This file contains internal function definitions for performing the PME calculations on GPU.
39 * These are not meant to be exposed outside of the PME GPU code.
40 * As of now, their bodies are still in the common pme-gpu.cpp files.
42 * \author Aleksei Iupinov <a.yupinov@gmail.com>
43 * \ingroup module_ewald
46 #ifndef GMX_EWALD_PME_GPU_INTERNAL_H
47 #define GMX_EWALD_PME_GPU_INTERNAL_H
49 #include "gromacs/fft/fft.h" // for the gmx_fft_direction enum
50 #include "gromacs/gpu_utils/gpu_macros.h" // for the CUDA_FUNC_ macros
52 #include "pme-gpu-types.h" // for the inline functions accessing PmeGpu members
56 struct gmx_pme_t; // only used in pme_gpu_reinit
58 struct gmx_wallclock_gpu_pme_t;
59 struct pme_atomcomm_t;
67 //! Type of spline data
68 enum class PmeSplineDataType
71 Derivatives, // dtheta
72 }; //TODO move this into new and shiny pme.h (pme-types.h?)
74 //! PME grid dimension ordering (from major to minor)
75 enum class GridOrdering
81 /* Some general constants for PME GPU behaviour follow. */
83 /*! \brief \libinternal
84 * false: The atom data GPU buffers are sized precisely according to the number of atoms.
85 * (Except GPU spline data layout which is regardless intertwined for 2 atoms per warp).
86 * The atom index checks in the spread/gather code potentially hinder the performance.
87 * true: The atom data GPU buffers are padded with zeroes so that the possible number of atoms
88 * fitting in is divisible by PME_ATOM_DATA_ALIGNMENT.
89 * The atom index checks are not performed. There should be a performance win, but how big is it, remains to be seen.
90 * Additional cudaMemsetAsync calls are done occasionally (only charges/coordinates; spline data is always recalculated now).
91 * \todo Estimate performance differences
93 const bool c_usePadding = true;
95 /*! \brief \libinternal
96 * false: Atoms with zero charges are processed by PME. Could introduce some overhead.
97 * true: Atoms with zero charges are not processed by PME. Adds branching to the spread/gather.
98 * Could be good for performance in specific systems with lots of neutral atoms.
99 * \todo Estimate performance differences.
101 const bool c_skipNeutralAtoms = false;
103 /*! \brief \libinternal
104 * Number of PME solve output floating point numbers.
105 * 6 for symmetric virial matrix + 1 for reciprocal energy.
107 const int c_virialAndEnergyCount = 7;
109 /* A block of CUDA-only functions that live in pme.cu */
111 /*! \libinternal \brief
112 * Returns the number of atoms per chunk in the atom charges/coordinates data layout.
113 * Depends on CUDA-specific block sizes, needed for the atom data padding.
115 * \param[in] pmeGPU The PME GPU structure.
116 * \returns Number of atoms in a single GPU atom data chunk.
118 CUDA_FUNC_QUALIFIER int pme_gpu_get_atom_data_alignment(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM_WITH_RETURN(1)
120 /*! \libinternal \brief
121 * Returns the number of atoms per chunk in the atom spline theta/dtheta data layout.
123 * \param[in] pmeGPU The PME GPU structure.
124 * \returns Number of atoms in a single GPU atom spline data chunk.
126 CUDA_FUNC_QUALIFIER int pme_gpu_get_atoms_per_warp(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM_WITH_RETURN(1)
128 /*! \libinternal \brief
129 * Synchronizes the current computation, waiting for the GPU kernels/transfers to finish.
131 * \param[in] pmeGPU The PME GPU structure.
133 CUDA_FUNC_QUALIFIER void pme_gpu_synchronize(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
135 /*! \libinternal \brief
136 * Allocates the fixed size energy and virial buffer both on GPU and CPU.
138 * \param[in] pmeGPU The PME GPU structure.
140 CUDA_FUNC_QUALIFIER void pme_gpu_alloc_energy_virial(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
142 /*! \libinternal \brief
143 * Frees the energy and virial memory both on GPU and CPU.
145 * \param[in] pmeGPU The PME GPU structure.
147 CUDA_FUNC_QUALIFIER void pme_gpu_free_energy_virial(PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
149 /*! \libinternal \brief
150 * Clears the energy and virial memory on GPU with 0.
151 * Should be called at the end of PME computation which returned energy/virial.
153 * \param[in] pmeGPU The PME GPU structure.
155 CUDA_FUNC_QUALIFIER void pme_gpu_clear_energy_virial(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
157 /*! \libinternal \brief
158 * Reallocates and copies the pre-computed B-spline values to the GPU.
160 * \param[in] pmeGPU The PME GPU structure.
162 CUDA_FUNC_QUALIFIER void pme_gpu_realloc_and_copy_bspline_values(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
164 /*! \libinternal \brief
165 * Frees the pre-computed B-spline values on the GPU (and the transfer CPU buffers).
167 * \param[in] pmeGPU The PME GPU structure.
169 CUDA_FUNC_QUALIFIER void pme_gpu_free_bspline_values(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
171 /*! \libinternal \brief
172 * Reallocates the GPU buffer for the PME forces.
174 * \param[in] pmeGPU The PME GPU structure.
176 CUDA_FUNC_QUALIFIER void pme_gpu_realloc_forces(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
178 /*! \libinternal \brief
179 * Frees the GPU buffer for the PME forces.
181 * \param[in] pmeGPU The PME GPU structure.
183 CUDA_FUNC_QUALIFIER void pme_gpu_free_forces(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
185 /*! \libinternal \brief
186 * Copies the forces from the CPU buffer to the GPU (to reduce them with the PME GPU gathered forces).
187 * To be called e.g. after the bonded calculations.
189 * \param[in] pmeGPU The PME GPU structure.
190 * \param[in] h_forces The input forces rvec buffer.
192 CUDA_FUNC_QUALIFIER void pme_gpu_copy_input_forces(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU),
193 const float *CUDA_FUNC_ARGUMENT(h_forces)) CUDA_FUNC_TERM
195 /*! \libinternal \brief
196 * Copies the forces from the GPU to the CPU buffer. To be called after the gathering stage.
198 * \param[in] pmeGPU The PME GPU structure.
199 * \param[out] h_forces The output forces rvec buffer.
201 CUDA_FUNC_QUALIFIER void pme_gpu_copy_output_forces(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU),
202 float *CUDA_FUNC_ARGUMENT(h_forces)) CUDA_FUNC_TERM
204 /*! \libinternal \brief
205 * Reallocates the input coordinates buffer on the GPU (and clears the padded part if needed).
207 * \param[in] pmeGPU The PME GPU structure.
209 * Needs to be called on every DD step/in the beginning.
211 CUDA_FUNC_QUALIFIER void pme_gpu_realloc_coordinates(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
213 /*! \libinternal \brief
214 * Copies the input coordinates from the CPU buffer onto the GPU.
216 * \param[in] pmeGPU The PME GPU structure.
217 * \param[in] h_coordinates Input coordinates (XYZ rvec array).
219 * Needs to be called for every PME computation. The coordinates are then used in the spline calculation.
221 CUDA_FUNC_QUALIFIER void pme_gpu_copy_input_coordinates(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU),
222 const rvec *CUDA_FUNC_ARGUMENT(h_coordinates)) CUDA_FUNC_TERM
224 /*! \libinternal \brief
225 * Frees the coordinates on the GPU.
227 * \param[in] pmeGPU The PME GPU structure.
229 CUDA_FUNC_QUALIFIER void pme_gpu_free_coordinates(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
231 /*! \libinternal \brief
232 * Reallocates the buffer on the GPU and copies the charges/coefficients from the CPU buffer.
233 * Clears the padded part if needed.
235 * \param[in] pmeGPU The PME GPU structure.
236 * \param[in] h_coefficients The input atom charges/coefficients.
238 * Does not need to be done for every PME computation, only whenever the local charges change.
239 * (So, in the beginning of the run, or on DD step).
241 CUDA_FUNC_QUALIFIER void pme_gpu_realloc_and_copy_input_coefficients(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU),
242 const float *CUDA_FUNC_ARGUMENT(h_coefficients)) CUDA_FUNC_TERM
244 /*! \libinternal \brief
245 * Frees the charges/coefficients on the GPU.
247 * \param[in] pmeGPU The PME GPU structure.
249 CUDA_FUNC_QUALIFIER void pme_gpu_free_coefficients(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
251 /*! \libinternal \brief
252 * Reallocates the buffers on the GPU and the host for the atoms spline data.
254 * \param[in] pmeGPU The PME GPU structure.
256 CUDA_FUNC_QUALIFIER void pme_gpu_realloc_spline_data(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
258 /*! \libinternal \brief
259 * Frees the buffers on the GPU for the atoms spline data.
261 * \param[in] pmeGPU The PME GPU structure.
263 CUDA_FUNC_QUALIFIER void pme_gpu_free_spline_data(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
265 /*! \libinternal \brief
266 * Reallocates the buffers on the GPU and the host for the particle gridline indices.
268 * \param[in] pmeGPU The PME GPU structure.
270 CUDA_FUNC_QUALIFIER void pme_gpu_realloc_grid_indices(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
272 /*! \libinternal \brief
273 * Frees the buffer on the GPU for the particle gridline indices.
275 * \param[in] pmeGPU The PME GPU structure.
277 CUDA_FUNC_QUALIFIER void pme_gpu_free_grid_indices(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
279 /*! \libinternal \brief
280 * Reallocates the real space grid and the complex reciprocal grid (if needed) on the GPU.
282 * \param[in] pmeGPU The PME GPU structure.
284 CUDA_FUNC_QUALIFIER void pme_gpu_realloc_grids(PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
286 /*! \libinternal \brief
287 * Frees the real space grid and the complex reciprocal grid (if needed) on the GPU.
289 * \param[in] pmeGPU The PME GPU structure.
291 CUDA_FUNC_QUALIFIER void pme_gpu_free_grids(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
293 /*! \libinternal \brief
294 * Clears the real space grid on the GPU.
295 * Should be called at the end of each computation.
297 * \param[in] pmeGPU The PME GPU structure.
299 CUDA_FUNC_QUALIFIER void pme_gpu_clear_grids(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
301 /*! \libinternal \brief
302 * Reallocates and copies the pre-computed fractional coordinates' shifts to the GPU.
304 * \param[in] pmeGPU The PME GPU structure.
306 CUDA_FUNC_QUALIFIER void pme_gpu_realloc_and_copy_fract_shifts(PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
308 /*! \libinternal \brief
309 * Frees the pre-computed fractional coordinates' shifts on the GPU.
311 * \param[in] pmeGPU The PME GPU structure.
313 CUDA_FUNC_QUALIFIER void pme_gpu_free_fract_shifts(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
315 /*! \libinternal \brief
316 * Copies the input real-space grid from the host to the GPU.
318 * \param[in] pmeGPU The PME GPU structure.
319 * \param[in] h_grid The host-side grid buffer.
321 CUDA_FUNC_QUALIFIER void pme_gpu_copy_input_gather_grid(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU),
322 float *CUDA_FUNC_ARGUMENT(h_grid)) CUDA_FUNC_TERM
324 /*! \libinternal \brief
325 * Copies the output real-space grid from the GPU to the host.
327 * \param[in] pmeGPU The PME GPU structure.
328 * \param[out] h_grid The host-side grid buffer.
330 CUDA_FUNC_QUALIFIER void pme_gpu_copy_output_spread_grid(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU),
331 float *CUDA_FUNC_ARGUMENT(h_grid)) CUDA_FUNC_TERM
333 /*! \libinternal \brief
334 * Copies the spread output spline data and gridline indices from the GPU to the host.
336 * \param[in] pmeGPU The PME GPU structure.
338 CUDA_FUNC_QUALIFIER void pme_gpu_copy_output_spread_atom_data(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
340 /*! \libinternal \brief
341 * Copies the gather input spline data and gridline indices from the host to the GPU.
343 * \param[in] pmeGPU The PME GPU structure.
345 CUDA_FUNC_QUALIFIER void pme_gpu_copy_input_gather_atom_data(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
347 /*! \libinternal \brief
348 * Waits for the grid copying to the host-side buffer after spreading to finish.
350 * \param[in] pmeGPU The PME GPU structure.
352 CUDA_FUNC_QUALIFIER void pme_gpu_sync_spread_grid(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
354 /*! \libinternal \brief
355 * Does the one-time GPU-framework specific PME initialization.
356 * For CUDA, the PME stream is created with the highest priority.
358 * \param[in] pmeGPU The PME GPU structure.
360 CUDA_FUNC_QUALIFIER void pme_gpu_init_internal(PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
362 /*! \libinternal \brief
363 * Destroys the PME GPU-framework specific data.
364 * Should be called last in the PME GPU destructor.
366 * \param[in] pmeGPU The PME GPU structure.
368 CUDA_FUNC_QUALIFIER void pme_gpu_destroy_specific(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
370 /*! \libinternal \brief
371 * Initializes the PME GPU synchronization events.
373 * \param[in] pmeGPU The PME GPU structure.
375 CUDA_FUNC_QUALIFIER void pme_gpu_init_sync_events(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
377 /*! \libinternal \brief
378 * Destroys the PME GPU synchronization events.
380 * \param[in] pmeGPU The PME GPU structure.
382 CUDA_FUNC_QUALIFIER void pme_gpu_destroy_sync_events(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
384 /*! \libinternal \brief
385 * Initializes the CUDA FFT structures.
387 * \param[in] pmeGPU The PME GPU structure.
389 CUDA_FUNC_QUALIFIER void pme_gpu_reinit_3dfft(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
391 /*! \libinternal \brief
392 * Destroys the CUDA FFT structures.
394 * \param[in] pmeGPU The PME GPU structure.
396 CUDA_FUNC_QUALIFIER void pme_gpu_destroy_3dfft(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
398 /* Several CUDA event-based timing functions that live in pme-timings.cu */
400 /*! \libinternal \brief
401 * Finalizes all the active PME GPU stage timings for the current computation. Should be called at the end of every computation.
403 * \param[in] pmeGPU The PME GPU structure.
405 CUDA_FUNC_QUALIFIER void pme_gpu_update_timings(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
407 /*! \libinternal \brief
408 * Updates the internal list of active PME GPU stages (if timings are enabled).
410 * \param[in] pmeGPU The PME GPU data structure.
412 CUDA_FUNC_QUALIFIER void pme_gpu_reinit_timings(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
415 * Resets the PME GPU timings. To be called at the reset MD step.
417 * \param[in] pmeGPU The PME GPU structure.
419 CUDA_FUNC_QUALIFIER void pme_gpu_reset_timings(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU)) CUDA_FUNC_TERM
421 /*! \libinternal \brief
422 * Copies the PME GPU timings to the gmx_wallclock_gpu_t structure (for log output). To be called at the run end.
424 * \param[in] pmeGPU The PME GPU structure.
425 * \param[in] timings The gmx_wallclock_gpu_pme_t structure.
427 CUDA_FUNC_QUALIFIER void pme_gpu_get_timings(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGPU),
428 gmx_wallclock_gpu_pme_t *CUDA_FUNC_ARGUMENT(timings)) CUDA_FUNC_TERM
430 /* The PME stages themselves */
432 /*! \libinternal \brief
433 * A GPU spline computation and charge spreading function.
435 * \param[in] pmeGpu The PME GPU structure.
436 * \param[in] gridIndex Index of the PME grid - unused, assumed to be 0.
437 * \param[out] h_grid The host-side grid buffer (used only if the result of the spread is expected on the host,
438 * e.g. testing or host-side FFT)
439 * \param[in] computeSplines Should the computation of spline parameters and gridline indices be performed.
440 * \param[in] spreadCharges Should the charges/coefficients be spread on the grid.
442 CUDA_FUNC_QUALIFIER void pme_gpu_spread(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGpu),
443 int CUDA_FUNC_ARGUMENT(gridIndex),
444 real *CUDA_FUNC_ARGUMENT(h_grid),
445 bool CUDA_FUNC_ARGUMENT(computeSplines),
446 bool CUDA_FUNC_ARGUMENT(spreadCharges)) CUDA_FUNC_TERM
448 /*! \libinternal \brief
449 * 3D FFT R2C/C2R routine.
451 * \param[in] pmeGpu The PME GPU structure.
452 * \param[in] direction Transform direction (real-to-complex or complex-to-real)
453 * \param[in] gridIndex Index of the PME grid - unused, assumed to be 0.
455 CUDA_FUNC_QUALIFIER void pme_gpu_3dfft(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGpu),
456 enum gmx_fft_direction CUDA_FUNC_ARGUMENT(direction),
457 const int CUDA_FUNC_ARGUMENT(gridIndex)) CUDA_FUNC_TERM
459 /*! \libinternal \brief
460 * A GPU Fourier space solving function.
462 * \param[in] pmeGpu The PME GPU structure.
463 * \param[in,out] h_grid The host-side input and output Fourier grid buffer (used only with testing or host-side FFT)
464 * \param[in] gridOrdering Specifies the dimenion ordering of the complex grid. TODO: store this information?
465 * \param[in] computeEnergyAndVirial Tells if the energy and virial computation should also be performed.
467 CUDA_FUNC_QUALIFIER void pme_gpu_solve(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGpu),
468 t_complex *CUDA_FUNC_ARGUMENT(h_grid),
469 GridOrdering CUDA_FUNC_ARGUMENT(gridOrdering),
470 bool CUDA_FUNC_ARGUMENT(computeEnergyAndVirial)) CUDA_FUNC_TERM
472 /*! \libinternal \brief
473 * A GPU force gathering function.
475 * \param[in] pmeGpu The PME GPU structure.
476 * \param[in,out] h_forces The host buffer with input and output forces.
477 * \param[in] forceTreatment Tells how data in h_forces should be treated.
478 * TODO: determine efficiency/balance of host/device-side reductions.
479 * \param[in] h_grid The host-side grid buffer (used only in testing mode)
481 CUDA_FUNC_QUALIFIER void pme_gpu_gather(const PmeGpu *CUDA_FUNC_ARGUMENT(pmeGpu),
482 float *CUDA_FUNC_ARGUMENT(h_forces),
483 PmeForceOutputHandling CUDA_FUNC_ARGUMENT(forceTreatment),
484 const float *CUDA_FUNC_ARGUMENT(h_grid)
488 /* The inlined convenience PME GPU status getters */
490 /*! \libinternal \brief
491 * Tells if PME runs on multiple GPUs with the decomposition.
493 * \param[in] pmeGPU The PME GPU structure.
494 * \returns True if PME runs on multiple GPUs, false otherwise.
496 gmx_inline bool pme_gpu_uses_dd(const PmeGpu *pmeGPU)
498 return !pmeGPU->settings.useDecomposition;
501 /*! \libinternal \brief
502 * Tells if PME performs the gathering stage on GPU.
504 * \param[in] pmeGPU The PME GPU structure.
505 * \returns True if the gathering is performed on GPU, false otherwise.
507 gmx_inline bool pme_gpu_performs_gather(const PmeGpu *pmeGPU)
509 return pmeGPU->settings.performGPUGather;
512 /*! \libinternal \brief
513 * Tells if PME performs the FFT stages on GPU.
515 * \param[in] pmeGPU The PME GPU structure.
516 * \returns True if FFT is performed on GPU, false otherwise.
518 gmx_inline bool pme_gpu_performs_FFT(const PmeGpu *pmeGPU)
520 return pmeGPU->settings.performGPUFFT;
523 /*! \libinternal \brief
524 * Tells if PME performs the grid (un-)wrapping on GPU.
526 * \param[in] pmeGPU The PME GPU structure.
527 * \returns True if (un-)wrapping is performed on GPU, false otherwise.
529 gmx_inline bool pme_gpu_performs_wrapping(const PmeGpu *pmeGPU)
531 return pmeGPU->settings.useDecomposition;
534 /*! \libinternal \brief
535 * Tells if PME performs the grid solving on GPU.
537 * \param[in] pmeGPU The PME GPU structure.
538 * \returns True if solving is performed on GPU, false otherwise.
540 gmx_inline bool pme_gpu_performs_solve(const PmeGpu *pmeGPU)
542 return pmeGPU->settings.performGPUSolve;
545 /*! \libinternal \brief
546 * Enables or disables the testing mode.
547 * Testing mode only implies copying all the outputs, even the intermediate ones, to the host,
548 * and also makes the copies synchronous.
550 * \param[in] pmeGPU The PME GPU structure.
551 * \param[in] testing Should the testing mode be enabled, or disabled.
553 gmx_inline void pme_gpu_set_testing(PmeGpu *pmeGPU, bool testing)
555 pmeGPU->settings.copyAllOutputs = testing;
556 pmeGPU->settings.transferKind = testing ? GpuApiCallBehavior::Sync : GpuApiCallBehavior::Async;
559 /*! \libinternal \brief
560 * Tells if PME is in the testing mode.
562 * \param[in] pmeGPU The PME GPU structure.
563 * \returns true if testing mode is enabled, false otherwise.
565 gmx_inline bool pme_gpu_is_testing(const PmeGpu *pmeGPU)
567 return pmeGPU->settings.copyAllOutputs;
570 /* A block of C++ functions that live in pme-gpu-internal.cpp */
572 /*! \libinternal \brief
573 * Returns the output virial and energy of the PME solving.
574 * Should be called after pme_gpu_finish_computation.
576 * \param[in] pmeGPU The PME GPU structure.
577 * \param[out] energy The output energy.
578 * \param[out] virial The output virial matrix.
580 void pme_gpu_get_energy_virial(const PmeGpu *pmeGPU, real *energy, matrix virial);
582 /*! \libinternal \brief
583 * Updates the unit cell parameters. Does not check if update is necessary - that is done in pme_gpu_prepare_computation().
585 * \param[in] pmeGPU The PME GPU structure.
586 * \param[in] box The unit cell box.
588 void pme_gpu_update_input_box(PmeGpu *pmeGPU, const matrix box);
590 /*! \libinternal \brief
591 * Finishes the PME GPU computation, waiting for the output forces and/or energy/virial to be copied to the host.
592 * If forces were computed, they will have arrived at the external host buffer provided to gather.
593 * If virial/energy were computed, they will have arrived into the internal staging buffer
594 * (even though that should have already happened before even launching the gather).
595 * Finally, cudaEvent_t based GPU timers get updated if enabled. They also need stream synchronization for correctness.
596 * Additionally, device-side buffers are cleared asynchronously for the next computation.
598 * \param[in] pmeGPU The PME GPU structure.
600 void pme_gpu_finish_computation(const PmeGpu *pmeGPU);
602 //! A binary enum for spline data layout transformation
603 enum class PmeLayoutTransform
609 /*! \libinternal \brief
610 * Rearranges the atom spline data between the GPU and host layouts.
611 * Only used for test purposes so far, likely to be horribly slow.
613 * \param[in] pmeGPU The PME GPU structure.
614 * \param[out] atc The PME CPU atom data structure (with a single-threaded layout).
615 * \param[in] type The spline data type (values or derivatives).
616 * \param[in] dimIndex Dimension index.
617 * \param[in] transform Layout transform type
619 void pme_gpu_transform_spline_atom_data(const PmeGpu *pmeGPU, const pme_atomcomm_t *atc,
620 PmeSplineDataType type, int dimIndex, PmeLayoutTransform transform);
622 /*! \libinternal \brief
623 * Get the normal/padded grid dimensions of the real-space PME grid on GPU. Only used in tests.
625 * \param[in] pmeGPU The PME GPU structure.
626 * \param[out] gridSize Pointer to the grid dimensions to fill in.
627 * \param[out] paddedGridSize Pointer to the padded grid dimensions to fill in.
629 void pme_gpu_get_real_grid_sizes(const PmeGpu *pmeGPU, gmx::IVec *gridSize, gmx::IVec *paddedGridSize);
631 /*! \libinternal \brief
632 * (Re-)initializes the PME GPU data at the beginning of the run or on DLB.
634 * \param[in,out] pme The PME structure.
635 * \param[in,out] gpuInfo The GPU information structure.
636 * \param[in] mdlog The logger.
637 * \param[in] cr The communication structure.
638 * \throws gmx::NotImplementedError if this generally valid PME structure is not valid for GPU runs.
640 void pme_gpu_reinit(gmx_pme_t *pme, gmx_device_info_t *gpuInfo, const gmx::MDLogger &mdlog, const t_commrec *cr);
642 /*! \libinternal \brief
643 * Destroys the PME GPU data at the end of the run.
645 * \param[in] pmeGPU The PME GPU structure.
647 void pme_gpu_destroy(PmeGpu *pmeGPU);
649 /*! \libinternal \brief
650 * Reallocates the local atoms data (charges, coordinates, etc.). Copies the charges to the GPU.
652 * \param[in] pmeGPU The PME GPU structure.
653 * \param[in] nAtoms The number of particles.
654 * \param[in] charges The pointer to the host-side array of particle charges.
656 * This is a function that should only be called in the beginning of the run and on domain decomposition.
657 * Should be called before the pme_gpu_set_io_ranges.
659 void pme_gpu_reinit_atoms(PmeGpu *pmeGPU,
661 const real *charges);