runner_doiact_vec.c 68.7 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
31
#ifdef WITH_VECTORIZATION
static const vector kernel_gamma2_vec = FILL_VEC(kernel_gamma2);

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

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

  /* Work out the number of remainder interactions and pad secondary cache. */
73
74
75
76
77
78
  *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;

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

James Willis's avatar
James Willis committed
83
84
85
    /* Pad secondary cache so that there are no contributions in the interaction
     * function. */
    for (int i = icount; i < *icount_align; i++) {
86
87
88
89
90
91
92
93
      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;
94
95
96
97
    }

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

James Willis's avatar
James Willis committed
104
105
    /* Perform remainder interaction and remove remainder from aligned
     * interaction count. */
106
    *icount_align = icount - rem;
James Willis's avatar
James Willis committed
107
108
109
110
111
    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
112
        &int_cache->mq[*icount_align], v_rhoSum, v_rho_dhSum, v_wcountSum,
113
114
        v_wcount_dhSum, v_div_vSum, v_curlvxSum, v_curlvySum, v_curlvzSum,
        int_mask, int_mask2, 1);
115
116
117
  }
}

James Willis's avatar
James Willis committed
118
/**
James Willis's avatar
James Willis committed
119
120
 * @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
121
122
 *
 * @param mask Contains which particles need to interact.
Matthieu Schaller's avatar
Matthieu Schaller committed
123
 * @param pjd Index of the particle to store into.
James Willis's avatar
James Willis committed
124
125
126
127
128
 * @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
129
 * @param int_cache (return) secondary #cache of interactions between two
James Willis's avatar
James Willis committed
130
 * particles.
James Willis's avatar
James Willis committed
131
 * @param icount Interaction count.
132
133
 * @param v_rhoSum #vector holding the cumulative sum of the density update on
 * pi.
James Willis's avatar
James Willis committed
134
 * @param v_rho_dhSum #vector holding the cumulative sum of the density gradient
James Willis's avatar
James Willis committed
135
 * update on pi.
James Willis's avatar
James Willis committed
136
 * @param v_wcountSum #vector holding the cumulative sum of the wcount update on
James Willis's avatar
James Willis committed
137
 * pi.
138
139
 * @param v_wcount_dhSum #vector holding the cumulative sum of the wcount
 * gradient
James Willis's avatar
James Willis committed
140
 * update on pi.
James Willis's avatar
James Willis committed
141
 * @param v_div_vSum #vector holding the cumulative sum of the divergence update
James Willis's avatar
James Willis committed
142
 * on pi.
143
144
 * @param v_curlvxSum #vector holding the cumulative sum of the curl of vx
 * update
James Willis's avatar
James Willis committed
145
 * on pi.
146
147
 * @param v_curlvySum #vector holding the cumulative sum of the curl of vy
 * update
James Willis's avatar
James Willis committed
148
 * on pi.
149
150
 * @param v_curlvzSum #vector holding the cumulative sum of the curl of vz
 * update
James Willis's avatar
James Willis committed
151
 * on pi.
James Willis's avatar
James Willis committed
152
153
154
155
156
 * @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
157
__attribute__((always_inline)) INLINE static void storeInteractions(
158
    const int mask, const int pjd, vector *v_r2, vector *v_dx, vector *v_dy,
James Willis's avatar
James Willis committed
159
    vector *v_dz, const struct cache *const cell_cache,
James Willis's avatar
James Willis committed
160
161
    struct c2_cache *const int_cache, int *icount, vector *v_rhoSum,
    vector *v_rho_dhSum, vector *v_wcountSum, vector *v_wcount_dhSum,
162
163
164
    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
165
166
167

/* Left-pack values needed into the secondary cache using the interaction mask.
 */
168
#if defined(HAVE_AVX2) || defined(HAVE_AVX512_F)
169
170
171
172
173
174
175
  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
176
177
178
179
180
181
182
183
  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]);
184
185
186

  /* Increment interaction count by number of bits set in mask. */
  (*icount) += __builtin_popcount(mask);
187
#else
James Willis's avatar
James Willis committed
188
  /* Quicker to do it serially in AVX rather than use intrinsics. */
