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