runner_iact.h 23 KB
Newer Older
1
/*******************************************************************************
2
 * This file is part of SWIFT.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 * Coypright (c) 2012 Pedro Gonnet (pedro.gonnet@durham.ac.uk)
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 ******************************************************************************/
19
 
20

Pedro Gonnet's avatar
Pedro Gonnet committed
21
/* Load the vector stuff. */
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// #ifdef __SSE2__
//     #define VECTORIZE
//     #include <immintrin.h>
//     #ifdef __AVX__
//         typedef union {
//             __m256 v;
//             __m256i m;
//             float f[8];
//             int i[8];
//             } vector;
//         #define VEC_SIZE 8
//         #define vec_load(a) _mm256_load_ps(a)
//         #define vec_set1(a) _mm256_set1_ps(a)
//         #define vec_sqrt(a) _mm256_sqrt_ps(a)
//         #define vec_rcp(a) _mm256_rcp_ps(a)
//         #define vec_rsqrt(a) _mm256_rsqrt_ps(a)
//         #define vec_ftoi(a) _mm256_cvttps_epi32(a)
//         #define vec_fmin(a,b) _mm256_min_ps(a,b)
//     #else
//         typedef union {
//             __m128 v;
//             __m128i m;
//             float f[4];
//             int i[4];
//             } vector;
//         #define VEC_SIZE 4
//         #define vec_load(a) _mm_load_ps(a)
//         #define vec_set1(a) _mm_set1_ps(a)
//         #define vec_sqrt(a) _mm_sqrt_ps(a)
//         #define vec_rcp(a) _mm_rcp_ps(a)
//         #define vec_rsqrt(a) _mm_rsqrt_ps(a)
//         #define vec_ftoi(a) _mm_cvttps_epi32(a)
//         #define vec_fmin(a,b) _mm_min_ps(a,b)
//     #endif
// #else
//     #define VEC_SIZE 4
// #endif
#include "vector.h"
Pedro Gonnet's avatar
Pedro Gonnet committed
60
61

/* Coefficients for the kernel. */ 
62
63
64
65
66
67
#define kernel_degree 3
#define kernel_ivals 2
#define kernel_gamma 0.5f
#define kernel_igamma 2.0f
#define kernel_igamma3 8.0f
#define kernel_igamma4 16.0f
Pedro Gonnet's avatar
Pedro Gonnet committed
68
static float kernel_coeffs[ (kernel_degree + 1) * (kernel_ivals + 1) ] __attribute__ ((aligned (16))) =
69
70
71
    { 3.0/4.0*M_1_PI , -3.0/2.0*M_1_PI , 0.0 , M_1_PI , 
      -0.25*M_1_PI , 3.0/2.0*M_1_PI , -3.0*M_1_PI , M_2_PI , 
      0.0 , 0.0 , 0.0 , 0.0 };
72
73
#define kernel_root ( kernel_coeffs[ kernel_degree ] )
#define kernel_wroot ( 4.0/3.0*M_PI*kernel_igamma3*kernel_coeffs[ kernel_degree ] )
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
      
      
/**
 * @brief Helper function to evaluate the kernel at a given x.
 */

__attribute__ ((always_inline)) INLINE static void kernel_deval ( float x , float *W , float *dW_dx ) {
    int ind = fmin( x , kernel_ivals );
    float *coeffs = &kernel_coeffs[ ind*(kernel_degree + 1) ];
    float w = coeffs[0]*x + coeffs[1];
    float dw_dx = coeffs[0];
    for ( int k = 2 ; k <= kernel_degree ; k++ ) {
        dw_dx = dw_dx*x + w;
        w = x*w + coeffs[k];
        }
    *W = w;
    *dW_dx = dw_dx;
    }


94
95
#ifdef VECTORIZE

Pedro Gonnet's avatar
Pedro Gonnet committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
__attribute__ ((always_inline)) INLINE static void kernel_deval_vec ( vector *x , vector *w , vector *dw_dx ) {
    
    vector ind, c[kernel_degree+1];
    int j, k;
    
    /* Load x and get the interval id. */
    ind.m = vec_ftoi( vec_fmin( x->v , vec_set1( (float)kernel_ivals ) ) );
    
    /* load the coefficients. */
    for ( k = 0 ; k < VEC_SIZE ; k++ )
        for ( j = 0 ; j < kernel_degree+1 ; j++ )
            c[j].f[k] = kernel_coeffs[ ind.i[k]*(kernel_degree + 1) + j ];

    /* Init the iteration for Horner's scheme. */
    w->v = ( c[0].v * x->v ) + c[1].v;
    dw_dx->v = c[0].v;
    
    /* And we're off! */
    for ( int k = 2 ; k <= kernel_degree ; k++ ) {
        dw_dx->v = ( dw_dx->v * x->v ) + w->v;
        w->v = ( x->v * w->v ) + c[k].v;
        }
        
119
    }
