a3ede7190f921840e851d48b81fb5554c8bb5365
[alexxy/gromacs.git] / src / gromacs / mdlib / nbnxn_kernels / nbnxn_kernel_file_generator / nbnxn_kernel_simd_template.c.pre
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 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include "typedefs.h"
40
41 #include "gromacs/mdlib/nbnxn_simd.h"
42
43 #ifdef {0}
44
45 {1}#include "gromacs/simd/vector_operations.h"
46
47 {2}
48 #define GMX_SIMD_J_UNROLL_SIZE {3}
49 #include "{4}"
50 #include "../nbnxn_kernel_common.h"
51 #include "gmx_omp_nthreads.h"
52 #include "types/force_flags.h"
53 #include "gmx_fatal.h"
54
55 /*! \brief Kinds of electrostatic treatments in SIMD Verlet kernels
56  */
57 enum {{
58     coulktRF, coulktTAB, coulktTAB_TWIN, coulktEWALD, coulktEWALD_TWIN, coulktNR
59 }};
60
61 /*! \brief Kinds of Van der Waals treatments in SIMD Verlet kernels
62  */
63 enum {{
64     vdwktLJCUT_COMBGEOM, vdwktLJCUT_COMBLB, vdwktLJCUT_COMBNONE, vdwktLJFORCESWITCH, vdwktLJPOTSWITCH, vdwktLJEWALDCOMBGEOM, vdwktNR
65 }};
66
67 /* Declare and define the kernel function pointer lookup tables.
68  * The minor index of the array goes over both the LJ combination rules,
69  * which is only supported by plain cut-off, and the LJ switch/PME functions.
70  */
71 static p_nbk_func_noener p_nbk_noener[coulktNR][vdwktNR] =
72 {7}
73 static p_nbk_func_ener p_nbk_ener[coulktNR][vdwktNR] =
74 {8}
75 static p_nbk_func_ener p_nbk_energrp[coulktNR][vdwktNR] =
76 {9}
77
78 static void
79 reduce_group_energies(int ng, int ng_2log,
80                       const real *VSvdw, const real *VSc,
81                       real *Vvdw, real *Vc)
82 {{
83     const int unrollj      = GMX_SIMD_REAL_WIDTH/GMX_SIMD_J_UNROLL_SIZE;
84     const int unrollj_half = unrollj/2;
85     int       ng_p2, i, j, j0, j1, c, s;
86
87     ng_p2 = (1<<ng_2log);
88
89     /* The size of the x86 SIMD energy group buffer array is:
90      * ng*ng*ng_p2*unrollj_half*simd_width
91      */
92     for (i = 0; i < ng; i++)
93     {{
94         for (j = 0; j < ng; j++)
95         {{
96             Vvdw[i*ng+j] = 0;
97             Vc[i*ng+j]   = 0;
98         }}
99
100         for (j1 = 0; j1 < ng; j1++)
101         {{
102             for (j0 = 0; j0 < ng; j0++)
103             {{
104                 c = ((i*ng + j1)*ng_p2 + j0)*unrollj_half*unrollj;
105                 for (s = 0; s < unrollj_half; s++)
106                 {{
107                     Vvdw[i*ng+j0] += VSvdw[c+0];
108                     Vvdw[i*ng+j1] += VSvdw[c+1];
109                     Vc  [i*ng+j0] += VSc  [c+0];
110                     Vc  [i*ng+j1] += VSc  [c+1];
111                     c             += unrollj + 2;
112                 }}
113             }}
114         }}
115     }}
116 }}
117
118 #else /* {0} */
119
120 #include "gmx_fatal.h"
121
122 #endif /* {0} */
123
124 void
125 {5}(nbnxn_pairlist_set_t      gmx_unused *nbl_list,
126 {6}const nbnxn_atomdata_t    gmx_unused *nbat,
127 {6}const interaction_const_t gmx_unused *ic,
128 {6}int                       gmx_unused  ewald_excl,
129 {6}rvec                      gmx_unused *shift_vec,
130 {6}int                       gmx_unused  force_flags,
131 {6}int                       gmx_unused  clearF,
132 {6}real                      gmx_unused *fshift,
133 {6}real                      gmx_unused *Vc,
134 {6}real                      gmx_unused *Vvdw)
135 #ifdef {0}
136 {{
137     int                nnbl;
138     nbnxn_pairlist_t **nbl;
139     int                coulkt, vdwkt = 0;
140     int                nb;
141
142     nnbl = nbl_list->nnbl;
143     nbl  = nbl_list->nbl;
144
145     if (EEL_RF(ic->eeltype) || ic->eeltype == eelCUT)
146     {{
147         coulkt = coulktRF;
148     }}
149     else
150     {{
151         if (ewald_excl == ewaldexclTable)
152         {{
153             if (ic->rcoulomb == ic->rvdw)
154             {{
155                 coulkt = coulktTAB;
156             }}
157             else
158             {{
159                 coulkt = coulktTAB_TWIN;
160             }}
161         }}
162         else
163         {{
164             if (ic->rcoulomb == ic->rvdw)
165             {{
166                 coulkt = coulktEWALD;
167             }}
168             else
169             {{
170                 coulkt = coulktEWALD_TWIN;
171             }}
172         }}
173     }}
174
175     if (ic->vdwtype == evdwCUT)
176     {{
177         switch (ic->vdw_modifier)
178         {{
179             case eintmodNONE:
180             case eintmodPOTSHIFT:
181                 switch (nbat->comb_rule)
182                 {{
183                     case ljcrGEOM: vdwkt = vdwktLJCUT_COMBGEOM; break;
184                     case ljcrLB:   vdwkt = vdwktLJCUT_COMBLB;   break;
185                     case ljcrNONE: vdwkt = vdwktLJCUT_COMBNONE; break;
186                     default:       gmx_incons("Unknown combination rule");
187                 }}
188                 break;
189             case eintmodFORCESWITCH:
190                 vdwkt = vdwktLJFORCESWITCH;
191                 break;
192             case eintmodPOTSWITCH:
193                 vdwkt = vdwktLJPOTSWITCH;
194                 break;
195             default:
196                 gmx_incons("Unsupported VdW interaction modifier");
197         }}
198     }}
199     else if (ic->vdwtype == evdwPME)
200     {{
201         if (ic->ljpme_comb_rule == eljpmeLB)
202         {{
203             gmx_incons("The nbnxn SIMD kernels don't suport LJ-PME with LB");
204         }}
205         vdwkt = vdwktLJEWALDCOMBGEOM;
206     }}
207     else
208     {{
209         gmx_incons("Unsupported VdW interaction type");
210     }}
211
212 #pragma omp parallel for schedule(static) num_threads(gmx_omp_nthreads_get(emntNonbonded))
213     for (nb = 0; nb < nnbl; nb++)
214     {{
215         nbnxn_atomdata_output_t *out;
216         real                    *fshift_p;
217
218         out = &nbat->out[nb];
219
220         if (clearF == enbvClearFYes)
221         {{
222             clear_f(nbat, nb, out->f);
223         }}
224
225         if ((force_flags & GMX_FORCE_VIRIAL) && nnbl == 1)
226         {{
227             fshift_p = fshift;
228         }}
229         else
230         {{
231             fshift_p = out->fshift;
232
233             if (clearF == enbvClearFYes)
234             {{
235                 clear_fshift(fshift_p);
236             }}
237         }}
238
239         if (!(force_flags & GMX_FORCE_ENERGY))
240         {{
241             /* Don't calculate energies */
242             p_nbk_noener[coulkt][vdwkt](nbl[nb], nbat,
243                                         ic,
244                                         shift_vec,
245                                         out->f,
246                                         fshift_p);
247         }}
248         else if (out->nV == 1)
249         {{
250             /* No energy groups */
251             out->Vvdw[0] = 0;
252             out->Vc[0]   = 0;
253
254             p_nbk_ener[coulkt][vdwkt](nbl[nb], nbat,
255                                       ic,
256                                       shift_vec,
257                                       out->f,
258                                       fshift_p,
259                                       out->Vvdw,
260                                       out->Vc);
261         }}
262         else
263         {{
264             /* Calculate energy group contributions */
265             int i;
266
267             for (i = 0; i < out->nVS; i++)
268             {{
269                 out->VSvdw[i] = 0;
270             }}
271             for (i = 0; i < out->nVS; i++)
272             {{
273                 out->VSc[i] = 0;
274             }}
275
276             p_nbk_energrp[coulkt][vdwkt](nbl[nb], nbat,
277                                          ic,
278                                          shift_vec,
279                                          out->f,
280                                          fshift_p,
281                                          out->VSvdw,
282                                          out->VSc);
283
284             reduce_group_energies(nbat->nenergrp, nbat->neg_2log,
285                                   out->VSvdw, out->VSc,
286                                   out->Vvdw, out->Vc);
287         }}
288     }}
289
290     if (force_flags & GMX_FORCE_ENERGY)
291     {{
292         reduce_energies_over_lists(nbat, nnbl, Vvdw, Vc);
293     }}
294 }}
295 #else
296 {{
297     gmx_incons("{5} called when such kernels "
298                " are not enabled.");
299 }}
300 #endif
301 #undef GMX_SIMD_J_UNROLL_SIZE