James Willis's avatar
James Willis committed
189
  for (int bit_index = 0; bit_index < VEC_SIZE; bit_index++) {
190
191
    if (mask & (1 << bit_index)) {
      /* Add this interaction to the queue. */
192
193
194
195
196
197
198
199
      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];
200
201
202
203

      (*icount)++;
    }
  }
204

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

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

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

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

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

    /* Perform interactions. */
223
    for (int j = 0; j < icount_align; j += (NUM_VEC_PROC * VEC_SIZE)) {
James Willis's avatar
James Willis committed
224
      runner_iact_nonsym_2_vec_density(
225
226
          &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
227
          &int_cache->vyq[j], &int_cache->vzq[j], &int_cache->mq[j], v_rhoSum,
228
229
          v_rho_dhSum, v_wcountSum, v_wcount_dhSum, v_div_vSum, v_curlvxSum,
          v_curlvySum, v_curlvzSum, int_mask, int_mask2, 0);
230
    }
James Willis's avatar
James Willis committed
231
232

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

237
/**
238
239
 * @brief Populates the arrays max_index_i and max_index_j with the maximum
 * indices of
James Willis's avatar
James Willis committed
240
241
242
 * 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.
243
 *
James Willis's avatar
James Willis committed
244
245
246
247
248
249
 * @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
250
251
252
253
 * @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
254
 * @param max_index_i array to hold the maximum distances of pi particles into
255
 * #cell cj
256
 * @param max_index_j array to hold the maximum distances of pj particles into
257
 * #cell cj
James Willis's avatar
James Willis committed
258
259
 * @param init_pi first pi to interact with a pj particle
 * @param init_pj last pj to interact with a pi particle
260
 * @param max_active_bin The largest time-bin active during this step.
James Willis's avatar
James Willis committed
261
 */
262
__attribute__((always_inline)) INLINE static void populate_max_index_no_cache(
James Willis's avatar
James Willis committed
263
264
    const struct cell *ci, const struct cell *cj,
    const struct entry *restrict sort_i, const struct entry *restrict sort_j,
265
266
    const float dx_max, const float rshift, const double hi_max,
    const double hj_max, const double di_max, const double dj_min,
267
    int *max_index_i, int *max_index_j, int *init_pi, int *init_pj,
268
    const timebin_t max_active_bin) {
269

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

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

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

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

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

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

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

James Willis's avatar
James Willis committed
300
301
    /* 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.*/
302
    while (temp < cj->count - 1 && first_di > sort_j[temp].d) temp++;
303

304
    max_index_i[first_pi] = temp;
305

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

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

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

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

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

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

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

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

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

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

351
    max_index_j[last_pj] = temp;
352

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

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

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

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

James Willis's avatar
James Willis committed
372
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
/**
 * @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
399
400
401
402
403
__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
404
405
406
407
408
                                  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,
409
                                  const timebin_t max_active_bin) {
410

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

414
  int first_pi = 0, last_pj = cj->count - 1;
415
416
417
418
419
420
  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
421
422
  while (first_pi > 0 &&
         sort_i[first_pi - 1].d + dx_max + max(hi_max, hj_max) > dj_min) {
423
424
    first_pi--;
    /* Store the index of the particle if it is active. */
James Willis's avatar
James Willis committed
425
426
    if (part_is_active_no_debug(&parts_i[sort_i[first_pi].i], max_active_bin))
      active_id = first_pi;
427
  }
428

429
430
431
432
433
434
435
436
437
438
  /* 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
439
440
441
    const float first_di = sort_i[first_pi].d +
                           max(pi->h, hj_max_raw) * kernel_gamma + dx_max -
                           rshift;
442

James Willis's avatar
James Willis committed
443
444
    /* 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.*/
445
    while (temp < cj->count - 1 && first_di > sort_j[temp].d) temp++;
446
447
448
449
450
451
452
453

    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
454
455
456
      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
457
      /* Make sure that temp stays between 0 and cj->count - 1.*/
458
      while (temp < cj->count - 1 && di > sort_j[temp].d) temp++;