Pedro Gonnet's avatar
Pedro Gonnet committed
120
121
122
123
    
#endif


124
125
126
127
128
129
130
131
__attribute__ ((always_inline)) INLINE static void kernel_eval ( float x , float *W ) {
    int ind = fmin( x , kernel_ivals );
    float *coeffs = &kernel_coeffs[ ind*(kernel_degree + 1) ];
    float w = coeffs[0]*x + coeffs[1];
    for ( int k = 2 ; k <= kernel_degree ; k++ )
        w = x*w + coeffs[k];
    *W = w;
    }
132
133


134
135
136
/**
 * @brief Density kernel
 */
137

138
__attribute__ ((always_inline)) INLINE static void runner_iact_density ( float r2 , float *dx , float hi , float hj , struct part *pi , struct part *pj ) {
139
140

    float r = sqrtf( r2 );
141
    float xi, xj;
142
    float h_inv, hg_inv;
143
    float wi, wj, wi_dx, wj_dx;
144
145
146
    
    if ( r2 < hi*hi && pi != NULL ) {
        
147
148
        h_inv = 1.0 / hi;
        hg_inv = kernel_igamma * h_inv;
149
150
151
        xi = r * hg_inv;
        kernel_deval( xi , &wi , &wi_dx );
        
Pedro Gonnet's avatar
Pedro Gonnet committed
152
        pi->rho += pj->mass * wi;
153
        pi->rho_dh -= pj->mass * ( 3.0*wi + xi*wi_dx );
Pedro Gonnet's avatar
Pedro Gonnet committed
154
        pi->wcount += wi * ( 4.0f * M_PI / 3.0f * kernel_igamma3 );
155
        pi->wcount_dh -= xi * h_inv * wi_dx * ( 4.0f * M_PI / 3.0f * kernel_igamma3 );
156
        // pi->icount += 1;
157
158
159
160
161
        
        }

    if ( r2 < hj*hj && pj != NULL ) {
        
162
163
        h_inv = 1.0 / hj;
        hg_inv = kernel_igamma * h_inv;
164
165
166
        xj = r * hg_inv;
        kernel_deval( xj , &wj , &wj_dx );
        
Pedro Gonnet's avatar
Pedro Gonnet committed
167
        pj->rho += pi->mass * wj;
168
        pj->rho_dh -= pi->mass * ( 3.0*wj + xj*wj_dx );
Pedro Gonnet's avatar
Pedro Gonnet committed
169
        pj->wcount += wj * ( 4.0f * M_PI / 3.0f * kernel_igamma3 );
170
        pj->wcount_dh -= xj * h_inv * wj_dx * ( 4.0f * M_PI / 3.0f * kernel_igamma3 );
171
        // pj->icount += 1;
172
        
173
174
175
176
177
178
179
180
181
182
183
184
        }
        
    #ifdef HIST
    if ( hi > hj )
        runner_hist_hit( hi / hj );
    else
        runner_hist_hit( hj / hi );
    #endif
    
    }
    

