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