James Willis's avatar
James Willis committed
459

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

467
468
469
470
471
  /* 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 &&
472
         sort_j[last_pj + 1].d - max(hj_max, hi_max) - dx_max < di_max) {
473
474
    last_pj++;
    /* Store the index of the particle if it is active. */
James Willis's avatar
James Willis committed
475
476
    if (part_is_active_no_debug(&parts_j[sort_j[last_pj].i], max_active_bin))
      active_id = last_pj;
477
  }
James Willis's avatar
James Willis committed
478

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

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

485
486
487
488
    /* 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
489
490
    const float last_dj = sort_j[last_pj].d - dx_max -
                          max(pj->h, hi_max_raw) * kernel_gamma + rshift;
491
492

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

495
496
497
498
499
500
501
    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
502
503
504
      const float dj = sort_j[i].d - dx_max -
                       (max(pj->h, hi_max_raw) * kernel_gamma) + rshift;

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

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

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

James Willis's avatar
James Willis committed
518
#endif /* WITH_VECTORIZATION */
519
520

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

#ifdef WITH_VECTORIZATION
Matthieu Schaller's avatar
Matthieu Schaller committed
531
  const int num_vec_proc = NUM_VEC_PROC;
532

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

James Willis's avatar
James Willis committed
546
  /* Get the particle cache from the runner and re-allocate
547
   * the cache if it is not big enough for the cell. */
548
  struct cache *restrict cell_cache = &r->ci_cache;
James Willis's avatar
James Willis committed
549
550
551

  if (cell_cache->count < count) {
    cache_init(cell_cache, count);
552
553
  }

James Willis's avatar
James Willis committed
554
  /* Read the particles from the cell and store them locally in the cache. */
James Willis's avatar
James Willis committed
555
  cache_read_particles(c, cell_cache);
556
557
558
559

  /* Create secondary cache to store particle interactions. */
  struct c2_cache int_cache;
  int icount = 0, icount_align = 0;
560
561
562
563
564

  /* 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
565
    struct part *restrict pi = &parts[pid];
566
567

    /* Is the ith particle active? */
568
    if (!part_is_active_no_debug(pi, max_active_bin)) continue;
569

570
    vector v_r2;
571
572
573

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

James Willis's avatar
James Willis committed
574
    /* Fill particle pi vectors. */
575
576
577
578
579
580
581
    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]);
582
583

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

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

589
590
591
592
593
594
595
596
597
    /* 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();
598
599

    /* Pad cache if there is a serial remainder. */
Matthieu Schaller's avatar
Matthieu Schaller committed
600
601
    int count_align = count;
    const int rem = count % (num_vec_proc * VEC_SIZE);
602
    if (rem != 0) {
Matthieu Schaller's avatar
Matthieu Schaller committed
603
      count_align += (num_vec_proc * VEC_SIZE) - rem;
604
605
606
607

      /* 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
608
609
610
        cell_cache->x[i] = v_pix.f[0];
        cell_cache->y[i] = v_piy.f[0];
        cell_cache->z[i] = v_piz.f[0];
611
      }
612
613
    }

James Willis's avatar
James Willis committed
614
615
    /* Find all of particle pi's interacions and store needed values in the
     * secondary cache.*/
616
617
618
    for (int pjd = 0; pjd < count_align; pjd += (num_vec_proc * VEC_SIZE)) {

      /* Load 2 sets of vectors from the particle cache. */
619
620
621
      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]);
622

623
624
625
      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
626

627
      /* Compute the pairwise distance. */
628
629
630
      vector v_dx, v_dy, v_dz;
      vector v_dx_2, v_dy_2, v_dz_2, v_r2_2;

James Willis's avatar
James Willis committed
631
632
633
634
635
636
      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);
637
638
639
640
641
642
643

      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
644

645
      /* Form a mask from r2 < hig2 and r2 > 0.*/
James Willis's avatar
James Willis committed
646
647
      mask_t v_doi_mask, v_doi_mask_self_check, v_doi_mask2,
          v_doi_mask2_self_check;
648

James Willis's avatar
James Willis committed
649
      /* Form r2 > 0 mask and r2 < hig2 mask. */