Pedro Gonnet's avatar
Pedro Gonnet committed
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
__attribute__ ((always_inline)) INLINE static void runner_iact_vec_density ( float *R2 , float *Dx , float *Hi , float *Hj , struct part **pi , struct part **pj ) {

#ifdef VECTORIZE

    vector r, xi, xj, hi, hj, hi_inv, hj_inv, hig_inv, hjg_inv, wi, wj, wi_dx, wj_dx;
    vector rhoi, rhoj, rhoi_dh, rhoj_dh, wcounti, wcountj, wcounti_dh, wcountj_dh;
    vector mi, mj, wscale;
    int k;
    
    r.v = vec_sqrt( vec_load( R2 ) );
    #if VEC_SIZE==8
        mi.v = _mm256_set_ps( pi[7]->mass , pi[6]->mass , pi[5]->mass , pi[4]->mass , pi[3]->mass , pi[2]->mass , pi[1]->mass , pi[0]->mass );
        mj.v = _mm256_set_ps( pj[7]->mass , pj[6]->mass , pj[5]->mass , pj[4]->mass , pj[3]->mass , pj[2]->mass , pj[1]->mass , pj[0]->mass );
    #elif VEC_SIZE==4
        mi.v = _mm_set_ps( pi[3]->mass , pi[2]->mass , pi[1]->mass , pi[0]->mass );
        mj.v = _mm_set_ps( pj[3]->mass , pj[2]->mass , pj[1]->mass , pj[0]->mass );
    #endif
    wscale.v = vec_set1( 4.0f * M_PI / 3.0f * kernel_igamma3 );
    
    hi.v = vec_load( Hi );
    hi_inv.v = vec_rcp( hi.v );
    hi_inv.v = hi_inv.v - hi_inv.v * ( hi_inv.v * hi.v  - vec_set1( 1.0f ) );
    hig_inv.v = hi_inv.v * vec_set1( kernel_igamma );
    xi.v = r.v * hig_inv.v;

    hj.v = vec_load( Hj );
    hj_inv.v = vec_rcp( hj.v );
    hj_inv.v = hj_inv.v - hj_inv.v * ( hj_inv.v * hj.v  - vec_set1( 1.0f ) );
    hjg_inv.v = hj_inv.v * vec_set1( kernel_igamma );
    xj.v = r.v * hjg_inv.v;
    
    kernel_deval_vec( &xi , &wi , &wi_dx );
    kernel_deval_vec( &xj , &wj , &wj_dx );
    
    rhoi.v = mj.v * wi.v;
    rhoi_dh.v = mj.v * ( vec_set1( 3.0f ) * wi.v + xi.v * wi_dx.v );
    wcounti.v = wi.v * wscale.v;
    wcounti_dh.v = xi.v * hi_inv.v * wi_dx.v * wscale.v;
        
    rhoj.v = mi.v * wj.v;
    rhoj_dh.v = mi.v * ( vec_set1( 3.0f ) * wj.v + xj.v * wj_dx.v );
    wcountj.v = wj.v * wscale.v;
    wcountj_dh.v = xj.v * hj_inv.v * wj_dx.v * wscale.v;
        
    for ( k = 0 ; k < VEC_SIZE ; k++ ) {
        pi[k]->rho += rhoi.f[k];
        pi[k]->rho_dh -= rhoi_dh.f[k];
        pi[k]->wcount += wcounti.f[k];
        pi[k]->wcount_dh -= wcounti_dh.f[k];
        pj[k]->rho += rhoj.f[k];
        pj[k]->rho_dh -= rhoj_dh.f[k];
        pj[k]->wcount += wcountj.f[k];
        pj[k]->wcount_dh -= wcountj_dh.f[k];
        }
        
#else

    for ( int k = 0 ; k < VEC_SIZE ; k++ )
        runner_iact_density( R2[k] , &Dx[3*k] , Hi[k] , Hj[k] , pi[k] , pj[k] );
        
#endif
    
    }
    

250

251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
 * @brief Density kernel
 */

__attribute__ ((always_inline)) INLINE static void runner_iact_nonsym_density ( float r2 , float *dx , float hi , float hj , struct part *pi , struct part *pj ) {

    float r = sqrtf( r2 );
    float xi;
    float h_inv, hg_inv;
    float wi, wi_dx;
    
    if ( r2 < hi*hi && pi != NULL ) {
        
        h_inv = 1.0 / hi;
        hg_inv = kernel_igamma * h_inv;
        xi = r * hg_inv;
        kernel_deval( xi , &wi , &wi_dx );
        
        pi->rho += pj->mass * wi;
270
        pi->rho_dh -= pj->mass * ( 3.0*wi + xi*wi_dx );
271
272
        pi->wcount += wi * ( 4.0f * M_PI / 3.0f * kernel_igamma3 );
        pi->wcount_dh -= xi * h_inv * wi_dx * ( 4.0f * M_PI / 3.0f * kernel_igamma3 );
273
        // pi->icount += 1;
274
275
276
277
278
279
        
        }

    }
    

