3 * This file is part of the GROMACS molecular simulation package.
5 * Copyright (c) 2012,2013,2014,2015,2017, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
36 #error This file must be processed with the Gromacs pre-preprocessor
38 /* #if INCLUDE_HEADER */
45 #include "../nb_kernel.h"
46 #include "gromacs/gmxlib/nrnb.h"
48 #include "kernelutil_x86_avx_256_single.h"
51 /* ## List of variables set by the generating script: */
53 /* ## Setttings that apply to the entire kernel: */
54 /* ## KERNEL_ELEC: String, choice for electrostatic interactions */
55 /* ## KERNEL_VDW: String, choice for van der Waals interactions */
56 /* ## KERNEL_NAME: String, name of this kernel */
57 /* ## KERNEL_VF: String telling if we calculate potential, force, or both */
58 /* ## GEOMETRY_I/GEOMETRY_J: String, name of each geometry, e.g. 'Water3' or '1Particle' */
60 /* ## Setttings that apply to particles in the outer (I) or inner (J) loops: */
61 /* ## PARTICLES_I[]/ Arrays with lists of i/j particles to use in kernel. It is */
62 /* ## PARTICLES_J[]: just [0] for particle geometry, but can be longer for water */
63 /* ## PARTICLES_ELEC_I[]/ Arrays with lists of i/j particle that have electrostatics */
64 /* ## PARTICLES_ELEC_J[]: interactions that should be calculated in this kernel. */
65 /* ## PARTICLES_VDW_I[]/ Arrays with the list of i/j particle that have VdW */
66 /* ## PARTICLES_VDW_J[]: interactions that should be calculated in this kernel. */
68 /* ## Setttings for pairs of interactions (e.g. 2nd i particle against 1st j particle) */
69 /* ## PAIRS_IJ[]: Array with (i,j) tuples of pairs for which interactions */
70 /* ## should be calculated in this kernel. Zero-charge particles */
71 /* ## do not have interactions with particles without vdw, and */
72 /* ## Vdw-only interactions are not evaluated in a no-vdw-kernel. */
73 /* ## INTERACTION_FLAGS[][]: 2D matrix, dimension e.g. 3*3 for water-water interactions. */
74 /* ## For each i-j pair, the element [I][J] is a list of strings */
75 /* ## defining properties/flags of this interaction. Examples */
76 /* ## include 'electrostatics'/'vdw' if that type of interaction */
77 /* ## should be evaluated, 'rsq'/'rinv'/'rinvsq' if those values */
78 /* ## are needed, and 'exactcutoff' or 'shift','switch' to */
79 /* ## decide if the force/potential should be modified. This way */
80 /* ## we only calculate values absolutely needed for each case. */
82 /* ## Calculate the size and offset for (merged/interleaved) table data */
88 * Gromacs nonbonded kernel: {KERNEL_NAME}
89 * Electrostatics interaction: {KERNEL_ELEC}
90 * VdW interaction: {KERNEL_VDW}
91 * Geometry: {GEOMETRY_I}-{GEOMETRY_J}
92 * Calculate force/pot: {KERNEL_VF}
96 (t_nblist * gmx_restrict nlist,
97 rvec * gmx_restrict xx,
98 rvec * gmx_restrict ff,
99 struct t_forcerec * gmx_restrict fr,
100 t_mdatoms * gmx_restrict mdatoms,
101 nb_kernel_data_t gmx_unused * gmx_restrict kernel_data,
102 t_nrnb * gmx_restrict nrnb)
104 /* ## Not all variables are used for all kernels, but any optimizing compiler fixes that, */
105 /* ## so there is no point in going to extremes to exclude variables that are not needed. */
106 /* Suffixes 0,1,2,3 refer to particle indices for waters in the inner or outer loop, or
107 * just 0 for non-waters.
108 * Suffixes A,B,C,D,E,F,G,H refer to j loop unrolling done with AVX, e.g. for the eight different
109 * jnr indices corresponding to data put in the four positions in the SIMD register.
111 int i_shift_offset,i_coord_offset,outeriter,inneriter;
112 int j_index_start,j_index_end,jidx,nri,inr,ggid,iidx;
113 int jnrA,jnrB,jnrC,jnrD;
114 int jnrE,jnrF,jnrG,jnrH;
115 int jnrlistA,jnrlistB,jnrlistC,jnrlistD;
116 int jnrlistE,jnrlistF,jnrlistG,jnrlistH;
117 int j_coord_offsetA,j_coord_offsetB,j_coord_offsetC,j_coord_offsetD;
118 int j_coord_offsetE,j_coord_offsetF,j_coord_offsetG,j_coord_offsetH;
119 int *iinr,*jindex,*jjnr,*shiftidx,*gid;
121 real *shiftvec,*fshift,*x,*f;
122 real *fjptrA,*fjptrB,*fjptrC,*fjptrD,*fjptrE,*fjptrF,*fjptrG,*fjptrH;
124 __m256 tx,ty,tz,fscal,rcutoff,rcutoff2,jidxall;
125 /* #for I in PARTICLES_I */
126 real * vdwioffsetptr{I};
127 /* #if 'LJEwald' in KERNEL_VDW */
128 real * vdwgridioffsetptr{I};
130 __m256 ix{I},iy{I},iz{I},fix{I},fiy{I},fiz{I},iq{I},isai{I};
132 /* #for J in PARTICLES_J */
133 int vdwjidx{J}A,vdwjidx{J}B,vdwjidx{J}C,vdwjidx{J}D,vdwjidx{J}E,vdwjidx{J}F,vdwjidx{J}G,vdwjidx{J}H;
134 __m256 jx{J},jy{J},jz{J},fjx{J},fjy{J},fjz{J},jq{J},isaj{J};
136 /* #for I,J in PAIRS_IJ */
137 __m256 dx{I}{J},dy{I}{J},dz{I}{J},rsq{I}{J},rinv{I}{J},rinvsq{I}{J},r{I}{J},qq{I}{J},c6_{I}{J},c12_{I}{J};
139 /* #if KERNEL_ELEC != 'None' */
140 __m256 velec,felec,velecsum,facel,crf,krf,krf2;
143 /* #if 'GeneralizedBorn' in KERNEL_ELEC */
145 __m128i gbitab_lo,gbitab_hi;
146 __m256 vgb,fgb,vgbsum,dvdasum,gbscale,gbtabscale,isaprod,gbqqfactor,gbinvepsdiff,gbeps,dvdatmp;
147 __m256 minushalf = _mm256_set1_ps(-0.5);
148 real *invsqrta,*dvda,*gbtab;
150 /* #if KERNEL_VDW != 'None' */
152 __m256 rinvsix,rvdw,vvdw,vvdw6,vvdw12,fvdw,fvdw6,fvdw12,vvdwsum,sh_vdw_invrcut6;
155 __m256 one_sixth = _mm256_set1_ps(1.0/6.0);
156 __m256 one_twelfth = _mm256_set1_ps(1.0/12.0);
158 /* #if 'Table' in KERNEL_ELEC or 'GeneralizedBorn' in KERNEL_ELEC or 'Table' in KERNEL_VDW */
160 __m128i vfitab_lo,vfitab_hi;
161 __m128i ifour = _mm_set1_epi32(4);
162 __m256 rt,vfeps,vftabscale,Y,F,G,H,Heps,Fp,VV,FF;
165 /* #if 'LJEwald' in KERNEL_VDW */
166 /* #for I,J in PAIRS_IJ */
167 __m256 c6grid_{I}{J};
170 __m256 ewclj,ewclj2,ewclj6,ewcljrsq,poly,exponent,f6A,f6B,sh_lj_ewald;
171 __m256 one_half = _mm256_set1_ps(0.5);
172 __m256 minus_one = _mm256_set1_ps(-1.0);
174 /* #if 'Ewald' in KERNEL_ELEC */
176 __m128i ewitab_lo,ewitab_hi;
177 __m256 ewtabscale,eweps,sh_ewald,ewrt,ewtabhalfspace,ewtabF,ewtabFn,ewtabD,ewtabV;
178 __m256 beta,beta2,beta3,zeta2,pmecorrF,pmecorrV,rinv3;
181 /* #if 'PotentialSwitch' in [KERNEL_MOD_ELEC,KERNEL_MOD_VDW] */
182 __m256 rswitch,swV3,swV4,swV5,swF2,swF3,swF4,d,d2,sw,dsw;
183 real rswitch_scalar,d_scalar;
185 __m256 dummy_mask,cutoff_mask;
186 __m256 signbit = _mm256_castsi256_ps( _mm256_set1_epi32(0x80000000) );
187 __m256 one = _mm256_set1_ps(1.0);
188 __m256 two = _mm256_set1_ps(2.0);
194 jindex = nlist->jindex;
196 shiftidx = nlist->shift;
198 shiftvec = fr->shift_vec[0];
199 fshift = fr->fshift[0];
200 /* #if KERNEL_ELEC != 'None' */
201 facel = _mm256_set1_ps(fr->ic->epsfac);
202 charge = mdatoms->chargeA;
203 /* #if 'ReactionField' in KERNEL_ELEC */
204 krf = _mm256_set1_ps(fr->ic->k_rf);
205 krf2 = _mm256_set1_ps(fr->ic->k_rf*2.0);
206 crf = _mm256_set1_ps(fr->ic->c_rf);
209 /* #if KERNEL_VDW != 'None' */
210 nvdwtype = fr->ntype;
212 vdwtype = mdatoms->typeA;
214 /* #if 'LJEwald' in KERNEL_VDW */
215 vdwgridparam = fr->ljpme_c6grid;
216 sh_lj_ewald = _mm256_set1_ps(fr->ic->sh_lj_ewald);
217 ewclj = _mm256_set1_ps(fr->ic->ewaldcoeff_lj);
218 ewclj2 = _mm256_mul_ps(minus_one,_mm256_mul_ps(ewclj,ewclj));
221 /* #if 'Table' in KERNEL_ELEC and 'Table' in KERNEL_VDW */
222 vftab = kernel_data->table_elec_vdw->data;
223 vftabscale = _mm256_set1_ps(kernel_data->table_elec_vdw->scale);
224 /* #elif 'Table' in KERNEL_ELEC */
225 vftab = kernel_data->table_elec->data;
226 vftabscale = _mm256_set1_ps(kernel_data->table_elec->scale);
227 /* #elif 'Table' in KERNEL_VDW */
228 vftab = kernel_data->table_vdw->data;
229 vftabscale = _mm256_set1_ps(kernel_data->table_vdw->scale);
232 /* #if 'Ewald' in KERNEL_ELEC */
233 sh_ewald = _mm256_set1_ps(fr->ic->sh_ewald);
234 beta = _mm256_set1_ps(fr->ic->ewaldcoeff_q);
235 beta2 = _mm256_mul_ps(beta,beta);
236 beta3 = _mm256_mul_ps(beta,beta2);
238 /* #if KERNEL_VF=='Force' and KERNEL_MOD_ELEC!='PotentialSwitch' */
239 ewtab = fr->ic->tabq_coul_F;
240 ewtabscale = _mm256_set1_ps(fr->ic->tabq_scale);
241 ewtabhalfspace = _mm256_set1_ps(0.5/fr->ic->tabq_scale);
243 ewtab = fr->ic->tabq_coul_FDV0;
244 ewtabscale = _mm256_set1_ps(fr->ic->tabq_scale);
245 ewtabhalfspace = _mm256_set1_ps(0.5/fr->ic->tabq_scale);
249 /* #if KERNEL_ELEC=='GeneralizedBorn' */
250 invsqrta = fr->invsqrta;
252 gbtabscale = _mm256_set1_ps(fr->gbtab->scale);
253 gbtab = fr->gbtab->data;
254 gbinvepsdiff = _mm256_set1_ps((1.0/fr->ic->epsilon_r) - (1.0/fr->gb_epsilon_solvent));
257 /* #if 'Water' in GEOMETRY_I */
258 /* Setup water-specific parameters */
259 inr = nlist->iinr[0];
260 /* #for I in PARTICLES_ELEC_I */
261 iq{I} = _mm256_mul_ps(facel,_mm256_set1_ps(charge[inr+{I}]));
263 /* #for I in PARTICLES_VDW_I */
264 vdwioffsetptr{I} = vdwparam+2*nvdwtype*vdwtype[inr+{I}];
265 /* #if 'LJEwald' in KERNEL_VDW */
266 vdwgridioffsetptr{I} = vdwgridparam+2*nvdwtype*vdwtype[inr+{I}];
271 /* #if 'Water' in GEOMETRY_J */
272 /* #for J in PARTICLES_ELEC_J */
273 jq{J} = _mm256_set1_ps(charge[inr+{J}]);
275 /* #for J in PARTICLES_VDW_J */
276 vdwjidx{J}A = 2*vdwtype[inr+{J}];
278 /* #for I,J in PAIRS_IJ */
279 /* #if 'electrostatics' in INTERACTION_FLAGS[I][J] */
280 qq{I}{J} = _mm256_mul_ps(iq{I},jq{J});
282 /* #if 'vdw' in INTERACTION_FLAGS[I][J] */
283 /* #if 'LJEwald' in KERNEL_VDW */
284 c6_{I}{J} = _mm256_set1_ps(vdwioffsetptr{I}[vdwjidx{J}A]);
285 c12_{I}{J} = _mm256_set1_ps(vdwioffsetptr{I}[vdwjidx{J}A+1]);
286 c6grid_{I}{J} = _mm256_set1_ps(vdwgridioffsetptr{I}[vdwjidx{J}A]);
288 c6_{I}{J} = _mm256_set1_ps(vdwioffsetptr{I}[vdwjidx{J}A]);
289 c12_{I}{J} = _mm256_set1_ps(vdwioffsetptr{I}[vdwjidx{J}A+1]);
295 /* #if KERNEL_MOD_ELEC!='None' or KERNEL_MOD_VDW!='None' */
296 /* #if KERNEL_ELEC!='None' */
297 /* When we use explicit cutoffs the value must be identical for elec and VdW, so use elec as an arbitrary choice */
298 rcutoff_scalar = fr->ic->rcoulomb;
300 rcutoff_scalar = fr->ic->rvdw;
302 rcutoff = _mm256_set1_ps(rcutoff_scalar);
303 rcutoff2 = _mm256_mul_ps(rcutoff,rcutoff);
306 /* #if KERNEL_MOD_VDW=='PotentialShift' */
307 sh_vdw_invrcut6 = _mm256_set1_ps(fr->ic->sh_invrc6);
308 rvdw = _mm256_set1_ps(fr->ic->rvdw);
311 /* #if 'PotentialSwitch' in [KERNEL_MOD_ELEC,KERNEL_MOD_VDW] */
312 /* #if KERNEL_MOD_ELEC=='PotentialSwitch' */
313 rswitch_scalar = fr->ic->rcoulomb_switch;
314 rswitch = _mm256_set1_ps(rswitch_scalar);
316 rswitch_scalar = fr->ic->rvdw_switch;
317 rswitch = _mm256_set1_ps(rswitch_scalar);
319 /* Setup switch parameters */
320 d_scalar = rcutoff_scalar-rswitch_scalar;
321 d = _mm256_set1_ps(d_scalar);
322 swV3 = _mm256_set1_ps(-10.0/(d_scalar*d_scalar*d_scalar));
323 swV4 = _mm256_set1_ps( 15.0/(d_scalar*d_scalar*d_scalar*d_scalar));
324 swV5 = _mm256_set1_ps( -6.0/(d_scalar*d_scalar*d_scalar*d_scalar*d_scalar));
325 /* #if 'Force' in KERNEL_VF */
326 swF2 = _mm256_set1_ps(-30.0/(d_scalar*d_scalar*d_scalar));
327 swF3 = _mm256_set1_ps( 60.0/(d_scalar*d_scalar*d_scalar*d_scalar));
328 swF4 = _mm256_set1_ps(-30.0/(d_scalar*d_scalar*d_scalar*d_scalar*d_scalar));
332 /* Avoid stupid compiler warnings */
333 jnrA = jnrB = jnrC = jnrD = jnrE = jnrF = jnrG = jnrH = 0;
343 /* ## Keep track of the floating point operations we issue for reporting! */
344 /* #define OUTERFLOPS 0 */
348 for(iidx=0;iidx<4*DIM;iidx++)
353 /* Start outer loop over neighborlists */
354 for(iidx=0; iidx<nri; iidx++)
356 /* Load shift vector for this list */
357 i_shift_offset = DIM*shiftidx[iidx];
359 /* Load limits for loop over neighbors */
360 j_index_start = jindex[iidx];
361 j_index_end = jindex[iidx+1];
363 /* Get outer coordinate index */
365 i_coord_offset = DIM*inr;
367 /* Load i particle coords and add shift vector */
368 /* #if GEOMETRY_I == 'Particle' */
369 gmx_mm256_load_shift_and_1rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset,&ix0,&iy0,&iz0);
370 /* #elif GEOMETRY_I == 'Water3' */
371 gmx_mm256_load_shift_and_3rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset,
372 &ix0,&iy0,&iz0,&ix1,&iy1,&iz1,&ix2,&iy2,&iz2);
373 /* #elif GEOMETRY_I == 'Water4' */
374 /* #if 0 in PARTICLES_I */
375 gmx_mm256_load_shift_and_4rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset,
376 &ix0,&iy0,&iz0,&ix1,&iy1,&iz1,&ix2,&iy2,&iz2,&ix3,&iy3,&iz3);
378 gmx_mm256_load_shift_and_3rvec_broadcast_ps(shiftvec+i_shift_offset,x+i_coord_offset+DIM,
379 &ix1,&iy1,&iz1,&ix2,&iy2,&iz2,&ix3,&iy3,&iz3);
383 /* #if 'Force' in KERNEL_VF */
384 /* #for I in PARTICLES_I */
385 fix{I} = _mm256_setzero_ps();
386 fiy{I} = _mm256_setzero_ps();
387 fiz{I} = _mm256_setzero_ps();
391 /* ## For water we already preloaded parameters at the start of the kernel */
392 /* #if not 'Water' in GEOMETRY_I */
393 /* Load parameters for i particles */
394 /* #for I in PARTICLES_ELEC_I */
395 iq{I} = _mm256_mul_ps(facel,_mm256_set1_ps(charge[inr+{I}]));
396 /* #define OUTERFLOPS OUTERFLOPS+1 */
397 /* #if KERNEL_ELEC=='GeneralizedBorn' */
398 isai{I} = _mm256_set1_ps(invsqrta[inr+{I}]);
401 /* #for I in PARTICLES_VDW_I */
402 vdwioffsetptr{I} = vdwparam+2*nvdwtype*vdwtype[inr+{I}];
403 /* #if 'LJEwald' in KERNEL_VDW */
404 vdwgridioffsetptr{I} = vdwgridparam+2*nvdwtype*vdwtype[inr+{I}];
409 /* #if 'Potential' in KERNEL_VF */
410 /* Reset potential sums */
411 /* #if KERNEL_ELEC != 'None' */
412 velecsum = _mm256_setzero_ps();
414 /* #if 'GeneralizedBorn' in KERNEL_ELEC */
415 vgbsum = _mm256_setzero_ps();
417 /* #if KERNEL_VDW != 'None' */
418 vvdwsum = _mm256_setzero_ps();
421 /* #if 'GeneralizedBorn' in KERNEL_ELEC and 'Force' in KERNEL_VF */
422 dvdasum = _mm256_setzero_ps();
425 /* #for ROUND in ['Loop','Epilogue'] */
427 /* #if ROUND =='Loop' */
428 /* Start inner kernel loop */
429 for(jidx=j_index_start; jidx<j_index_end && jjnr[jidx+7]>=0; jidx+=8)
431 /* ## First round is normal loop (next statement resets indentation) */
438 /* ## Second round is epilogue */
440 /* #define INNERFLOPS 0 */
442 /* Get j neighbor index, and coordinate index */
443 /* #if ROUND =='Loop' */
453 jnrlistA = jjnr[jidx];
454 jnrlistB = jjnr[jidx+1];
455 jnrlistC = jjnr[jidx+2];
456 jnrlistD = jjnr[jidx+3];
457 jnrlistE = jjnr[jidx+4];
458 jnrlistF = jjnr[jidx+5];
459 jnrlistG = jjnr[jidx+6];
460 jnrlistH = jjnr[jidx+7];
461 /* Sign of each element will be negative for non-real atoms.
462 * This mask will be 0xFFFFFFFF for dummy entries and 0x0 for real ones,
463 * so use it as val = _mm_andnot_ps(mask,val) to clear dummy entries.
465 dummy_mask = gmx_mm256_set_m128(gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx+4)),_mm_setzero_si128())),
466 gmx_mm_castsi128_ps(_mm_cmplt_epi32(_mm_loadu_si128((const __m128i *)(jjnr+jidx)),_mm_setzero_si128())));
468 jnrA = (jnrlistA>=0) ? jnrlistA : 0;
469 jnrB = (jnrlistB>=0) ? jnrlistB : 0;
470 jnrC = (jnrlistC>=0) ? jnrlistC : 0;
471 jnrD = (jnrlistD>=0) ? jnrlistD : 0;
472 jnrE = (jnrlistE>=0) ? jnrlistE : 0;
473 jnrF = (jnrlistF>=0) ? jnrlistF : 0;
474 jnrG = (jnrlistG>=0) ? jnrlistG : 0;
475 jnrH = (jnrlistH>=0) ? jnrlistH : 0;
477 j_coord_offsetA = DIM*jnrA;
478 j_coord_offsetB = DIM*jnrB;
479 j_coord_offsetC = DIM*jnrC;
480 j_coord_offsetD = DIM*jnrD;
481 j_coord_offsetE = DIM*jnrE;
482 j_coord_offsetF = DIM*jnrF;
483 j_coord_offsetG = DIM*jnrG;
484 j_coord_offsetH = DIM*jnrH;
486 /* load j atom coordinates */
487 /* #if GEOMETRY_J == 'Particle' */
488 gmx_mm256_load_1rvec_8ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
489 x+j_coord_offsetC,x+j_coord_offsetD,
490 x+j_coord_offsetE,x+j_coord_offsetF,
491 x+j_coord_offsetG,x+j_coord_offsetH,
493 /* #elif GEOMETRY_J == 'Water3' */
494 gmx_mm256_load_3rvec_8ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
495 x+j_coord_offsetC,x+j_coord_offsetD,
496 x+j_coord_offsetE,x+j_coord_offsetF,
497 x+j_coord_offsetG,x+j_coord_offsetH,
498 &jx0,&jy0,&jz0,&jx1,&jy1,&jz1,&jx2,&jy2,&jz2);
499 /* #elif GEOMETRY_J == 'Water4' */
500 /* #if 0 in PARTICLES_J */
501 gmx_mm256_load_4rvec_8ptr_swizzle_ps(x+j_coord_offsetA,x+j_coord_offsetB,
502 x+j_coord_offsetC,x+j_coord_offsetD,
503 x+j_coord_offsetE,x+j_coord_offsetF,
504 x+j_coord_offsetG,x+j_coord_offsetH,
505 &jx0,&jy0,&jz0,&jx1,&jy1,&jz1,&jx2,
506 &jy2,&jz2,&jx3,&jy3,&jz3);
508 gmx_mm256_load_3rvec_8ptr_swizzle_ps(x+j_coord_offsetA+DIM,x+j_coord_offsetB+DIM,
509 x+j_coord_offsetC+DIM,x+j_coord_offsetD+DIM,
510 x+j_coord_offsetE+DIM,x+j_coord_offsetF+DIM,
511 x+j_coord_offsetG+DIM,x+j_coord_offsetH+DIM,
512 &jx1,&jy1,&jz1,&jx2,&jy2,&jz2,&jx3,&jy3,&jz3);
516 /* Calculate displacement vector */
517 /* #for I,J in PAIRS_IJ */
518 dx{I}{J} = _mm256_sub_ps(ix{I},jx{J});
519 dy{I}{J} = _mm256_sub_ps(iy{I},jy{J});
520 dz{I}{J} = _mm256_sub_ps(iz{I},jz{J});
521 /* #define INNERFLOPS INNERFLOPS+3 */
524 /* Calculate squared distance and things based on it */
525 /* #for I,J in PAIRS_IJ */
526 rsq{I}{J} = gmx_mm256_calc_rsq_ps(dx{I}{J},dy{I}{J},dz{I}{J});
527 /* #define INNERFLOPS INNERFLOPS+5 */
530 /* #for I,J in PAIRS_IJ */
531 /* #if 'rinv' in INTERACTION_FLAGS[I][J] */
532 rinv{I}{J} = avx256_invsqrt_f(rsq{I}{J});
533 /* #define INNERFLOPS INNERFLOPS+5 */
537 /* #for I,J in PAIRS_IJ */
538 /* #if 'rinvsq' in INTERACTION_FLAGS[I][J] */
539 /* # if 'rinv' not in INTERACTION_FLAGS[I][J] */
540 rinvsq{I}{J} = avx256_inv_f(rsq{I}{J});
541 /* #define INNERFLOPS INNERFLOPS+4 */
543 rinvsq{I}{J} = _mm256_mul_ps(rinv{I}{J},rinv{I}{J});
544 /* #define INNERFLOPS INNERFLOPS+1 */
549 /* #if not 'Water' in GEOMETRY_J */
550 /* Load parameters for j particles */
551 /* #for J in PARTICLES_ELEC_J */
552 jq{J} = gmx_mm256_load_8real_swizzle_ps(charge+jnrA+{J},charge+jnrB+{J},
553 charge+jnrC+{J},charge+jnrD+{J},
554 charge+jnrE+{J},charge+jnrF+{J},
555 charge+jnrG+{J},charge+jnrH+{J});
556 /* #if KERNEL_ELEC=='GeneralizedBorn' */
557 isaj{J} = gmx_mm256_load_8real_swizzle_ps(invsqrta+jnrA+{J},invsqrta+jnrB+{J},
558 invsqrta+jnrC+{J},invsqrta+jnrD+{J},
559 invsqrta+jnrE+{J},invsqrta+jnrF+{J},
560 invsqrta+jnrG+{J},invsqrta+jnrH+{J});
563 /* #for J in PARTICLES_VDW_J */
564 vdwjidx{J}A = 2*vdwtype[jnrA+{J}];
565 vdwjidx{J}B = 2*vdwtype[jnrB+{J}];
566 vdwjidx{J}C = 2*vdwtype[jnrC+{J}];
567 vdwjidx{J}D = 2*vdwtype[jnrD+{J}];
568 vdwjidx{J}E = 2*vdwtype[jnrE+{J}];
569 vdwjidx{J}F = 2*vdwtype[jnrF+{J}];
570 vdwjidx{J}G = 2*vdwtype[jnrG+{J}];
571 vdwjidx{J}H = 2*vdwtype[jnrH+{J}];
575 /* #if 'Force' in KERNEL_VF and not 'Particle' in GEOMETRY_I */
576 /* #for J in PARTICLES_J */
577 fjx{J} = _mm256_setzero_ps();
578 fjy{J} = _mm256_setzero_ps();
579 fjz{J} = _mm256_setzero_ps();
583 /* #for I,J in PAIRS_IJ */
585 /**************************
586 * CALCULATE INTERACTIONS *
587 **************************/
589 /* ## Note special check for TIP4P-TIP4P. Since we are cutting of all hydrogen interactions we also cut the LJ-only O-O interaction */
590 /* #if 'exactcutoff' in INTERACTION_FLAGS[I][J] or (GEOMETRY_I=='Water4' and GEOMETRY_J=='Water4' and 'exactcutoff' in INTERACTION_FLAGS[1][1]) */
591 /* ## We always calculate rinv/rinvsq above to enable pipelineing in compilers (performance tested on x86) */
592 if (gmx_mm256_any_lt(rsq{I}{J},rcutoff2))
594 /* #if 0 ## this and the next two lines is a hack to maintain auto-indentation in template file */
597 /* #define INNERFLOPS INNERFLOPS+1 */
600 /* #if 'r' in INTERACTION_FLAGS[I][J] */
601 r{I}{J} = _mm256_mul_ps(rsq{I}{J},rinv{I}{J});
602 /* #if ROUND == 'Epilogue' */
603 r{I}{J} = _mm256_andnot_ps(dummy_mask,r{I}{J});
604 /* #define INNERFLOPS INNERFLOPS+1 */
606 /* #define INNERFLOPS INNERFLOPS+1 */
609 /* ## For water geometries we already loaded parameters at the start of the kernel */
610 /* #if not 'Water' in GEOMETRY_J */
611 /* Compute parameters for interactions between i and j atoms */
612 /* #if 'electrostatics' in INTERACTION_FLAGS[I][J] */
613 qq{I}{J} = _mm256_mul_ps(iq{I},jq{J});
614 /* #define INNERFLOPS INNERFLOPS+1 */
616 /* #if 'vdw' in INTERACTION_FLAGS[I][J] */
617 gmx_mm256_load_8pair_swizzle_ps(vdwioffsetptr{I}+vdwjidx{J}A,
618 vdwioffsetptr{I}+vdwjidx{J}B,
619 vdwioffsetptr{I}+vdwjidx{J}C,
620 vdwioffsetptr{I}+vdwjidx{J}D,
621 vdwioffsetptr{I}+vdwjidx{J}E,
622 vdwioffsetptr{I}+vdwjidx{J}F,
623 vdwioffsetptr{I}+vdwjidx{J}G,
624 vdwioffsetptr{I}+vdwjidx{J}H,
625 &c6_{I}{J},&c12_{I}{J});
627 /* #if 'LJEwald' in KERNEL_VDW */
628 c6grid_{I}{J} = gmx_mm256_load_8real_swizzle_ps(vdwgridioffsetptr{I}+vdwjidx{J}A,
629 vdwgridioffsetptr{I}+vdwjidx{J}B,
630 vdwgridioffsetptr{I}+vdwjidx{J}C,
631 vdwgridioffsetptr{I}+vdwjidx{J}D,
632 vdwgridioffsetptr{I}+vdwjidx{J}E,
633 vdwgridioffsetptr{I}+vdwjidx{J}F,
634 vdwgridioffsetptr{I}+vdwjidx{J}G,
635 vdwgridioffsetptr{I}+vdwjidx{J}H);
640 /* #if 'table' in INTERACTION_FLAGS[I][J] */
641 /* Calculate table index by multiplying r with table scale and truncate to integer */
642 rt = _mm256_mul_ps(r{I}{J},vftabscale);
643 vfitab = _mm256_cvttps_epi32(rt);
644 vfeps = _mm256_sub_ps(rt,_mm256_round_ps(rt, _MM_FROUND_FLOOR));
645 /* #define INNERFLOPS INNERFLOPS+4 */
646 /* AVX1 does not support 256-bit integer operations, so now we go to 128-bit mode... */
647 vfitab_lo = _mm256_extractf128_si256(vfitab,0x0);
648 vfitab_hi = _mm256_extractf128_si256(vfitab,0x1);
649 /* #if 'Table' in KERNEL_ELEC and 'Table' in KERNEL_VDW */
650 /* ## 3 tables, 4 bytes per point: multiply index by 12 */
651 vfitab_lo = _mm_slli_epi32(_mm_add_epi32(vfitab_lo,_mm_slli_epi32(vfitab_lo,1)),2);
652 vfitab_hi = _mm_slli_epi32(_mm_add_epi32(vfitab_hi,_mm_slli_epi32(vfitab_hi,1)),2);
653 /* #elif 'Table' in KERNEL_ELEC */
654 /* ## 1 table, 4 bytes per point: multiply index by 4 */
655 vfitab_lo = _mm_slli_epi32(vfitab_lo,2);
656 vfitab_hi = _mm_slli_epi32(vfitab_hi,2);
657 /* #elif 'Table' in KERNEL_VDW */
658 /* ## 2 tables, 4 bytes per point: multiply index by 8 */
659 vfitab_lo = _mm_slli_epi32(vfitab_lo,3);
660 vfitab_hi = _mm_slli_epi32(vfitab_hi,3);
664 /* ## ELECTROSTATIC INTERACTIONS */
665 /* #if 'electrostatics' in INTERACTION_FLAGS[I][J] */
667 /* #if KERNEL_ELEC=='Coulomb' */
669 /* COULOMB ELECTROSTATICS */
670 velec = _mm256_mul_ps(qq{I}{J},rinv{I}{J});
671 /* #define INNERFLOPS INNERFLOPS+1 */
672 /* #if 'Force' in KERNEL_VF */
673 felec = _mm256_mul_ps(velec,rinvsq{I}{J});
674 /* #define INNERFLOPS INNERFLOPS+1 */
677 /* #elif KERNEL_ELEC=='ReactionField' */
679 /* REACTION-FIELD ELECTROSTATICS */
680 /* #if 'Potential' in KERNEL_VF */
681 velec = _mm256_mul_ps(qq{I}{J},_mm256_sub_ps(_mm256_add_ps(rinv{I}{J},_mm256_mul_ps(krf,rsq{I}{J})),crf));
682 /* #define INNERFLOPS INNERFLOPS+4 */
684 /* #if 'Force' in KERNEL_VF */
685 felec = _mm256_mul_ps(qq{I}{J},_mm256_sub_ps(_mm256_mul_ps(rinv{I}{J},rinvsq{I}{J}),krf2));
686 /* #define INNERFLOPS INNERFLOPS+3 */
689 /* #elif KERNEL_ELEC=='GeneralizedBorn' */
691 /* GENERALIZED BORN AND COULOMB ELECTROSTATICS */
692 isaprod = _mm256_mul_ps(isai{I},isaj{J});
693 gbqqfactor = _mm256_xor_ps(signbit,_mm256_mul_ps(qq{I}{J},_mm256_mul_ps(isaprod,gbinvepsdiff)));
694 gbscale = _mm256_mul_ps(isaprod,gbtabscale);
695 /* #define INNERFLOPS INNERFLOPS+5 */
697 /* Calculate generalized born table index - this is a separate table from the normal one,
698 * but we use the same procedure by multiplying r with scale and truncating to integer.
700 rt = _mm256_mul_ps(r{I}{J},gbscale);
701 gbitab = _mm256_cvttps_epi32(rt);
702 gbeps = _mm256_sub_ps(rt,_mm256_round_ps(rt, _MM_FROUND_FLOOR));
703 /* AVX1 does not support 256-bit integer operations, so now we go to 128-bit mode... */
704 gbitab_lo = _mm256_extractf128_si256(gbitab,0x0);
705 gbitab_hi = _mm256_extractf128_si256(gbitab,0x1);
706 gbitab_lo = _mm_slli_epi32(gbitab_lo,2);
707 gbitab_hi = _mm_slli_epi32(gbitab_hi,2);
708 Y = gmx_mm256_set_m128(_mm_load_ps(gbtab + _mm_extract_epi32(gbitab_hi,0)),
709 _mm_load_ps(gbtab + _mm_extract_epi32(gbitab_lo,0)));
710 F = gmx_mm256_set_m128(_mm_load_ps(gbtab + _mm_extract_epi32(gbitab_hi,1)),
711 _mm_load_ps(gbtab + _mm_extract_epi32(gbitab_lo,1)));
712 G = gmx_mm256_set_m128(_mm_load_ps(gbtab + _mm_extract_epi32(gbitab_hi,2)),
713 _mm_load_ps(gbtab + _mm_extract_epi32(gbitab_lo,2)));
714 H = gmx_mm256_set_m128(_mm_load_ps(gbtab + _mm_extract_epi32(gbitab_hi,3)),
715 _mm_load_ps(gbtab + _mm_extract_epi32(gbitab_lo,3)));
716 GMX_MM256_HALFTRANSPOSE4_PS(Y,F,G,H);
717 Heps = _mm256_mul_ps(gbeps,H);
718 Fp = _mm256_add_ps(F,_mm256_mul_ps(gbeps,_mm256_add_ps(G,Heps)));
719 VV = _mm256_add_ps(Y,_mm256_mul_ps(gbeps,Fp));
720 vgb = _mm256_mul_ps(gbqqfactor,VV);
721 /* #define INNERFLOPS INNERFLOPS+10 */
723 /* #if 'Force' in KERNEL_VF */
724 FF = _mm256_add_ps(Fp,_mm256_mul_ps(gbeps,_mm256_add_ps(G,_mm256_add_ps(Heps,Heps))));
725 fgb = _mm256_mul_ps(gbqqfactor,_mm256_mul_ps(FF,gbscale));
726 dvdatmp = _mm256_mul_ps(minushalf,_mm256_add_ps(vgb,_mm256_mul_ps(fgb,r{I}{J})));
727 /* #if ROUND == 'Epilogue' */
728 dvdatmp = _mm256_andnot_ps(dummy_mask,dvdatmp);
730 dvdasum = _mm256_add_ps(dvdasum,dvdatmp);
731 /* #if ROUND == 'Loop' */
741 /* The pointers to scratch make sure that this code with compilers that take gmx_restrict seriously (e.g. icc 13) really can't screw things up. */
742 fjptrA = (jnrlistA>=0) ? dvda+jnrA : scratch;
743 fjptrB = (jnrlistB>=0) ? dvda+jnrB : scratch;
744 fjptrC = (jnrlistC>=0) ? dvda+jnrC : scratch;
745 fjptrD = (jnrlistD>=0) ? dvda+jnrD : scratch;
746 fjptrE = (jnrlistE>=0) ? dvda+jnrE : scratch;
747 fjptrF = (jnrlistF>=0) ? dvda+jnrF : scratch;
748 fjptrG = (jnrlistG>=0) ? dvda+jnrG : scratch;
749 fjptrH = (jnrlistH>=0) ? dvda+jnrH : scratch;
751 gmx_mm256_increment_8real_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjptrE,fjptrF,fjptrG,fjptrH,
752 _mm256_mul_ps(dvdatmp,_mm256_mul_ps(isaj{J},isaj{J})));
753 /* #define INNERFLOPS INNERFLOPS+12 */
755 velec = _mm256_mul_ps(qq{I}{J},rinv{I}{J});
756 /* #define INNERFLOPS INNERFLOPS+1 */
757 /* #if 'Force' in KERNEL_VF */
758 felec = _mm256_mul_ps(_mm256_sub_ps(_mm256_mul_ps(velec,rinv{I}{J}),fgb),rinv{I}{J});
759 /* #define INNERFLOPS INNERFLOPS+3 */
762 /* #elif KERNEL_ELEC=='Ewald' */
763 /* EWALD ELECTROSTATICS */
765 /* Analytical PME correction */
766 zeta2 = _mm256_mul_ps(beta2,rsq{I}{J});
767 /* #if 'Force' in KERNEL_VF */
768 rinv3 = _mm256_mul_ps(rinvsq{I}{J},rinv{I}{J});
769 pmecorrF = avx256_pmecorrF_f(zeta2);
770 felec = _mm256_add_ps( _mm256_mul_ps(pmecorrF,beta3), rinv3);
771 felec = _mm256_mul_ps(qq{I}{J},felec);
772 /* #define INNERFLOPS INNERFLOPS+31 */
774 /* #if 'Potential' in KERNEL_VF or KERNEL_MOD_ELEC=='PotentialSwitch' */
775 pmecorrV = avx256_pmecorrV_f(zeta2);
776 pmecorrV = _mm256_mul_ps(pmecorrV,beta);
777 /* #define INNERFLOPS INNERFLOPS+27 */
778 /* #if KERNEL_MOD_ELEC=='PotentialShift' */
779 velec = _mm256_sub_ps(_mm256_sub_ps(rinv{I}{J},sh_ewald),pmecorrV);
780 /* #define INNERFLOPS INNERFLOPS+21 */
782 velec = _mm256_sub_ps(rinv{I}{J},pmecorrV);
784 velec = _mm256_mul_ps(qq{I}{J},velec);
787 /* #elif KERNEL_ELEC=='CubicSplineTable' */
789 /* CUBIC SPLINE TABLE ELECTROSTATICS */
790 Y = gmx_mm256_set_m128(_mm_load_ps(vftab + _mm_extract_epi32(vfitab_hi,0)),
791 _mm_load_ps(vftab + _mm_extract_epi32(vfitab_lo,0)));
792 F = gmx_mm256_set_m128(_mm_load_ps(vftab + _mm_extract_epi32(vfitab_hi,1)),
793 _mm_load_ps(vftab + _mm_extract_epi32(vfitab_lo,1)));
794 G = gmx_mm256_set_m128(_mm_load_ps(vftab + _mm_extract_epi32(vfitab_hi,2)),
795 _mm_load_ps(vftab + _mm_extract_epi32(vfitab_lo,2)));
796 H = gmx_mm256_set_m128(_mm_load_ps(vftab + _mm_extract_epi32(vfitab_hi,3)),
797 _mm_load_ps(vftab + _mm_extract_epi32(vfitab_lo,3)));
798 GMX_MM256_HALFTRANSPOSE4_PS(Y,F,G,H);
799 Heps = _mm256_mul_ps(vfeps,H);
800 Fp = _mm256_add_ps(F,_mm256_mul_ps(vfeps,_mm256_add_ps(G,Heps)));
801 /* #define INNERFLOPS INNERFLOPS+4 */
802 /* #if 'Potential' in KERNEL_VF */
803 VV = _mm256_add_ps(Y,_mm256_mul_ps(vfeps,Fp));
804 velec = _mm256_mul_ps(qq{I}{J},VV);
805 /* #define INNERFLOPS INNERFLOPS+3 */
807 /* #if 'Force' in KERNEL_VF */
808 FF = _mm256_add_ps(Fp,_mm256_mul_ps(vfeps,_mm256_add_ps(G,_mm256_add_ps(Heps,Heps))));
809 felec = _mm256_xor_ps(signbit,_mm256_mul_ps(_mm256_mul_ps(qq{I}{J},FF),_mm256_mul_ps(vftabscale,rinv{I}{J})));
810 /* #define INNERFLOPS INNERFLOPS+7 */
813 /* ## End of check for electrostatics interaction forms */
815 /* ## END OF ELECTROSTATIC INTERACTION CHECK FOR PAIR I-J */
817 /* #if 'vdw' in INTERACTION_FLAGS[I][J] */
819 /* #if KERNEL_VDW=='LennardJones' */
821 /* LENNARD-JONES DISPERSION/REPULSION */
823 rinvsix = _mm256_mul_ps(_mm256_mul_ps(rinvsq{I}{J},rinvsq{I}{J}),rinvsq{I}{J});
824 /* #define INNERFLOPS INNERFLOPS+2 */
825 /* #if 'Potential' in KERNEL_VF or KERNEL_MOD_VDW=='PotentialSwitch' */
826 vvdw6 = _mm256_mul_ps(c6_{I}{J},rinvsix);
827 vvdw12 = _mm256_mul_ps(c12_{I}{J},_mm256_mul_ps(rinvsix,rinvsix));
828 /* #define INNERFLOPS INNERFLOPS+3 */
829 /* #if KERNEL_MOD_VDW=='PotentialShift' */
830 vvdw = _mm256_sub_ps(_mm256_mul_ps( _mm256_sub_ps(vvdw12 , _mm256_mul_ps(c12_{I}{J},_mm256_mul_ps(sh_vdw_invrcut6,sh_vdw_invrcut6))), one_twelfth) ,
831 _mm256_mul_ps( _mm256_sub_ps(vvdw6,_mm256_mul_ps(c6_{I}{J},sh_vdw_invrcut6)),one_sixth));
832 /* #define INNERFLOPS INNERFLOPS+8 */
834 vvdw = _mm256_sub_ps( _mm256_mul_ps(vvdw12,one_twelfth) , _mm256_mul_ps(vvdw6,one_sixth) );
835 /* #define INNERFLOPS INNERFLOPS+3 */
837 /* ## Check for force inside potential check, i.e. this means we already did the potential part */
838 /* #if 'Force' in KERNEL_VF */
839 fvdw = _mm256_mul_ps(_mm256_sub_ps(vvdw12,vvdw6),rinvsq{I}{J});
840 /* #define INNERFLOPS INNERFLOPS+2 */
842 /* #elif KERNEL_VF=='Force' */
843 /* ## Force-only LennardJones makes it possible to save 1 flop (they do add up...) */
844 fvdw = _mm256_mul_ps(_mm256_sub_ps(_mm256_mul_ps(c12_{I}{J},rinvsix),c6_{I}{J}),_mm256_mul_ps(rinvsix,rinvsq{I}{J}));
845 /* #define INNERFLOPS INNERFLOPS+4 */
848 /* #elif KERNEL_VDW=='CubicSplineTable' */
850 /* CUBIC SPLINE TABLE DISPERSION */
851 /* #if 'Table' in KERNEL_ELEC */
852 vfitab_lo = _mm_add_epi32(vfitab_lo,ifour);
853 vfitab_hi = _mm_add_epi32(vfitab_hi,ifour);
855 Y = gmx_mm256_set_m128(_mm_load_ps(vftab + _mm_extract_epi32(vfitab_hi,0)),
856 _mm_load_ps(vftab + _mm_extract_epi32(vfitab_lo,0)));
857 F = gmx_mm256_set_m128(_mm_load_ps(vftab + _mm_extract_epi32(vfitab_hi,1)),
858 _mm_load_ps(vftab + _mm_extract_epi32(vfitab_lo,1)));
859 G = gmx_mm256_set_m128(_mm_load_ps(vftab + _mm_extract_epi32(vfitab_hi,2)),
860 _mm_load_ps(vftab + _mm_extract_epi32(vfitab_lo,2)));
861 H = gmx_mm256_set_m128(_mm_load_ps(vftab + _mm_extract_epi32(vfitab_hi,3)),
862 _mm_load_ps(vftab + _mm_extract_epi32(vfitab_lo,3)));
863 GMX_MM256_HALFTRANSPOSE4_PS(Y,F,G,H);
864 Heps = _mm256_mul_ps(vfeps,H);
865 Fp = _mm256_add_ps(F,_mm256_mul_ps(vfeps,_mm256_add_ps(G,Heps)));
866 /* #define INNERFLOPS INNERFLOPS+4 */
867 /* #if 'Potential' in KERNEL_VF */
868 VV = _mm256_add_ps(Y,_mm256_mul_ps(vfeps,Fp));
869 vvdw6 = _mm256_mul_ps(c6_{I}{J},VV);
870 /* #define INNERFLOPS INNERFLOPS+3 */
872 /* #if 'Force' in KERNEL_VF */
873 FF = _mm256_add_ps(Fp,_mm256_mul_ps(vfeps,_mm256_add_ps(G,_mm256_add_ps(Heps,Heps))));
874 fvdw6 = _mm256_mul_ps(c6_{I}{J},FF);
875 /* #define INNERFLOPS INNERFLOPS+4 */
878 /* CUBIC SPLINE TABLE REPULSION */
879 vfitab_lo = _mm_add_epi32(vfitab_lo,ifour);
880 vfitab_hi = _mm_add_epi32(vfitab_hi,ifour);
881 Y = gmx_mm256_set_m128(_mm_load_ps(vftab + _mm_extract_epi32(vfitab_hi,0)),
882 _mm_load_ps(vftab + _mm_extract_epi32(vfitab_lo,0)));
883 F = gmx_mm256_set_m128(_mm_load_ps(vftab + _mm_extract_epi32(vfitab_hi,1)),
884 _mm_load_ps(vftab + _mm_extract_epi32(vfitab_lo,1)));
885 G = gmx_mm256_set_m128(_mm_load_ps(vftab + _mm_extract_epi32(vfitab_hi,2)),
886 _mm_load_ps(vftab + _mm_extract_epi32(vfitab_lo,2)));
887 H = gmx_mm256_set_m128(_mm_load_ps(vftab + _mm_extract_epi32(vfitab_hi,3)),
888 _mm_load_ps(vftab + _mm_extract_epi32(vfitab_lo,3)));
889 GMX_MM256_HALFTRANSPOSE4_PS(Y,F,G,H);
890 Heps = _mm256_mul_ps(vfeps,H);
891 Fp = _mm256_add_ps(F,_mm256_mul_ps(vfeps,_mm256_add_ps(G,Heps)));
892 /* #define INNERFLOPS INNERFLOPS+4 */
893 /* #if 'Potential' in KERNEL_VF */
894 VV = _mm256_add_ps(Y,_mm256_mul_ps(vfeps,Fp));
895 vvdw12 = _mm256_mul_ps(c12_{I}{J},VV);
896 /* #define INNERFLOPS INNERFLOPS+3 */
898 /* #if 'Force' in KERNEL_VF */
899 FF = _mm256_add_ps(Fp,_mm256_mul_ps(vfeps,_mm256_add_ps(G,_mm256_add_ps(Heps,Heps))));
900 fvdw12 = _mm256_mul_ps(c12_{I}{J},FF);
901 /* #define INNERFLOPS INNERFLOPS+5 */
903 /* #if 'Potential' in KERNEL_VF */
904 vvdw = _mm256_add_ps(vvdw12,vvdw6);
905 /* #define INNERFLOPS INNERFLOPS+1 */
907 /* #if 'Force' in KERNEL_VF */
908 fvdw = _mm256_xor_ps(signbit,_mm256_mul_ps(_mm256_add_ps(fvdw6,fvdw12),_mm256_mul_ps(vftabscale,rinv{I}{J})));
909 /* #define INNERFLOPS INNERFLOPS+4 */
912 /* #elif KERNEL_VDW=='LJEwald' */
914 /* Analytical LJ-PME */
915 rinvsix = _mm256_mul_ps(_mm256_mul_ps(rinvsq{I}{J},rinvsq{I}{J}),rinvsq{I}{J});
916 ewcljrsq = _mm256_mul_ps(ewclj2,rsq{I}{J});
917 ewclj6 = _mm256_mul_ps(ewclj2,_mm256_mul_ps(ewclj2,ewclj2));
918 exponent = avx256_exp_f(ewcljrsq);
919 /* poly = exp(-(beta*r)^2) * (1 + (beta*r)^2 + (beta*r)^4 /2) */
920 poly = _mm256_mul_ps(exponent,_mm256_add_ps(_mm256_sub_ps(one,ewcljrsq),_mm256_mul_ps(_mm256_mul_ps(ewcljrsq,ewcljrsq),one_half)));
921 /* #define INNERFLOPS INNERFLOPS+11 */
922 /* #if 'Potential' in KERNEL_VF or KERNEL_MOD_VDW=='PotentialSwitch' */
923 /* vvdw6 = [C6 - C6grid * (1-poly)]/r6 */
924 vvdw6 = _mm256_mul_ps(_mm256_sub_ps(c6_{I}{J},_mm256_mul_ps(c6grid_{I}{J},_mm256_sub_ps(one,poly))),rinvsix);
925 vvdw12 = _mm256_mul_ps(c12_{I}{J},_mm256_mul_ps(rinvsix,rinvsix));
926 /* #define INNERFLOPS INNERFLOPS+6 */
927 /* #if KERNEL_MOD_VDW=='PotentialShift' */
928 vvdw = _mm256_sub_ps(_mm256_mul_ps( _mm256_sub_ps(vvdw12 , _mm256_mul_ps(c12_{I}{J},_mm256_mul_ps(sh_vdw_invrcut6,sh_vdw_invrcut6))), one_twelfth) ,
929 _mm256_mul_ps( _mm256_sub_ps(vvdw6,_mm256_add_ps(_mm256_mul_ps(c6_{I}{J},sh_vdw_invrcut6),_mm256_mul_ps(c6grid_{I}{J},sh_lj_ewald))),one_sixth));
930 /* #define INNERFLOPS INNERFLOPS+10 */
932 vvdw = _mm256_sub_ps(_mm256_mul_ps(vvdw12,one_twelfth),_mm256_mul_ps(vvdw6,one_sixth));
933 /* #define INNERFLOPS INNERFLOPS+3 */
935 /* ## Check for force inside potential check, i.e. this means we already did the potential part */
936 /* #if 'Force' in KERNEL_VF */
937 /* fvdw = vvdw12/r - (vvdw6/r + (C6grid * exponent * beta^6)/r) */
938 fvdw = _mm256_mul_ps(_mm256_sub_ps(vvdw12,_mm256_sub_ps(vvdw6,_mm256_mul_ps(_mm256_mul_ps(c6grid_{I}{J},one_sixth),_mm256_mul_ps(exponent,ewclj6)))),rinvsq{I}{J});
939 /* #define INNERFLOPS INNERFLOPS+6 */
941 /* #elif KERNEL_VF=='Force' */
942 /* f6A = 6 * C6grid * (1 - poly) */
943 f6A = _mm256_mul_ps(c6grid_{I}{J},_mm256_sub_ps(one,poly));
944 /* f6B = C6grid * exponent * beta^6 */
945 f6B = _mm256_mul_ps(_mm256_mul_ps(c6grid_{I}{J},one_sixth),_mm256_mul_ps(exponent,ewclj6));
946 /* fvdw = 12*C12/r13 - ((6*C6 - f6A)/r6 + f6B)/r */
947 fvdw = _mm256_mul_ps(_mm256_add_ps(_mm256_mul_ps(_mm256_sub_ps(_mm256_mul_ps(c12_{I}{J},rinvsix),_mm256_sub_ps(c6_{I}{J},f6A)),rinvsix),f6B),rinvsq{I}{J});
948 /* #define INNERFLOPS INNERFLOPS+11 */
951 /* ## End of check for vdw interaction forms */
953 /* ## END OF VDW INTERACTION CHECK FOR PAIR I-J */
955 /* #if 'switch' in INTERACTION_FLAGS[I][J] */
956 d = _mm256_sub_ps(r{I}{J},rswitch);
957 d = _mm256_max_ps(d,_mm256_setzero_ps());
958 d2 = _mm256_mul_ps(d,d);
959 sw = _mm256_add_ps(one,_mm256_mul_ps(d2,_mm256_mul_ps(d,_mm256_add_ps(swV3,_mm256_mul_ps(d,_mm256_add_ps(swV4,_mm256_mul_ps(d,swV5)))))));
960 /* #define INNERFLOPS INNERFLOPS+10 */
962 /* #if 'Force' in KERNEL_VF */
963 dsw = _mm256_mul_ps(d2,_mm256_add_ps(swF2,_mm256_mul_ps(d,_mm256_add_ps(swF3,_mm256_mul_ps(d,swF4)))));
964 /* #define INNERFLOPS INNERFLOPS+5 */
967 /* Evaluate switch function */
968 /* #if 'Force' in KERNEL_VF */
969 /* fscal'=f'/r=-(v*sw)'/r=-(v'*sw+v*dsw)/r=-v'*sw/r-v*dsw/r=fscal*sw-v*dsw/r */
970 /* #if 'electrostatics' in INTERACTION_FLAGS[I][J] and KERNEL_MOD_ELEC=='PotentialSwitch' */
971 felec = _mm256_sub_ps( _mm256_mul_ps(felec,sw) , _mm256_mul_ps(rinv{I}{J},_mm256_mul_ps(velec,dsw)) );
972 /* #define INNERFLOPS INNERFLOPS+4 */
974 /* #if 'vdw' in INTERACTION_FLAGS[I][J] and KERNEL_MOD_VDW=='PotentialSwitch' */
975 fvdw = _mm256_sub_ps( _mm256_mul_ps(fvdw,sw) , _mm256_mul_ps(rinv{I}{J},_mm256_mul_ps(vvdw,dsw)) );
976 /* #define INNERFLOPS INNERFLOPS+4 */
979 /* #if 'Potential' in KERNEL_VF */
980 /* #if 'electrostatics' in INTERACTION_FLAGS[I][J] and KERNEL_MOD_ELEC=='PotentialSwitch' */
981 velec = _mm256_mul_ps(velec,sw);
982 /* #define INNERFLOPS INNERFLOPS+1 */
984 /* #if 'vdw' in INTERACTION_FLAGS[I][J] and KERNEL_MOD_VDW=='PotentialSwitch' */
985 vvdw = _mm256_mul_ps(vvdw,sw);
986 /* #define INNERFLOPS INNERFLOPS+1 */
990 /* ## Note special check for TIP4P-TIP4P. Since we are cutting of all hydrogen interactions we also cut the LJ-only O-O interaction */
991 /* #if 'exactcutoff' in INTERACTION_FLAGS[I][J] or (GEOMETRY_I=='Water4' and GEOMETRY_J=='Water4' and 'exactcutoff' in INTERACTION_FLAGS[1][1]) */
992 cutoff_mask = _mm256_cmp_ps(rsq{I}{J},rcutoff2,_CMP_LT_OQ);
993 /* #define INNERFLOPS INNERFLOPS+1 */
996 /* #if 'Potential' in KERNEL_VF */
997 /* Update potential sum for this i atom from the interaction with this j atom. */
998 /* #if 'electrostatics' in INTERACTION_FLAGS[I][J] */
999 /* #if 'exactcutoff' in INTERACTION_FLAGS[I][J] */
1000 velec = _mm256_and_ps(velec,cutoff_mask);
1001 /* #define INNERFLOPS INNERFLOPS+1 */
1003 /* #if ROUND == 'Epilogue' */
1004 velec = _mm256_andnot_ps(dummy_mask,velec);
1006 velecsum = _mm256_add_ps(velecsum,velec);
1007 /* #define INNERFLOPS INNERFLOPS+1 */
1008 /* #if KERNEL_ELEC=='GeneralizedBorn' */
1009 /* #if 'exactcutoff' in INTERACTION_FLAGS[I][J] */
1010 vgb = _mm256_and_ps(vgb,cutoff_mask);
1011 /* #define INNERFLOPS INNERFLOPS+1 */
1013 /* #if ROUND == 'Epilogue' */
1014 vgb = _mm256_andnot_ps(dummy_mask,vgb);
1016 vgbsum = _mm256_add_ps(vgbsum,vgb);
1017 /* #define INNERFLOPS INNERFLOPS+1 */
1020 /* #if 'vdw' in INTERACTION_FLAGS[I][J] */
1021 /* ## Note special check for TIP4P-TIP4P. Since we are cutting of all hydrogen interactions we also cut the LJ-only O-O interaction */
1022 /* #if 'exactcutoff' in INTERACTION_FLAGS[I][J] or (GEOMETRY_I=='Water4' and GEOMETRY_J=='Water4' and 'exactcutoff' in INTERACTION_FLAGS[1][1]) */
1023 vvdw = _mm256_and_ps(vvdw,cutoff_mask);
1024 /* #define INNERFLOPS INNERFLOPS+1 */
1026 /* #if ROUND == 'Epilogue' */
1027 vvdw = _mm256_andnot_ps(dummy_mask,vvdw);
1029 vvdwsum = _mm256_add_ps(vvdwsum,vvdw);
1030 /* #define INNERFLOPS INNERFLOPS+1 */
1034 /* #if 'Force' in KERNEL_VF */
1036 /* #if 'electrostatics' in INTERACTION_FLAGS[I][J] and 'vdw' in INTERACTION_FLAGS[I][J] */
1037 fscal = _mm256_add_ps(felec,fvdw);
1038 /* #define INNERFLOPS INNERFLOPS+1 */
1039 /* #elif 'electrostatics' in INTERACTION_FLAGS[I][J] */
1041 /* #elif 'vdw' in INTERACTION_FLAGS[I][J] */
1045 /* ## Note special check for TIP4P-TIP4P. Since we are cutting of all hydrogen interactions we also cut the LJ-only O-O interaction */
1046 /* #if 'exactcutoff' in INTERACTION_FLAGS[I][J] or (GEOMETRY_I=='Water4' and GEOMETRY_J=='Water4' and 'exactcutoff' in INTERACTION_FLAGS[1][1]) */
1047 fscal = _mm256_and_ps(fscal,cutoff_mask);
1048 /* #define INNERFLOPS INNERFLOPS+1 */
1051 /* #if ROUND == 'Epilogue' */
1052 fscal = _mm256_andnot_ps(dummy_mask,fscal);
1055 /* Calculate temporary vectorial force */
1056 tx = _mm256_mul_ps(fscal,dx{I}{J});
1057 ty = _mm256_mul_ps(fscal,dy{I}{J});
1058 tz = _mm256_mul_ps(fscal,dz{I}{J});
1060 /* Update vectorial force */
1061 fix{I} = _mm256_add_ps(fix{I},tx);
1062 fiy{I} = _mm256_add_ps(fiy{I},ty);
1063 fiz{I} = _mm256_add_ps(fiz{I},tz);
1064 /* #define INNERFLOPS INNERFLOPS+6 */
1066 /* #if GEOMETRY_I == 'Particle' */
1067 /* #if ROUND == 'Loop' */
1068 fjptrA = f+j_coord_offsetA;
1069 fjptrB = f+j_coord_offsetB;
1070 fjptrC = f+j_coord_offsetC;
1071 fjptrD = f+j_coord_offsetD;
1072 fjptrE = f+j_coord_offsetE;
1073 fjptrF = f+j_coord_offsetF;
1074 fjptrG = f+j_coord_offsetG;
1075 fjptrH = f+j_coord_offsetH;
1077 fjptrA = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
1078 fjptrB = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
1079 fjptrC = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
1080 fjptrD = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
1081 fjptrE = (jnrlistE>=0) ? f+j_coord_offsetE : scratch;
1082 fjptrF = (jnrlistF>=0) ? f+j_coord_offsetF : scratch;
1083 fjptrG = (jnrlistG>=0) ? f+j_coord_offsetG : scratch;
1084 fjptrH = (jnrlistH>=0) ? f+j_coord_offsetH : scratch;
1086 gmx_mm256_decrement_1rvec_8ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjptrE,fjptrF,fjptrG,fjptrH,tx,ty,tz);
1087 /* #define INNERFLOPS INNERFLOPS+3 */
1089 fjx{J} = _mm256_add_ps(fjx{J},tx);
1090 fjy{J} = _mm256_add_ps(fjy{J},ty);
1091 fjz{J} = _mm256_add_ps(fjz{J},tz);
1092 /* #define INNERFLOPS INNERFLOPS+3 */
1097 /* ## Note special check for TIP4P-TIP4P. Since we are cutting of all hydrogen interactions we also cut the LJ-only O-O interaction */
1098 /* #if 'exactcutoff' in INTERACTION_FLAGS[I][J] or (GEOMETRY_I=='Water4' and GEOMETRY_J=='Water4' and 'exactcutoff' in INTERACTION_FLAGS[1][1]) */
1099 /* #if 0 ## This and next two lines is a hack to maintain indentation in template file */
1104 /* ## End of check for the interaction being outside the cutoff */
1107 /* ## End of loop over i-j interaction pairs */
1109 /* #if GEOMETRY_I != 'Particle' */
1110 /* #if ROUND == 'Loop' */
1111 fjptrA = f+j_coord_offsetA;
1112 fjptrB = f+j_coord_offsetB;
1113 fjptrC = f+j_coord_offsetC;
1114 fjptrD = f+j_coord_offsetD;
1115 fjptrE = f+j_coord_offsetE;
1116 fjptrF = f+j_coord_offsetF;
1117 fjptrG = f+j_coord_offsetG;
1118 fjptrH = f+j_coord_offsetH;
1120 fjptrA = (jnrlistA>=0) ? f+j_coord_offsetA : scratch;
1121 fjptrB = (jnrlistB>=0) ? f+j_coord_offsetB : scratch;
1122 fjptrC = (jnrlistC>=0) ? f+j_coord_offsetC : scratch;
1123 fjptrD = (jnrlistD>=0) ? f+j_coord_offsetD : scratch;
1124 fjptrE = (jnrlistE>=0) ? f+j_coord_offsetE : scratch;
1125 fjptrF = (jnrlistF>=0) ? f+j_coord_offsetF : scratch;
1126 fjptrG = (jnrlistG>=0) ? f+j_coord_offsetG : scratch;
1127 fjptrH = (jnrlistH>=0) ? f+j_coord_offsetH : scratch;
1131 /* #if 'Water' in GEOMETRY_I and GEOMETRY_J == 'Particle' */
1132 gmx_mm256_decrement_1rvec_8ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjptrE,fjptrF,fjptrG,fjptrH,fjx0,fjy0,fjz0);
1133 /* #define INNERFLOPS INNERFLOPS+3 */
1134 /* #elif GEOMETRY_J == 'Water3' */
1135 gmx_mm256_decrement_3rvec_8ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjptrE,fjptrF,fjptrG,fjptrH,
1136 fjx0,fjy0,fjz0,fjx1,fjy1,fjz1,fjx2,fjy2,fjz2);
1137 /* #define INNERFLOPS INNERFLOPS+9 */
1138 /* #elif GEOMETRY_J == 'Water4' */
1139 /* #if 0 in PARTICLES_J */
1140 gmx_mm256_decrement_4rvec_8ptr_swizzle_ps(fjptrA,fjptrB,fjptrC,fjptrD,fjptrE,fjptrF,fjptrG,fjptrH,
1141 fjx0,fjy0,fjz0,fjx1,fjy1,fjz1,
1142 fjx2,fjy2,fjz2,fjx3,fjy3,fjz3);
1143 /* #define INNERFLOPS INNERFLOPS+12 */
1145 gmx_mm256_decrement_3rvec_8ptr_swizzle_ps(fjptrA+DIM,fjptrB+DIM,fjptrC+DIM,fjptrD+DIM,
1146 fjptrE+DIM,fjptrF+DIM,fjptrG+DIM,fjptrH+DIM,
1147 fjx1,fjy1,fjz1,fjx2,fjy2,fjz2,fjx3,fjy3,fjz3);
1148 /* #define INNERFLOPS INNERFLOPS+9 */
1152 /* Inner loop uses {INNERFLOPS} flops */
1157 /* End of innermost loop */
1159 /* #if 'Force' in KERNEL_VF */
1160 /* #if GEOMETRY_I == 'Particle' */
1161 gmx_mm256_update_iforce_1atom_swizzle_ps(fix0,fiy0,fiz0,
1162 f+i_coord_offset,fshift+i_shift_offset);
1163 /* #define OUTERFLOPS OUTERFLOPS+6 */
1164 /* #elif GEOMETRY_I == 'Water3' */
1165 gmx_mm256_update_iforce_3atom_swizzle_ps(fix0,fiy0,fiz0,fix1,fiy1,fiz1,fix2,fiy2,fiz2,
1166 f+i_coord_offset,fshift+i_shift_offset);
1167 /* #define OUTERFLOPS OUTERFLOPS+18 */
1168 /* #elif GEOMETRY_I == 'Water4' */
1169 /* #if 0 in PARTICLES_I */
1170 gmx_mm256_update_iforce_4atom_swizzle_ps(fix0,fiy0,fiz0,fix1,fiy1,fiz1,fix2,fiy2,fiz2,fix3,fiy3,fiz3,
1171 f+i_coord_offset,fshift+i_shift_offset);
1172 /* #define OUTERFLOPS OUTERFLOPS+24 */
1174 gmx_mm256_update_iforce_3atom_swizzle_ps(fix1,fiy1,fiz1,fix2,fiy2,fiz2,fix3,fiy3,fiz3,
1175 f+i_coord_offset+DIM,fshift+i_shift_offset);
1176 /* #define OUTERFLOPS OUTERFLOPS+18 */
1181 /* #if 'Potential' in KERNEL_VF */
1183 /* Update potential energies */
1184 /* #if KERNEL_ELEC != 'None' */
1185 gmx_mm256_update_1pot_ps(velecsum,kernel_data->energygrp_elec+ggid);
1186 /* #define OUTERFLOPS OUTERFLOPS+1 */
1188 /* #if 'GeneralizedBorn' in KERNEL_ELEC */
1189 gmx_mm256_update_1pot_ps(vgbsum,kernel_data->energygrp_polarization+ggid);
1190 /* #define OUTERFLOPS OUTERFLOPS+1 */
1192 /* #if KERNEL_VDW != 'None' */
1193 gmx_mm256_update_1pot_ps(vvdwsum,kernel_data->energygrp_vdw+ggid);
1194 /* #define OUTERFLOPS OUTERFLOPS+1 */
1197 /* #if 'GeneralizedBorn' in KERNEL_ELEC and 'Force' in KERNEL_VF */
1198 dvdasum = _mm256_mul_ps(dvdasum, _mm256_mul_ps(isai{I},isai{I}));
1199 gmx_mm256_update_1pot_ps(dvdasum,dvda+inr);
1202 /* Increment number of inner iterations */
1203 inneriter += j_index_end - j_index_start;
1205 /* Outer loop uses {OUTERFLOPS} flops */
1208 /* Increment number of outer iterations */
1211 /* Update outer/inner flops */
1212 /* ## NB: This is not important, it just affects the flopcount. However, since our preprocessor is */
1213 /* ## primitive and replaces aggressively even in strings inside these directives, we need to */
1214 /* ## assemble the main part of the name (containing KERNEL/ELEC/VDW) directly in the source. */
1215 /* #if GEOMETRY_I == 'Water3' */
1216 /* #define ISUFFIX '_W3' */
1217 /* #elif GEOMETRY_I == 'Water4' */
1218 /* #define ISUFFIX '_W4' */
1220 /* #define ISUFFIX '' */
1222 /* #if GEOMETRY_J == 'Water3' */
1223 /* #define JSUFFIX 'W3' */
1224 /* #elif GEOMETRY_J == 'Water4' */
1225 /* #define JSUFFIX 'W4' */
1227 /* #define JSUFFIX '' */
1229 /* #if 'PotentialAndForce' in KERNEL_VF */
1230 /* #define VFSUFFIX '_VF' */
1231 /* #elif 'Potential' in KERNEL_VF */
1232 /* #define VFSUFFIX '_V' */
1234 /* #define VFSUFFIX '_F' */
1237 /* #if KERNEL_ELEC != 'None' and KERNEL_VDW != 'None' */
1238 inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_VDW{ISUFFIX}{JSUFFIX}{VFSUFFIX},outeriter*{OUTERFLOPS} + inneriter*{INNERFLOPS});
1239 /* #elif KERNEL_ELEC != 'None' */
1240 inc_nrnb(nrnb,eNR_NBKERNEL_ELEC{ISUFFIX}{JSUFFIX}{VFSUFFIX},outeriter*{OUTERFLOPS} + inneriter*{INNERFLOPS});
1242 inc_nrnb(nrnb,eNR_NBKERNEL_VDW{ISUFFIX}{JSUFFIX}{VFSUFFIX},outeriter*{OUTERFLOPS} + inneriter*{INNERFLOPS});