650
      vec_create_mask(v_doi_mask_self_check, vec_cmp_gt(v_r2.v, vec_setzero()));
651
      vec_create_mask(v_doi_mask, vec_cmp_lt(v_r2.v, v_hig2.v));
652

James Willis's avatar
James Willis committed
653
      /* Form r2 > 0 mask and r2 < hig2 mask. */
James Willis's avatar
James Willis committed
654
655
      vec_create_mask(v_doi_mask2_self_check,
                      vec_cmp_gt(v_r2_2.v, vec_setzero()));
656
      vec_create_mask(v_doi_mask2, vec_cmp_lt(v_r2_2.v, v_hig2.v));
657

James Willis's avatar
James Willis committed
658
      /* Combine two masks and form integer masks. */
659
660
661
662
      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);
663

James Willis's avatar
James Willis committed
664
665
      /* If there are any interactions left pack interaction values into c2
       * cache. */
666
      if (doi_mask) {
James Willis's avatar
James Willis committed
667
        storeInteractions(doi_mask, pjd, &v_r2, &v_dx, &v_dy, &v_dz, cell_cache,
668
669
670
671
                          &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);
672
      }
James Willis's avatar
James Willis committed
673
674
      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
675
                          &v_dz_2, cell_cache, &int_cache, &icount, &v_rhoSum,
676
677
678
                          &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
679
      }
680
681
    }

James Willis's avatar
James Willis committed
682
    /* Perform padded vector remainder interactions if any are present. */
683
684
685
686
    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
687
688
689

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

    /* Perform interaction with 2 vectors. */
James Willis's avatar
James Willis committed
695
696
697
698
699
    for (int pjd = 0; pjd < icount_align; pjd += (num_vec_proc * VEC_SIZE)) {
      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],
700
701
702
          &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);
703
704
    }

James Willis's avatar
James Willis committed
705
706
    /* Perform horizontal adds on vector sums and store result in particle pi.
     */
James Willis's avatar
James Willis committed
707
708
709
710
711
712
713
714
    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]);
715
716
717
718
719

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

James Willis's avatar
James Willis committed
720
  TIMER_TOC(timer_doself_density);
721
#endif /* WITH_VECTORIZATION */
722
723
}