Pedro Gonnet's avatar
Pedro Gonnet committed
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
__attribute__ ((always_inline)) INLINE static void runner_iact_nonsym_vec_density ( float *R2 , float *Dx , float *Hi , float *Hj , struct part **pi , struct part **pj ) {

#ifdef VECTORIZE

    vector r, xi, hi, hi_inv, hig_inv, wi, wi_dx;
    vector rhoi, rhoi_dh, wcounti, wcounti_dh;
    vector mj, wscale;
    int k;
    
    r.v = vec_sqrt( vec_load( R2 ) );
    #if VEC_SIZE==8
        mj.v = _mm256_set_ps( pj[7]->mass , pj[6]->mass , pj[5]->mass , pj[4]->mass , pj[3]->mass , pj[2]->mass , pj[1]->mass , pj[0]->mass );
    #elif VEC_SIZE==4
        mj.v = _mm_set_ps( pj[3]->mass , pj[2]->mass , pj[1]->mass , pj[0]->mass );
    #endif
    wscale.v = vec_set1( 4.0f * M_PI / 3.0f * kernel_igamma3 );
    
    hi.v = vec_load( Hi );
    hi_inv.v = vec_rcp( hi.v );
    hi_inv.v = hi_inv.v - hi_inv.v * ( hi_inv.v * hi.v  - vec_set1( 1.0f ) );
    hig_inv.v = hi_inv.v * vec_set1( kernel_igamma );
    xi.v = r.v * hig_inv.v;

    kernel_deval_vec( &xi , &wi , &wi_dx );
    
    rhoi.v = mj.v * wi.v;
    rhoi_dh.v = mj.v * ( vec_set1( 3.0f ) * wi.v + xi.v * wi_dx.v );
    wcounti.v = wi.v * wscale.v;
    wcounti_dh.v = xi.v * hi_inv.v * wi_dx.v * wscale.v;
        
    for ( k = 0 ; k < VEC_SIZE ; k++ ) {
        pi[k]->rho += rhoi.f[k];
        pi[k]->rho_dh -= rhoi_dh.f[k];
        pi[k]->wcount += wcounti.f[k];
        pi[k]->wcount_dh -= wcounti_dh.f[k];
        }
        
#else

    for ( int k = 0 ; k < VEC_SIZE ; k++ )
        runner_iact_nonsym_density( R2[k] , &Dx[3*k] , Hi[k] , Hj[k] , pi[k] , pj[k] );

#endif
        
    }
    

327

328
__attribute__ ((always_inline)) INLINE static void runner_iact_force ( float r2 , float *dx , float hi , float hj , struct part *pi , struct part *pj ) {
329

330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
    float r = sqrtf( r2 ), ri = 1.0f / r;
    float xi, xj;
    float hig_inv, hig2_inv;
    float hjg_inv, hjg2_inv;
    float wi, wj, wi_dx, wj_dx, wi_dr, wj_dr, w, dvdr;
    float f;
    int k;
    
    /* Get the kernel for hi. */
    hig_inv = kernel_igamma / hi;
    hig2_inv = hig_inv * hig_inv;
    xi = r * hig_inv;
    kernel_deval( xi , &wi , &wi_dx );
    wi_dr = hig2_inv * hig2_inv * wi_dx;
        
    /* Get the kernel for hj. */
    hjg_inv = kernel_igamma / hj;
    hjg2_inv = hjg_inv * hjg_inv;
    xj = r * hjg_inv;
    kernel_deval( xj , &wj , &wj_dx );
Pedro Gonnet's avatar
Pedro Gonnet committed
350
    wj_dr = hjg2_inv * hjg2_inv * wj_dx;
351
352
353
354
355
356
357
    
    /* Get the common factor out. */
    w = ri * ( pi->POrho2 * wi_dr + pj->POrho2 * wj_dr );
        
    /* Use the force, Luke! */
    for ( k = 0 ; k < 3 ; k++ ) {
        f = dx[k] * w;
358
359
        pi->a[k] -= pj->mass * f;
        pj->a[k] += pi->mass * f;
360
361
362
363
        }
        
    /* Compute dv dot r. */
    dvdr = ( pi->v[0] - pj->v[0] ) * dx[0] + ( pi->v[1] - pj->v[1] ) * dx[1] + ( pi->v[2] - pj->v[2] ) * dx[2];
Pedro Gonnet's avatar
Pedro Gonnet committed
364
    dvdr *= ri;
365
366
367
368
369
370
371
        
    /* Get the time derivative for u. */
    pi->u_dt += pj->mass * dvdr * wi_dr;
    pj->u_dt += pi->mass * dvdr * wj_dr;
    
    /* Get the time derivative for h. */
    pi->h_dt += pj->mass / pj->rho * dvdr * wi_dr;
372
    pj->h_dt -= pi->mass / pi->rho * dvdr * wj_dr;
373
374
375
376
377
378
379
        
    #ifdef HIST
    if ( hi > hj )
        runner_hist_hit( hi / hj );
    else
        runner_hist_hit( hj / hi );
    #endif
380
381
382
383
    
    }
    

