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