runner_doiact_vec.c 69.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*******************************************************************************
 * This file is part of SWIFT.
 * Copyright (c) 2016 James Willis (james.s.willis@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/>.
 *
 ******************************************************************************/

/* Config parameters. */
#include "../config.h"

/* This object's header. */
#include "runner_doiact_vec.h"

26
27
28
/* Local headers. */
#include "active.h"

29
30
#if defined(WITH_VECTORIZATION) && defined(GADGET2_SPH)

31
32
static const vector kernel_gamma2_vec = FILL_VEC(kernel_gamma2);

James Willis's avatar
James Willis committed
33
34
35
/**
 * @brief Compute the vector remainder interactions from the secondary cache.
 *
Matthieu Schaller's avatar
Matthieu Schaller committed
36
 * @param int_cache (return) secondary #cache of interactions between two
James Willis's avatar
James Willis committed
37
 * particles.
James Willis's avatar
James Willis committed
38
 * @param icount Interaction count.
James Willis's avatar
James Willis committed
39
 * @param v_rhoSum (return) #vector holding the cumulative sum of the density
James Willis's avatar
James Willis committed
40
 * update on pi.
James Willis's avatar
James Willis committed
41
 * @param v_rho_dhSum (return) #vector holding the cumulative sum of the density
James Willis's avatar
James Willis committed
42
 * gradient update on pi.
James Willis's avatar
James Willis committed
43
 * @param v_wcountSum (return) #vector holding the cumulative sum of the wcount
James Willis's avatar
James Willis committed
44
 * update on pi.
45
46
 * @param v_wcount_dhSum (return) #vector holding the cumulative sum of the
 * wcount
James Willis's avatar
James Willis committed
47
 * gradient update on pi.
48
49
 * @param v_div_vSum (return) #vector holding the cumulative sum of the
 * divergence
James Willis's avatar
James Willis committed
50
 * update on pi.
James Willis's avatar
James Willis committed
51
 * @param v_curlvxSum (return) #vector holding the cumulative sum of the curl of
James Willis's avatar
James Willis committed
52
 * vx update on pi.
James Willis's avatar
James Willis committed
53
 * @param v_curlvySum (return) #vector holding the cumulative sum of the curl of
James Willis's avatar
James Willis committed
54
 * vy update on pi.
James Willis's avatar
James Willis committed
55
 * @param v_curlvzSum (return) #vector holding the cumulative sum of the curl of
James Willis's avatar
James Willis committed
56
 * vz update on pi.
James Willis's avatar
James Willis committed
57
58
59
60
 * @param v_hi_inv #vector of 1/h for pi.
 * @param v_vix #vector of x velocity of pi.
 * @param v_viy #vector of y velocity of pi.
 * @param v_viz #vector of z velocity of pi.
Matthieu Schaller's avatar
Matthieu Schaller committed
61
 * @param icount_align (return) Interaction count after the remainder
James Willis's avatar
James Willis committed
62
 * interactions have been performed, should be a multiple of the vector length.
James Willis's avatar
James Willis committed
63
 */
James Willis's avatar
James Willis committed
64
__attribute__((always_inline)) INLINE static void calcRemInteractions(
James Willis's avatar
James Willis committed
65
66
    struct c2_cache *const int_cache, const int icount, vector *v_rhoSum,
    vector *v_rho_dhSum, vector *v_wcountSum, vector *v_wcount_dhSum,
67
68
69
    vector *v_div_vSum, vector *v_curlvxSum, vector *v_curlvySum,
    vector *v_curlvzSum, vector v_hi_inv, vector v_vix, vector v_viy,
    vector v_viz, int *icount_align) {
70

71
  mask_t int_mask, int_mask2;
James Willis's avatar
James Willis committed
72
73

  /* Work out the number of remainder interactions and pad secondary cache. */
74
75
76
77
78
79
  *icount_align = icount;
  int rem = icount % (NUM_VEC_PROC * VEC_SIZE);
  if (rem != 0) {
    int pad = (NUM_VEC_PROC * VEC_SIZE) - rem;
    *icount_align += pad;

80
    /* Initialise masks to true. */
81
82
    vec_init_mask_true(int_mask);
    vec_init_mask_true(int_mask2);
83

James Willis's avatar
James Willis committed
84
85
86
    /* Pad secondary cache so that there are no contributions in the interaction
     * function. */
    for (int i = icount; i < *icount_align; i++) {
87
88
89
90
91
92
93
94
      int_cache->mq[i] = 0.f;
      int_cache->r2q[i] = 1.f;
      int_cache->dxq[i] = 0.f;
      int_cache->dyq[i] = 0.f;
      int_cache->dzq[i] = 0.f;
      int_cache->vxq[i] = 0.f;
      int_cache->vyq[i] = 0.f;
      int_cache->vzq[i] = 0.f;
95
96
97
98
    }

    /* Zero parts of mask that represent the padded values.*/
    if (pad < VEC_SIZE) {
James Willis's avatar
James Willis committed
99
      vec_pad_mask(int_mask2, pad);
James Willis's avatar
James Willis committed
100
    } else {
James Willis's avatar
James Willis committed
101
      vec_pad_mask(int_mask, VEC_SIZE - rem);
102
      vec_zero_mask(int_mask2);
103
104
    }

James Willis's avatar
James Willis committed
105
106
    /* Perform remainder interaction and remove remainder from aligned
     * interaction count. */
107
    *icount_align = icount - rem;
James Willis's avatar
James Willis committed
108
109
110
111
112
    runner_iact_nonsym_2_vec_density(
        &int_cache->r2q[*icount_align], &int_cache->dxq[*icount_align],
        &int_cache->dyq[*icount_align], &int_cache->dzq[*icount_align],
        v_hi_inv, v_vix, v_viy, v_viz, &int_cache->vxq[*icount_align],
        &int_cache->vyq[*icount_align], &int_cache->vzq[*icount_align],
James Willis's avatar
James Willis committed
113
        &int_cache->mq[*icount_align], v_rhoSum, v_rho_dhSum, v_wcountSum,
114
115
        v_wcount_dhSum, v_div_vSum, v_curlvxSum, v_curlvySum, v_curlvzSum,
        int_mask, int_mask2, 1);
116
117
118
  }
}

James Willis's avatar
James Willis committed
119
/**
James Willis's avatar
James Willis committed
120
121
 * @brief Left-packs the values needed by an interaction into the secondary
 * cache (Supports AVX, AVX2 and AVX512 instruction sets).
James Willis's avatar
James Willis committed
122
123
 *
 * @param mask Contains which particles need to interact.
Matthieu Schaller's avatar
Matthieu Schaller committed
124
 * @param pjd Index of the particle to store into.
James Willis's avatar
James Willis committed
125
126
127
128
129
 * @param v_r2 #vector of the separation between two particles squared.
 * @param v_dx #vector of the x separation between two particles.
 * @param v_dy #vector of the y separation between two particles.
 * @param v_dz #vector of the z separation between two particles.
 * @param cell_cache #cache of all particles in the cell.
Matthieu Schaller's avatar
Matthieu Schaller committed
130
 * @param int_cache (return) secondary #cache of interactions between two
James Willis's avatar
James Willis committed
131
 * particles.
James Willis's avatar
James Willis committed
132
 * @param icount Interaction count.
133
134
 * @param v_rhoSum #vector holding the cumulative sum of the density update on
 * pi.
James Willis's avatar
James Willis committed
135
 * @param v_rho_dhSum #vector holding the cumulative sum of the density gradient
James Willis's avatar
James Willis committed
136
 * update on pi.
James Willis's avatar
James Willis committed
137
 * @param v_wcountSum #vector holding the cumulative sum of the wcount update on
James Willis's avatar
James Willis committed
138
 * pi.
139
140
 * @param v_wcount_dhSum #vector holding the cumulative sum of the wcount
 * gradient
James Willis's avatar
James Willis committed
141
 * update on pi.
James Willis's avatar
James Willis committed
142
 * @param v_div_vSum #vector holding the cumulative sum of the divergence update
James Willis's avatar
James Willis committed
143
 * on pi.
144
145
 * @param v_curlvxSum #vector holding the cumulative sum of the curl of vx
 * update
James Willis's avatar
James Willis committed
146
 * on pi.
147
148
 * @param v_curlvySum #vector holding the cumulative sum of the curl of vy
 * update
James Willis's avatar
James Willis committed
149
 * on pi.
150
151
 * @param v_curlvzSum #vector holding the cumulative sum of the curl of vz
 * update
James Willis's avatar
James Willis committed
152
 * on pi.
James Willis's avatar
James Willis committed
153
154
155
156
157
 * @param v_hi_inv #vector of 1/h for pi.
 * @param v_vix #vector of x velocity of pi.
 * @param v_viy #vector of y velocity of pi.
 * @param v_viz #vector of z velocity of pi.
 */
James Willis's avatar
James Willis committed
158
__attribute__((always_inline)) INLINE static void storeInteractions(
159
    const int mask, const int pjd, vector *v_r2, vector *v_dx, vector *v_dy,
James Willis's avatar
James Willis committed
160
    vector *v_dz, const struct cache *const cell_cache,
James Willis's avatar
James Willis committed
161
162
    struct c2_cache *const int_cache, int *icount, vector *v_rhoSum,
    vector *v_rho_dhSum, vector *v_wcountSum, vector *v_wcount_dhSum,
163
164
165
    vector *v_div_vSum, vector *v_curlvxSum, vector *v_curlvySum,
    vector *v_curlvzSum, vector v_hi_inv, vector v_vix, vector v_viy,
    vector v_viz) {
James Willis's avatar
James Willis committed
166
167
168

/* Left-pack values needed into the secondary cache using the interaction mask.
 */
169
#if defined(HAVE_AVX2) || defined(HAVE_AVX512_F)
170
171
172
173
174
175
176
  mask_t packed_mask;
  VEC_FORM_PACKED_MASK(mask, packed_mask);

  VEC_LEFT_PACK(v_r2->v, packed_mask, &int_cache->r2q[*icount]);
  VEC_LEFT_PACK(v_dx->v, packed_mask, &int_cache->dxq[*icount]);
  VEC_LEFT_PACK(v_dy->v, packed_mask, &int_cache->dyq[*icount]);
  VEC_LEFT_PACK(v_dz->v, packed_mask, &int_cache->dzq[*icount]);
James Willis's avatar
James Willis committed
177
178
179
180
181
182
183
184
  VEC_LEFT_PACK(vec_load(&cell_cache->m[pjd]), packed_mask,
                &int_cache->mq[*icount]);
  VEC_LEFT_PACK(vec_load(&cell_cache->vx[pjd]), packed_mask,
                &int_cache->vxq[*icount]);
  VEC_LEFT_PACK(vec_load(&cell_cache->vy[pjd]), packed_mask,
                &int_cache->vyq[*icount]);
  VEC_LEFT_PACK(vec_load(&cell_cache->vz[pjd]), packed_mask,
                &int_cache->vzq[*icount]);
185
186
187

  /* Increment interaction count by number of bits set in mask. */
  (*icount) += __builtin_popcount(mask);
188
#else
James Willis's avatar
James Willis committed
189
  /* Quicker to do it serially in AVX rather than use intrinsics. */
James Willis's avatar
James Willis committed
190
  for (int bit_index = 0; bit_index < VEC_SIZE; bit_index++) {
191
192
    if (mask & (1 << bit_index)) {
      /* Add this interaction to the queue. */
193
194
195
196
197
198
199
200
      int_cache->r2q[*icount] = v_r2->f[bit_index];
      int_cache->dxq[*icount] = v_dx->f[bit_index];
      int_cache->dyq[*icount] = v_dy->f[bit_index];
      int_cache->dzq[*icount] = v_dz->f[bit_index];
      int_cache->mq[*icount] = cell_cache->m[pjd + bit_index];
      int_cache->vxq[*icount] = cell_cache->vx[pjd + bit_index];
      int_cache->vyq[*icount] = cell_cache->vy[pjd + bit_index];
      int_cache->vzq[*icount] = cell_cache->vz[pjd + bit_index];
201
202
203
204

      (*icount)++;
    }
  }
205

James Willis's avatar
James Willis committed
206
207
#endif /* defined(HAVE_AVX2) || defined(HAVE_AVX512_F) */

James Willis's avatar
James Willis committed
208
  /* Flush the c2 cache if it has reached capacity. */
James Willis's avatar
James Willis committed
209
  if (*icount >= (C2_CACHE_SIZE - (NUM_VEC_PROC * VEC_SIZE))) {
210
211

    int icount_align = *icount;
James Willis's avatar
James Willis committed
212

James Willis's avatar
James Willis committed
213
    /* Peform remainder interactions. */
James Willis's avatar
James Willis committed
214
    calcRemInteractions(int_cache, *icount, v_rhoSum, v_rho_dhSum, v_wcountSum,
215
216
217
                        v_wcount_dhSum, v_div_vSum, v_curlvxSum, v_curlvySum,
                        v_curlvzSum, v_hi_inv, v_vix, v_viy, v_viz,
                        &icount_align);
218

219
    mask_t int_mask, int_mask2;
220
221
    vec_init_mask_true(int_mask);
    vec_init_mask_true(int_mask2);
James Willis's avatar
James Willis committed
222
223

    /* Perform interactions. */
224
    for (int j = 0; j < icount_align; j += (NUM_VEC_PROC * VEC_SIZE)) {
James Willis's avatar
James Willis committed
225
      runner_iact_nonsym_2_vec_density(
226
227
          &int_cache->r2q[j], &int_cache->dxq[j], &int_cache->dyq[j],
          &int_cache->dzq[j], v_hi_inv, v_vix, v_viy, v_viz, &int_cache->vxq[j],
James Willis's avatar
James Willis committed
228
          &int_cache->vyq[j], &int_cache->vzq[j], &int_cache->mq[j], v_rhoSum,
229
230
          v_rho_dhSum, v_wcountSum, v_wcount_dhSum, v_div_vSum, v_curlvxSum,
          v_curlvySum, v_curlvzSum, int_mask, int_mask2, 0);
231
    }
James Willis's avatar
James Willis committed
232
233

    /* Reset interaction count. */
234
235
236
    *icount = 0;
  }
}
237

238
/**
239
240
 * @brief Populates the arrays max_index_i and max_index_j with the maximum
 * indices of
James Willis's avatar
James Willis committed
241
242
243
 * particles into their neighbouring cells. Also finds the first pi that
 * interacts with any particle in cj and the last pj that interacts with any
 * particle in ci.
244
 *
James Willis's avatar
James Willis committed
245
246
247
248
249
250
 * @param ci #cell pointer to ci
 * @param cj #cell pointer to cj
 * @param sort_i #entry array for particle distance in ci
 * @param sort_j #entry array for particle distance in cj
 * @param dx_max maximum particle movement allowed in cell
 * @param rshift cutoff shift
251
252
253
254
 * @param hi_max Maximal smoothing length in cell ci
 * @param hj_max Maximal smoothing length in cell cj
 * @param di_max Maximal position on the axis that can interact in cell ci
 * @param dj_min Minimal position on the axis that can interact in cell ci
255
 * @param max_index_i array to hold the maximum distances of pi particles into
256
 * #cell cj
257
 * @param max_index_j array to hold the maximum distances of pj particles into
258
 * #cell cj
James Willis's avatar
James Willis committed
259
260
 * @param init_pi first pi to interact with a pj particle
 * @param init_pj last pj to interact with a pi particle
261
 * @param max_active_bin The largest time-bin active during this step.
James Willis's avatar
James Willis committed
262
 */
263
__attribute__((always_inline)) INLINE static void populate_max_index_no_cache(
James Willis's avatar
James Willis committed
264
265
    const struct cell *ci, const struct cell *cj,
    const struct entry *restrict sort_i, const struct entry *restrict sort_j,
266
267
    const float dx_max, const float rshift, const double hi_max,
    const double hj_max, const double di_max, const double dj_min,
268
    int *max_index_i, int *max_index_j, int *init_pi, int *init_pj,
269
    const timebin_t max_active_bin) {
270

271
272
  const struct part *restrict parts_i = ci->parts;
  const struct part *restrict parts_j = cj->parts;
273
274

  int first_pi = 0, last_pj = cj->count - 1;
275
  int temp;
276

James Willis's avatar
James Willis committed
277
278
  /* Find the leftmost active particle in cell i that interacts with any
   * particle in cell j. */
279
  first_pi = ci->count;
280
  int active_id = first_pi - 1;
281
  while (first_pi > 0 && sort_i[first_pi - 1].d + dx_max + hi_max > dj_min) {
282
    first_pi--;
283
    /* Store the index of the particle if it is active. */
James Willis's avatar
James Willis committed
284
285
    if (part_is_active_no_debug(&parts_i[sort_i[first_pi].i], max_active_bin))
      active_id = first_pi;
286
  }
James Willis's avatar
James Willis committed
287

288
289
  /* Set the first active pi in range of any particle in cell j. */
  first_pi = active_id;
290

291
  /* Find the maximum index into cell j for each particle in range in cell i. */
James Willis's avatar
James Willis committed
292
  if (first_pi < ci->count) {
293

294
295
    /* Start from the first particle in cell j. */
    temp = 0;
296

297
    const struct part *pi = &parts_i[sort_i[first_pi].i];
298
299
    const float first_di =
        sort_i[first_pi].d + pi->h * kernel_gamma + dx_max - rshift;
300

James Willis's avatar
James Willis committed
301
302
    /* Loop through particles in cell j until they are not in range of pi.
     * Make sure that temp stays between 0 and cj->count - 1.*/
303
    while (temp < cj->count - 1 && first_di > sort_j[temp].d) temp++;
304

305
    max_index_i[first_pi] = temp;
306

307
    /* Populate max_index_i for remaining particles that are within range. */
James Willis's avatar
James Willis committed
308
    for (int i = first_pi + 1; i < ci->count; i++) {
309
      temp = max_index_i[i - 1];
310
      pi = &parts_i[sort_i[i].i];
311

312
      const float di = sort_i[i].d + pi->h * kernel_gamma + dx_max - rshift;
313

James Willis's avatar
James Willis committed
314
      /* Make sure that temp stays between 0 and cj->count - 1.*/
315
      while (temp < cj->count - 1 && di > sort_j[temp].d) temp++;
316

317
      max_index_i[i] = temp;
318
    }
James Willis's avatar
James Willis committed
319
  } else {
320
321
    /* Make sure that max index is set to first particle in cj.*/
    max_index_i[ci->count - 1] = 0;
322
323
  }

James Willis's avatar
James Willis committed
324
325
  /* Find the rightmost active particle in cell j that interacts with any
   * particle in cell i. */
326
  last_pj = -1;
327
  active_id = last_pj;
James Willis's avatar
James Willis committed
328
329
  while (last_pj < cj->count &&
         sort_j[last_pj + 1].d - hj_max - dx_max < di_max) {
330
    last_pj++;
331
    /* Store the index of the particle if it is active. */
James Willis's avatar
James Willis committed
332
333
    if (part_is_active_no_debug(&parts_j[sort_j[last_pj].i], max_active_bin))
      active_id = last_pj;
334
  }
James Willis's avatar
James Willis committed
335

336
  /* Set the last active pj in range of any particle in cell i. */
337
  last_pj = active_id;
Matthieu Schaller's avatar
Matthieu Schaller committed
338

339
  /* Find the maximum index into cell i for each particle in range in cell j. */
340
  if (last_pj >= 0) {
Matthieu Schaller's avatar
Matthieu Schaller committed
341

342
343
    /* Start from the last particle in cell i. */
    temp = ci->count - 1;
344

345
    const struct part *pj = &parts_j[sort_j[last_pj].i];
346
347
    const float last_dj =
        sort_j[last_pj].d - dx_max - pj->h * kernel_gamma + rshift;
348

349
    /* Loop through particles in cell i until they are not in range of pj. */
350
    while (temp > 0 && last_dj < sort_i[temp].d) temp--;
351

352
    max_index_j[last_pj] = temp;
353

354
    /* Populate max_index_j for remaining particles that are within range. */
James Willis's avatar
James Willis committed
355
    for (int i = last_pj - 1; i >= 0; i--) {
356
      temp = max_index_j[i + 1];
357
      pj = &parts_j[sort_j[i].i];
358
      const float dj = sort_j[i].d - dx_max - (pj->h * kernel_gamma) + rshift;
359

360
      while (temp > 0 && dj < sort_i[temp].d) temp--;
361

362
363
      max_index_j[i] = temp;
    }
James Willis's avatar
James Willis committed
364
  } else {
365
    /* Make sure that max index is set to last particle in ci.*/
James Willis's avatar
James Willis committed
366
    max_index_j[0] = ci->count - 1;
367
368
  }

James Willis's avatar
James Willis committed
369
370
  *init_pi = first_pi;
  *init_pj = last_pj;
371
}
372

James Willis's avatar
James Willis committed
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/**
 * @brief Populates the arrays max_index_i and max_index_j with the maximum
 * indices of
 * particles into their neighbouring cells. Also finds the first pi that
 * interacts with any particle in cj and the last pj that interacts with any
 * particle in ci.
 *
 * @param ci #cell pointer to ci
 * @param cj #cell pointer to cj
 * @param sort_i #entry array for particle distance in ci
 * @param sort_j #entry array for particle distance in cj
 * @param dx_max maximum particle movement allowed in cell
 * @param rshift cutoff shift
 * @param hi_max_raw Maximal smoothing length in cell ci
 * @param hj_max_raw Maximal smoothing length in cell cj
 * @param hi_max Maximal smoothing length in cell ci scaled by kernel_gamma
 * @param hj_max Maximal smoothing length in cell cj scaled by kernel_gamma
 * @param di_max Maximal position on the axis that can interact in cell ci
 * @param dj_min Minimal position on the axis that can interact in cell ci
 * @param max_index_i array to hold the maximum distances of pi particles into
 * #cell cj
 * @param max_index_j array to hold the maximum distances of pj particles into
 * #cell cj
 * @param init_pi first pi to interact with a pj particle
 * @param init_pj last pj to interact with a pi particle
 * @param max_active_bin The largest time-bin active during this step.
 */
James Willis's avatar
James Willis committed
400
401
402
403
404
__attribute__((always_inline)) INLINE static void
populate_max_index_no_cache_force(const struct cell *ci, const struct cell *cj,
                                  const struct entry *restrict sort_i,
                                  const struct entry *restrict sort_j,
                                  const float dx_max, const float rshift,
James Willis's avatar
James Willis committed
405
406
407
408
409
                                  const double hi_max_raw,
                                  const double hj_max_raw, const double hi_max,
                                  const double hj_max, const double di_max,
                                  const double dj_min, int *max_index_i,
                                  int *max_index_j, int *init_pi, int *init_pj,
410
                                  const timebin_t max_active_bin) {
411

412
413
  const struct part *restrict parts_i = ci->parts;
  const struct part *restrict parts_j = cj->parts;
James Willis's avatar
James Willis committed
414

415
  int first_pi = 0, last_pj = cj->count - 1;
416
417
418
419
420
421
  int temp;

  /* Find the leftmost active particle in cell i that interacts with any
   * particle in cell j. */
  first_pi = ci->count;
  int active_id = first_pi - 1;
James Willis's avatar
James Willis committed
422
423
  while (first_pi > 0 &&
         sort_i[first_pi - 1].d + dx_max + max(hi_max, hj_max) > dj_min) {
424
425
    first_pi--;
    /* Store the index of the particle if it is active. */
James Willis's avatar
James Willis committed
426
427
    if (part_is_active_no_debug(&parts_i[sort_i[first_pi].i], max_active_bin))
      active_id = first_pi;
428
  }
429

430
431
432
433
434
435
436
437
438
439
  /* Set the first active pi in range of any particle in cell j. */
  first_pi = active_id;

  /* Find the maximum index into cell j for each particle in range in cell i. */
  if (first_pi < ci->count) {

    /* Start from the first particle in cell j. */
    temp = 0;

    const struct part *pi = &parts_i[sort_i[first_pi].i];
James Willis's avatar
James Willis committed
440
441
442
    const float first_di = sort_i[first_pi].d +
                           max(pi->h, hj_max_raw) * kernel_gamma + dx_max -
                           rshift;
443

James Willis's avatar
James Willis committed
444
445
    /* Loop through particles in cell j until they are not in range of pi.
     * Make sure that temp stays between 0 and cj->count - 1.*/
446
    while (temp < cj->count - 1 && first_di > sort_j[temp].d) temp++;
447
448
449
450
451
452
453
454

    max_index_i[first_pi] = temp;

    /* Populate max_index_i for remaining particles that are within range. */
    for (int i = first_pi + 1; i < ci->count; i++) {
      temp = max_index_i[i - 1];
      pi = &parts_i[sort_i[i].i];

James Willis's avatar
James Willis committed
455
456
457
      const float di =
          sort_i[i].d + max(pi->h, hj_max_raw) * kernel_gamma + dx_max - rshift;

James Willis's avatar
James Willis committed
458
      /* Make sure that temp stays between 0 and cj->count - 1.*/
459
      while (temp < cj->count - 1 && di > sort_j[temp].d) temp++;
James Willis's avatar
James Willis committed
460

461
      max_index_i[i] = temp;
462
    }
463
464
465
  } else {
    /* Make sure that max index is set to first particle in cj.*/
    max_index_i[ci->count - 1] = 0;
466
467
  }

468
469
470
471
472
  /* Find the rightmost active particle in cell j that interacts with any
   * particle in cell i. */
  last_pj = -1;
  active_id = last_pj;
  while (last_pj < cj->count &&
473
         sort_j[last_pj + 1].d - max(hj_max, hi_max) - dx_max < di_max) {
474
475
    last_pj++;
    /* Store the index of the particle if it is active. */
James Willis's avatar
James Willis committed
476
477
    if (part_is_active_no_debug(&parts_j[sort_j[last_pj].i], max_active_bin))
      active_id = last_pj;
478
  }
James Willis's avatar
James Willis committed
479

480
481
  /* Set the last active pj in range of any particle in cell i. */
  last_pj = active_id;
Matthieu Schaller's avatar
Matthieu Schaller committed
482

483
  /* Find the maximum index into cell i for each particle in range in cell j. */
484
  if (last_pj >= 0) {
Matthieu Schaller's avatar
Matthieu Schaller committed
485

486
487
488
489
    /* Start from the last particle in cell i. */
    temp = ci->count - 1;

    const struct part *pj = &parts_j[sort_j[last_pj].i];
James Willis's avatar
James Willis committed
490
491
    const float last_dj = sort_j[last_pj].d - dx_max -
                          max(pj->h, hi_max_raw) * kernel_gamma + rshift;
492
493

    /* Loop through particles in cell i until they are not in range of pj. */
494
    while (temp > 0 && last_dj < sort_i[temp].d) temp--;
James Willis's avatar
James Willis committed
495

496
497
498
499
500
501
502
    max_index_j[last_pj] = temp;

    /* Populate max_index_j for remaining particles that are within range. */
    for (int i = last_pj - 1; i >= 0; i--) {
      temp = max_index_j[i + 1];
      pj = &parts_j[sort_j[i].i];

James Willis's avatar
James Willis committed
503
504
505
      const float dj = sort_j[i].d - dx_max -
                       (max(pj->h, hi_max_raw) * kernel_gamma) + rshift;

506
      while (temp > 0 && dj < sort_i[temp].d) temp--;
James Willis's avatar
James Willis committed
507

508
      max_index_j[i] = temp;
509
    }
510
511
512
  } else {
    /* Make sure that max index is set to last particle in ci.*/
    max_index_j[0] = ci->count - 1;
513
514
  }

James Willis's avatar
James Willis committed
515
516
  *init_pi = first_pi;
  *init_pj = last_pj;
517
}
518

519
#endif /* WITH_VECTORIZATION && GADGET2_SPH */
520
521

/**
James Willis's avatar
James Willis committed
522
523
 * @brief Compute the cell self-interaction (non-symmetric) using vector
 * intrinsics with one particle pi at a time.
524
525
526
527
 *
 * @param r The #runner.
 * @param c The #cell.
 */
James Willis's avatar
James Willis committed
528
529
__attribute__((always_inline)) INLINE void runner_doself1_density_vec(
    struct runner *r, struct cell *restrict c) {
530

531
532
#if defined(WITH_VECTORIZATION) && defined(GADGET2_SPH)

Matthieu Schaller's avatar
Matthieu Schaller committed
533
534
535
  /* Get some local variables */
  const struct engine *e = r->e;
  const timebin_t max_active_bin = e->max_active_bin;
536
537
  struct part *restrict parts = c->parts;
  const int count = c->count;
James Willis's avatar
James Willis committed
538

Matthieu Schaller's avatar
Matthieu Schaller committed
539
  TIMER_TIC;
540

Matthieu Schaller's avatar
Matthieu Schaller committed
541
  /* Anything to do here? */
542
543
  if (!cell_is_active(c, e)) return;

544
  if (!cell_are_part_drifted(c, e)) error("Interacting undrifted cell.");
545

546
547
548
549
550
551
552
553
#ifdef SWIFT_DEBUG_CHECKS
  for (int i = 0; i < count; i++) {
    /* Check that particles have been drifted to the current time */
    if (parts[i].ti_drift != e->ti_current)
      error("Particle pi not drifted to current time");
  }
#endif

James Willis's avatar
James Willis committed
554
  /* Get the particle cache from the runner and re-allocate
555
   * the cache if it is not big enough for the cell. */
556
  struct cache *restrict cell_cache = &r->ci_cache;
James Willis's avatar
James Willis committed
557

558
  if (cell_cache->count < count) cache_init(cell_cache, count);
559

James Willis's avatar
James Willis committed
560
  /* Read the particles from the cell and store them locally in the cache. */
James Willis's avatar
James Willis committed
561
  cache_read_particles(c, cell_cache);
562
563
564

  /* Create secondary cache to store particle interactions. */
  struct c2_cache int_cache;
565
566
567
568
569

  /* Loop over the particles in the cell. */
  for (int pid = 0; pid < count; pid++) {

    /* Get a pointer to the ith particle. */
Matthieu Schaller's avatar
Matthieu Schaller committed
570
    struct part *restrict pi = &parts[pid];
571
572

    /* Is the ith particle active? */
573
    if (!part_is_active_no_debug(pi, max_active_bin)) continue;
574
575
576

    const float hi = cell_cache->h[pid];

James Willis's avatar
James Willis committed
577
    /* Fill particle pi vectors. */
578
579
580
581
582
583
584
    const vector v_pix = vector_set1(cell_cache->x[pid]);
    const vector v_piy = vector_set1(cell_cache->y[pid]);
    const vector v_piz = vector_set1(cell_cache->z[pid]);
    const vector v_hi = vector_set1(hi);
    const vector v_vix = vector_set1(cell_cache->vx[pid]);
    const vector v_viy = vector_set1(cell_cache->vy[pid]);
    const vector v_viz = vector_set1(cell_cache->vz[pid]);
585
586

    const float hig2 = hi * hi * kernel_gamma2;
587
    const vector v_hig2 = vector_set1(hig2);
James Willis's avatar
James Willis committed
588

James Willis's avatar
James Willis committed
589
    /* Get the inverse of hi. */
590
    vector v_hi_inv = vec_reciprocal(v_hi);
James Willis's avatar
James Willis committed
591

592
593
594
595
596
597
598
599
600
    /* Reset cumulative sums of update vectors. */
    vector v_rhoSum = vector_setzero();
    vector v_rho_dhSum = vector_setzero();
    vector v_wcountSum = vector_setzero();
    vector v_wcount_dhSum = vector_setzero();
    vector v_div_vSum = vector_setzero();
    vector v_curlvxSum = vector_setzero();
    vector v_curlvySum = vector_setzero();
    vector v_curlvzSum = vector_setzero();
601
602

    /* Pad cache if there is a serial remainder. */
Matthieu Schaller's avatar
Matthieu Schaller committed
603
    int count_align = count;
James Willis's avatar
James Willis committed
604
    const int rem = count % (NUM_VEC_PROC * VEC_SIZE);
605
    if (rem != 0) {
James Willis's avatar
James Willis committed
606
      count_align += (NUM_VEC_PROC * VEC_SIZE) - rem;
607
608
609
610

      /* Set positions to the same as particle pi so when the r2 > 0 mask is
       * applied these extra contributions are masked out.*/
      for (int i = count; i < count_align; i++) {
James Willis's avatar
James Willis committed
611
612
613
        cell_cache->x[i] = v_pix.f[0];
        cell_cache->y[i] = v_piy.f[0];
        cell_cache->z[i] = v_piz.f[0];
614
      }
615
616
    }

James Willis's avatar
James Willis committed
617
    /* The number of interactions for pi and the padded version of it to
618
     * make it a multiple of VEC_SIZE. */
619
620
    int icount = 0, icount_align = 0;

James Willis's avatar
James Willis committed
621
622
    /* Find all of particle pi's interacions and store needed values in the
     * secondary cache.*/
James Willis's avatar
James Willis committed
623
    for (int pjd = 0; pjd < count_align; pjd += (NUM_VEC_PROC * VEC_SIZE)) {
624
625

      /* Load 2 sets of vectors from the particle cache. */
626
627
628
      const vector v_pjx = vector_load(&cell_cache->x[pjd]);
      const vector v_pjy = vector_load(&cell_cache->y[pjd]);
      const vector v_pjz = vector_load(&cell_cache->z[pjd]);
629

630
631
632
      const vector v_pjx2 = vector_load(&cell_cache->x[pjd + VEC_SIZE]);
      const vector v_pjy2 = vector_load(&cell_cache->y[pjd + VEC_SIZE]);
      const vector v_pjz2 = vector_load(&cell_cache->z[pjd + VEC_SIZE]);
James Willis's avatar
James Willis committed
633

634
      /* Compute the pairwise distance. */
James Willis's avatar
James Willis committed
635
      vector v_dx, v_dy, v_dz, v_r2;
636
637
      vector v_dx_2, v_dy_2, v_dz_2, v_r2_2;

James Willis's avatar
James Willis committed
638
639
640
641
642
643
      v_dx.v = vec_sub(v_pix.v, v_pjx.v);
      v_dx_2.v = vec_sub(v_pix.v, v_pjx2.v);
      v_dy.v = vec_sub(v_piy.v, v_pjy.v);
      v_dy_2.v = vec_sub(v_piy.v, v_pjy2.v);
      v_dz.v = vec_sub(v_piz.v, v_pjz.v);
      v_dz_2.v = vec_sub(v_piz.v, v_pjz2.v);
644
645
646
647
648
649
650

      v_r2.v = vec_mul(v_dx.v, v_dx.v);
      v_r2_2.v = vec_mul(v_dx_2.v, v_dx_2.v);
      v_r2.v = vec_fma(v_dy.v, v_dy.v, v_r2.v);
      v_r2_2.v = vec_fma(v_dy_2.v, v_dy_2.v, v_r2_2.v);
      v_r2.v = vec_fma(v_dz.v, v_dz.v, v_r2.v);
      v_r2_2.v = vec_fma(v_dz_2.v, v_dz_2.v, v_r2_2.v);
James Willis's avatar
James Willis committed
651

652
      /* Form a mask from r2 < hig2 and r2 > 0.*/
James Willis's avatar
James Willis committed
653
654
      mask_t v_doi_mask, v_doi_mask_self_check, v_doi_mask2,
          v_doi_mask2_self_check;
655

James Willis's avatar
James Willis committed
656
      /* Form r2 > 0 mask and r2 < hig2 mask. */
657
      vec_create_mask(v_doi_mask_self_check, vec_cmp_gt(v_r2.v, vec_setzero()));
658
      vec_create_mask(v_doi_mask, vec_cmp_lt(v_r2.v, v_hig2.v));
659

James Willis's avatar
James Willis committed
660
      /* Form r2 > 0 mask and r2 < hig2 mask. */
James Willis's avatar
James Willis committed
661
662
      vec_create_mask(v_doi_mask2_self_check,
                      vec_cmp_gt(v_r2_2.v, vec_setzero()));
663
      vec_create_mask(v_doi_mask2, vec_cmp_lt(v_r2_2.v, v_hig2.v));
664

James Willis's avatar
James Willis committed
665
      /* Combine two masks and form integer masks. */
666
667
668
669
      const int doi_mask = vec_is_mask_true(v_doi_mask) &
                           vec_is_mask_true(v_doi_mask_self_check);
      const int doi_mask2 = vec_is_mask_true(v_doi_mask2) &
                            vec_is_mask_true(v_doi_mask2_self_check);
670

James Willis's avatar
James Willis committed
671
672
      /* If there are any interactions left pack interaction values into c2
       * cache. */
673
      if (doi_mask) {
James Willis's avatar
James Willis committed
674
        storeInteractions(doi_mask, pjd, &v_r2, &v_dx, &v_dy, &v_dz, cell_cache,
675
676
677
678
                          &int_cache, &icount, &v_rhoSum, &v_rho_dhSum,
                          &v_wcountSum, &v_wcount_dhSum, &v_div_vSum,
                          &v_curlvxSum, &v_curlvySum, &v_curlvzSum, v_hi_inv,
                          v_vix, v_viy, v_viz);
679
      }
James Willis's avatar
James Willis committed
680
681
      if (doi_mask2) {
        storeInteractions(doi_mask2, pjd + VEC_SIZE, &v_r2_2, &v_dx_2, &v_dy_2,
James Willis's avatar
James Willis committed
682
                          &v_dz_2, cell_cache, &int_cache, &icount, &v_rhoSum,
683
684
685
                          &v_rho_dhSum, &v_wcountSum, &v_wcount_dhSum,
                          &v_div_vSum, &v_curlvxSum, &v_curlvySum, &v_curlvzSum,
                          v_hi_inv, v_vix, v_viy, v_viz);
James Willis's avatar
James Willis committed
686
      }
687
688
    }

James Willis's avatar
James Willis committed
689
    /* Perform padded vector remainder interactions if any are present. */
690
691
692
693
    calcRemInteractions(&int_cache, icount, &v_rhoSum, &v_rho_dhSum,
                        &v_wcountSum, &v_wcount_dhSum, &v_div_vSum,
                        &v_curlvxSum, &v_curlvySum, &v_curlvzSum, v_hi_inv,
                        v_vix, v_viy, v_viz, &icount_align);
James Willis's avatar
James Willis committed
694
695
696

    /* Initialise masks to true in case remainder interactions have been
     * performed. */
697
    mask_t int_mask, int_mask2;
698
699
    vec_init_mask_true(int_mask);
    vec_init_mask_true(int_mask2);
700
701

    /* Perform interaction with 2 vectors. */
James Willis's avatar
James Willis committed
702
    for (int pjd = 0; pjd < icount_align; pjd += (NUM_VEC_PROC * VEC_SIZE)) {
James Willis's avatar
James Willis committed
703
704
705
706
      runner_iact_nonsym_2_vec_density(
          &int_cache.r2q[pjd], &int_cache.dxq[pjd], &int_cache.dyq[pjd],
          &int_cache.dzq[pjd], v_hi_inv, v_vix, v_viy, v_viz,
          &int_cache.vxq[pjd], &int_cache.vyq[pjd], &int_cache.vzq[pjd],
707
708
709
          &int_cache.mq[pjd], &v_rhoSum, &v_rho_dhSum, &v_wcountSum,
          &v_wcount_dhSum, &v_div_vSum, &v_curlvxSum, &v_curlvySum,
          &v_curlvzSum, int_mask, int_mask2, 0);
710
711
    }

James Willis's avatar
James Willis committed
712
713
    /* Perform horizontal adds on vector sums and store result in particle pi.
     */
James Willis's avatar
James Willis committed
714
715
716
717
718
719
720
721
    VEC_HADD(v_rhoSum, pi->rho);
    VEC_HADD(v_rho_dhSum, pi->density.rho_dh);
    VEC_HADD(v_wcountSum, pi->density.wcount);
    VEC_HADD(v_wcount_dhSum, pi->density.wcount_dh);
    VEC_HADD(v_div_vSum, pi->density.div_v);
    VEC_HADD(v_curlvxSum, pi->density.rot_v[0]);
    VEC_HADD(v_curlvySum, pi->density.rot_v[1]);
    VEC_HADD(v_curlvzSum, pi->density.rot_v[2]);
722
723
724
725
726

    /* Reset interaction count. */
    icount = 0;
  } /* loop over all particles. */

James Willis's avatar
James Willis committed
727
  TIMER_TOC(timer_doself_density);
728
729
730
731
732

#else

  error("Incorrectly calling vectorized Gadget-2 functions!");

733
#endif /* WITH_VECTORIZATION */
734
735
}

736
737
738
739
740
741
742
743
744
745
/**
 * @brief Compute the interactions between a cell pair, but only for the
 *      given indices in ci. (Vectorised)
 *
 * @param r The #runner.
 * @param c The first #cell.
 * @param parts The #part to interact.
 * @param ind The list of indices of particles in @c c to interact with.
 * @param pi_count The number of particles in @c ind.
 */
746
__attribute__((always_inline)) INLINE void runner_doself_subset_density_vec(
James Willis's avatar
James Willis committed
747
748
    struct runner *r, struct cell *restrict c, struct part *restrict parts,
    int *restrict ind, int pi_count) {
749

750
751
#if defined(WITH_VECTORIZATION) && defined(GADGET2_SPH)

752
753
  const int count = c->count;

754
  TIMER_TIC;
755
756
757
758
759

  /* Get the particle cache from the runner and re-allocate
   * the cache if it is not big enough for the cell. */
  struct cache *restrict cell_cache = &r->ci_cache;

760
  if (cell_cache->count < count) cache_init(cell_cache, count);
761
762
763
764
765
766
767

  /* Read the particles from the cell and store them locally in the cache. */
  cache_read_particles(c, cell_cache);

  /* Create secondary cache to store particle interactions. */
  struct c2_cache int_cache;

768
  /* Loop over the subset of particles in the parts that need updating. */
769
770
771
  for (int pid = 0; pid < pi_count; pid++) {

    /* Get a pointer to the ith particle. */
Matthieu Schaller's avatar
Matthieu Schaller committed
772
    struct part *pi = &parts[ind[pid]];
773

774
775
776
777
#ifdef SWIFT_DEBUG_CHECKS
    const struct engine *e = r->e;
    if (!part_is_active(pi, e)) error("Inactive particle in subset function!");
#endif
778

779
    const float hi = pi->h;
780
781

    /* Fill particle pi vectors. */
James Willis's avatar
James Willis committed
782
783
784
785
786
787
788
    const vector v_pix = vector_set1(pi->x[0] - c->loc[0]);
    const vector v_piy = vector_set1(pi->x[1] - c->loc[1]);
    const vector v_piz = vector_set1(pi->x[2] - c->loc[2]);
    const vector v_hi = vector_set1(hi);
    const vector v_vix = vector_set1(pi->v[0]);
    const vector v_viy = vector_set1(pi->v[1]);
    const vector v_viz = vector_set1(pi->v[2]);
789
790

    const float hig2 = hi * hi * kernel_gamma2;
James Willis's avatar
James Willis committed
791
    const vector v_hig2 = vector_set1(hig2);
792
793

    /* Get the inverse of hi. */
James Willis's avatar
James Willis committed
794
    vector v_hi_inv = vec_reciprocal(v_hi);
795

James Willis's avatar
James Willis committed
796
797
798
799
800
801
802
803
804
    /* Reset cumulative sums of update vectors. */
    vector v_rhoSum = vector_setzero();
    vector v_rho_dhSum = vector_setzero();
    vector v_wcountSum = vector_setzero();
    vector v_wcount_dhSum = vector_setzero();
    vector v_div_vSum = vector_setzero();
    vector v_curlvxSum = vector_setzero();
    vector v_curlvySum = vector_setzero();
    vector v_curlvzSum = vector_setzero();
805
806

    /* Pad cache if there is a serial remainder. */
James Willis's avatar
James Willis committed
807
    int count_align = count;
Matthieu Schaller's avatar
Matthieu Schaller committed
808
    const int rem = count % (NUM_VEC_PROC * VEC_SIZE);
809
    if (rem != 0) {
Matthieu Schaller's avatar
Matthieu Schaller committed
810
      const int pad = (NUM_VEC_PROC * VEC_SIZE) - rem;
811
812
813
814
815
816

      count_align += pad;

      /* Set positions to the same as particle pi so when the r2 > 0 mask is
       * applied these extra contributions are masked out.*/
      for (int i = count; i < count_align; i++) {
James Willis's avatar
James Willis committed
817
818
819
        cell_cache->x[i] = v_pix.f[0];
        cell_cache->y[i] = v_piy.f[0];
        cell_cache->z[i] = v_piz.f[0];
820
821
      }
    }
James Willis's avatar
James Willis committed
822
823

    /* The number of interactions for pi and the padded version of it to
824
     * make it a multiple of VEC_SIZE. */
825
    int icount = 0, icount_align = 0;
826
827
828

    /* Find all of particle pi's interacions and store needed values in the
     * secondary cache.*/
James Willis's avatar
James Willis committed
829
    for (int pjd = 0; pjd < count_align; pjd += (NUM_VEC_PROC * VEC_SIZE)) {
830
831

      /* Load 2 sets of vectors from the particle cache. */
James Willis's avatar
James Willis committed
832
833
834
      const vector v_pjx = vector_load(&cell_cache->x[pjd]);
      const vector v_pjy = vector_load(&cell_cache->y[pjd]);
      const vector v_pjz = vector_load(&cell_cache->z[pjd]);
835

James Willis's avatar
James Willis committed
836
837
838
      const vector v_pjx2 = vector_load(&cell_cache->x[pjd + VEC_SIZE]);
      const vector v_pjy2 = vector_load(&cell_cache->y[pjd + VEC_SIZE]);
      const vector v_pjz2 = vector_load(&cell_cache->z[pjd + VEC_SIZE]);
839
840

      /* Compute the pairwise distance. */
James Willis's avatar
James Willis committed
841
      vector v_dx, v_dy, v_dz, v_r2;
842
843
      vector v_dx_2, v_dy_2, v_dz_2, v_r2_2;

Matthieu Schaller's avatar
Matthieu Schaller committed
844
      /* p_i - p_j */
James Willis's avatar
James Willis committed
845
846
847
848
849
850
      v_dx.v = vec_sub(v_pix.v, v_pjx.v);
      v_dx_2.v = vec_sub(v_pix.v, v_pjx2.v);
      v_dy.v = vec_sub(v_piy.v, v_pjy.v);
      v_dy_2.v = vec_sub(v_piy.v, v_pjy2.v);
      v_dz.v = vec_sub(v_piz.v, v_pjz.v);
      v_dz_2.v = vec_sub(v_piz.v, v_pjz2.v);
851

Matthieu Schaller's avatar
Matthieu Schaller committed
852
      /* r2 = dx^2 + dy^2 + dz^2 */
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
      v_r2.v = vec_mul(v_dx.v, v_dx.v);
      v_r2_2.v = vec_mul(v_dx_2.v, v_dx_2.v);
      v_r2.v = vec_fma(v_dy.v, v_dy.v, v_r2.v);
      v_r2_2.v = vec_fma(v_dy_2.v, v_dy_2.v, v_r2_2.v);
      v_r2.v = vec_fma(v_dz.v, v_dz.v, v_r2.v);
      v_r2_2.v = vec_fma(v_dz_2.v, v_dz_2.v, v_r2_2.v);

      /* Form a mask from r2 < hig2 and r2 > 0.*/
      mask_t v_doi_mask, v_doi_mask_self_check, v_doi_mask2,
          v_doi_mask2_self_check;

      /* Form r2 > 0 mask and r2 < hig2 mask. */
      vec_create_mask(v_doi_mask_self_check, vec_cmp_gt(v_r2.v, vec_setzero()));
      vec_create_mask(v_doi_mask, vec_cmp_lt(v_r2.v, v_hig2.v));

      /* Form r2 > 0 mask and r2 < hig2 mask. */
      vec_create_mask(v_doi_mask2_self_check,
                      vec_cmp_gt(v_r2_2.v, vec_setzero()));
      vec_create_mask(v_doi_mask2, vec_cmp_lt(v_r2_2.v, v_hig2.v));

James Willis's avatar
James Willis committed
873
874
875
876
877
      /* Combine two masks and form integer masks. */
      const int doi_mask = vec_is_mask_true(v_doi_mask) &
                           vec_is_mask_true(v_doi_mask_self_check);
      const int doi_mask2 = vec_is_mask_true(v_doi_mask2) &
                            vec_is_mask_true(v_doi_mask2_self_check);
James Willis's avatar
James Willis committed
878

879
880
881
882
      /* If there are any interactions left pack interaction values into c2
       * cache. */
      if (doi_mask) {
        storeInteractions(doi_mask, pjd, &v_r2, &v_dx, &v_dy, &v_dz, cell_cache,
James Willis's avatar
James Willis committed
883
884
885
886
                          &int_cache, &icount, &v_rhoSum, &v_rho_dhSum,
                          &v_wcountSum, &v_wcount_dhSum, &v_div_vSum,
                          &v_curlvxSum, &v_curlvySum, &v_curlvzSum, v_hi_inv,
                          v_vix, v_viy, v_viz);
887
888
889
      }
      if (doi_mask2) {
        storeInteractions(doi_mask2, pjd + VEC_SIZE, &v_r2_2, &v_dx_2, &v_dy_2,
James Willis's avatar
James Willis committed
890
                          &v_dz_2, cell_cache, &int_cache, &icount, &v_rhoSum,
James Willis's avatar
James Willis committed
891
892
893
                          &v_rho_dhSum, &v_wcountSum, &v_wcount_dhSum,
                          &v_div_vSum, &v_curlvxSum, &v_curlvySum, &v_curlvzSum,
                          v_hi_inv, v_vix, v_viy, v_viz);
894
895
896
897
      }
    }

    /* Perform padded vector remainder interactions if any are present. */
James Willis's avatar
James Willis committed
898
899
900
901
    calcRemInteractions(&int_cache, icount, &v_rhoSum, &v_rho_dhSum,
                        &v_wcountSum, &v_wcount_dhSum, &v_div_vSum,
                        &v_curlvxSum, &v_curlvySum, &v_curlvzSum, v_hi_inv,
                        v_vix, v_viy, v_viz, &icount_align);
902
903
904
905
906
907
908
909

    /* Initialise masks to true in case remainder interactions have been
     * performed. */
    mask_t int_mask, int_mask2;
    vec_init_mask_true(int_mask);
    vec_init_mask_true(int_mask2);

    /* Perform interaction with 2 vectors. */
James Willis's avatar
James Willis committed
910
    for (int pjd = 0; pjd < icount_align; pjd += (NUM_VEC_PROC * VEC_SIZE)) {
911
912
913
914
      runner_iact_nonsym_2_vec_density(
          &int_cache.r2q[pjd], &int_cache.dxq[pjd], &int_cache.dyq[pjd],
          &int_cache.dzq[pjd], v_hi_inv, v_vix, v_viy, v_viz,
          &int_cache.vxq[pjd], &int_cache.vyq[pjd], &int_cache.vzq[pjd],
James Willis's avatar
James Willis committed
915
916
917
          &int_cache.mq[pjd], &v_rhoSum, &v_rho_dhSum, &v_wcountSum,
          &v_wcount_dhSum, &v_div_vSum, &v_curlvxSum, &v_curlvySum,
          &v_curlvzSum, int_mask, int_mask2, 0);
918
919
920
921
    }

    /* Perform horizontal adds on vector sums and store result in particle pi.
     */
James Willis's avatar
James Willis committed
922
923
924
925
926
927
928
929
    VEC_HADD(v_rhoSum, pi->rho);
    VEC_HADD(v_rho_dhSum, pi->density.rho_dh);
    VEC_HADD(v_wcountSum, pi->density.wcount);
    VEC_HADD(v_wcount_dhSum, pi->density.wcount_dh);
    VEC_HADD(v_div_vSum, pi->density