Pedro Gonnet's avatar
Pedro Gonnet committed
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
__attribute__ ((always_inline)) INLINE static void runner_iact_vec_force ( float *R2 , float *Dx , float *Hi , float *Hj , struct part **pi , struct part **pj ) {

#ifdef VECTORIZE

    vector r, r2, ri;
    vector xi, xj;
    vector hi, hj, hi_inv, hj_inv;
    vector hig_inv, hig2_inv;
    vector hjg_inv, hjg2_inv;
    vector wi, wj, wi_dx, wj_dx, wi_dr, wj_dr, dvdr;
    vector w;
    vector piPOrho2, pjPOrho2, pirho, pjrho;
    vector mi, mj;
    vector f;
    vector dx[3];
    vector vi[3], vj[3];
    vector pia[3], pja[3];
    vector piu_dt, pju_dt;
    vector pih_dt, pjh_dt;
    int j, k;

    /* Load stuff. */
    #if VEC_SIZE==8
        mi.v = _mm256_set_ps( pi[7]->mass , pi[6]->mass , pi[5]->mass , pi[4]->mass , pi[3]->mass , pi[2]->mass , pi[1]->mass , pi[0]->mass );
        mj.v = _mm256_set_ps( pj[7]->mass , pj[6]->mass , pj[5]->mass , pj[4]->mass , pj[3]->mass , pj[2]->mass , pj[1]->mass , pj[0]->mass );
        piPOrho2.v = _mm256_set_ps( pi[7]->POrho2 , pi[6]->POrho2 , pi[5]->POrho2 , pi[4]->POrho2 , pi[3]->POrho2 , pi[2]->POrho2 , pi[1]->POrho2 , pi[0]->POrho2 );
        pjPOrho2.v = _mm256_set_ps( pj[7]->POrho2 , pj[6]->POrho2 , pj[5]->POrho2 , pj[4]->POrho2 , pj[3]->POrho2 , pj[2]->POrho2 , pj[1]->POrho2 , pj[0]->POrho2 );
        pirho.v = _mm256_set_ps( pi[7]->rho , pi[6]->rho , pi[5]->rho , pi[4]->rho , pi[3]->rho , pi[2]->rho , pi[1]->rho , pi[0]->rho );
        pjrho.v = _mm256_set_ps( pj[7]->rho , pj[6]->rho , pj[5]->rho , pj[4]->rho , pj[3]->rho , pj[2]->rho , pj[1]->rho , pj[0]->rho );
        for ( k = 0 ; k < 3 ; k++ ) {
            vi[k].v = _mm256_set_ps( pi[7]->v[k] , pi[6]->v[k] , pi[5]->v[k] , pi[4]->v[k] , pi[3]->v[k] , pi[2]->v[k] , pi[1]->v[k] , pi[0]->v[k] );
            vj[k].v = _mm256_set_ps( pj[7]->v[k] , pj[6]->v[k] , pj[5]->v[k] , pj[4]->v[k] , pj[3]->v[k] , pj[2]->v[k] , pj[1]->v[k] , pj[0]->v[k] );
            }
        for ( k = 0 ; k < 3 ; k++ )
            dx[k].v = _mm256_set_ps( Dx[21+k] , Dx[18+k] , Dx[15+k] , Dx[12+k] , Dx[9+k] , Dx[6+k] , Dx[3+k] , Dx[0+k] );
    #elif VEC_SIZE==4
        mi.v = _mm_set_ps( pi[3]->mass , pi[2]->mass , pi[1]->mass , pi[0]->mass );
        mj.v = _mm_set_ps( pj[3]->mass , pj[2]->mass , pj[1]->mass , pj[0]->mass );
        piPOrho2.v = _mm_set_ps( pi[3]->POrho2 , pi[2]->POrho2 , pi[1]->POrho2 , pi[0]->POrho2 );
        pjPOrho2.v = _mm_set_ps( pj[3]->POrho2 , pj[2]->POrho2 , pj[1]->POrho2 , pj[0]->POrho2 );
        pirho.v = _mm_set_ps( pi[3]->rho , pi[2]->rho , pi[1]->rho , pi[0]->rho );
        pjrho.v = _mm_set_ps( pj[3]->rho , pj[2]->rho , pj[1]->rho , pj[0]->rho );
        for ( k = 0 ; k < 3 ; k++ ) {
            vi[k].v = _mm_set_ps( pi[3]->v[k] , pi[2]->v[k] , pi[1]->v[k] , pi[0]->v[k] );
            vj[k].v = _mm_set_ps( pj[3]->v[k] , pj[2]->v[k] , pj[1]->v[k] , pj[0]->v[k] );
            }
        for ( k = 0 ; k < 3 ; k++ )
            dx[k].v = _mm_set_ps( Dx[9+k] , Dx[6+k] , Dx[3+k] , Dx[0+k] );
    #else
        #error
    #endif
    
    /* Get the radius and inverse radius. */
    r2.v = vec_load( R2 );
    ri.v = vec_rsqrt( r2.v );
    ri.v = ri.v - vec_set1( 0.5f ) * ri.v * ( r2.v * ri.v * ri.v - vec_set1( 1.0f ) );
    r.v = r2.v * ri.v;
    
    /* Get the kernel for hi. */
    hi.v = vec_load( Hi );
    hi_inv.v = vec_rcp( hi.v );
    hi_inv.v = hi_inv.v - hi_inv.v * ( hi.v * hi_inv.v - vec_set1( 1.0f ) );
    hig_inv.v = vec_set1( kernel_igamma ) * hi_inv.v;
    hig2_inv.v = hig_inv.v * hig_inv.v;
    xi.v = r.v * hig_inv.v;
    kernel_deval_vec( &xi , &wi , &wi_dx );
    wi_dr.v = hig2_inv.v * hig2_inv.v * wi_dx.v;
        
    /* Get the kernel for hj. */
    hj.v = vec_load( Hj );
    hj_inv.v = vec_rcp( hj.v );
    hj_inv.v = hj_inv.v - hj_inv.v * ( hj.v * hj_inv.v - vec_set1( 1.0f ) );
    hjg_inv.v = vec_set1( kernel_igamma ) * hj_inv.v;
    hjg2_inv.v = hjg_inv.v * hjg_inv.v;
    xj.v = r.v * hjg_inv.v;
    kernel_deval_vec( &xj , &wj , &wj_dx );
    wj_dr.v = hjg2_inv.v * hjg2_inv.v * wj_dx.v;
        
    /* Get the common factor out. */
    w.v = ri.v * ( piPOrho2.v * wi_dr.v + pjPOrho2.v * wj_dr.v );
        
    /* Use the force, Luke! */
    for ( k = 0 ; k < 3 ; k++ ) {
        f.v = dx[k].v * w.v;
        pia[k].v = mj.v * f.v;
        pja[k].v = mi.v * f.v;
        }
        
    /* Compute dv dot r. */
    dvdr.v = ( (vi[0].v - vj[0].v) * dx[0].v ) + ( (vi[1].v - vj[1].v) * dx[1].v ) + ( (vi[2].v - vj[2].v) * dx[2].v );
    dvdr.v = dvdr.v * ri.v;
        
    /* Get the time derivative for u. */
    piu_dt.v = mj.v * dvdr.v * wi_dr.v;
    pju_dt.v = mi.v * dvdr.v * wj_dr.v;
    
    /* Get the time derivative for h. */
    pih_dt.v = mj.v / pjrho.v * dvdr.v * wi_dr.v;
    pjh_dt.v = mi.v / pirho.v * dvdr.v * wj_dr.v;
    
    /* Store the forces back on the particles. */
485
    for ( k = 0 ; k < VEC_SIZE ; k++ ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
486
487
488
489
490
        pi[k]->u_dt += piu_dt.f[k];
        pj[k]->u_dt += pju_dt.f[k];
        pi[k]->h_dt += pih_dt.f[k];
        pj[k]->h_dt += pjh_dt.f[k];
        for ( j = 0 ; j < 3 ; j++ ) {
491
492
            pi[k]->a[j] -= pia[j].f[k];
            pj[k]->a[j] += pja[j].f[k];
Pedro Gonnet's avatar
Pedro Gonnet committed
493
494
495
496
497
498
499
500
501
502
503
504
505
            }
        }
        
#else

    for ( int k = 0 ; k < VEC_SIZE ; k++ )
        runner_iact_force( R2[k] , &Dx[3*k] , Hi[k] , Hj[k] , pi[k] , pj[k] );

#endif
        
    }
    

