Disable PME Mixed mode with FEP
[alexxy/gromacs.git] / src / gromacs / ewald / pme.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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,2021, 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.
11  *
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.
16  *
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.
21  *
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.
26  *
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.
34  *
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.
37  */
38 /*! \libinternal \file
39  *
40  * \brief This file contains function declarations necessary for
41  * computing energies and forces for the PME long-ranged part (Coulomb
42  * and LJ).
43  *
44  * \author Berk Hess <hess@kth.se>
45  * \inlibraryapi
46  * \ingroup module_ewald
47  */
48
49 #ifndef GMX_EWALD_PME_H
50 #define GMX_EWALD_PME_H
51
52 #include <string>
53
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/basedefinitions.h"
58 #include "gromacs/utility/real.h"
59
60 struct gmx_hw_info_t;
61 struct t_commrec;
62 struct t_inputrec;
63 struct t_nrnb;
64 struct PmeGpu;
65 struct gmx_wallclock_gpu_pme_t;
66 struct gmx_enerdata_t;
67 struct gmx_mtop_t;
68 struct gmx_pme_t;
69 struct gmx_wallcycle;
70 struct NumPmeDomains;
71
72 class DeviceContext;
73 class DeviceStream;
74 enum class GpuTaskCompletion;
75 class PmeGpuProgram;
76 class GpuEventSynchronizer;
77
78 namespace gmx
79 {
80 template<typename>
81 class ArrayRef;
82 class ForceWithVirial;
83 class MDLogger;
84 enum class PinningPolicy : int;
85 class StepWorkload;
86 } // namespace gmx
87
88 enum
89 {
90     GMX_SUM_GRID_FORWARD,
91     GMX_SUM_GRID_BACKWARD
92 };
93
94 /*! \brief Possible PME codepaths on a rank.
95  * \todo: make this enum class with gmx_pme_t C++ refactoring
96  */
97 enum class PmeRunMode
98 {
99     None,  //!< No PME task is done
100     CPU,   //!< Whole PME computation is done on CPU
101     GPU,   //!< Whole PME computation is done on GPU
102     Mixed, //!< Mixed mode: only spread and gather run on GPU; FFT and solving are done on CPU.
103 };
104
105 /*! \brief Return the smallest allowed PME grid size for \p pmeOrder */
106 int minimalPmeGridSize(int pmeOrder);
107
108 //! Return whether the grid of \c pme is identical to \c grid_size.
109 bool gmx_pme_grid_matches(const gmx_pme_t& pme, const ivec grid_size);
110
111 /*! \brief Check restrictions on pme_order and the PME grid nkx,nky,nkz.
112  *
113  * With errorsAreFatal=true, an exception or fatal error is generated
114  * on violation of restrictions.
115  * With errorsAreFatal=false, false is returned on violation of restrictions.
116  * When all restrictions are obeyed, true is returned.
117  * Argument useThreads tells if any MPI rank doing PME uses more than 1 threads.
118  * If at calling useThreads is unknown, pass true for conservative checking.
119  *
120  * The PME GPU restrictions are checked separately during pme_gpu_init().
121  */
122 bool gmx_pme_check_restrictions(int  pme_order,
123                                 int  nkx,
124                                 int  nky,
125                                 int  nkz,
126                                 int  numPmeDomainsAlongX,
127                                 bool useThreads,
128                                 bool errorsAreFatal);
129
130 /*! \brief Construct PME data
131  *
132  * \throws   gmx::InconsistentInputError if input grid sizes/PME order are inconsistent.
133  * \returns  Pointer to newly allocated and initialized PME data.
134  *
135  * \todo We should evolve something like a \c GpuManager that holds \c
136  * DeviceInformation* and \c PmeGpuProgram* and perhaps other
137  * related things whose lifetime can/should exceed that of a task (or
138  * perhaps task manager). See Issue #2522.
139  */
140 gmx_pme_t* gmx_pme_init(const t_commrec*     cr,
141                         const NumPmeDomains& numPmeDomains,
142                         const t_inputrec*    ir,
143                         gmx_bool             bFreeEnergy_q,
144                         gmx_bool             bFreeEnergy_lj,
145                         gmx_bool             bReproducible,
146                         real                 ewaldcoeff_q,
147                         real                 ewaldcoeff_lj,
148                         int                  nthread,
149                         PmeRunMode           runMode,
150                         PmeGpu*              pmeGpu,
151                         const DeviceContext* deviceContext,
152                         const DeviceStream*  deviceStream,
153                         const PmeGpuProgram* pmeGpuProgram,
154                         const gmx::MDLogger& mdlog);
155
156 /*! \brief As gmx_pme_init, but takes most settings, except the grid/Ewald coefficients, from
157  * pme_src. This is only called when the PME cut-off/grid size changes.
158  */
159 void gmx_pme_reinit(gmx_pme_t**       pmedata,
160                     const t_commrec*  cr,
161                     gmx_pme_t*        pme_src,
162                     const t_inputrec* ir,
163                     const ivec        grid_size,
164                     real              ewaldcoeff_q,
165                     real              ewaldcoeff_lj);
166
167 /*! \brief Destroys the PME data structure.*/
168 void gmx_pme_destroy(gmx_pme_t* pme);
169
170 /*! \brief Do a PME calculation on a CPU for the long range electrostatics and/or LJ.
171  *
172  * Computes the PME forces and the energy and viral, when requested,
173  * for all atoms in \p coordinates. Forces, when requested, are added
174  * to the buffer \p forces, which is allowed to contain more elements
175  * than the number of elements in \p coordinates.
176  * The meaning of \p flags is defined above, and determines which
177  * parts of the calculation are performed.
178  *
179  * \return 0 indicates all well, non zero is an error code.
180  */
181 int gmx_pme_do(struct gmx_pme_t*              pme,
182                gmx::ArrayRef<const gmx::RVec> coordinates,
183                gmx::ArrayRef<gmx::RVec>       forces,
184                real                           chargeA[],
185                real                           chargeB[],
186                real                           c6A[],
187                real                           c6B[],
188                real                           sigmaA[],
189                real                           sigmaB[],
190                const matrix                   box,
191                const t_commrec*               cr,
192                int                            maxshift_x,
193                int                            maxshift_y,
194                t_nrnb*                        nrnb,
195                gmx_wallcycle*                 wcycle,
196                matrix                         vir_q,
197                matrix                         vir_lj,
198                real*                          energy_q,
199                real*                          energy_lj,
200                real                           lambda_q,
201                real                           lambda_lj,
202                real*                          dvdlambda_q,
203                real*                          dvdlambda_lj,
204                const gmx::StepWorkload&       stepWork);
205
206 /*! \brief Calculate the PME grid energy V for n charges.
207  *
208  * The potential (found in \p pme) must have been found already with a
209  * call to gmx_pme_do(). Note that the charges are not spread on the grid in the
210  * pme struct. Currently does not work in parallel or with free
211  * energy.
212  */
213 void gmx_pme_calc_energy(gmx_pme_t* pme, gmx::ArrayRef<const gmx::RVec> x, gmx::ArrayRef<const real> q, real* V);
214
215 /*! \brief
216  * This function updates the local atom data on GPU after DD (charges, coordinates, etc.).
217  * TODO: it should update the PME CPU atom data as well.
218  * (currently PME CPU call gmx_pme_do() gets passed the input pointers for each computation).
219  *
220  * \param[in,out] pme        The PME structure.
221  * \param[in]     numAtoms   The number of particles.
222  * \param[in]     chargesA   The pointer to the array of particle charges in the normal state or FEP
223  * state A. Can be nullptr if PME is not performed on the GPU.
224  * \param[in]     chargesB   The pointer to the array of particle charges in state B. Only used if
225  * charges are perturbed and can otherwise be nullptr.
226  */
227 void gmx_pme_reinit_atoms(gmx_pme_t* pme, int numAtoms, const real* chargesA, const real* chargesB);
228
229 /* A block of PME GPU functions */
230
231 /*! \brief Checks whether the GROMACS build allows to run PME on GPU.
232  * TODO: this partly duplicates an internal PME assert function
233  * pme_gpu_check_restrictions(), except that works with a
234  * formed gmx_pme_t structure. Should that one go away/work with inputrec?
235  *
236  * \param[out] error   If non-null, the error message when PME is not supported on GPU.
237  *
238  * \returns true if PME can run on GPU on this build, false otherwise.
239  */
240 bool pme_gpu_supports_build(std::string* error);
241
242 /*! \brief Checks whether the detected (GPU) hardware allows to run PME on GPU.
243  *
244  * \param[in]  hwinfo  Information about the detected hardware
245  * \param[out] error   If non-null, the error message when PME is not supported on GPU.
246  *
247  * \returns true if PME can run on GPU on this build, false otherwise.
248  */
249 bool pme_gpu_supports_hardware(const gmx_hw_info_t& hwinfo, std::string* error);
250
251 /*! \brief Checks whether the input system allows to run PME on GPU.
252  * TODO: this partly duplicates an internal PME assert function
253  * pme_gpu_check_restrictions(), except that works with a
254  * formed gmx_pme_t structure. Should that one go away/work with inputrec?
255  *
256  * \param[in]  ir     Input system.
257  * \param[out] error  If non-null, the error message if the input is not supported on GPU.
258  *
259  * \returns true if PME can run on GPU with this input, false otherwise.
260  */
261 bool pme_gpu_supports_input(const t_inputrec& ir, std::string* error);
262
263 /*! \brief Checks whether the input system allows to run PME on GPU in Mixed mode.
264  * Assumes that the input system is compatible with GPU PME otherwise, that is,
265  * before calling this function one should check that \ref pme_gpu_supports_input returns \c true.
266  *
267  * \param[in]  ir     Input system.
268  * \param[out] error  If non-null, the error message if the input is not supported.
269  *
270  * \returns true if PME can run on GPU in Mixed mode with this input, false otherwise.
271  */
272 bool pme_gpu_mixed_mode_supports_input(const t_inputrec& ir, std::string* error);
273
274 /*! \brief
275  * Returns the active PME codepath (CPU, GPU, mixed).
276  * \todo This is a rather static data that should be managed by the higher level task scheduler.
277  *
278  * \param[in]  pme            The PME data structure.
279  * \returns active PME codepath.
280  */
281 PmeRunMode pme_run_mode(const gmx_pme_t* pme);
282
283 /*! \libinternal \brief
284  * Return the pinning policy appropriate for this build configuration
285  * for relevant buffers used for PME task on this rank (e.g. running
286  * on a GPU). */
287 gmx::PinningPolicy pme_get_pinning_policy();
288
289 /*! \brief
290  * Tells if PME is enabled to run on GPU (not necessarily active at the moment).
291  * \todo This is a rather static data that should be managed by the hardware assignment manager.
292  * For now, it is synonymous with the active PME codepath (in the absence of dynamic switching).
293  *
294  * \param[in]  pme            The PME data structure.
295  * \returns true if PME can run on GPU, false otherwise.
296  */
297 inline bool pme_gpu_task_enabled(const gmx_pme_t* pme)
298 {
299     return (pme != nullptr) && (pme_run_mode(pme) != PmeRunMode::CPU);
300 }
301
302 /*! \brief Returns the block size requirement
303  *
304  * The GPU version of PME requires that the coordinates array have a
305  * size divisible by the returned number.
306  *
307  * \param[in]  pme  The PME data structure.
308  */
309 GPU_FUNC_QUALIFIER int pme_gpu_get_block_size(const gmx_pme_t* GPU_FUNC_ARGUMENT(pme))
310         GPU_FUNC_TERM_WITH_RETURN(0);
311
312 // The following functions are all the PME GPU entry points,
313 // currently inlining to nothing on non-CUDA builds.
314
315 /*! \brief
316  * Resets the PME GPU timings. To be called at the reset step.
317  *
318  * \param[in] pme            The PME structure.
319  */
320 GPU_FUNC_QUALIFIER void pme_gpu_reset_timings(const gmx_pme_t* GPU_FUNC_ARGUMENT(pme)) GPU_FUNC_TERM;
321
322 /*! \brief
323  * Copies the PME GPU timings to the gmx_wallclock_gpu_pme_t structure (for log output). To be called at the run end.
324  *
325  * \param[in] pme               The PME structure.
326  * \param[in] timings           The gmx_wallclock_gpu_pme_t structure.
327  */
328 GPU_FUNC_QUALIFIER void pme_gpu_get_timings(const gmx_pme_t* GPU_FUNC_ARGUMENT(pme),
329                                             gmx_wallclock_gpu_pme_t* GPU_FUNC_ARGUMENT(timings)) GPU_FUNC_TERM;
330
331 /* The main PME GPU functions */
332
333 /*! \brief
334  * Prepares PME on GPU computation (updating the box if needed)
335  * \param[in] pme               The PME data structure.
336  * \param[in] box               The unit cell box.
337  * \param[in] wcycle            The wallclock counter.
338  * \param[in] stepWork          The required work for this simulation step
339  */
340 GPU_FUNC_QUALIFIER void pme_gpu_prepare_computation(gmx_pme_t*     GPU_FUNC_ARGUMENT(pme),
341                                                     const matrix   GPU_FUNC_ARGUMENT(box),
342                                                     gmx_wallcycle* GPU_FUNC_ARGUMENT(wcycle),
343                                                     const gmx::StepWorkload& GPU_FUNC_ARGUMENT(stepWork)) GPU_FUNC_TERM;
344
345 /*! \brief
346  * Launches first stage of PME on GPU - spreading kernel.
347  *
348  * \param[in] pme                The PME data structure.
349  * \param[in] xReadyOnDevice     Event synchronizer indicating that the coordinates
350  * are ready in the device memory; nullptr allowed only on separate PME ranks.
351  * \param[in] wcycle             The wallclock counter.
352  * \param[in] lambdaQ            The Coulomb lambda of the current state of the
353  * system. Only used if FEP of Coulomb is active.
354  */
355 GPU_FUNC_QUALIFIER void pme_gpu_launch_spread(gmx_pme_t*            GPU_FUNC_ARGUMENT(pme),
356                                               GpuEventSynchronizer* GPU_FUNC_ARGUMENT(xReadyOnDevice),
357                                               gmx_wallcycle*        GPU_FUNC_ARGUMENT(wcycle),
358                                               real GPU_FUNC_ARGUMENT(lambdaQ)) GPU_FUNC_TERM;
359
360 /*! \brief
361  * Launches middle stages of PME (FFT R2C, solving, FFT C2R) either on GPU or on CPU, depending on the run mode.
362  *
363  * \param[in] pme               The PME data structure.
364  * \param[in] wcycle            The wallclock counter.
365  * \param[in] stepWork          The required work for this simulation step
366  */
367 GPU_FUNC_QUALIFIER void
368 pme_gpu_launch_complex_transforms(gmx_pme_t*               GPU_FUNC_ARGUMENT(pme),
369                                   gmx_wallcycle*           GPU_FUNC_ARGUMENT(wcycle),
370                                   const gmx::StepWorkload& GPU_FUNC_ARGUMENT(stepWork)) GPU_FUNC_TERM;
371
372 /*! \brief
373  * Launches last stage of PME on GPU - force gathering and D2H force transfer.
374  *
375  * \param[in] pme               The PME data structure.
376  * \param[in] wcycle            The wallclock counter.
377  * \param[in] lambdaQ           The Coulomb lambda to use when calculating the results.
378  */
379 GPU_FUNC_QUALIFIER void pme_gpu_launch_gather(const gmx_pme_t* GPU_FUNC_ARGUMENT(pme),
380                                               gmx_wallcycle*   GPU_FUNC_ARGUMENT(wcycle),
381                                               real GPU_FUNC_ARGUMENT(lambdaQ)) GPU_FUNC_TERM;
382
383 /*! \brief
384  * Attempts to complete PME GPU tasks.
385  *
386  * The \p completionKind argument controls whether the function blocks until all
387  * PME GPU tasks enqueued completed (as pme_gpu_wait_finish_task() does) or only
388  * checks and returns immediately if they did not.
389  * When blocking or the tasks have completed it also gets the output forces
390  * by assigning the ArrayRef to the \p forces pointer passed in.
391  * Virial/energy are also outputs if they were to be computed.
392  *
393  * \param[in]  pme             The PME data structure.
394  * \param[in]  stepWork        The required work for this simulation step
395  * \param[in]  wcycle          The wallclock counter.
396  * \param[out] forceWithVirial The output force and virial
397  * \param[out] enerd           The output energies
398  * \param[in]  lambdaQ         The Coulomb lambda to use when calculating the results.
399  * \param[in]  completionKind  Indicates whether PME task completion should only be checked rather
400  *                             than waited for
401  * \returns                    True if the PME GPU tasks have completed
402  */
403 GPU_FUNC_QUALIFIER bool pme_gpu_try_finish_task(gmx_pme_t*               GPU_FUNC_ARGUMENT(pme),
404                                                 const gmx::StepWorkload& GPU_FUNC_ARGUMENT(stepWork),
405                                                 gmx_wallcycle*           GPU_FUNC_ARGUMENT(wcycle),
406                                                 gmx::ForceWithVirial* GPU_FUNC_ARGUMENT(forceWithVirial),
407                                                 gmx_enerdata_t*       GPU_FUNC_ARGUMENT(enerd),
408                                                 real                  GPU_FUNC_ARGUMENT(lambdaQ),
409                                                 GpuTaskCompletion GPU_FUNC_ARGUMENT(completionKind))
410         GPU_FUNC_TERM_WITH_RETURN(false);
411
412 /*! \brief
413  * Blocks until PME GPU tasks are completed, and gets the output forces and virial/energy
414  * (if they were to be computed).
415  *
416  * \param[in]  pme             The PME data structure.
417  * \param[in]  stepWork        The required work for this simulation step
418  * \param[in]  wcycle          The wallclock counter.
419  * \param[out] forceWithVirial The output force and virial
420  * \param[out] enerd           The output energies
421  * \param[in]  lambdaQ         The Coulomb lambda to use when calculating the results.
422  */
423 GPU_FUNC_QUALIFIER void pme_gpu_wait_and_reduce(gmx_pme_t*               GPU_FUNC_ARGUMENT(pme),
424                                                 const gmx::StepWorkload& GPU_FUNC_ARGUMENT(stepWork),
425                                                 gmx_wallcycle*           GPU_FUNC_ARGUMENT(wcycle),
426                                                 gmx::ForceWithVirial* GPU_FUNC_ARGUMENT(forceWithVirial),
427                                                 gmx_enerdata_t*       GPU_FUNC_ARGUMENT(enerd),
428                                                 real GPU_FUNC_ARGUMENT(lambdaQ)) GPU_FUNC_TERM;
429
430 /*! \brief
431  * The PME GPU reinitialization function that is called both at the end of any PME computation and on any load balancing.
432  *
433  * Clears the internal grid and energy/virial buffers; it is not safe to start
434  * the PME computation without calling this.
435  * Note that unlike in the nbnxn module, the force buffer does not need clearing.
436  *
437  * \todo Rename this function to *clear* -- it clearly only does output resetting
438  * and we should be clear about what the function does..
439  *
440  * \param[in] pme            The PME data structure.
441  * \param[in] wcycle         The wallclock counter.
442  */
443 GPU_FUNC_QUALIFIER void pme_gpu_reinit_computation(const gmx_pme_t* GPU_FUNC_ARGUMENT(pme),
444                                                    gmx_wallcycle* GPU_FUNC_ARGUMENT(wcycle)) GPU_FUNC_TERM;
445
446 /*! \brief Set pointer to device copy of coordinate data.
447  * \param[in] pme            The PME data structure.
448  * \param[in] d_x            The pointer to the positions buffer to be set
449  */
450 GPU_FUNC_QUALIFIER void pme_gpu_set_device_x(const gmx_pme_t*        GPU_FUNC_ARGUMENT(pme),
451                                              DeviceBuffer<gmx::RVec> GPU_FUNC_ARGUMENT(d_x)) GPU_FUNC_TERM;
452
453 /*! \brief Get pointer to device copy of force data.
454  * \param[in] pme            The PME data structure.
455  * \returns                  Pointer to force data
456  */
457 GPU_FUNC_QUALIFIER void* pme_gpu_get_device_f(const gmx_pme_t* GPU_FUNC_ARGUMENT(pme))
458         GPU_FUNC_TERM_WITH_RETURN(nullptr);
459
460 /*! \brief Get pointer to the device synchronizer object that allows syncing on PME force calculation completion
461  * \param[in] pme            The PME data structure.
462  * \returns                  Pointer to sychronizer
463  */
464 GPU_FUNC_QUALIFIER GpuEventSynchronizer* pme_gpu_get_f_ready_synchronizer(const gmx_pme_t* GPU_FUNC_ARGUMENT(pme))
465         GPU_FUNC_TERM_WITH_RETURN(nullptr);
466
467 #endif