Merge release-5-0 into master
[alexxy/gromacs.git] / src / gromacs / mdlib / nbnxn_kernels / simd_4xn / nbnxn_kernel_simd_4xn.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014, 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  * Note: this file was generated by the Verlet kernel generator for
37  * kernel type 4xn.
38  */
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 #include "typedefs.h"
45
46 #include "gromacs/mdlib/nbnxn_simd.h"
47
48 #ifdef GMX_NBNXN_SIMD_4XN
49
50 #include "gromacs/simd/vector_operations.h"
51 #if !(GMX_SIMD_REAL_WIDTH == 2 || GMX_SIMD_REAL_WIDTH == 4 || GMX_SIMD_REAL_WIDTH == 8)
52 #error "unsupported SIMD width"
53 #endif
54
55 #define GMX_SIMD_J_UNROLL_SIZE 1
56 #include "nbnxn_kernel_simd_4xn.h"
57 #include "../nbnxn_kernel_common.h"
58 #include "gmx_omp_nthreads.h"
59 #include "types/force_flags.h"
60 #include "gromacs/utility/fatalerror.h"
61
62 /*! \brief Kinds of electrostatic treatments in SIMD Verlet kernels
63  */
64 enum {
65     coulktRF, coulktTAB, coulktTAB_TWIN, coulktEWALD, coulktEWALD_TWIN, coulktNR
66 };
67
68 /*! \brief Kinds of Van der Waals treatments in SIMD Verlet kernels
69  */
70 enum {
71     vdwktLJCUT_COMBGEOM, vdwktLJCUT_COMBLB, vdwktLJCUT_COMBNONE, vdwktLJFORCESWITCH, vdwktLJPOTSWITCH, vdwktLJEWALDCOMBGEOM, vdwktNR
72 };
73
74 /* Declare and define the kernel function pointer lookup tables.
75  * The minor index of the array goes over both the LJ combination rules,
76  * which is only supported by plain cut-off, and the LJ switch/PME functions.
77  */
78 static p_nbk_func_noener p_nbk_noener[coulktNR][vdwktNR] =
79 {
80     {
81         nbnxn_kernel_ElecRF_VdwLJCombGeom_F_4xn,
82         nbnxn_kernel_ElecRF_VdwLJCombLB_F_4xn,
83         nbnxn_kernel_ElecRF_VdwLJ_F_4xn,
84         nbnxn_kernel_ElecRF_VdwLJFSw_F_4xn,
85         nbnxn_kernel_ElecRF_VdwLJPSw_F_4xn,
86         nbnxn_kernel_ElecRF_VdwLJEwCombGeom_F_4xn,
87     },
88     {
89         nbnxn_kernel_ElecQSTab_VdwLJCombGeom_F_4xn,
90         nbnxn_kernel_ElecQSTab_VdwLJCombLB_F_4xn,
91         nbnxn_kernel_ElecQSTab_VdwLJ_F_4xn,
92         nbnxn_kernel_ElecQSTab_VdwLJFSw_F_4xn,
93         nbnxn_kernel_ElecQSTab_VdwLJPSw_F_4xn,
94         nbnxn_kernel_ElecQSTab_VdwLJEwCombGeom_F_4xn,
95     },
96     {
97         nbnxn_kernel_ElecQSTabTwinCut_VdwLJCombGeom_F_4xn,
98         nbnxn_kernel_ElecQSTabTwinCut_VdwLJCombLB_F_4xn,
99         nbnxn_kernel_ElecQSTabTwinCut_VdwLJ_F_4xn,
100         nbnxn_kernel_ElecQSTabTwinCut_VdwLJFSw_F_4xn,
101         nbnxn_kernel_ElecQSTabTwinCut_VdwLJPSw_F_4xn,
102         nbnxn_kernel_ElecQSTabTwinCut_VdwLJEwCombGeom_F_4xn,
103     },
104     {
105         nbnxn_kernel_ElecEw_VdwLJCombGeom_F_4xn,
106         nbnxn_kernel_ElecEw_VdwLJCombLB_F_4xn,
107         nbnxn_kernel_ElecEw_VdwLJ_F_4xn,
108         nbnxn_kernel_ElecEw_VdwLJFSw_F_4xn,
109         nbnxn_kernel_ElecEw_VdwLJPSw_F_4xn,
110         nbnxn_kernel_ElecEw_VdwLJEwCombGeom_F_4xn,
111     },
112     {
113         nbnxn_kernel_ElecEwTwinCut_VdwLJCombGeom_F_4xn,
114         nbnxn_kernel_ElecEwTwinCut_VdwLJCombLB_F_4xn,
115         nbnxn_kernel_ElecEwTwinCut_VdwLJ_F_4xn,
116         nbnxn_kernel_ElecEwTwinCut_VdwLJFSw_F_4xn,
117         nbnxn_kernel_ElecEwTwinCut_VdwLJPSw_F_4xn,
118         nbnxn_kernel_ElecEwTwinCut_VdwLJEwCombGeom_F_4xn,
119     },
120 };
121
122 static p_nbk_func_ener p_nbk_ener[coulktNR][vdwktNR] =
123 {
124     {
125         nbnxn_kernel_ElecRF_VdwLJCombGeom_VF_4xn,
126         nbnxn_kernel_ElecRF_VdwLJCombLB_VF_4xn,
127         nbnxn_kernel_ElecRF_VdwLJ_VF_4xn,
128         nbnxn_kernel_ElecRF_VdwLJFSw_VF_4xn,
129         nbnxn_kernel_ElecRF_VdwLJPSw_VF_4xn,
130         nbnxn_kernel_ElecRF_VdwLJEwCombGeom_VF_4xn,
131     },
132     {
133         nbnxn_kernel_ElecQSTab_VdwLJCombGeom_VF_4xn,
134         nbnxn_kernel_ElecQSTab_VdwLJCombLB_VF_4xn,
135         nbnxn_kernel_ElecQSTab_VdwLJ_VF_4xn,
136         nbnxn_kernel_ElecQSTab_VdwLJFSw_VF_4xn,
137         nbnxn_kernel_ElecQSTab_VdwLJPSw_VF_4xn,
138         nbnxn_kernel_ElecQSTab_VdwLJEwCombGeom_VF_4xn,
139     },
140     {
141         nbnxn_kernel_ElecQSTabTwinCut_VdwLJCombGeom_VF_4xn,
142         nbnxn_kernel_ElecQSTabTwinCut_VdwLJCombLB_VF_4xn,
143         nbnxn_kernel_ElecQSTabTwinCut_VdwLJ_VF_4xn,
144         nbnxn_kernel_ElecQSTabTwinCut_VdwLJFSw_VF_4xn,
145         nbnxn_kernel_ElecQSTabTwinCut_VdwLJPSw_VF_4xn,
146         nbnxn_kernel_ElecQSTabTwinCut_VdwLJEwCombGeom_VF_4xn,
147     },
148     {
149         nbnxn_kernel_ElecEw_VdwLJCombGeom_VF_4xn,
150         nbnxn_kernel_ElecEw_VdwLJCombLB_VF_4xn,
151         nbnxn_kernel_ElecEw_VdwLJ_VF_4xn,
152         nbnxn_kernel_ElecEw_VdwLJFSw_VF_4xn,
153         nbnxn_kernel_ElecEw_VdwLJPSw_VF_4xn,
154         nbnxn_kernel_ElecEw_VdwLJEwCombGeom_VF_4xn,
155     },
156     {
157         nbnxn_kernel_ElecEwTwinCut_VdwLJCombGeom_VF_4xn,
158         nbnxn_kernel_ElecEwTwinCut_VdwLJCombLB_VF_4xn,
159         nbnxn_kernel_ElecEwTwinCut_VdwLJ_VF_4xn,
160         nbnxn_kernel_ElecEwTwinCut_VdwLJFSw_VF_4xn,
161         nbnxn_kernel_ElecEwTwinCut_VdwLJPSw_VF_4xn,
162         nbnxn_kernel_ElecEwTwinCut_VdwLJEwCombGeom_VF_4xn,
163     },
164 };
165
166 static p_nbk_func_ener p_nbk_energrp[coulktNR][vdwktNR] =
167 {
168     {
169         nbnxn_kernel_ElecRF_VdwLJCombGeom_VgrpF_4xn,
170         nbnxn_kernel_ElecRF_VdwLJCombLB_VgrpF_4xn,
171         nbnxn_kernel_ElecRF_VdwLJ_VgrpF_4xn,
172         nbnxn_kernel_ElecRF_VdwLJFSw_VgrpF_4xn,
173         nbnxn_kernel_ElecRF_VdwLJPSw_VgrpF_4xn,
174         nbnxn_kernel_ElecRF_VdwLJEwCombGeom_VgrpF_4xn,
175     },
176     {
177         nbnxn_kernel_ElecQSTab_VdwLJCombGeom_VgrpF_4xn,
178         nbnxn_kernel_ElecQSTab_VdwLJCombLB_VgrpF_4xn,
179         nbnxn_kernel_ElecQSTab_VdwLJ_VgrpF_4xn,
180         nbnxn_kernel_ElecQSTab_VdwLJFSw_VgrpF_4xn,
181         nbnxn_kernel_ElecQSTab_VdwLJPSw_VgrpF_4xn,
182         nbnxn_kernel_ElecQSTab_VdwLJEwCombGeom_VgrpF_4xn,
183     },
184     {
185         nbnxn_kernel_ElecQSTabTwinCut_VdwLJCombGeom_VgrpF_4xn,
186         nbnxn_kernel_ElecQSTabTwinCut_VdwLJCombLB_VgrpF_4xn,
187         nbnxn_kernel_ElecQSTabTwinCut_VdwLJ_VgrpF_4xn,
188         nbnxn_kernel_ElecQSTabTwinCut_VdwLJFSw_VgrpF_4xn,
189         nbnxn_kernel_ElecQSTabTwinCut_VdwLJPSw_VgrpF_4xn,
190         nbnxn_kernel_ElecQSTabTwinCut_VdwLJEwCombGeom_VgrpF_4xn,
191     },
192     {
193         nbnxn_kernel_ElecEw_VdwLJCombGeom_VgrpF_4xn,
194         nbnxn_kernel_ElecEw_VdwLJCombLB_VgrpF_4xn,
195         nbnxn_kernel_ElecEw_VdwLJ_VgrpF_4xn,
196         nbnxn_kernel_ElecEw_VdwLJFSw_VgrpF_4xn,
197         nbnxn_kernel_ElecEw_VdwLJPSw_VgrpF_4xn,
198         nbnxn_kernel_ElecEw_VdwLJEwCombGeom_VgrpF_4xn,
199     },
200     {
201         nbnxn_kernel_ElecEwTwinCut_VdwLJCombGeom_VgrpF_4xn,
202         nbnxn_kernel_ElecEwTwinCut_VdwLJCombLB_VgrpF_4xn,
203         nbnxn_kernel_ElecEwTwinCut_VdwLJ_VgrpF_4xn,
204         nbnxn_kernel_ElecEwTwinCut_VdwLJFSw_VgrpF_4xn,
205         nbnxn_kernel_ElecEwTwinCut_VdwLJPSw_VgrpF_4xn,
206         nbnxn_kernel_ElecEwTwinCut_VdwLJEwCombGeom_VgrpF_4xn,
207     },
208 };
209
210
211 static void
212 reduce_group_energies(int ng, int ng_2log,
213                       const real *VSvdw, const real *VSc,
214                       real *Vvdw, real *Vc)
215 {
216     const int unrollj      = GMX_SIMD_REAL_WIDTH/GMX_SIMD_J_UNROLL_SIZE;
217     const int unrollj_half = unrollj/2;
218     int       ng_p2, i, j, j0, j1, c, s;
219
220     ng_p2 = (1<<ng_2log);
221
222     /* The size of the x86 SIMD energy group buffer array is:
223      * ng*ng*ng_p2*unrollj_half*simd_width
224      */
225     for (i = 0; i < ng; i++)
226     {
227         for (j = 0; j < ng; j++)
228         {
229             Vvdw[i*ng+j] = 0;
230             Vc[i*ng+j]   = 0;
231         }
232
233         for (j1 = 0; j1 < ng; j1++)
234         {
235             for (j0 = 0; j0 < ng; j0++)
236             {
237                 c = ((i*ng + j1)*ng_p2 + j0)*unrollj_half*unrollj;
238                 for (s = 0; s < unrollj_half; s++)
239                 {
240                     Vvdw[i*ng+j0] += VSvdw[c+0];
241                     Vvdw[i*ng+j1] += VSvdw[c+1];
242                     Vc  [i*ng+j0] += VSc  [c+0];
243                     Vc  [i*ng+j1] += VSc  [c+1];
244                     c             += unrollj + 2;
245                 }
246             }
247         }
248     }
249 }
250
251 #else /* GMX_NBNXN_SIMD_4XN */
252
253 #include "gromacs/utility/fatalerror.h"
254
255 #endif /* GMX_NBNXN_SIMD_4XN */
256
257 void
258 nbnxn_kernel_simd_4xn(nbnxn_pairlist_set_t      gmx_unused *nbl_list,
259                       const nbnxn_atomdata_t    gmx_unused *nbat,
260                       const interaction_const_t gmx_unused *ic,
261                       int                       gmx_unused  ewald_excl,
262                       rvec                      gmx_unused *shift_vec,
263                       int                       gmx_unused  force_flags,
264                       int                       gmx_unused  clearF,
265                       real                      gmx_unused *fshift,
266                       real                      gmx_unused *Vc,
267                       real                      gmx_unused *Vvdw)
268 #ifdef GMX_NBNXN_SIMD_4XN
269 {
270     int                nnbl;
271     nbnxn_pairlist_t **nbl;
272     int                coulkt, vdwkt = 0;
273     int                nb;
274
275     nnbl = nbl_list->nnbl;
276     nbl  = nbl_list->nbl;
277
278     if (EEL_RF(ic->eeltype) || ic->eeltype == eelCUT)
279     {
280         coulkt = coulktRF;
281     }
282     else
283     {
284         if (ewald_excl == ewaldexclTable)
285         {
286             if (ic->rcoulomb == ic->rvdw)
287             {
288                 coulkt = coulktTAB;
289             }
290             else
291             {
292                 coulkt = coulktTAB_TWIN;
293             }
294         }
295         else
296         {
297             if (ic->rcoulomb == ic->rvdw)
298             {
299                 coulkt = coulktEWALD;
300             }
301             else
302             {
303                 coulkt = coulktEWALD_TWIN;
304             }
305         }
306     }
307
308     if (ic->vdwtype == evdwCUT)
309     {
310         switch (ic->vdw_modifier)
311         {
312             case eintmodNONE:
313             case eintmodPOTSHIFT:
314                 switch (nbat->comb_rule)
315                 {
316                     case ljcrGEOM: vdwkt = vdwktLJCUT_COMBGEOM; break;
317                     case ljcrLB:   vdwkt = vdwktLJCUT_COMBLB;   break;
318                     case ljcrNONE: vdwkt = vdwktLJCUT_COMBNONE; break;
319                     default:       gmx_incons("Unknown combination rule");
320                 }
321                 break;
322             case eintmodFORCESWITCH:
323                 vdwkt = vdwktLJFORCESWITCH;
324                 break;
325             case eintmodPOTSWITCH:
326                 vdwkt = vdwktLJPOTSWITCH;
327                 break;
328             default:
329                 gmx_incons("Unsupported VdW interaction modifier");
330         }
331     }
332     else if (ic->vdwtype == evdwPME)
333     {
334         if (ic->ljpme_comb_rule == eljpmeLB)
335         {
336             gmx_incons("The nbnxn SIMD kernels don't suport LJ-PME with LB");
337         }
338         vdwkt = vdwktLJEWALDCOMBGEOM;
339     }
340     else
341     {
342         gmx_incons("Unsupported VdW interaction type");
343     }
344
345 #pragma omp parallel for schedule(static) num_threads(gmx_omp_nthreads_get(emntNonbonded))
346     for (nb = 0; nb < nnbl; nb++)
347     {
348         nbnxn_atomdata_output_t *out;
349         real                    *fshift_p;
350
351         out = &nbat->out[nb];
352
353         if (clearF == enbvClearFYes)
354         {
355             clear_f(nbat, nb, out->f);
356         }
357
358         if ((force_flags & GMX_FORCE_VIRIAL) && nnbl == 1)
359         {
360             fshift_p = fshift;
361         }
362         else
363         {
364             fshift_p = out->fshift;
365
366             if (clearF == enbvClearFYes)
367             {
368                 clear_fshift(fshift_p);
369             }
370         }
371
372         if (!(force_flags & GMX_FORCE_ENERGY))
373         {
374             /* Don't calculate energies */
375             p_nbk_noener[coulkt][vdwkt](nbl[nb], nbat,
376                                         ic,
377                                         shift_vec,
378                                         out->f,
379                                         fshift_p);
380         }
381         else if (out->nV == 1)
382         {
383             /* No energy groups */
384             out->Vvdw[0] = 0;
385             out->Vc[0]   = 0;
386
387             p_nbk_ener[coulkt][vdwkt](nbl[nb], nbat,
388                                       ic,
389                                       shift_vec,
390                                       out->f,
391                                       fshift_p,
392                                       out->Vvdw,
393                                       out->Vc);
394         }
395         else
396         {
397             /* Calculate energy group contributions */
398             int i;
399
400             for (i = 0; i < out->nVS; i++)
401             {
402                 out->VSvdw[i] = 0;
403             }
404             for (i = 0; i < out->nVS; i++)
405             {
406                 out->VSc[i] = 0;
407             }
408
409             p_nbk_energrp[coulkt][vdwkt](nbl[nb], nbat,
410                                          ic,
411                                          shift_vec,
412                                          out->f,
413                                          fshift_p,
414                                          out->VSvdw,
415                                          out->VSc);
416
417             reduce_group_energies(nbat->nenergrp, nbat->neg_2log,
418                                   out->VSvdw, out->VSc,
419                                   out->Vvdw, out->Vc);
420         }
421     }
422
423     if (force_flags & GMX_FORCE_ENERGY)
424     {
425         reduce_energies_over_lists(nbat, nnbl, Vvdw, Vc);
426     }
427 }
428 #else
429 {
430     gmx_incons("nbnxn_kernel_simd_4xn called when such kernels "
431                " are not enabled.");
432 }
433 #endif
434 #undef GMX_SIMD_J_UNROLL_SIZE