506

507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
__attribute__ ((always_inline)) INLINE static void runner_iact_nonsym_force ( float r2 , float *dx , float hi , float hj , struct part *pi , struct part *pj ) {

    float r = sqrtf( r2 ), ri = 1.0f / r;
    float xi, xj;
    float hig_inv, hig2_inv;
    float hjg_inv, hjg2_inv;
    float wi, wj, wi_dx, wj_dx, wi_dr, wj_dr, w, dvdr;
    float f;
    int k;
    
    /* Get the kernel for hi. */
    hig_inv = kernel_igamma / hi;
    hig2_inv = hig_inv * hig_inv;
    xi = r * hig_inv;
    kernel_deval( xi , &wi , &wi_dx );
    wi_dr = hig2_inv * hig2_inv * wi_dx;
        
    /* Get the kernel for hj. */
    hjg_inv = kernel_igamma / hj;
    hjg2_inv = hjg_inv * hjg_inv;
    xj = r * hjg_inv;
    kernel_deval( xj , &wj , &wj_dx );
    wj_dr = hjg2_inv * hjg2_inv * wj_dx;
    
    /* Get the common factor out. */
    w = ri * ( pi->POrho2 * wi_dr + pj->POrho2 * wj_dr );
        
    /* Use the force, Luke! */
    for ( k = 0 ; k < 3 ; k++ ) {
        f = dx[k] * w;
537
        pi->a[k] -= pj->mass * f;
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
        }
        
    /* Compute dv dot r. */
    dvdr = ( pi->v[0] - pj->v[0] ) * dx[0] + ( pi->v[1] - pj->v[1] ) * dx[1] + ( pi->v[2] - pj->v[2] ) * dx[2];
    dvdr *= ri;
        
    /* Get the time derivative for u. */
    pi->u_dt += pj->mass * dvdr * wi_dr;
    
    /* Get the time derivative for h. */
    pi->h_dt += pj->mass / pj->rho * dvdr * wi_dr;
        
    }
    

