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