724
725
726
727
728
729
730
731
732
733
/**
 * @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.
 */
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
__attribute__((always_inline)) INLINE void runner_doself_subset_density_vec(
    struct runner *r, struct cell *restrict c, struct part *restrict parts, int *restrict ind, int pi_count) {

#ifdef WITH_VECTORIZATION
  struct part *restrict pi;
  int count_align;
  int num_vec_proc = NUM_VEC_PROC;

  const int count = c->count;

  vector v_hi, v_vix, v_viy, v_viz, v_hig2, v_r2;

  TIMER_TIC

  /* 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;

  if (cell_cache->count < count) {
    cache_init(cell_cache, count);
  }

  /* 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;
  int icount = 0, icount_align = 0;

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

    const int pi_index = ind[pid];

    /* Get a pointer to the ith particle. */
    pi = &parts[pi_index];

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

    vector pix, piy, piz;

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

    /* Fill particle pi vectors. */
781
782
783
    pix.v = vec_set1(pi->x[0] - c->loc[0]);
    piy.v = vec_set1(pi->x[1] - c->loc[1]);
    piz.v = vec_set1(pi->x[2] - c->loc[2]);
784
    v_hi.v = vec_set1(hi);
785
786
787
    v_vix.v = vec_set1(pi->v[0]);
    v_viy.v = vec_set1(pi->v[1]);
    v_viz.v = vec_set1(pi->v[2]);
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940

    const float hig2 = hi * hi * kernel_gamma2;
    v_hig2.v = vec_set1(hig2);

    /* Reset cumulative sums of update vectors. */
    vector rhoSum, rho_dhSum, wcountSum, wcount_dhSum, div_vSum, curlvxSum,
        curlvySum, curlvzSum;

    /* Get the inverse of hi. */
    vector v_hi_inv;

    v_hi_inv = vec_reciprocal(v_hi);

    rhoSum.v = vec_setzero();
    rho_dhSum.v = vec_setzero();
    wcountSum.v = vec_setzero();
    wcount_dhSum.v = vec_setzero();
    div_vSum.v = vec_setzero();
    curlvxSum.v = vec_setzero();
    curlvySum.v = vec_setzero();
    curlvzSum.v = vec_setzero();

    /* Pad cache if there is a serial remainder. */
    count_align = count;
    int rem = count % (num_vec_proc * VEC_SIZE);
    if (rem != 0) {
      int pad = (num_vec_proc * VEC_SIZE) - rem;

      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++) {
        cell_cache->x[i] = pix.f[0];
        cell_cache->y[i] = piy.f[0];
        cell_cache->z[i] = piz.f[0];
      }
    }

    vector pjx, pjy, pjz;
    vector pjx2, pjy2, pjz2;

    /* Find all of particle pi's interacions and store needed values in the
     * secondary cache.*/
    for (int pjd = 0; pjd < count_align; pjd += (num_vec_proc * VEC_SIZE)) {

      /* Load 2 sets of vectors from the particle cache. */
      pjx.v = vec_load(&cell_cache->x[pjd]);
      pjy.v = vec_load(&cell_cache->y[pjd]);
      pjz.v = vec_load(&cell_cache->z[pjd]);

      pjx2.v = vec_load(&cell_cache->x[pjd + VEC_SIZE]);
      pjy2.v = vec_load(&cell_cache->y[pjd + VEC_SIZE]);
      pjz2.v = vec_load(&cell_cache->z[pjd + VEC_SIZE]);

      /* Compute the pairwise distance. */
      vector v_dx, v_dy, v_dz;
      vector v_dx_2, v_dy_2, v_dz_2, v_r2_2;

      v_dx.v = vec_sub(pix.v, pjx.v);
      v_dx_2.v = vec_sub(pix.v, pjx2.v);
      v_dy.v = vec_sub(piy.v, pjy.v);
      v_dy_2.v = vec_sub(piy.v, pjy2.v);
      v_dz.v = vec_sub(piz.v, pjz.v);
      v_dz_2.v = vec_sub(piz.v, pjz2.v);

      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;
      int doi_mask, doi_mask_self_check, doi_mask2, 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));

      /* Form integer masks. */
      doi_mask_self_check = vec_form_int_mask(v_doi_mask_self_check);
      doi_mask = vec_form_int_mask(v_doi_mask);

      doi_mask2_self_check = vec_form_int_mask(v_doi_mask2_self_check);
      doi_mask2 = vec_form_int_mask(v_doi_mask2);

      /* Combine the two masks. */
      doi_mask = doi_mask & doi_mask_self_check;
      doi_mask2 = doi_mask2 & doi_mask2_self_check;

      /* 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,
                          &int_cache, &icount, &rhoSum, &rho_dhSum, &wcountSum,
                          &wcount_dhSum, &div_vSum, &curlvxSum, &curlvySum,
                          &curlvzSum, v_hi_inv, v_vix, v_viy, v_viz);
      }
      if (doi_mask2) {
        storeInteractions(doi_mask2, pjd + VEC_SIZE, &v_r2_2, &v_dx_2, &v_dy_2,
                          &v_dz_2, cell_cache, &int_cache, &icount, &rhoSum,
                          &rho_dhSum, &wcountSum, &wcount_dhSum, &div_vSum,
                          &curlvxSum, &curlvySum, &curlvzSum, v_hi_inv, v_vix,
                          v_viy, v_viz);
      }
    }

    /* Perform padded vector remainder interactions if any are present. */
    calcRemInteractions(&int_cache, icount, &rhoSum, &rho_dhSum, &wcountSum,
                        &wcount_dhSum, &div_vSum, &curlvxSum, &curlvySum,
                        &curlvzSum, v_hi_inv, v_vix, v_viy, v_viz,
                        &icount_align);

    /* 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. */
    for (int pjd = 0; pjd < icount_align; pjd += (num_vec_proc * VEC_SIZE)) {
      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],
          &int_cache.mq[pjd], &rhoSum, &rho_dhSum, &wcountSum, &wcount_dhSum,
          &div_vSum, &curlvxSum, &curlvySum, &curlvzSum, int_mask, int_mask2,
          0);
    }

    /* Perform horizontal adds on vector sums and store result in particle pi.
     */
    VEC_HADD(rhoSum, pi->rho);
    VEC_HADD(rho_dhSum, pi->density.rho_dh);
    VEC_HADD(wcountSum, pi->density.wcount);
    VEC_HADD(wcount_dhSum, pi->density.wcount_dh);
    VEC_HADD(div_vSum, pi->density.div_v);
    VEC_HADD(curlvxSum, pi->density.rot_v[0]);
    VEC_HADD(curlvySum, pi->density.rot_v[1]);
    VEC_HADD(curlvzSum, pi->density.rot_v[2]);

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