Pedro Gonnet's avatar
Pedro Gonnet committed
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
__attribute__ ((always_inline)) INLINE static void runner_iact_nonsym_vec_force ( float *R2 , float *Dx , float *Hi , float *Hj , struct part **pi , struct part **pj ) {

#ifdef VECTORIZE

    vector r, r2, ri;
    vector xi, xj;
    vector hi, hj, hi_inv, hj_inv;
    vector hig_inv, hig2_inv;
    vector hjg_inv, hjg2_inv;
    vector wi, wj, wi_dx, wj_dx, wi_dr, wj_dr, dvdr;
    vector w;
    vector piPOrho2, pjPOrho2, pjrho;
    vector mj;
    vector f;
    vector dx[3];
    vector vi[3], vj[3];
    vector pia[3];
    vector piu_dt;
    vector pih_dt;
    int j, k;

    /* Load stuff. */
    #if VEC_SIZE==8
        mj.v = _mm256_set_ps( pj[7]->mass , pj[6]->mass , pj[5]->mass , pj[4]->mass , pj[3]->mass , pj[2]->mass , pj[1]->mass , pj[0]->mass );
        piPOrho2.v = _mm256_set_ps( pi[7]->POrho2 , pi[6]->POrho2 , pi[5]->POrho2 , pi[4]->POrho2 , pi[3]->POrho2 , pi[2]->POrho2 , pi[1]->POrho2 , pi[0]->POrho2 );
        pjPOrho2.v = _mm256_set_ps( pj[7]->POrho2 , pj[6]->POrho2 , pj[5]->POrho2 , pj[4]->POrho2 , pj[3]->POrho2 , pj[2]->POrho2 , pj[1]->POrho2 , pj[0]->POrho2 );
        pjrho.v = _mm256_set_ps( pj[7]->rho , pj[6]->rho , pj[5]->rho , pj[4]->rho , pj[3]->rho , pj[2]->rho , pj[1]->rho , pj[0]->rho );
        for ( k = 0 ; k < 3 ; k++ ) {
            vi[k].v = _mm256_set_ps( pi[7]->v[k] , pi[6]->v[k] , pi[5]->v[k] , pi[4]->v[k] , pi[3]->v[k] , pi[2]->v[k] , pi[1]->v[k] , pi[0]->v[k] );
            vj[k].v = _mm256_set_ps( pj[7]->v[k] , pj[6]->v[k] , pj[5]->v[k] , pj[4]->v[k] , pj[3]->v[k] , pj[2]->v[k] , pj[1]->v[k] , pj[0]->v[k] );
            }
        for ( k = 0 ; k < 3 ; k++ )
            dx[k].v = _mm256_set_ps( Dx[21+k] , Dx[18+k] , Dx[15+k] , Dx[12+k] , Dx[9+k] , Dx[6+k] , Dx[3+k] , Dx[0+k] );
    #elif VEC_SIZE==4
        mj.v = _mm_set_ps( pj[3]->mass , pj[2]->mass , pj[1]->mass , pj[0]->mass );
        piPOrho2.v = _mm_set_ps( pi[3]->POrho2 , pi[2]->POrho2 , pi[1]->POrho2 , pi[0]->POrho2 );
        pjPOrho2.v = _mm_set_ps( pj[3]->POrho2 , pj[2]->POrho2 , pj[1]->POrho2 , pj[0]->POrho2 );
        pjrho.v = _mm_set_ps( pj[3]->rho , pj[2]->rho , pj[1]->rho , pj[0]->rho );
        for ( k = 0 ; k < 3 ; k++ ) {
            vi[k].v = _mm_set_ps( pi[3]->v[k] , pi[2]->v[k] , pi[1]->v[k] , pi[0]->v[k] );
            vj[k].v = _mm_set_ps( pj[3]->v[k] , pj[2]->v[k] , pj[1]->v[k] , pj[0]->v[k] );
            }
        for ( k = 0 ; k < 3 ; k++ )
            dx[k].v = _mm_set_ps( Dx[9+k] , Dx[6+k] , Dx[3+k] , Dx[0+k] );
    #else
        #error
    #endif
    
    /* Get the radius and inverse radius. */
    r2.v = vec_load( R2 );
    ri.v = vec_rsqrt( r2.v );
    ri.v = ri.v - vec_set1( 0.5f ) * ri.v * ( r2.v * ri.v * ri.v - vec_set1( 1.0f ) );
    r.v = r2.v * ri.v;
    
    /* Get the kernel for hi. */
    hi.v = vec_load( Hi );
    hi_inv.v = vec_rcp( hi.v );
    hi_inv.v = hi_inv.v - hi_inv.v * ( hi.v * hi_inv.v - vec_set1( 1.0f ) );
    hig_inv.v = vec_set1( kernel_igamma ) * hi_inv.v;
    hig2_inv.v = hig_inv.v * hig_inv.v;
    xi.v = r.v * hig_inv.v;
    kernel_deval_vec( &xi , &wi , &wi_dx );
    wi_dr.v = hig2_inv.v * hig2_inv.v * wi_dx.v;
        
    /* Get the kernel for hj. */
    hj.v = vec_load( Hj );
    hj_inv.v = vec_rcp( hj.v );
    hj_inv.v = hj_inv.v - hj_inv.v * ( hj.v * hj_inv.v - vec_set1( 1.0f ) );
    hjg_inv.v = vec_set1( kernel_igamma ) * hj_inv.v;
    hjg2_inv.v = hjg_inv.v * hjg_inv.v;
    xj.v = r.v * hjg_inv.v;
    kernel_deval_vec( &xj , &wj , &wj_dx );
    wj_dr.v = hjg2_inv.v * hjg2_inv.v * wj_dx.v;
        
    /* Get the common factor out. */
    w.v = ri.v * ( piPOrho2.v * wi_dr.v + pjPOrho2.v * wj_dr.v );
        
    /* Use the force, Luke! */
    for ( k = 0 ; k < 3 ; k++ ) {
        f.v = dx[k].v * w.v;
        pia[k].v = mj.v * f.v;
        }
        
    /* Compute dv dot r. */
    dvdr.v = ( (vi[0].v - vj[0].v) * dx[0].v ) + ( (vi[1].v - vj[1].v) * dx[1].v ) + ( (vi[2].v - vj[2].v) * dx[2].v );
    dvdr.v = dvdr.v * ri.v;
        
    /* Get the time derivative for u. */
    piu_dt.v = mj.v * dvdr.v * wi_dr.v;
    
    /* Get the time derivative for h. */
    pih_dt.v = mj.v / pjrho.v * dvdr.v * wi_dr.v;
    
    /* Store the forces back on the particles. */
647
    for ( k = 0 ; k < VEC_SIZE ; k++ ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
648
649
650
        pi[k]->u_dt += piu_dt.f[k];
        pi[k]->h_dt += pih_dt.f[k];
        for ( j = 0 ; j < 3 ; j++ )
651
            pi[k]->a[j] -= pia[j].f[k];
Pedro Gonnet's avatar
Pedro Gonnet committed
652
653
654
655
656
657
658
659
660
661
662
663
664
        }

#else

    for ( int k = 0 ; k < VEC_SIZE ; k++ )
        runner_iact_nonsym_force( R2[k] , &Dx[3*k] , Hi[k] , Hj[k] , pi[k] , pj[k] );

#endif
        
    }
    


665