475c1d68b063d692ca5ed82bbfa4b41825395d97
[alexxy/gromacs.git] / src / gromacs / nbnxm / opencl / nbnxm_ocl_kernel_pruneonly.clh
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2016,2017,2018,2019, 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  *  \brief OpenCL pruning kernel.
38  *
39  *  OpenCL 1.2 support is expected; tested on AMD GCN and NVIDIA CC >3.0.
40  *
41  *  \author Szilárd Páll <pall.szilard@gmail.com>
42  *  \ingroup module_nbnxm
43  */
44
45 #include "nbnxm_ocl_kernel_utils.clh"
46
47 /* Note: the AMD compiler testing was done with (fglrx 15.12) performs best with wg
48  * size 256 (this is an artificial compiler limitation). The compiler is also
49  * sensitive to tidx/widx declaration and warp_any initialization.
50  * With the current tweaks the regular prune kenel achieves 90%, the rolling 100%
51  * occupancy with both Fiji and Hawaii.
52  * TODO: if the wg size limit is removed in an upcoming AMD compiler the NTHREAD_Z=4
53  * should be revisited.
54  *
55  */
56 #define NTHREAD_Z GMX_NBNXN_PRUNE_KERNEL_J4_CONCURRENCY
57
58 __attribute__((reqd_work_group_size(CL_SIZE, CL_SIZE, NTHREAD_Z)))
59 #ifdef HAVE_FRESH_LIST
60 __kernel void
61 nbnxn_kernel_prune_opencl
62 #else
63 __kernel void
64 nbnxn_kernel_prune_rolling_opencl
65 #endif
66         (cl_nbparam_params_t nbparam_params,
67          const __global float4* restrict xq,
68          const __global float* restrict shift_vec,
69          const __global nbnxn_sci_t* pl_sci,
70          __global nbnxn_cj4_t* pl_cj4,
71 #if !defined HAVE_FRESH_LIST
72          const
73 #endif
74          __global unsigned int* restrict prePrunedImask,
75          int                             numParts,
76          int                             part,
77          __local float4* xib)
78 {
79     /* convenience variables */
80     const cl_nbparam_params_t* const nbparam = &nbparam_params;
81
82     const float rlistOuter_sq = nbparam->rlistOuter_sq;
83     const float rlistInner_sq = nbparam->rlistInner_sq;
84
85     /* thread/block/warp id-s */
86     const int tidxi = get_local_id(0);
87     const int tidxj = get_local_id(1);
88     const int tidx  = (int)(get_local_id(1) * get_local_size(0) + get_local_id(0));
89 #if NTHREAD_Z == 1
90     const int tidxz = 0;
91 #else
92     const int          tidxz         = get_local_id(2);
93 #endif
94     const int bidx = get_group_id(0);
95     const int widx = tidx / WARP_SIZE;
96
97 #ifdef HAVE_FRESH_LIST
98     const bool haveFreshList = true;
99 #else
100     const bool         haveFreshList = false;
101 #endif
102
103     // TODO move these consts to utils and unify their use with the nonbonded kernels
104     const int c_numClPerSupercl = NCL_PER_SUPERCL;
105     const int c_clSize          = CL_SIZE;
106
107     // TODO pass this value at compile-time as a macro
108     const int c_nbnxnGpuClusterpairSplit = 2;
109
110     /*! i-cluster interaction mask for a super-cluster with all c_numClPerSupercl=8 bits set */
111     const unsigned superClInteractionMask = ((1U << c_numClPerSupercl) - 1U);
112
113 #define LOCAL_OFFSET (xib + c_numClPerSupercl * c_clSize)
114     /* shmem buffer for i cj pre-loading */
115     CjType cjs = 0;
116 #if USE_CJ_PREFETCH
117     cjs = (((__local int*)(LOCAL_OFFSET)) + tidxz * c_nbnxnGpuClusterpairSplit * c_nbnxnGpuJgroupSize);
118 #    undef LOCAL_OFFSET
119 /* Offset calculated using xib because cjs depends on on tidxz! */
120 #    define LOCAL_OFFSET                                      \
121         (((__local int*)(xib + c_numClPerSupercl * c_clSize)) \
122          + (NTHREAD_Z * c_nbnxnGpuClusterpairSplit * c_nbnxnGpuJgroupSize))
123 #endif
124 #if !USE_SUBGROUP_ANY
125     /* Local buffer used to implement __any warp vote function from CUDA.
126        volatile is used to avoid compiler optimizations for AMD builds. */
127     //NOLINTNEXTLINE(google-readability-casting)
128     volatile __local int* const warp_any     = (__local int*)(LOCAL_OFFSET);
129     const int                   warpVoteSlot = NTHREAD_Z * tidxz + widx;
130     /* Initialise warp vote.*/
131     // if (tidx == 0 || tidx == 32)
132     if (tidx % (c_clSize * c_clSize / c_nbnxnGpuClusterpairSplit) == 0)
133     {
134         warp_any[warpVoteSlot] = 0;
135     }
136 #else
137     __local int* const warp_any      = 0;
138     const int          warpVoteSlot  = 0;
139 #endif
140 #undef LOCAL_OFFSET
141
142     const nbnxn_sci_t nb_sci =
143             pl_sci[bidx * numParts + part]; /* my i super-cluster's index = sciOffset + current bidx * numParts + part */
144     const int sci        = nb_sci.sci;           /* super-cluster */
145     const int cij4_start = nb_sci.cj4_ind_start; /* first ...*/
146     const int cij4_end   = nb_sci.cj4_ind_end;   /* and last index of j clusters */
147
148     if (tidxz == 0)
149     {
150         for (int i = 0; i < NCL_PER_SUPERCL; i += CL_SIZE)
151         {
152             /* Pre-load i-atom x and q into shared memory */
153             const int ci = sci * c_numClPerSupercl + tidxj + i;
154             const int ai = ci * c_clSize + tidxi;
155
156             /* We don't need q, but using float4 in shmem avoids bank conflicts */
157             const float4 tmp = xq[ai];
158             const float4 xi  = tmp
159                               + (float4)(shift_vec[3 * nb_sci.shift], shift_vec[3 * nb_sci.shift + 1],
160                                          shift_vec[3 * nb_sci.shift + 2], 0.0F);
161             xib[(tidxj + i) * c_clSize + tidxi] = xi;
162         }
163     }
164     barrier(CLK_LOCAL_MEM_FENCE);
165
166
167     /* loop over the j clusters = seen by any of the atoms in the current super-cluster */
168     for (int j4 = cij4_start + tidxz; j4 < cij4_end; j4 += NTHREAD_Z)
169     {
170         unsigned int imaskFull, imaskCheck, imaskNew;
171
172         if (haveFreshList)
173         {
174             /* Read the mask from the list transferred from the CPU */
175             imaskFull = pl_cj4[j4].imei[widx].imask;
176             /* We attempt to prune all pairs present in the original list */
177             imaskCheck = imaskFull;
178             imaskNew   = 0;
179         }
180         else
181         {
182             /* Read the mask from the "warp-pruned" by rlistOuter mask array */
183             imaskFull = prePrunedImask[j4 * c_nbnxnGpuClusterpairSplit + widx];
184             /* Read the old rolling pruned mask, use as a base for new */
185             imaskNew = pl_cj4[j4].imei[widx].imask;
186             /* We only need to check pairs with different mask */
187             imaskCheck = (imaskNew ^ imaskFull);
188         }
189
190         preloadCj4(&cjs, pl_cj4[j4].cj, tidxi, tidxj, imaskCheck != 0U);
191
192         if (imaskCheck)
193         {
194 #pragma unroll 4
195             for (int jm = 0; jm < c_nbnxnGpuJgroupSize; jm++)
196             {
197                 if (imaskCheck & (superClInteractionMask << (jm * c_numClPerSupercl)))
198                 {
199                     unsigned int mask_ji = (1U << (jm * c_numClPerSupercl));
200
201                     const int cj = loadCj(cjs, pl_cj4[j4].cj, jm, tidxi, tidxj);
202                     const int aj = cj * c_clSize + tidxj;
203
204                     /* load j atom data */
205                     const float4 tmp = xq[aj];
206                     const float3 xj  = (float3)(tmp.xyz);
207
208 #pragma unroll 8
209                     for (int i = 0; i < c_numClPerSupercl; i++)
210                     {
211                         if (imaskCheck & mask_ji)
212                         {
213                             /* load i-cluster coordinates from shmem */
214                             const float4 xi = xib[i * c_clSize + tidxi];
215
216                             /* distance between i and j atoms */
217                             const float3 rv = (float3)(xi.xyz) - xj;
218                             const float  r2 = norm2(rv);
219
220                             if (haveFreshList)
221                             {
222                                 /* If _none_ of the atoms pairs are in cutoff range,
223                                    the bit corresponding to the current
224                                    cluster-pair in imask gets set to 0. */
225                                 if (!gmx_sub_group_any(warp_any, warpVoteSlot, r2 < rlistOuter_sq))
226                                 {
227                                     imaskFull &= ~mask_ji;
228                                 }
229                             }
230                             /* If any atom pair is within range, set the bit
231                                corresponding to the current cluster-pair. */
232                             if (gmx_sub_group_any(warp_any, warpVoteSlot, r2 < rlistInner_sq))
233                             {
234                                 imaskNew |= mask_ji;
235                             }
236                         }
237
238                         /* shift the mask bit by 1 */
239                         mask_ji += mask_ji;
240                     }
241                 }
242             }
243
244 #ifdef HAVE_FRESH_LIST
245             {
246                 /* copy the list pruned to rlistOuter to a separate buffer */
247                 prePrunedImask[j4 * c_nbnxnGpuClusterpairSplit + widx] = imaskFull;
248             }
249 #endif
250             /* update the imask with only the pairs up to rlistInner */
251             pl_cj4[j4].imei[widx].imask = imaskNew;
252         }
253     }
254 }
255
256 #undef USE_CJ_PREFETCH