941
  TIMER_TOC(timer_doself_subset);
942
943
944
#endif /* WITH_VECTORIZATION */
}

945
/**
James Willis's avatar
James Willis committed
946
 * @brief Compute the force cell self-interaction (non-symmetric) using vector
947
948
949
950
951
952
953
954
955
956
957
958
 * intrinsics with one particle pi at a time.
 *
 * @param r The #runner.
 * @param c The #cell.
 */
__attribute__((always_inline)) INLINE void runner_doself2_force_vec(
    struct runner *r, struct cell *restrict c) {

#ifdef WITH_VECTORIZATION
  const struct engine *e = r->e;
  struct part *restrict pi;
  int count_align;
James Willis's avatar
James Willis committed
959
  const int num_vec_proc = 1;
960

961
  const timebin_t max_active_bin = e->max_active_bin;
James Willis's avatar
James Willis committed
962

963
964
965
  struct part *restrict parts = c->parts;
  const int count = c->count;

James Willis's avatar
James Willis committed
966
  TIMER_TIC;
967
968
969

  if (!cell_is_active(c, e)) return;

970
  if (!cell_are_part_drifted(c, e)) error("Interacting undrifted cell.");
971
972
973
974
975
976
977
978
979
980

  /* 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;

  if (cell_cache->count < count) {
    cache_init(cell_cache, count);
  }

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

James Willis's avatar
James Willis committed
983
#ifdef SWIFT_DEBUG_CHECKS
James Willis's avatar
James Willis committed
984
  for (int i = 0; i < count; i++) {
James Willis's avatar
James Willis committed
985
986
987
988
989
990
991
    pi = &c->parts[i];
    /* Check that particles have been drifted to the current time */
    if (pi->ti_drift != e->ti_current)
      error("Particle pi not drifted to current time");
  }
#endif

James Willis's avatar
James Willis committed
992
993
  /* Loop over the particles in the cell. */
  for (int pid = 0; pid < count; pid++) {
994

James Willis's avatar
James Willis committed
995
996
    /* Get a pointer to the ith particle. */
    pi = &parts[pid];
997

James Willis's avatar
James Willis committed
998
999
    /* Is the ith particle active? */
    if (!part_is_active_no_debug(pi, max_active_bin)) continue;
1000

James Willis's avatar
James Willis committed
1001
    const float hi = cell_cache->h[pid];
1002

James Willis's avatar
James Willis committed
1003
    /* Fill particle pi vectors. */
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
    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]);

    const vector v_rhoi = vector_set1(cell_cache->rho[pid]);
    const vector v_grad_hi = vector_set1(cell_cache->grad_h[pid]);
    const vector v_pOrhoi2 = vector_set1(cell_cache->pOrho2[pid]);
    const vector v_balsara_i = vector_set1(cell_cache->balsara[pid]);
    const vector v_ci = vector_set1(cell_cache->soundspeed[pid]);
1017

James Willis's avatar
James Willis committed
1018
    const float hig2 = hi * hi * kernel_gamma2;
1019
    const vector v_hig2 = vector_set1(hig2);
1020

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

1024
1025
1026
1027
1028
1029
1030
    /* Reset cumulative sums of update vectors. */
    vector v_a_hydro_xSum = vector_setzero();