2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2015,2017,2018, 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.
36 * \brief Declare interface for GPU execution for NBNXN module
38 * \author Szilard Pall <pall.szilard@gmail.com>
39 * \author Mark Abraham <mark.j.abraham@gmail.com>
40 * \ingroup module_mdlib
43 #ifndef GMX_MDLIB_NBNXN_GPU_H
44 #define GMX_MDLIB_NBNXN_GPU_H
46 #include "gromacs/gpu_utils/gpu_macros.h"
47 #include "gromacs/math/vectypes.h"
48 #include "gromacs/mdlib/nbnxn_gpu_types.h"
49 #include "gromacs/utility/basedefinitions.h"
50 #include "gromacs/utility/real.h"
52 struct nbnxn_atomdata_t
;
53 enum class GpuTaskCompletion
;
56 * Launch asynchronously the nonbonded force calculations.
58 * This consists of the following (async) steps launched:
60 * - upload shift vector;
62 * The local and non-local interaction calculations are launched in two
66 void nbnxn_gpu_launch_kernel(gmx_nbnxn_gpu_t gmx_unused
*nb
,
67 const struct nbnxn_atomdata_t gmx_unused
*nbdata
,
69 int gmx_unused iloc
) GPU_FUNC_TERM
72 * Launch asynchronously the nonbonded prune-only kernel.
74 * The local and non-local list pruning are launched in their separate streams.
76 * Notes for future scheduling tuning:
77 * Currently we schedule the dynamic pruning between two MD steps *after* both local and
78 * nonlocal force D2H transfers completed. We could launch already after the cpyback
79 * is launched, but we want to avoid prune kernels (especially in the non-local
80 * high prio-stream) competing with nonbonded work.
82 * However, this is not ideal as this schedule does not expose the available
83 * concurrency. The dynamic pruning kernel:
84 * - should be allowed to overlap with any task other than force compute, including
85 * transfers (F D2H and the next step's x H2D as well as force clearing).
86 * - we'd prefer to avoid competition with non-bonded force kernels belonging
87 * to the same rank and ideally other ranks too.
89 * In the most general case, the former would require scheduling pruning in a separate
90 * stream and adding additional event sync points to ensure that force kernels read
91 * consistent pair list data. This would lead to some overhead (due to extra
92 * cudaStreamWaitEvent calls, 3-5 us/call) which we might be able to live with.
93 * The gains from additional overlap might not be significant as long as
94 * update+constraints anyway takes longer than pruning, but there will still
95 * be use-cases where more overlap may help (e.g. multiple ranks per GPU,
96 * no/hbonds only constraints).
97 * The above second point is harder to address given that multiple ranks will often
98 * share a GPU. Ranks that complete their nonbondeds sooner can schedule pruning earlier
99 * and without a third priority level it is difficult to avoid some interference of
100 * prune kernels with force tasks (in particular preemption of low-prio local force task).
102 * \param [inout] nb GPU nonbonded data.
103 * \param [in] iloc Interaction locality flag.
104 * \param [in] numParts Number of parts the pair list is split into in the rolling kernel.
107 void nbnxn_gpu_launch_kernel_pruneonly(gmx_nbnxn_gpu_t gmx_unused
*nb
,
109 int gmx_unused numParts
) GPU_FUNC_TERM
112 * Launch asynchronously the download of nonbonded forces from the GPU
113 * (and energies/shift forces if required).
116 void nbnxn_gpu_launch_cpyback(gmx_nbnxn_gpu_t gmx_unused
*nb
,
117 const struct nbnxn_atomdata_t gmx_unused
*nbatom
,
118 int gmx_unused flags
,
119 int gmx_unused aloc
) GPU_FUNC_TERM
121 /*! \brief Attempts to complete nonbonded GPU task.
123 * This function attempts to complete the nonbonded task (both GPU and CPU auxiliary work).
124 * Success, i.e. that the tasks completed and results are ready to be consumed, is signaled
125 * by the return value (always true if blocking wait mode requested).
127 * The \p completionKind parameter controls whether the behavior is non-blocking
128 * (achieved by passing GpuTaskCompletion::Check) or blocking wait until the results
129 * are ready (when GpuTaskCompletion::Wait is passed).
130 * As the "Check" mode the function will return immediately if the GPU stream
131 * still contain tasks that have not completed, it allows more flexible overlapping
132 * of work on the CPU with GPU execution.
134 * Note that it is only safe to use the results, and to continue to the next MD
135 * step when this function has returned true which indicates successful completion of
136 * - All nonbonded GPU tasks: both compute and device transfer(s)
137 * - auxiliary tasks: updating the internal module state (timing accumulation, list pruning states) and
138 * - internal staging reduction of (\p fshift, \p e_el, \p e_lj).
140 * TODO: improve the handling of outputs e.g. by ensuring that this function explcitly returns the
141 * force buffer (instead of that being passed only to nbnxn_gpu_launch_cpyback()) and by returning
142 * the energy and Fshift contributions for some external/centralized reduction.
144 * \param[in] nb The nonbonded data GPU structure
145 * \param[in] flags Force flags
146 * \param[in] aloc Atom locality identifier
147 * \param[out] e_lj Pointer to the LJ energy output to accumulate into
148 * \param[out] e_el Pointer to the electrostatics energy output to accumulate into
149 * \param[out] fshift Pointer to the shift force buffer to accumulate into
150 * \param[in] completionKind Indicates whether nnbonded task completion should only be checked rather than waited for
151 * \returns True if the nonbonded tasks associated with \p aloc locality have completed
154 bool nbnxn_gpu_try_finish_task(gmx_nbnxn_gpu_t gmx_unused
*nb
,
155 int gmx_unused flags
,
157 real gmx_unused
*e_lj
,
158 real gmx_unused
*e_el
,
159 rvec gmx_unused
*fshift
,
160 GpuTaskCompletion gmx_unused completionKind
) GPU_FUNC_TERM_WITH_RETURN(false)
162 /*! \brief Completes the nonbonded GPU task blocking until GPU tasks and data
163 * transfers to finish.
165 * Also does timing accounting and reduction of the internal staging buffers.
166 * As this is called at the end of the step, it also resets the pair list and
169 * \param[in] nb The nonbonded data GPU structure
170 * \param[in] flags Force flags
171 * \param[in] aloc Atom locality identifier
172 * \param[out] e_lj Pointer to the LJ energy output to accumulate into
173 * \param[out] e_el Pointer to the electrostatics energy output to accumulate into
174 * \param[out] fshift Pointer to the shift force buffer to accumulate into
177 void nbnxn_gpu_wait_finish_task(gmx_nbnxn_gpu_t gmx_unused
*nb
,
178 int gmx_unused flags
,
180 real gmx_unused
*e_lj
,
181 real gmx_unused
*e_el
,
182 rvec gmx_unused
*fshift
) GPU_FUNC_TERM
184 /*! \brief Selects the Ewald kernel type, analytical or tabulated, single or twin cut-off. */
186 int nbnxn_gpu_pick_ewald_kernel_type(bool gmx_unused bTwinCut
) GPU_FUNC_TERM_WITH_RETURN(-1)