2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014,2015,2016,2017 by the GROMACS development team.
7 * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
8 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9 * and including many others, as listed in the AUTHORS file in the
10 * top-level source directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
38 /*! \libinternal \file
40 * \brief This file contains function declarations necessary for
41 * computing energies and forces for the PME long-ranged part (Coulomb
44 * \author Berk Hess <hess@kth.se>
46 * \ingroup module_ewald
49 #ifndef GMX_EWALD_PME_H
50 #define GMX_EWALD_PME_H
54 #include "gromacs/gpu_utils/devicebuffer_datatype.h"
55 #include "gromacs/gpu_utils/gpu_macros.h"
56 #include "gromacs/math/vectypes.h"
57 #include "gromacs/utility/arrayref.h"
58 #include "gromacs/utility/basedefinitions.h"
59 #include "gromacs/utility/real.h"
66 struct gmx_wallclock_gpu_pme_t;
67 struct gmx_enerdata_t;
75 enum class GpuTaskCompletion;
77 class GpuEventSynchronizer;
81 class ForceWithVirial;
83 enum class PinningPolicy : int;
93 /*! \brief Possible PME codepaths on a rank.
94 * \todo: make this enum class with gmx_pme_t C++ refactoring
98 None, //!< No PME task is done
99 CPU, //!< Whole PME computation is done on CPU
100 GPU, //!< Whole PME computation is done on GPU
101 Mixed, //!< Mixed mode: only spread and gather run on GPU; FFT and solving are done on CPU.
104 /*! \brief Return the smallest allowed PME grid size for \p pmeOrder */
105 int minimalPmeGridSize(int pmeOrder);
107 //! Return whether the grid of \c pme is identical to \c grid_size.
108 bool gmx_pme_grid_matches(const gmx_pme_t& pme, const ivec grid_size);
110 /*! \brief Check restrictions on pme_order and the PME grid nkx,nky,nkz.
112 * With errorsAreFatal=true, an exception or fatal error is generated
113 * on violation of restrictions.
114 * With errorsAreFatal=false, false is returned on violation of restrictions.
115 * When all restrictions are obeyed, true is returned.
116 * Argument useThreads tells if any MPI rank doing PME uses more than 1 threads.
117 * If at calling useThreads is unknown, pass true for conservative checking.
119 * The PME GPU restrictions are checked separately during pme_gpu_init().
121 bool gmx_pme_check_restrictions(int pme_order,
125 int numPmeDomainsAlongX,
127 bool errorsAreFatal);
129 /*! \brief Construct PME data
131 * \throws gmx::InconsistentInputError if input grid sizes/PME order are inconsistent.
132 * \returns Pointer to newly allocated and initialized PME data.
134 * \todo We should evolve something like a \c GpuManager that holds \c
135 * DeviceInformation* and \c PmeGpuProgram* and perhaps other
136 * related things whose lifetime can/should exceed that of a task (or
137 * perhaps task manager). See Issue #2522.
139 gmx_pme_t* gmx_pme_init(const t_commrec* cr,
140 const NumPmeDomains& numPmeDomains,
141 const t_inputrec* ir,
142 gmx_bool bFreeEnergy_q,
143 gmx_bool bFreeEnergy_lj,
144 gmx_bool bReproducible,
150 const DeviceContext* deviceContext,
151 const DeviceStream* deviceStream,
152 const PmeGpuProgram* pmeGpuProgram,
153 const gmx::MDLogger& mdlog);
155 /*! \brief As gmx_pme_init, but takes most settings, except the grid/Ewald coefficients, from
156 * pme_src. This is only called when the PME cut-off/grid size changes.
158 void gmx_pme_reinit(gmx_pme_t** pmedata,
161 const t_inputrec* ir,
162 const ivec grid_size,
166 /*! \brief Destroys the PME data structure.*/
167 void gmx_pme_destroy(gmx_pme_t* pme);
169 /*! \brief Do a PME calculation on a CPU for the long range electrostatics and/or LJ.
171 * Computes the PME forces and the energy and viral, when requested,
172 * for all atoms in \p coordinates. Forces, when requested, are added
173 * to the buffer \p forces, which is allowed to contain more elements
174 * than the number of elements in \p coordinates.
175 * The meaning of \p flags is defined above, and determines which
176 * parts of the calculation are performed.
178 * \return 0 indicates all well, non zero is an error code.
180 int gmx_pme_do(struct gmx_pme_t* pme,
181 gmx::ArrayRef<const gmx::RVec> coordinates,
182 gmx::ArrayRef<gmx::RVec> forces,
194 gmx_wallcycle* wcycle,
203 const gmx::StepWorkload& stepWork);
205 /*! \brief Calculate the PME grid energy V for n charges.
207 * The potential (found in \p pme) must have been found already with a
208 * call to gmx_pme_do(). Note that the charges are not spread on the grid in the
209 * pme struct. Currently does not work in parallel or with free
212 void gmx_pme_calc_energy(gmx_pme_t* pme, gmx::ArrayRef<const gmx::RVec> x, gmx::ArrayRef<const real> q, real* V);
215 * This function updates the local atom data on GPU after DD (charges, coordinates, etc.).
216 * TODO: it should update the PME CPU atom data as well.
217 * (currently PME CPU call gmx_pme_do() gets passed the input pointers for each computation).
219 * \param[in,out] pme The PME structure.
220 * \param[in] numAtoms The number of particles.
221 * \param[in] charges The pointer to the array of particle charges.
223 void gmx_pme_reinit_atoms(gmx_pme_t* pme, int numAtoms, const real* charges);
225 /* A block of PME GPU functions */
227 /*! \brief Checks whether the GROMACS build allows to run PME on GPU.
228 * TODO: this partly duplicates an internal PME assert function
229 * pme_gpu_check_restrictions(), except that works with a
230 * formed gmx_pme_t structure. Should that one go away/work with inputrec?
232 * \param[out] error If non-null, the error message when PME is not supported on GPU.
234 * \returns true if PME can run on GPU on this build, false otherwise.
236 bool pme_gpu_supports_build(std::string* error);
238 /*! \brief Checks whether the detected (GPU) hardware allows to run PME on GPU.
240 * \param[in] hwinfo Information about the detected hardware
241 * \param[out] error If non-null, the error message when PME is not supported on GPU.
243 * \returns true if PME can run on GPU on this build, false otherwise.
245 bool pme_gpu_supports_hardware(const gmx_hw_info_t& hwinfo, std::string* error);
247 /*! \brief Checks whether the input system allows to run PME on GPU.
248 * TODO: this partly duplicates an internal PME assert function
249 * pme_gpu_check_restrictions(), except that works with a
250 * formed gmx_pme_t structure. Should that one go away/work with inputrec?
252 * \param[in] ir Input system.
253 * \param[in] mtop Complete system topology to check if an FE simulation perturbs charges.
254 * \param[out] error If non-null, the error message if the input is not supported on GPU.
256 * \returns true if PME can run on GPU with this input, false otherwise.
258 bool pme_gpu_supports_input(const t_inputrec& ir, const gmx_mtop_t& mtop, std::string* error);
261 * Returns the active PME codepath (CPU, GPU, mixed).
262 * \todo This is a rather static data that should be managed by the higher level task scheduler.
264 * \param[in] pme The PME data structure.
265 * \returns active PME codepath.
267 PmeRunMode pme_run_mode(const gmx_pme_t* pme);
269 /*! \libinternal \brief
270 * Return the pinning policy appropriate for this build configuration
271 * for relevant buffers used for PME task on this rank (e.g. running
273 gmx::PinningPolicy pme_get_pinning_policy();
276 * Tells if PME is enabled to run on GPU (not necessarily active at the moment).
277 * \todo This is a rather static data that should be managed by the hardware assignment manager.
278 * For now, it is synonymous with the active PME codepath (in the absence of dynamic switching).
280 * \param[in] pme The PME data structure.
281 * \returns true if PME can run on GPU, false otherwise.
283 inline bool pme_gpu_task_enabled(const gmx_pme_t* pme)
285 return (pme != nullptr) && (pme_run_mode(pme) != PmeRunMode::CPU);
288 /*! \brief Returns the block size requirement
290 * The GPU version of PME requires that the coordinates array have a
291 * size divisible by the returned number.
293 * \param[in] pme The PME data structure.
295 GPU_FUNC_QUALIFIER int pme_gpu_get_block_size(const gmx_pme_t* GPU_FUNC_ARGUMENT(pme))
296 GPU_FUNC_TERM_WITH_RETURN(0);
298 // The following functions are all the PME GPU entry points,
299 // currently inlining to nothing on non-CUDA builds.
302 * Resets the PME GPU timings. To be called at the reset step.
304 * \param[in] pme The PME structure.
306 GPU_FUNC_QUALIFIER void pme_gpu_reset_timings(const gmx_pme_t* GPU_FUNC_ARGUMENT(pme)) GPU_FUNC_TERM;
309 * Copies the PME GPU timings to the gmx_wallclock_gpu_pme_t structure (for log output). To be called at the run end.
311 * \param[in] pme The PME structure.
312 * \param[in] timings The gmx_wallclock_gpu_pme_t structure.
314 GPU_FUNC_QUALIFIER void pme_gpu_get_timings(const gmx_pme_t* GPU_FUNC_ARGUMENT(pme),
315 gmx_wallclock_gpu_pme_t* GPU_FUNC_ARGUMENT(timings)) GPU_FUNC_TERM;
317 /* The main PME GPU functions */
320 * Prepares PME on GPU computation (updating the box if needed)
321 * \param[in] pme The PME data structure.
322 * \param[in] box The unit cell box.
323 * \param[in] wcycle The wallclock counter.
324 * \param[in] stepWork The required work for this simulation step
326 GPU_FUNC_QUALIFIER void pme_gpu_prepare_computation(gmx_pme_t* GPU_FUNC_ARGUMENT(pme),
327 const matrix GPU_FUNC_ARGUMENT(box),
328 gmx_wallcycle* GPU_FUNC_ARGUMENT(wcycle),
329 const gmx::StepWorkload& GPU_FUNC_ARGUMENT(stepWork)) GPU_FUNC_TERM;
332 * Launches first stage of PME on GPU - spreading kernel.
334 * \param[in] pme The PME data structure.
335 * \param[in] xReadyOnDevice Event synchronizer indicating that the coordinates are ready in the device memory; nullptr allowed only on separate PME ranks.
336 * \param[in] wcycle The wallclock counter.
338 GPU_FUNC_QUALIFIER void pme_gpu_launch_spread(gmx_pme_t* GPU_FUNC_ARGUMENT(pme),
339 GpuEventSynchronizer* GPU_FUNC_ARGUMENT(xReadyOnDevice),
340 gmx_wallcycle* GPU_FUNC_ARGUMENT(wcycle)) GPU_FUNC_TERM;
343 * Launches middle stages of PME (FFT R2C, solving, FFT C2R) either on GPU or on CPU, depending on the run mode.
345 * \param[in] pme The PME data structure.
346 * \param[in] wcycle The wallclock counter.
347 * \param[in] stepWork The required work for this simulation step
349 GPU_FUNC_QUALIFIER void
350 pme_gpu_launch_complex_transforms(gmx_pme_t* GPU_FUNC_ARGUMENT(pme),
351 gmx_wallcycle* GPU_FUNC_ARGUMENT(wcycle),
352 const gmx::StepWorkload& GPU_FUNC_ARGUMENT(stepWork)) GPU_FUNC_TERM;
355 * Launches last stage of PME on GPU - force gathering and D2H force transfer.
357 * \param[in] pme The PME data structure.
358 * \param[in] wcycle The wallclock counter.
360 GPU_FUNC_QUALIFIER void pme_gpu_launch_gather(const gmx_pme_t* GPU_FUNC_ARGUMENT(pme),
361 gmx_wallcycle* GPU_FUNC_ARGUMENT(wcycle)) GPU_FUNC_TERM;
364 * Attempts to complete PME GPU tasks.
366 * The \p completionKind argument controls whether the function blocks until all
367 * PME GPU tasks enqueued completed (as pme_gpu_wait_finish_task() does) or only
368 * checks and returns immediately if they did not.
369 * When blocking or the tasks have completed it also gets the output forces
370 * by assigning the ArrayRef to the \p forces pointer passed in.
371 * Virial/energy are also outputs if they were to be computed.
373 * \param[in] pme The PME data structure.
374 * \param[in] stepWork The required work for this simulation step
375 * \param[in] wcycle The wallclock counter.
376 * \param[out] forceWithVirial The output force and virial
377 * \param[out] enerd The output energies
378 * \param[in] completionKind Indicates whether PME task completion should only be checked rather
380 * \returns True if the PME GPU tasks have completed
382 GPU_FUNC_QUALIFIER bool pme_gpu_try_finish_task(gmx_pme_t* GPU_FUNC_ARGUMENT(pme),
383 const gmx::StepWorkload& GPU_FUNC_ARGUMENT(stepWork),
384 gmx_wallcycle* GPU_FUNC_ARGUMENT(wcycle),
385 gmx::ForceWithVirial* GPU_FUNC_ARGUMENT(forceWithVirial),
386 gmx_enerdata_t* GPU_FUNC_ARGUMENT(enerd),
387 GpuTaskCompletion GPU_FUNC_ARGUMENT(completionKind))
388 GPU_FUNC_TERM_WITH_RETURN(false);
391 * Blocks until PME GPU tasks are completed, and gets the output forces and virial/energy
392 * (if they were to be computed).
394 * \param[in] pme The PME data structure.
395 * \param[in] stepWork The required work for this simulation step
396 * \param[in] wcycle The wallclock counter.
397 * \param[out] forceWithVirial The output force and virial
398 * \param[out] enerd The output energies
400 GPU_FUNC_QUALIFIER void pme_gpu_wait_and_reduce(gmx_pme_t* GPU_FUNC_ARGUMENT(pme),
401 const gmx::StepWorkload& GPU_FUNC_ARGUMENT(stepWork),
402 gmx_wallcycle* GPU_FUNC_ARGUMENT(wcycle),
403 gmx::ForceWithVirial* GPU_FUNC_ARGUMENT(forceWithVirial),
404 gmx_enerdata_t* GPU_FUNC_ARGUMENT(enerd)) GPU_FUNC_TERM;
407 * The PME GPU reinitialization function that is called both at the end of any PME computation and on any load balancing.
409 * Clears the internal grid and energy/virial buffers; it is not safe to start
410 * the PME computation without calling this.
411 * Note that unlike in the nbnxn module, the force buffer does not need clearing.
413 * \todo Rename this function to *clear* -- it clearly only does output resetting
414 * and we should be clear about what the function does..
416 * \param[in] pme The PME data structure.
417 * \param[in] wcycle The wallclock counter.
419 GPU_FUNC_QUALIFIER void pme_gpu_reinit_computation(const gmx_pme_t* GPU_FUNC_ARGUMENT(pme),
420 gmx_wallcycle* GPU_FUNC_ARGUMENT(wcycle)) GPU_FUNC_TERM;
422 /*! \brief Set pointer to device copy of coordinate data.
423 * \param[in] pme The PME data structure.
424 * \param[in] d_x The pointer to the positions buffer to be set
426 GPU_FUNC_QUALIFIER void pme_gpu_set_device_x(const gmx_pme_t* GPU_FUNC_ARGUMENT(pme),
427 DeviceBuffer<gmx::RVec> GPU_FUNC_ARGUMENT(d_x)) GPU_FUNC_TERM;
429 /*! \brief Get pointer to device copy of force data.
430 * \param[in] pme The PME data structure.
431 * \returns Pointer to force data
433 GPU_FUNC_QUALIFIER void* pme_gpu_get_device_f(const gmx_pme_t* GPU_FUNC_ARGUMENT(pme))
434 GPU_FUNC_TERM_WITH_RETURN(nullptr);
436 /*! \brief Get pointer to the device synchronizer object that allows syncing on PME force calculation completion
437 * \param[in] pme The PME data structure.
438 * \returns Pointer to sychronizer
440 GPU_FUNC_QUALIFIER GpuEventSynchronizer* pme_gpu_get_f_ready_synchronizer(const gmx_pme_t* GPU_FUNC_ARGUMENT(pme))
441 GPU_FUNC_TERM_WITH_RETURN(nullptr);