Compile nonbonded kernels as C++
[alexxy/gromacs.git] / src / gromacs / gmxlib / nonbonded / nb_kernel_avx_256_single / nb_kernel_ElecRF_VdwLJ_GeomP1P1_avx_256_single.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015,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  * Note: this file was generated by the GROMACS avx_256_single kernel generator.
37  */
38 #include "gmxpre.h"
39
40 #include "config.h"
41
42 #include <math.h>
43
44 #include "../nb_kernel.h"
45 #include "gromacs/gmxlib/nrnb.h"
46
47 #include "kernelutil_x86_avx_256_single.h"
48
49 /*
50  * Gromacs nonbonded kernel:   nb_kernel_ElecRF_VdwLJ_GeomP1P1_VF_avx_256_single
51  * Electrostatics interaction: ReactionField
52  * VdW interaction:            LennardJones
53  * Geometry:                   Particle-Particle
54  * Calculate force/pot:        PotentialAndForce
55  */
56 void
57 nb_kernel_ElecRF_VdwLJ_GeomP1P1_VF_avx_256_single
58                     (t_nblist                    * gmx_restrict       nlist,
59                      rvec                        * gmx_restrict          xx,
60                      rvec                        * gmx_restrict          ff,
61                      struct t_forcerec           * gmx_restrict          fr,
62                      t_mdatoms                   * gmx_restrict     mdatoms,
63                      nb_kernel_data_t gmx_unused * gmx_restrict kernel_data,
64                      t_nrnb                      * gmx_restrict        nrnb)
65 {
66     /* Suffixes 0,1,2,3 refer to particle indices for waters in the inner or outer loop, or 
67      * just 0 for non-waters.
68      * Suffixes A,B,C,D,E,F,G,H refer to j loop unrolling done with AVX, e.g. for the eight different
69      * jnr indices corresponding to data put in the four positions in the SIMD register.
70      */
71     int              i_shift_offset,i_coord_offset,outeriter,inneriter;
72     int              j_index_start,j_index_end,jidx,nri,inr,ggid,iidx;
73     int              jnrA,jnrB,jnrC,jnrD;
74     int              jnrE,jnrF,jnrG,jnrH;
75     int              jnrlistA,jnrlistB,jnrlistC,jnrlistD;
76     int              jnrlistE,jnrlistF,jnrlistG,jnrlistH;
77     int              j_coord_offsetA,j_coord_offsetB,j_coord_offsetC,j_coord_offsetD;
78     int              j_coord_offsetE,j_coord_offsetF,j_coord_offsetG,j_coord_offsetH;
79     int              *iinr,*jindex,*jjnr,*shiftidx,*gid;
80     real             rcutoff_scalar;
81     real             *shiftvec,*fshift,*x,*f;
82     real             *fjptrA,*fjptrB,*fjptrC,*fjptrD,*fjptrE,*fjptrF,*fjptrG,*fjptrH;
83     real             scratch[4*DIM];
84     __m256           tx,ty,tz,fscal,rcutoff,rcutoff2,jidxall;
85     real *           vdwioffsetptr0;
86     __m256           ix0,iy0,iz0,fix0,fiy0,fiz0,iq0,isai0;
87     int              vdwjidx0A,vdwjidx0B,vdwjidx0C,vdwjidx0D,vdwjidx0E,vdwjidx0F,vdwjidx0G,vdwjidx0H;
88     __m256           jx0,jy0,jz0,fjx0,fjy0,fjz0,jq0,isaj0;
89     __m256           dx00,dy00,dz00,rsq00,rinv00,rinvsq00,r00,qq00,c6_00,c12_00;
90     __m256           velec,felec,velecsum,facel,crf,krf,krf2;
91     real             *charge;
92     int              nvdwtype;
93     __m256           rinvsix,rvdw,vvdw,vvdw6,vvdw12,fvdw,fvdw6,fvdw12,vvdwsum,sh_vdw_invrcut6;
94     int              *vdwtype;
95     real             *vdwparam;
96     __m256           one_sixth   = _mm256_set1_ps(1.0/6.0);
97     __m256           one_twelfth = _mm256_set1_ps(1.0/12.0);
98     __m256           dummy_mask,cutoff_mask;
99     __m256           signbit = _mm256_castsi256_ps( _mm256_set1_epi32(0x80000000) );
100     __m256           one     = _mm256_set1_ps(1.0);
101     __m256           two     = _mm256_set1_ps(2.0);
102     x                = xx[0];
103     f                = ff[0];
104
105     nri              = nlist->nri;
106     iinr             = nlist->iinr;
107     jindex           = nlist->jindex;
108     jjnr             = nlist->jjnr;
109     shiftidx         = nlist->shift;
110     gid              = nlist->gid;
111     shiftvec         = fr->shift_vec[0];
112     fshift           = fr->fshift[0];
113     facel            = _mm256_set1_ps(fr->ic->epsfac);
114     charge           = mdatoms->chargeA;
115     krf              = _mm256_set1_ps(fr->ic->k_rf);
116     krf2             = _mm256_set1_ps(fr->ic->k_rf*2.0);
117     crf              = _mm256_set1_ps(fr->ic->c_rf);
118     nvdwtype         = fr->ntype;
119     vdwparam         = fr->nbfp;
120     vdwtype          = mdatoms->typeA;
121
122     /* Avoid stupid compiler warnings */
123     jnrA = jnrB = jnrC = jnrD = jnrE = jnrF = jnrG = jnrH = 0;
124     j_coord_offsetA = 0;
125     j_coord_offsetB = 0;
126     j_coord_offsetC = 0;
127     j_coord_offsetD = 0;
128     j_coord_offsetE = 0;
129     j_coord_offsetF = 0;
130     j_coord_offsetG = 0;
131     j_coord_offsetH = 0;
132
133     outeriter        = 0;
134     inneriter        = 0;
135
136     for(iidx=0;iidx<4*DIM;iidx++)
137     {
138         scratch[iidx] = 0.0;
139     }
140
141     /* Start outer loop over neighborlists */
142     for(iidx=0; iidx<nri; iidx++)
143     {
144         /* Load shift vector for this list */
145         i_shift_offset   = DIM*shiftidx[iidx];
146
147         /* Load limits for loop over neighbors */
148         j_index_start    = jindex[iidx];
149         j_index_end      = jindex[iidx+1];
150
151         /* Get outer coordinate index */
152         inr              = iinr[iidx];
153         i_coord_offset   = DIM*inr;
154
155         /* Load i particle coords and add shift vector */
156         gmx_mm256_load_shift_and_1rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset,&ix0,&iy0,&iz0);
157
158         fix0             = _mm256_setzero_ps();
159         fiy0             = _mm256_setzero_ps();
160         fiz0             = _mm256_setzero_ps();
161
162         /* Load parameters for i particles */
163         iq0              = _mm256_mul_ps(facel,_mm256_set1_ps(charge[inr+0]));
164         vdwioffsetptr0   = vdwparam+2*nvdwtype*vdwtype[inr+0];
165
166         /* Reset potential sums */
167         velecsum         = _mm256_setzero_ps();
168         vvdwsum          = _mm256_setzero_ps();
169
170         /* Start inner kernel loop */
171         for(jidx=j_index_start; jidx<j_index_end && jjnr[jidx+7]>=0; jidx+=8)
172         {
173
174             /* Get j neighbor index, and coordinate index */
175             jnrA             = jjnr[jidx];
176             jnrB             = jjnr[jidx+1];
177             jnrC             = jjnr[jidx+2];
178             jnrD             = jjnr[jidx+3];
179             jnrE             = jjnr[jidx+4];
180             jnrF             = jjnr[jidx+5];
181             jnrG             = jjnr[jidx+6];
182             jnrH             = jjnr[jidx+7];
183             j_coord_offsetA  = DIM*jnrA;
184             j_coord_offsetB  = DIM*jnrB;
185             j_coord_offsetC  = DIM*jnrC;
186             j_coord_offsetD  = DIM*jnrD;
187             j_coord_offsetE  = DIM*jnrE;
188             j_coord_offsetF  = DIM*jnrF;
189             j_coord_offsetG  = DIM*jnrG;
190             j_coord_offsetH  = DIM*jnrH;
191
192             /* load j atom coordinates */
193             gmx_mm256_load_1rvec_8ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
194                                                  x+j_coord_offsetC,x+j_coord_offsetD,
195                                                  x+j_coord_offsetE,x+j_coord_offsetF,
196                                                  x+j_coord_offsetG,x+j_coord_offsetH,
197                                                  &jx0,&jy0,&jz0);
198
199             /* Calculate displacement vector */
200             dx00             = _mm256_sub_ps(ix0,jx0);
201             dy00             = _mm256_sub_ps(iy0,jy0);
202             dz00             = _mm256_sub_ps(iz0,jz0);
203
204             /* Calculate squared distance and things based on it */
205             rsq00            = gmx_mm256_calc_rsq_ps(dx00,dy00,dz00);
206
207             rinv00           = avx256_invsqrt_f(rsq00);
208
209             rinvsq00         = _mm256_mul_ps(rinv00,rinv00);
210
211             /* Load parameters for j particles */
212             jq0              = gmx_mm256_load_8real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
213                                                                  charge+jnrC+0,charge+jnrD+0,
214                                                                  charge+jnrE+0,charge+jnrF+0,
215                                                                  charge+jnrG+0,charge+jnrH+0);
216             vdwjidx0A        = 2*vdwtype[jnrA+0];
217             vdwjidx0B        = 2*vdwtype[jnrB+0];
218             vdwjidx0C        = 2*vdwtype[jnrC+0];
219             vdwjidx0D        = 2*vdwtype[jnrD+0];
220             vdwjidx0E        = 2*vdwtype[jnrE+0];
221             vdwjidx0F        = 2*vdwtype[jnrF+0];
222             vdwjidx0G        = 2*vdwtype[jnrG+0];
223             vdwjidx0H        = 2*vdwtype[jnrH+0];
224
225             /**************************
226              * CALCULATE INTERACTIONS *
227              **************************/
228
229             /* Compute parameters for interactions between i and j atoms */
230             qq00             = _mm256_mul_ps(iq0,jq0);
231             gmx_mm256_load_8pair_swizzle_ps(vdwioffsetptr0+vdwjidx0A,
232                                             vdwioffsetptr0+vdwjidx0B,
233                                             vdwioffsetptr0+vdwjidx0C,
234                                             vdwioffsetptr0+vdwjidx0D,
235                                             vdwioffsetptr0+vdwjidx0E,
236                                             vdwioffsetptr0+vdwjidx0F,
237                                             vdwioffsetptr0+vdwjidx0G,
238                                             vdwioffsetptr0+vdwjidx0H,
239                                             &c6_00,&c12_00);
240
241             /* REACTION-FIELD ELECTROSTATICS */
242             velec            = _mm256_mul_ps(qq00,_mm256_sub_ps(_mm256_add_ps(rinv00,_mm256_mul_ps(krf,rsq00)),crf));
243             felec            = _mm256_mul_ps(qq00,_mm256_sub_ps(_mm256_mul_ps(rinv00,rinvsq00),krf2));
244
245             /* LENNARD-JONES DISPERSION/REPULSION */
246
247             rinvsix          = _mm256_mul_ps(_mm256_mul_ps(rinvsq00,rinvsq00),rinvsq00);
248             vvdw6            = _mm256_mul_ps(c6_00,rinvsix);
249             vvdw12           = _mm256_mul_ps(c12_00,_mm256_mul_ps(rinvsix,rinvsix));
250             vvdw             = _mm256_sub_ps( _mm256_mul_ps(vvdw12,one_twelfth) , _mm256_mul_ps(vvdw6,one_sixth) );
251             fvdw             = _mm256_mul_ps(_mm256_sub_ps(vvdw12,vvdw6),rinvsq00);
252
253             /* Update potential sum for this i atom from the interaction with this j atom. */
254             velecsum         = _mm256_add_ps(velecsum,velec);
255             vvdwsum          = _mm256_add_ps(vvdwsum,vvdw);
256
257             fscal            = _mm256_add_ps(felec,fvdw);
258
259             /* Calculate temporary vectorial force */
260             tx               = _mm256_mul_ps(fscal,dx00);
261             ty               = _mm256_mul_ps(fscal,dy00);
262             tz               = _mm256_mul_ps(fscal,dz00);
263
264             /* Update vectorial force */
265             fix0             = _mm256_add_ps(fix0,tx);
266             fiy0             = _mm256_add_ps(fiy0,ty);
267             fiz0             = _mm256_add_ps(fiz0,tz);
268
269             fjptrA             = f+j_coord_offsetA;
270             fjptrB             = f+j_coord_offsetB;
271             fjptrC             = f+j_coord_offsetC;
272             fjptrD             = f+j_coord_offsetD;
273             fjptrE             = f+j_coord_offsetE;
274             fjptrF             = f+j_coord_offsetF;
275             fjptrG             = f+j_coord_offsetG;
276             fjptrH             = f+j_coord_offsetH;
277             gmx_mm256_decrement_1rvec_8ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjptrE,fjptrF,fjptrG,fjptrH,tx,ty,tz);
278
279             /* Inner loop uses 44 flops */
280         }
281
282         if(jidx<j_index_end)
283         {
284
285             /* Get j neighbor index, and coordinate index */
286             jnrlistA         = jjnr[jidx];
287             jnrlistB         = jjnr[jidx+1];
288             jnrlistC         = jjnr[jidx+2];
289             jnrlistD         = jjnr[jidx+3];
290             jnrlistE         = jjnr[jidx+4];
291             jnrlistF         = jjnr[jidx+5];
292             jnrlistG         = jjnr[jidx+6];
293             jnrlistH         = jjnr[jidx+7];
294             /* Sign of each element will be negative for non-real atoms.
295              * This mask will be 0xFFFFFFFF for dummy entries and 0x0 for real ones,
296              * so use it as val = _mm_andnot_ps(mask,val) to clear dummy entries.
297              */
298             dummy_mask = gmx_mm256_set_m128(gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx+4)),_mm_setzero_si128())),
299                                             gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx)),_mm_setzero_si128())));
300                                             
301             jnrA       = (jnrlistA>=0) ? jnrlistA : 0;
302             jnrB       = (jnrlistB>=0) ? jnrlistB : 0;
303             jnrC       = (jnrlistC>=0) ? jnrlistC : 0;
304             jnrD       = (jnrlistD>=0) ? jnrlistD : 0;
305             jnrE       = (jnrlistE>=0) ? jnrlistE : 0;
306             jnrF       = (jnrlistF>=0) ? jnrlistF : 0;
307             jnrG       = (jnrlistG>=0) ? jnrlistG : 0;
308             jnrH       = (jnrlistH>=0) ? jnrlistH : 0;
309             j_coord_offsetA  = DIM*jnrA;
310             j_coord_offsetB  = DIM*jnrB;
311             j_coord_offsetC  = DIM*jnrC;
312             j_coord_offsetD  = DIM*jnrD;
313             j_coord_offsetE  = DIM*jnrE;
314             j_coord_offsetF  = DIM*jnrF;
315             j_coord_offsetG  = DIM*jnrG;
316             j_coord_offsetH  = DIM*jnrH;
317
318             /* load j atom coordinates */
319             gmx_mm256_load_1rvec_8ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
320                                                  x+j_coord_offsetC,x+j_coord_offsetD,
321                                                  x+j_coord_offsetE,x+j_coord_offsetF,
322                                                  x+j_coord_offsetG,x+j_coord_offsetH,
323                                                  &jx0,&jy0,&jz0);
324
325             /* Calculate displacement vector */
326             dx00             = _mm256_sub_ps(ix0,jx0);
327             dy00             = _mm256_sub_ps(iy0,jy0);
328             dz00             = _mm256_sub_ps(iz0,jz0);
329
330             /* Calculate squared distance and things based on it */
331             rsq00            = gmx_mm256_calc_rsq_ps(dx00,dy00,dz00);
332
333             rinv00           = avx256_invsqrt_f(rsq00);
334
335             rinvsq00         = _mm256_mul_ps(rinv00,rinv00);
336
337             /* Load parameters for j particles */
338             jq0              = gmx_mm256_load_8real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
339                                                                  charge+jnrC+0,charge+jnrD+0,
340                                                                  charge+jnrE+0,charge+jnrF+0,
341                                                                  charge+jnrG+0,charge+jnrH+0);
342             vdwjidx0A        = 2*vdwtype[jnrA+0];
343             vdwjidx0B        = 2*vdwtype[jnrB+0];
344             vdwjidx0C        = 2*vdwtype[jnrC+0];
345             vdwjidx0D        = 2*vdwtype[jnrD+0];
346             vdwjidx0E        = 2*vdwtype[jnrE+0];
347             vdwjidx0F        = 2*vdwtype[jnrF+0];
348             vdwjidx0G        = 2*vdwtype[jnrG+0];
349             vdwjidx0H        = 2*vdwtype[jnrH+0];
350
351             /**************************
352              * CALCULATE INTERACTIONS *
353              **************************/
354
355             /* Compute parameters for interactions between i and j atoms */
356             qq00             = _mm256_mul_ps(iq0,jq0);
357             gmx_mm256_load_8pair_swizzle_ps(vdwioffsetptr0+vdwjidx0A,
358                                             vdwioffsetptr0+vdwjidx0B,
359                                             vdwioffsetptr0+vdwjidx0C,
360                                             vdwioffsetptr0+vdwjidx0D,
361                                             vdwioffsetptr0+vdwjidx0E,
362                                             vdwioffsetptr0+vdwjidx0F,
363                                             vdwioffsetptr0+vdwjidx0G,
364                                             vdwioffsetptr0+vdwjidx0H,
365                                             &c6_00,&c12_00);
366
367             /* REACTION-FIELD ELECTROSTATICS */
368             velec            = _mm256_mul_ps(qq00,_mm256_sub_ps(_mm256_add_ps(rinv00,_mm256_mul_ps(krf,rsq00)),crf));
369             felec            = _mm256_mul_ps(qq00,_mm256_sub_ps(_mm256_mul_ps(rinv00,rinvsq00),krf2));
370
371             /* LENNARD-JONES DISPERSION/REPULSION */
372
373             rinvsix          = _mm256_mul_ps(_mm256_mul_ps(rinvsq00,rinvsq00),rinvsq00);
374             vvdw6            = _mm256_mul_ps(c6_00,rinvsix);
375             vvdw12           = _mm256_mul_ps(c12_00,_mm256_mul_ps(rinvsix,rinvsix));
376             vvdw             = _mm256_sub_ps( _mm256_mul_ps(vvdw12,one_twelfth) , _mm256_mul_ps(vvdw6,one_sixth) );
377             fvdw             = _mm256_mul_ps(_mm256_sub_ps(vvdw12,vvdw6),rinvsq00);
378
379             /* Update potential sum for this i atom from the interaction with this j atom. */
380             velec            = _mm256_andnot_ps(dummy_mask,velec);
381             velecsum         = _mm256_add_ps(velecsum,velec);
382             vvdw             = _mm256_andnot_ps(dummy_mask,vvdw);
383             vvdwsum          = _mm256_add_ps(vvdwsum,vvdw);
384
385             fscal            = _mm256_add_ps(felec,fvdw);
386
387             fscal            = _mm256_andnot_ps(dummy_mask,fscal);
388
389             /* Calculate temporary vectorial force */
390             tx               = _mm256_mul_ps(fscal,dx00);
391             ty               = _mm256_mul_ps(fscal,dy00);
392             tz               = _mm256_mul_ps(fscal,dz00);
393
394             /* Update vectorial force */
395             fix0             = _mm256_add_ps(fix0,tx);
396             fiy0             = _mm256_add_ps(fiy0,ty);
397             fiz0             = _mm256_add_ps(fiz0,tz);
398
399             fjptrA             = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
400             fjptrB             = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
401             fjptrC             = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
402             fjptrD             = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
403             fjptrE             = (jnrlistE>=0) ? f+j_coord_offsetE : scratch;
404             fjptrF             = (jnrlistF>=0) ? f+j_coord_offsetF : scratch;
405             fjptrG             = (jnrlistG>=0) ? f+j_coord_offsetG : scratch;
406             fjptrH             = (jnrlistH>=0) ? f+j_coord_offsetH : scratch;
407             gmx_mm256_decrement_1rvec_8ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjptrE,fjptrF,fjptrG,fjptrH,tx,ty,tz);
408
409             /* Inner loop uses 44 flops */
410         }
411
412         /* End of innermost loop */
413
414         gmx_mm256_update_iforce_1atom_swizzle_ps(fix0,fiy0,fiz0,
415                                                  f+i_coord_offset,fshift+i_shift_offset);
416
417         ggid                        = gid[iidx];
418         /* Update potential energies */
419         gmx_mm256_update_1pot_ps(velecsum,kernel_data->energygrp_elec+ggid);
420         gmx_mm256_update_1pot_ps(vvdwsum,kernel_data->energygrp_vdw+ggid);
421
422         /* Increment number of inner iterations */
423         inneriter                  += j_index_end - j_index_start;
424
425         /* Outer loop uses 9 flops */
426     }
427
428     /* Increment number of outer iterations */
429     outeriter        += nri;
430
431     /* Update outer/inner flops */
432
433     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_VDW_VF,outeriter*9 + inneriter*44);
434 }
435 /*
436  * Gromacs nonbonded kernel:   nb_kernel_ElecRF_VdwLJ_GeomP1P1_F_avx_256_single
437  * Electrostatics interaction: ReactionField
438  * VdW interaction:            LennardJones
439  * Geometry:                   Particle-Particle
440  * Calculate force/pot:        Force
441  */
442 void
443 nb_kernel_ElecRF_VdwLJ_GeomP1P1_F_avx_256_single
444                     (t_nblist                    * gmx_restrict       nlist,
445                      rvec                        * gmx_restrict          xx,
446                      rvec                        * gmx_restrict          ff,
447                      struct t_forcerec           * gmx_restrict          fr,
448                      t_mdatoms                   * gmx_restrict     mdatoms,
449                      nb_kernel_data_t gmx_unused * gmx_restrict kernel_data,
450                      t_nrnb                      * gmx_restrict        nrnb)
451 {
452     /* Suffixes 0,1,2,3 refer to particle indices for waters in the inner or outer loop, or 
453      * just 0 for non-waters.
454      * Suffixes A,B,C,D,E,F,G,H refer to j loop unrolling done with AVX, e.g. for the eight different
455      * jnr indices corresponding to data put in the four positions in the SIMD register.
456      */
457     int              i_shift_offset,i_coord_offset,outeriter,inneriter;
458     int              j_index_start,j_index_end,jidx,nri,inr,ggid,iidx;
459     int              jnrA,jnrB,jnrC,jnrD;
460     int              jnrE,jnrF,jnrG,jnrH;
461     int              jnrlistA,jnrlistB,jnrlistC,jnrlistD;
462     int              jnrlistE,jnrlistF,jnrlistG,jnrlistH;
463     int              j_coord_offsetA,j_coord_offsetB,j_coord_offsetC,j_coord_offsetD;
464     int              j_coord_offsetE,j_coord_offsetF,j_coord_offsetG,j_coord_offsetH;
465     int              *iinr,*jindex,*jjnr,*shiftidx,*gid;
466     real             rcutoff_scalar;
467     real             *shiftvec,*fshift,*x,*f;
468     real             *fjptrA,*fjptrB,*fjptrC,*fjptrD,*fjptrE,*fjptrF,*fjptrG,*fjptrH;
469     real             scratch[4*DIM];
470     __m256           tx,ty,tz,fscal,rcutoff,rcutoff2,jidxall;
471     real *           vdwioffsetptr0;
472     __m256           ix0,iy0,iz0,fix0,fiy0,fiz0,iq0,isai0;
473     int              vdwjidx0A,vdwjidx0B,vdwjidx0C,vdwjidx0D,vdwjidx0E,vdwjidx0F,vdwjidx0G,vdwjidx0H;
474     __m256           jx0,jy0,jz0,fjx0,fjy0,fjz0,jq0,isaj0;
475     __m256           dx00,dy00,dz00,rsq00,rinv00,rinvsq00,r00,qq00,c6_00,c12_00;
476     __m256           velec,felec,velecsum,facel,crf,krf,krf2;
477     real             *charge;
478     int              nvdwtype;
479     __m256           rinvsix,rvdw,vvdw,vvdw6,vvdw12,fvdw,fvdw6,fvdw12,vvdwsum,sh_vdw_invrcut6;
480     int              *vdwtype;
481     real             *vdwparam;
482     __m256           one_sixth   = _mm256_set1_ps(1.0/6.0);
483     __m256           one_twelfth = _mm256_set1_ps(1.0/12.0);
484     __m256           dummy_mask,cutoff_mask;
485     __m256           signbit = _mm256_castsi256_ps( _mm256_set1_epi32(0x80000000) );
486     __m256           one     = _mm256_set1_ps(1.0);
487     __m256           two     = _mm256_set1_ps(2.0);
488     x                = xx[0];
489     f                = ff[0];
490
491     nri              = nlist->nri;
492     iinr             = nlist->iinr;
493     jindex           = nlist->jindex;
494     jjnr             = nlist->jjnr;
495     shiftidx         = nlist->shift;
496     gid              = nlist->gid;
497     shiftvec         = fr->shift_vec[0];
498     fshift           = fr->fshift[0];
499     facel            = _mm256_set1_ps(fr->ic->epsfac);
500     charge           = mdatoms->chargeA;
501     krf              = _mm256_set1_ps(fr->ic->k_rf);
502     krf2             = _mm256_set1_ps(fr->ic->k_rf*2.0);
503     crf              = _mm256_set1_ps(fr->ic->c_rf);
504     nvdwtype         = fr->ntype;
505     vdwparam         = fr->nbfp;
506     vdwtype          = mdatoms->typeA;
507
508     /* Avoid stupid compiler warnings */
509     jnrA = jnrB = jnrC = jnrD = jnrE = jnrF = jnrG = jnrH = 0;
510     j_coord_offsetA = 0;
511     j_coord_offsetB = 0;
512     j_coord_offsetC = 0;
513     j_coord_offsetD = 0;
514     j_coord_offsetE = 0;
515     j_coord_offsetF = 0;
516     j_coord_offsetG = 0;
517     j_coord_offsetH = 0;
518
519     outeriter        = 0;
520     inneriter        = 0;
521
522     for(iidx=0;iidx<4*DIM;iidx++)
523     {
524         scratch[iidx] = 0.0;
525     }
526
527     /* Start outer loop over neighborlists */
528     for(iidx=0; iidx<nri; iidx++)
529     {
530         /* Load shift vector for this list */
531         i_shift_offset   = DIM*shiftidx[iidx];
532
533         /* Load limits for loop over neighbors */
534         j_index_start    = jindex[iidx];
535         j_index_end      = jindex[iidx+1];
536
537         /* Get outer coordinate index */
538         inr              = iinr[iidx];
539         i_coord_offset   = DIM*inr;
540
541         /* Load i particle coords and add shift vector */
542         gmx_mm256_load_shift_and_1rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset,&ix0,&iy0,&iz0);
543
544         fix0             = _mm256_setzero_ps();
545         fiy0             = _mm256_setzero_ps();
546         fiz0             = _mm256_setzero_ps();
547
548         /* Load parameters for i particles */
549         iq0              = _mm256_mul_ps(facel,_mm256_set1_ps(charge[inr+0]));
550         vdwioffsetptr0   = vdwparam+2*nvdwtype*vdwtype[inr+0];
551
552         /* Start inner kernel loop */
553         for(jidx=j_index_start; jidx<j_index_end && jjnr[jidx+7]>=0; jidx+=8)
554         {
555
556             /* Get j neighbor index, and coordinate index */
557             jnrA             = jjnr[jidx];
558             jnrB             = jjnr[jidx+1];
559             jnrC             = jjnr[jidx+2];
560             jnrD             = jjnr[jidx+3];
561             jnrE             = jjnr[jidx+4];
562             jnrF             = jjnr[jidx+5];
563             jnrG             = jjnr[jidx+6];
564             jnrH             = jjnr[jidx+7];
565             j_coord_offsetA  = DIM*jnrA;
566             j_coord_offsetB  = DIM*jnrB;
567             j_coord_offsetC  = DIM*jnrC;
568             j_coord_offsetD  = DIM*jnrD;
569             j_coord_offsetE  = DIM*jnrE;
570             j_coord_offsetF  = DIM*jnrF;
571             j_coord_offsetG  = DIM*jnrG;
572             j_coord_offsetH  = DIM*jnrH;
573
574             /* load j atom coordinates */
575             gmx_mm256_load_1rvec_8ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
576                                                  x+j_coord_offsetC,x+j_coord_offsetD,
577                                                  x+j_coord_offsetE,x+j_coord_offsetF,
578                                                  x+j_coord_offsetG,x+j_coord_offsetH,
579                                                  &jx0,&jy0,&jz0);
580
581             /* Calculate displacement vector */
582             dx00             = _mm256_sub_ps(ix0,jx0);
583             dy00             = _mm256_sub_ps(iy0,jy0);
584             dz00             = _mm256_sub_ps(iz0,jz0);
585
586             /* Calculate squared distance and things based on it */
587             rsq00            = gmx_mm256_calc_rsq_ps(dx00,dy00,dz00);
588
589             rinv00           = avx256_invsqrt_f(rsq00);
590
591             rinvsq00         = _mm256_mul_ps(rinv00,rinv00);
592
593             /* Load parameters for j particles */
594             jq0              = gmx_mm256_load_8real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
595                                                                  charge+jnrC+0,charge+jnrD+0,
596                                                                  charge+jnrE+0,charge+jnrF+0,
597                                                                  charge+jnrG+0,charge+jnrH+0);
598             vdwjidx0A        = 2*vdwtype[jnrA+0];
599             vdwjidx0B        = 2*vdwtype[jnrB+0];
600             vdwjidx0C        = 2*vdwtype[jnrC+0];
601             vdwjidx0D        = 2*vdwtype[jnrD+0];
602             vdwjidx0E        = 2*vdwtype[jnrE+0];
603             vdwjidx0F        = 2*vdwtype[jnrF+0];
604             vdwjidx0G        = 2*vdwtype[jnrG+0];
605             vdwjidx0H        = 2*vdwtype[jnrH+0];
606
607             /**************************
608              * CALCULATE INTERACTIONS *
609              **************************/
610
611             /* Compute parameters for interactions between i and j atoms */
612             qq00             = _mm256_mul_ps(iq0,jq0);
613             gmx_mm256_load_8pair_swizzle_ps(vdwioffsetptr0+vdwjidx0A,
614                                             vdwioffsetptr0+vdwjidx0B,
615                                             vdwioffsetptr0+vdwjidx0C,
616                                             vdwioffsetptr0+vdwjidx0D,
617                                             vdwioffsetptr0+vdwjidx0E,
618                                             vdwioffsetptr0+vdwjidx0F,
619                                             vdwioffsetptr0+vdwjidx0G,
620                                             vdwioffsetptr0+vdwjidx0H,
621                                             &c6_00,&c12_00);
622
623             /* REACTION-FIELD ELECTROSTATICS */
624             felec            = _mm256_mul_ps(qq00,_mm256_sub_ps(_mm256_mul_ps(rinv00,rinvsq00),krf2));
625
626             /* LENNARD-JONES DISPERSION/REPULSION */
627
628             rinvsix          = _mm256_mul_ps(_mm256_mul_ps(rinvsq00,rinvsq00),rinvsq00);
629             fvdw             = _mm256_mul_ps(_mm256_sub_ps(_mm256_mul_ps(c12_00,rinvsix),c6_00),_mm256_mul_ps(rinvsix,rinvsq00));
630
631             fscal            = _mm256_add_ps(felec,fvdw);
632
633             /* Calculate temporary vectorial force */
634             tx               = _mm256_mul_ps(fscal,dx00);
635             ty               = _mm256_mul_ps(fscal,dy00);
636             tz               = _mm256_mul_ps(fscal,dz00);
637
638             /* Update vectorial force */
639             fix0             = _mm256_add_ps(fix0,tx);
640             fiy0             = _mm256_add_ps(fiy0,ty);
641             fiz0             = _mm256_add_ps(fiz0,tz);
642
643             fjptrA             = f+j_coord_offsetA;
644             fjptrB             = f+j_coord_offsetB;
645             fjptrC             = f+j_coord_offsetC;
646             fjptrD             = f+j_coord_offsetD;
647             fjptrE             = f+j_coord_offsetE;
648             fjptrF             = f+j_coord_offsetF;
649             fjptrG             = f+j_coord_offsetG;
650             fjptrH             = f+j_coord_offsetH;
651             gmx_mm256_decrement_1rvec_8ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjptrE,fjptrF,fjptrG,fjptrH,tx,ty,tz);
652
653             /* Inner loop uses 34 flops */
654         }
655
656         if(jidx<j_index_end)
657         {
658
659             /* Get j neighbor index, and coordinate index */
660             jnrlistA         = jjnr[jidx];
661             jnrlistB         = jjnr[jidx+1];
662             jnrlistC         = jjnr[jidx+2];
663             jnrlistD         = jjnr[jidx+3];
664             jnrlistE         = jjnr[jidx+4];
665             jnrlistF         = jjnr[jidx+5];
666             jnrlistG         = jjnr[jidx+6];
667             jnrlistH         = jjnr[jidx+7];
668             /* Sign of each element will be negative for non-real atoms.
669              * This mask will be 0xFFFFFFFF for dummy entries and 0x0 for real ones,
670              * so use it as val = _mm_andnot_ps(mask,val) to clear dummy entries.
671              */
672             dummy_mask = gmx_mm256_set_m128(gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx+4)),_mm_setzero_si128())),
673                                             gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx)),_mm_setzero_si128())));
674                                             
675             jnrA       = (jnrlistA>=0) ? jnrlistA : 0;
676             jnrB       = (jnrlistB>=0) ? jnrlistB : 0;
677             jnrC       = (jnrlistC>=0) ? jnrlistC : 0;
678             jnrD       = (jnrlistD>=0) ? jnrlistD : 0;
679             jnrE       = (jnrlistE>=0) ? jnrlistE : 0;
680             jnrF       = (jnrlistF>=0) ? jnrlistF : 0;
681             jnrG       = (jnrlistG>=0) ? jnrlistG : 0;
682             jnrH       = (jnrlistH>=0) ? jnrlistH : 0;
683             j_coord_offsetA  = DIM*jnrA;
684             j_coord_offsetB  = DIM*jnrB;
685             j_coord_offsetC  = DIM*jnrC;
686             j_coord_offsetD  = DIM*jnrD;
687             j_coord_offsetE  = DIM*jnrE;
688             j_coord_offsetF  = DIM*jnrF;
689             j_coord_offsetG  = DIM*jnrG;
690             j_coord_offsetH  = DIM*jnrH;
691
692             /* load j atom coordinates */
693             gmx_mm256_load_1rvec_8ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
694                                                  x+j_coord_offsetC,x+j_coord_offsetD,
695                                                  x+j_coord_offsetE,x+j_coord_offsetF,
696                                                  x+j_coord_offsetG,x+j_coord_offsetH,
697                                                  &jx0,&jy0,&jz0);
698
699             /* Calculate displacement vector */
700             dx00             = _mm256_sub_ps(ix0,jx0);
701             dy00             = _mm256_sub_ps(iy0,jy0);
702             dz00             = _mm256_sub_ps(iz0,jz0);
703
704             /* Calculate squared distance and things based on it */
705             rsq00            = gmx_mm256_calc_rsq_ps(dx00,dy00,dz00);
706
707             rinv00           = avx256_invsqrt_f(rsq00);
708
709             rinvsq00         = _mm256_mul_ps(rinv00,rinv00);
710
711             /* Load parameters for j particles */
712             jq0              = gmx_mm256_load_8real_swizzle_ps(charge+jnrA+0,charge+jnrB+0,
713                                                                  charge+jnrC+0,charge+jnrD+0,
714                                                                  charge+jnrE+0,charge+jnrF+0,
715                                                                  charge+jnrG+0,charge+jnrH+0);
716             vdwjidx0A        = 2*vdwtype[jnrA+0];
717             vdwjidx0B        = 2*vdwtype[jnrB+0];
718             vdwjidx0C        = 2*vdwtype[jnrC+0];
719             vdwjidx0D        = 2*vdwtype[jnrD+0];
720             vdwjidx0E        = 2*vdwtype[jnrE+0];
721             vdwjidx0F        = 2*vdwtype[jnrF+0];
722             vdwjidx0G        = 2*vdwtype[jnrG+0];
723             vdwjidx0H        = 2*vdwtype[jnrH+0];
724
725             /**************************
726              * CALCULATE INTERACTIONS *
727              **************************/
728
729             /* Compute parameters for interactions between i and j atoms */
730             qq00             = _mm256_mul_ps(iq0,jq0);
731             gmx_mm256_load_8pair_swizzle_ps(vdwioffsetptr0+vdwjidx0A,
732                                             vdwioffsetptr0+vdwjidx0B,
733                                             vdwioffsetptr0+vdwjidx0C,
734                                             vdwioffsetptr0+vdwjidx0D,
735                                             vdwioffsetptr0+vdwjidx0E,
736                                             vdwioffsetptr0+vdwjidx0F,
737                                             vdwioffsetptr0+vdwjidx0G,
738                                             vdwioffsetptr0+vdwjidx0H,
739                                             &c6_00,&c12_00);
740
741             /* REACTION-FIELD ELECTROSTATICS */
742             felec            = _mm256_mul_ps(qq00,_mm256_sub_ps(_mm256_mul_ps(rinv00,rinvsq00),krf2));
743
744             /* LENNARD-JONES DISPERSION/REPULSION */
745
746             rinvsix          = _mm256_mul_ps(_mm256_mul_ps(rinvsq00,rinvsq00),rinvsq00);
747             fvdw             = _mm256_mul_ps(_mm256_sub_ps(_mm256_mul_ps(c12_00,rinvsix),c6_00),_mm256_mul_ps(rinvsix,rinvsq00));
748
749             fscal            = _mm256_add_ps(felec,fvdw);
750
751             fscal            = _mm256_andnot_ps(dummy_mask,fscal);
752
753             /* Calculate temporary vectorial force */
754             tx               = _mm256_mul_ps(fscal,dx00);
755             ty               = _mm256_mul_ps(fscal,dy00);
756             tz               = _mm256_mul_ps(fscal,dz00);
757
758             /* Update vectorial force */
759             fix0             = _mm256_add_ps(fix0,tx);
760             fiy0             = _mm256_add_ps(fiy0,ty);
761             fiz0             = _mm256_add_ps(fiz0,tz);
762
763             fjptrA             = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
764             fjptrB             = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
765             fjptrC             = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
766             fjptrD             = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
767             fjptrE             = (jnrlistE>=0) ? f+j_coord_offsetE : scratch;
768             fjptrF             = (jnrlistF>=0) ? f+j_coord_offsetF : scratch;
769             fjptrG             = (jnrlistG>=0) ? f+j_coord_offsetG : scratch;
770             fjptrH             = (jnrlistH>=0) ? f+j_coord_offsetH : scratch;
771             gmx_mm256_decrement_1rvec_8ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjptrE,fjptrF,fjptrG,fjptrH,tx,ty,tz);
772
773             /* Inner loop uses 34 flops */
774         }
775
776         /* End of innermost loop */
777
778         gmx_mm256_update_iforce_1atom_swizzle_ps(fix0,fiy0,fiz0,
779                                                  f+i_coord_offset,fshift+i_shift_offset);
780
781         /* Increment number of inner iterations */
782         inneriter                  += j_index_end - j_index_start;
783
784         /* Outer loop uses 7 flops */
785     }
786
787     /* Increment number of outer iterations */
788     outeriter        += nri;
789
790     /* Update outer/inner flops */
791
792     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_VDW_F,outeriter*7 + inneriter*34);
793 }