AVX512 transposeScatterIncr/DecrU with load/store
[alexxy/gromacs.git] / src / gromacs / mdlib / nbnxn_kernels / simd_2xnn / nbnxn_kernel_simd_2xnn.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015, 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 2xnn.
38  */
39
40 #include "gmxpre.h"
41
42 #include "config.h"
43
44 #include "gromacs/math/vectypes.h"
45 #include "gromacs/mdlib/nb_verlet.h"
46 #include "gromacs/mdlib/nbnxn_simd.h"
47 #include "gromacs/mdtypes/interaction_const.h"
48 #include "gromacs/mdtypes/md_enums.h"
49
50 #ifdef GMX_NBNXN_SIMD_2XNN
51
52 /* Include the full-width SIMD macros */
53 #include "gromacs/simd/vector_operations.h"
54
55 #if !(GMX_SIMD_REAL_WIDTH == 8 || GMX_SIMD_REAL_WIDTH == 16)
56 #error "unsupported SIMD width"
57 #endif
58
59 #define GMX_SIMD_J_UNROLL_SIZE 2
60 #include "nbnxn_kernel_simd_2xnn.h"
61
62 #include "gromacs/mdlib/force_flags.h"
63 #include "gromacs/mdlib/gmx_omp_nthreads.h"
64 #include "gromacs/mdlib/nbnxn_kernels/nbnxn_kernel_common.h"
65 #include "gromacs/simd/simd.h"
66 #include "gromacs/utility/fatalerror.h"
67 #include "gromacs/utility/real.h"
68
69 /*! \brief Kinds of electrostatic treatments in SIMD Verlet kernels
70  */
71 enum {
72     coulktRF, coulktTAB, coulktTAB_TWIN, coulktEWALD, coulktEWALD_TWIN, coulktNR
73 };
74
75 /*! \brief Kinds of Van der Waals treatments in SIMD Verlet kernels
76  */
77 enum {
78     vdwktLJCUT_COMBGEOM, vdwktLJCUT_COMBLB, vdwktLJCUT_COMBNONE, vdwktLJFORCESWITCH, vdwktLJPOTSWITCH, vdwktLJEWALDCOMBGEOM, vdwktNR
79 };
80
81 /* Declare and define the kernel function pointer lookup tables.
82  * The minor index of the array goes over both the LJ combination rules,
83  * which is only supported by plain cut-off, and the LJ switch/PME functions.
84  */
85 static p_nbk_func_noener p_nbk_noener[coulktNR][vdwktNR] =
86 {
87     {
88         nbnxn_kernel_ElecRF_VdwLJCombGeom_F_2xnn,
89         nbnxn_kernel_ElecRF_VdwLJCombLB_F_2xnn,
90         nbnxn_kernel_ElecRF_VdwLJ_F_2xnn,
91         nbnxn_kernel_ElecRF_VdwLJFSw_F_2xnn,
92         nbnxn_kernel_ElecRF_VdwLJPSw_F_2xnn,
93         nbnxn_kernel_ElecRF_VdwLJEwCombGeom_F_2xnn,
94     },
95     {
96         nbnxn_kernel_ElecQSTab_VdwLJCombGeom_F_2xnn,
97         nbnxn_kernel_ElecQSTab_VdwLJCombLB_F_2xnn,
98         nbnxn_kernel_ElecQSTab_VdwLJ_F_2xnn,
99         nbnxn_kernel_ElecQSTab_VdwLJFSw_F_2xnn,
100         nbnxn_kernel_ElecQSTab_VdwLJPSw_F_2xnn,
101         nbnxn_kernel_ElecQSTab_VdwLJEwCombGeom_F_2xnn,
102     },
103     {
104         nbnxn_kernel_ElecQSTabTwinCut_VdwLJCombGeom_F_2xnn,
105         nbnxn_kernel_ElecQSTabTwinCut_VdwLJCombLB_F_2xnn,
106         nbnxn_kernel_ElecQSTabTwinCut_VdwLJ_F_2xnn,
107         nbnxn_kernel_ElecQSTabTwinCut_VdwLJFSw_F_2xnn,
108         nbnxn_kernel_ElecQSTabTwinCut_VdwLJPSw_F_2xnn,
109         nbnxn_kernel_ElecQSTabTwinCut_VdwLJEwCombGeom_F_2xnn,
110     },
111     {
112         nbnxn_kernel_ElecEw_VdwLJCombGeom_F_2xnn,
113         nbnxn_kernel_ElecEw_VdwLJCombLB_F_2xnn,
114         nbnxn_kernel_ElecEw_VdwLJ_F_2xnn,
115         nbnxn_kernel_ElecEw_VdwLJFSw_F_2xnn,
116         nbnxn_kernel_ElecEw_VdwLJPSw_F_2xnn,
117         nbnxn_kernel_ElecEw_VdwLJEwCombGeom_F_2xnn,
118     },
119     {
120         nbnxn_kernel_ElecEwTwinCut_VdwLJCombGeom_F_2xnn,
121         nbnxn_kernel_ElecEwTwinCut_VdwLJCombLB_F_2xnn,
122         nbnxn_kernel_ElecEwTwinCut_VdwLJ_F_2xnn,
123         nbnxn_kernel_ElecEwTwinCut_VdwLJFSw_F_2xnn,
124         nbnxn_kernel_ElecEwTwinCut_VdwLJPSw_F_2xnn,
125         nbnxn_kernel_ElecEwTwinCut_VdwLJEwCombGeom_F_2xnn,
126     },
127 };
128
129 static p_nbk_func_ener p_nbk_ener[coulktNR][vdwktNR] =
130 {
131     {
132         nbnxn_kernel_ElecRF_VdwLJCombGeom_VF_2xnn,
133         nbnxn_kernel_ElecRF_VdwLJCombLB_VF_2xnn,
134         nbnxn_kernel_ElecRF_VdwLJ_VF_2xnn,
135         nbnxn_kernel_ElecRF_VdwLJFSw_VF_2xnn,
136         nbnxn_kernel_ElecRF_VdwLJPSw_VF_2xnn,
137         nbnxn_kernel_ElecRF_VdwLJEwCombGeom_VF_2xnn,
138     },
139     {
140         nbnxn_kernel_ElecQSTab_VdwLJCombGeom_VF_2xnn,
141         nbnxn_kernel_ElecQSTab_VdwLJCombLB_VF_2xnn,
142         nbnxn_kernel_ElecQSTab_VdwLJ_VF_2xnn,
143         nbnxn_kernel_ElecQSTab_VdwLJFSw_VF_2xnn,
144         nbnxn_kernel_ElecQSTab_VdwLJPSw_VF_2xnn,
145         nbnxn_kernel_ElecQSTab_VdwLJEwCombGeom_VF_2xnn,
146     },
147     {
148         nbnxn_kernel_ElecQSTabTwinCut_VdwLJCombGeom_VF_2xnn,
149         nbnxn_kernel_ElecQSTabTwinCut_VdwLJCombLB_VF_2xnn,
150         nbnxn_kernel_ElecQSTabTwinCut_VdwLJ_VF_2xnn,
151         nbnxn_kernel_ElecQSTabTwinCut_VdwLJFSw_VF_2xnn,
152         nbnxn_kernel_ElecQSTabTwinCut_VdwLJPSw_VF_2xnn,
153         nbnxn_kernel_ElecQSTabTwinCut_VdwLJEwCombGeom_VF_2xnn,
154     },
155     {
156         nbnxn_kernel_ElecEw_VdwLJCombGeom_VF_2xnn,
157         nbnxn_kernel_ElecEw_VdwLJCombLB_VF_2xnn,
158         nbnxn_kernel_ElecEw_VdwLJ_VF_2xnn,
159         nbnxn_kernel_ElecEw_VdwLJFSw_VF_2xnn,
160         nbnxn_kernel_ElecEw_VdwLJPSw_VF_2xnn,
161         nbnxn_kernel_ElecEw_VdwLJEwCombGeom_VF_2xnn,
162     },
163     {
164         nbnxn_kernel_ElecEwTwinCut_VdwLJCombGeom_VF_2xnn,
165         nbnxn_kernel_ElecEwTwinCut_VdwLJCombLB_VF_2xnn,
166         nbnxn_kernel_ElecEwTwinCut_VdwLJ_VF_2xnn,
167         nbnxn_kernel_ElecEwTwinCut_VdwLJFSw_VF_2xnn,
168         nbnxn_kernel_ElecEwTwinCut_VdwLJPSw_VF_2xnn,
169         nbnxn_kernel_ElecEwTwinCut_VdwLJEwCombGeom_VF_2xnn,
170     },
171 };
172
173 static p_nbk_func_ener p_nbk_energrp[coulktNR][vdwktNR] =
174 {
175     {
176         nbnxn_kernel_ElecRF_VdwLJCombGeom_VgrpF_2xnn,
177         nbnxn_kernel_ElecRF_VdwLJCombLB_VgrpF_2xnn,
178         nbnxn_kernel_ElecRF_VdwLJ_VgrpF_2xnn,
179         nbnxn_kernel_ElecRF_VdwLJFSw_VgrpF_2xnn,
180         nbnxn_kernel_ElecRF_VdwLJPSw_VgrpF_2xnn,
181         nbnxn_kernel_ElecRF_VdwLJEwCombGeom_VgrpF_2xnn,
182     },
183     {
184         nbnxn_kernel_ElecQSTab_VdwLJCombGeom_VgrpF_2xnn,
185         nbnxn_kernel_ElecQSTab_VdwLJCombLB_VgrpF_2xnn,
186         nbnxn_kernel_ElecQSTab_VdwLJ_VgrpF_2xnn,
187         nbnxn_kernel_ElecQSTab_VdwLJFSw_VgrpF_2xnn,
188         nbnxn_kernel_ElecQSTab_VdwLJPSw_VgrpF_2xnn,
189         nbnxn_kernel_ElecQSTab_VdwLJEwCombGeom_VgrpF_2xnn,
190     },
191     {
192         nbnxn_kernel_ElecQSTabTwinCut_VdwLJCombGeom_VgrpF_2xnn,
193         nbnxn_kernel_ElecQSTabTwinCut_VdwLJCombLB_VgrpF_2xnn,
194         nbnxn_kernel_ElecQSTabTwinCut_VdwLJ_VgrpF_2xnn,
195         nbnxn_kernel_ElecQSTabTwinCut_VdwLJFSw_VgrpF_2xnn,
196         nbnxn_kernel_ElecQSTabTwinCut_VdwLJPSw_VgrpF_2xnn,
197         nbnxn_kernel_ElecQSTabTwinCut_VdwLJEwCombGeom_VgrpF_2xnn,
198     },
199     {
200         nbnxn_kernel_ElecEw_VdwLJCombGeom_VgrpF_2xnn,
201         nbnxn_kernel_ElecEw_VdwLJCombLB_VgrpF_2xnn,
202         nbnxn_kernel_ElecEw_VdwLJ_VgrpF_2xnn,
203         nbnxn_kernel_ElecEw_VdwLJFSw_VgrpF_2xnn,
204         nbnxn_kernel_ElecEw_VdwLJPSw_VgrpF_2xnn,
205         nbnxn_kernel_ElecEw_VdwLJEwCombGeom_VgrpF_2xnn,
206     },
207     {
208         nbnxn_kernel_ElecEwTwinCut_VdwLJCombGeom_VgrpF_2xnn,
209         nbnxn_kernel_ElecEwTwinCut_VdwLJCombLB_VgrpF_2xnn,
210         nbnxn_kernel_ElecEwTwinCut_VdwLJ_VgrpF_2xnn,
211         nbnxn_kernel_ElecEwTwinCut_VdwLJFSw_VgrpF_2xnn,
212         nbnxn_kernel_ElecEwTwinCut_VdwLJPSw_VgrpF_2xnn,
213         nbnxn_kernel_ElecEwTwinCut_VdwLJEwCombGeom_VgrpF_2xnn,
214     },
215 };
216
217
218 static void
219 reduce_group_energies(int ng, int ng_2log,
220                       const real *VSvdw, const real *VSc,
221                       real *Vvdw, real *Vc)
222 {
223     const int unrollj      = GMX_SIMD_REAL_WIDTH/GMX_SIMD_J_UNROLL_SIZE;
224     const int unrollj_half = unrollj/2;
225     int       ng_p2, i, j, j0, j1, c, s;
226
227     ng_p2 = (1<<ng_2log);
228
229     /* The size of the x86 SIMD energy group buffer array is:
230      * ng*ng*ng_p2*unrollj_half*simd_width
231      */
232     for (i = 0; i < ng; i++)
233     {
234         for (j = 0; j < ng; j++)
235         {
236             Vvdw[i*ng+j] = 0;
237             Vc[i*ng+j]   = 0;
238         }
239
240         for (j1 = 0; j1 < ng; j1++)
241         {
242             for (j0 = 0; j0 < ng; j0++)
243             {
244                 c = ((i*ng + j1)*ng_p2 + j0)*unrollj_half*unrollj;
245                 for (s = 0; s < unrollj_half; s++)
246                 {
247                     Vvdw[i*ng+j0] += VSvdw[c+0];
248                     Vvdw[i*ng+j1] += VSvdw[c+1];
249                     Vc  [i*ng+j0] += VSc  [c+0];
250                     Vc  [i*ng+j1] += VSc  [c+1];
251                     c             += unrollj + 2;
252                 }
253             }
254         }
255     }
256 }
257
258 #else /* GMX_NBNXN_SIMD_2XNN */
259
260 #include "gromacs/utility/fatalerror.h"
261
262 #endif /* GMX_NBNXN_SIMD_2XNN */
263
264 void
265 nbnxn_kernel_simd_2xnn(nbnxn_pairlist_set_t      gmx_unused *nbl_list,
266                        const nbnxn_atomdata_t    gmx_unused *nbat,
267                        const interaction_const_t gmx_unused *ic,
268                        int                       gmx_unused  ewald_excl,
269                        rvec                      gmx_unused *shift_vec,
270                        int                       gmx_unused  force_flags,
271                        int                       gmx_unused  clearF,
272                        real                      gmx_unused *fshift,
273                        real                      gmx_unused *Vc,
274                        real                      gmx_unused *Vvdw)
275 #ifdef GMX_NBNXN_SIMD_2XNN
276 {
277     int                nnbl;
278     nbnxn_pairlist_t **nbl;
279     int                coulkt, vdwkt = 0;
280     int                nb, nthreads;
281
282     nnbl = nbl_list->nnbl;
283     nbl  = nbl_list->nbl;
284
285     if (EEL_RF(ic->eeltype) || ic->eeltype == eelCUT)
286     {
287         coulkt = coulktRF;
288     }
289     else
290     {
291         if (ewald_excl == ewaldexclTable)
292         {
293             if (ic->rcoulomb == ic->rvdw)
294             {
295                 coulkt = coulktTAB;
296             }
297             else
298             {
299                 coulkt = coulktTAB_TWIN;
300             }
301         }
302         else
303         {
304             if (ic->rcoulomb == ic->rvdw)
305             {
306                 coulkt = coulktEWALD;
307             }
308             else
309             {
310                 coulkt = coulktEWALD_TWIN;
311             }
312         }
313     }
314
315     if (ic->vdwtype == evdwCUT)
316     {
317         switch (ic->vdw_modifier)
318         {
319             case eintmodNONE:
320             case eintmodPOTSHIFT:
321                 switch (nbat->comb_rule)
322                 {
323                     case ljcrGEOM: vdwkt = vdwktLJCUT_COMBGEOM; break;
324                     case ljcrLB:   vdwkt = vdwktLJCUT_COMBLB;   break;
325                     case ljcrNONE: vdwkt = vdwktLJCUT_COMBNONE; break;
326                     default:       gmx_incons("Unknown combination rule");
327                 }
328                 break;
329             case eintmodFORCESWITCH:
330                 vdwkt = vdwktLJFORCESWITCH;
331                 break;
332             case eintmodPOTSWITCH:
333                 vdwkt = vdwktLJPOTSWITCH;
334                 break;
335             default:
336                 gmx_incons("Unsupported VdW interaction modifier");
337         }
338     }
339     else if (ic->vdwtype == evdwPME)
340     {
341         if (ic->ljpme_comb_rule == eljpmeLB)
342         {
343             gmx_incons("The nbnxn SIMD kernels don't suport LJ-PME with LB");
344         }
345         vdwkt = vdwktLJEWALDCOMBGEOM;
346     }
347     else
348     {
349         gmx_incons("Unsupported VdW interaction type");
350     }
351     // cppcheck-suppress unreadVariable
352     nthreads = gmx_omp_nthreads_get(emntNonbonded);
353 #pragma omp parallel for schedule(static) num_threads(nthreads)
354     for (nb = 0; nb < nnbl; nb++)
355     {
356         // Presently, the kernels do not call C++ code that can throw, so
357         // no need for a try/catch pair in this OpenMP region.
358         nbnxn_atomdata_output_t *out;
359         real                    *fshift_p;
360
361         out = &nbat->out[nb];
362
363         if (clearF == enbvClearFYes)
364         {
365             clear_f(nbat, nb, out->f);
366         }
367
368         if ((force_flags & GMX_FORCE_VIRIAL) && nnbl == 1)
369         {
370             fshift_p = fshift;
371         }
372         else
373         {
374             fshift_p = out->fshift;
375
376             if (clearF == enbvClearFYes)
377             {
378                 clear_fshift(fshift_p);
379             }
380         }
381
382         if (!(force_flags & GMX_FORCE_ENERGY))
383         {
384             /* Don't calculate energies */
385             p_nbk_noener[coulkt][vdwkt](nbl[nb], nbat,
386                                         ic,
387                                         shift_vec,
388                                         out->f,
389                                         fshift_p);
390         }
391         else if (out->nV == 1)
392         {
393             /* No energy groups */
394             out->Vvdw[0] = 0;
395             out->Vc[0]   = 0;
396
397             p_nbk_ener[coulkt][vdwkt](nbl[nb], nbat,
398                                       ic,
399                                       shift_vec,
400                                       out->f,
401                                       fshift_p,
402                                       out->Vvdw,
403                                       out->Vc);
404         }
405         else
406         {
407             /* Calculate energy group contributions */
408             int i;
409
410             for (i = 0; i < out->nVS; i++)
411             {
412                 out->VSvdw[i] = 0;
413             }
414             for (i = 0; i < out->nVS; i++)
415             {
416                 out->VSc[i] = 0;
417             }
418
419             p_nbk_energrp[coulkt][vdwkt](nbl[nb], nbat,
420                                          ic,
421                                          shift_vec,
422                                          out->f,
423                                          fshift_p,
424                                          out->VSvdw,
425                                          out->VSc);
426
427             reduce_group_energies(nbat->nenergrp, nbat->neg_2log,
428                                   out->VSvdw, out->VSc,
429                                   out->Vvdw, out->Vc);
430         }
431     }
432
433     if (force_flags & GMX_FORCE_ENERGY)
434     {
435         reduce_energies_over_lists(nbat, nnbl, Vvdw, Vc);
436     }
437 }
438 #else
439 {
440     gmx_incons("nbnxn_kernel_simd_2xnn called when such kernels "
441                " are not enabled.");
442 }
443 #endif
444 #undef GMX_SIMD_J_UNROLL_SIZE