Merge commit d30f2cb6 from release-2020 into master
[alexxy/gromacs.git] / src / gromacs / nbnxm / cuda / nbnxm_buffer_ops_kernels.cuh
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2018,2019,2020, 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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35
36 /*! \internal \file
37  *
38  * \brief
39  * CUDA kernels for GPU versions of copy_rvec_to_nbat_real and add_nbat_f_to_f.
40  *
41  *  \author Alan Gray <alang@nvidia.com>
42  *  \author Jon Vincent <jvincent@nvidia.com>
43  */
44
45 #include "gromacs/gpu_utils/vectype_ops.cuh"
46 #include "gromacs/nbnxm/nbnxm.h"
47
48 /*! \brief CUDA kernel for transforming position coordinates from rvec to nbnxm layout.
49  *
50  * TODO:
51  *  - improve/simplify/document use of cxy_na and na_round
52  *  - rename kernel so naming matches with the other NBNXM kernels;
53  *  - enable separate compilation unit
54
55  * \param[in]     numColumns          Extent of cell-level parallelism.
56  * \param[out]    gm_xq               Coordinates buffer in nbnxm layout.
57  * \param[in]     setFillerCoords     Whether to set the coordinates of the filler particles.
58  * \param[in]     gm_x                Coordinates buffer.
59  * \param[in]     gm_atomIndex        Atom index mapping.
60  * \param[in]     gm_numAtoms         Array of number of atoms.
61  * \param[in]     gm_cellIndex        Array of cell indices.
62  * \param[in]     cellOffset          Airst cell.
63  * \param[in]     numAtomsPerCell     Number of atoms per cell.
64  */
65 static __global__ void nbnxn_gpu_x_to_nbat_x_kernel(int numColumns,
66                                                     float4* __restrict__ gm_xq,
67                                                     bool setFillerCoords,
68                                                     const float3* __restrict__ gm_x,
69                                                     const int* __restrict__ gm_atomIndex,
70                                                     const int* __restrict__ gm_numAtoms,
71                                                     const int* __restrict__ gm_cellIndex,
72                                                     int cellOffset,
73                                                     int numAtomsPerCell)
74 {
75
76
77     const float farAway = -1000000.0f;
78
79     // Map cell-level parallelism to y component of CUDA block index.
80     int cxy = blockIdx.y;
81
82     if (cxy < numColumns)
83     {
84
85         const int numAtoms = gm_numAtoms[cxy];
86         const int offset   = (cellOffset + gm_cellIndex[cxy]) * numAtomsPerCell;
87         int       numAtomsRounded;
88         if (setFillerCoords)
89         {
90             // TODO: This can be done more efficiently
91             numAtomsRounded = (gm_cellIndex[cxy + 1] - gm_cellIndex[cxy]) * numAtomsPerCell;
92         }
93         else
94         {
95             // We fill only the real particle locations.
96             // We assume the filling entries at the end have been
97             // properly set before during pair-list generation.
98             numAtomsRounded = numAtoms;
99         }
100
101         const int threadIndex = blockIdx.x * blockDim.x + threadIdx.x;
102
103         // Destination address where x should be stored in nbnxm layout. We use this cast here to
104         // save only x, y and z components, not touching the w (q) component, which is pre-defined.
105         float3* gm_xqDest = (float3*)&gm_xq[threadIndex + offset];
106
107         // Perform layout conversion of each element.
108         if (threadIndex < numAtomsRounded)
109         {
110             if (threadIndex < numAtoms)
111             {
112                 *gm_xqDest = gm_x[gm_atomIndex[threadIndex + offset]];
113             }
114             else
115             {
116                 *gm_xqDest = make_float3(farAway);
117             }
118         }
119     }
120 }
121
122 /*! \brief CUDA kernel to sum up the force components
123  *
124  * \tparam        accumulateForce  If the initial forces in \p gm_fTotal should be saved.
125  * \tparam        addPmeForce      Whether the PME force should be added to the total.
126  *
127  * \param[in]     gm_fNB     Non-bonded forces in nbnxm format.
128  * \param[in]     gm_fPme    PME forces.
129  * \param[in,out] gm_fTotal  Force buffer to be reduced into.
130  * \param[in]     cell       Cell index mapping.
131  * \param[in]     atomStart  Start atom index.
132  * \param[in]     numAtoms   Number of atoms.
133  */
134 template<bool accumulateForce, bool addPmeForce>
135 static __global__ void nbnxn_gpu_add_nbat_f_to_f_kernel(const float3* __restrict__ gm_fNB,
136                                                         const float3* __restrict__ gm_fPme,
137                                                         float3* gm_fTotal,
138                                                         const int* __restrict__ gm_cell,
139                                                         const int atomStart,
140                                                         const int numAtoms)
141 {
142
143     /* map particle-level parallelism to 1D CUDA thread and block index */
144     const int threadIndex = blockIdx.x * blockDim.x + threadIdx.x;
145
146     /* perform addition for each particle*/
147     if (threadIndex < numAtoms)
148     {
149
150         const int i        = gm_cell[atomStart + threadIndex];
151         float3*   gm_fDest = &gm_fTotal[atomStart + threadIndex];
152         float3    temp;
153
154         if (accumulateForce)
155         {
156             temp = *gm_fDest;
157             temp += gm_fNB[i];
158         }
159         else
160         {
161             temp = gm_fNB[i];
162         }
163         if (addPmeForce)
164         {
165             temp += gm_fPme[atomStart + threadIndex];
166         }
167         *gm_fDest = temp;
168     }
169     return;
170 }