runner_doiact_vec.c 95.1 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
#if defined(WITH_VECTORIZATION) && defined(GADGET2_SPH)

28
29
static const vector kernel_gamma2_vec = FILL_VEC(kernel_gamma2);

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

James Willis's avatar
James Willis committed
68
  /* Work out the number of remainder interactions and pad secondary cache. */
69
70
71
72
73
74
  *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;

75
    /* Initialise masks to true. */
76
    mask_t int_mask, int_mask2;
77
78
    vec_init_mask_true(int_mask);
    vec_init_mask_true(int_mask2);
79

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

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

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

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

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

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

      (*icount)++;
    }
  }
201

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

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

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

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

215
    mask_t int_mask, int_mask2;
216
217
    vec_init_mask_true(int_mask);
    vec_init_mask_true(int_mask2);
James Willis's avatar
James Willis committed
218
219

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

    /* Reset interaction count. */
230
231
232
    *icount = 0;
  }
}
233

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

269
270
  const struct part *restrict parts_i = ci->hydro.parts;
  const struct part *restrict parts_j = cj->hydro.parts;
271

272
  int first_pi = 0, last_pj = cj->hydro.count - 1;
273
274
  int temp, active_id;

James Willis's avatar
James Willis committed
275
276
  /* Only populate max_index array for local actve cells. */
  if (active_ci) {
James Willis's avatar
James Willis committed
277

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

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

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

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

299
300
      const struct part *pi = &parts_i[sort_i[first_pi].i];
      const float first_di =
James Willis's avatar
James Willis committed
301
          sort_i[first_pi].d + pi->h * kernel_gamma + dx_max - rshift;
302

303
      /* Loop through particles in cell j until they are not in range of pi.
304
305
       * Make sure that temp stays between 0 and cj->hydro.count - 1.*/
      while (temp < cj->hydro.count - 1 && first_di > sort_j[temp].d) temp++;
306

307
      max_index_i[first_pi] = temp;
308

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

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

316
317
        /* Make sure that temp stays between 0 and cj->hydro.count - 1.*/
        while (temp < cj->hydro.count - 1 && di > sort_j[temp].d) temp++;
318

319
320
321
322
        max_index_i[i] = temp;
      }
    } else {
      /* Make sure that max index is set to first particle in cj.*/
323
      max_index_i[ci->hydro.count - 1] = 0;
324
    }
James Willis's avatar
James Willis committed
325
  } else {
James Willis's avatar
James Willis committed
326
327
    /* Make sure that foreign cells are only read into the cache if the local
     * cell requires it.
328
     * Also ensure that it does not require any particles from cj. */
329
330
    first_pi = ci->hydro.count - 1;
    max_index_i[ci->hydro.count - 1] = 0;
331
332
  }

James Willis's avatar
James Willis committed
333
334
  /* Only populate max_index array for local actve cells. */
  if (active_cj) {
335
336
337
338
    /* Find the rightmost active particle in cell j that interacts with any
     * particle in cell i. */
    last_pj = -1;
    active_id = last_pj;
339
    while (last_pj < cj->hydro.count &&
James Willis's avatar
James Willis committed
340
           sort_j[last_pj + 1].d - hj_max - dx_max < di_max) {
341
342
343
344
345
      last_pj++;
      /* Store the index of the particle if it is active. */
      if (part_is_active_no_debug(&parts_j[sort_j[last_pj].i], max_active_bin))
        active_id = last_pj;
    }
James Willis's avatar
James Willis committed
346

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

James Willis's avatar
James Willis committed
350
351
    /* Find the maximum index into cell i for each particle in range in cell j.
     */
352
    if (last_pj >= 0) {
Matthieu Schaller's avatar
Matthieu Schaller committed
353

354
      /* Start from the last particle in cell i. */
355
      temp = ci->hydro.count - 1;
356

357
358
      const struct part *pj = &parts_j[sort_j[last_pj].i];
      const float last_dj =
James Willis's avatar
James Willis committed
359
          sort_j[last_pj].d - dx_max - pj->h * kernel_gamma + rshift;
360

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

364
      max_index_j[last_pj] = temp;
365

366
367
368
369
370
      /* 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];
        const float dj = sort_j[i].d - dx_max - (pj->h * kernel_gamma) + rshift;
371

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

374
375
376
377
        max_index_j[i] = temp;
      }
    } else {
      /* Make sure that max index is set to last particle in ci.*/
378
      max_index_j[0] = ci->hydro.count - 1;
379
    }
James Willis's avatar
James Willis committed
380
  } else {
James Willis's avatar
James Willis committed
381
382
    /* Make sure that foreign cells are only read into the cache if the local
     * cell requires it.
383
384
     * Also ensure that it does not require any particles from ci. */
    last_pj = 0;
385
    max_index_j[0] = ci->hydro.count - 1;
386
387
  }

James Willis's avatar
James Willis committed
388
389
  *init_pi = first_pi;
  *init_pj = last_pj;
390
}
391

James Willis's avatar
James Willis committed
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/**
 * @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
James Willis's avatar
James Willis committed
407
 * @param h_max Maximal smoothing length in both cells scaled by kernel_gamma
James Willis's avatar
James Willis committed
408
409
410
411
412
413
414
415
416
 * @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.
417
418
 * @param active_ci Is any particle in cell ci active?
 * @param active_cj Is any particle in cell cj active?
James Willis's avatar
James Willis committed
419
 */
James Willis's avatar
James Willis committed
420
__attribute__((always_inline)) INLINE static void populate_max_index_force(
421
422
423
424
425
    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, const double hi_max_raw,
    const double hj_max_raw, const double h_max, const double di_max,
    const double dj_min, int *max_index_i, int *max_index_j, int *init_pi,
Matthieu Schaller's avatar
Matthieu Schaller committed
426
427
    int *init_pj, const timebin_t max_active_bin, const int active_ci,
    const int active_cj) {
428

429
430
  const struct part *restrict parts_i = ci->hydro.parts;
  const struct part *restrict parts_j = cj->hydro.parts;
James Willis's avatar
James Willis committed
431

432
  int first_pi = 0, last_pj = cj->hydro.count - 1;
433
  int temp, active_id;
434

James Willis's avatar
James Willis committed
435
436
  /* Only populate max_index array for local actve cells. */
  if (active_ci) {
437

438
439
    /* Find the leftmost active particle in cell i that interacts with any
     * particle in cell j. */
440
    first_pi = ci->hydro.count;
441
    active_id = first_pi - 1;
Matthieu Schaller's avatar
Matthieu Schaller committed
442
    while (first_pi > 0 && sort_i[first_pi - 1].d + dx_max + h_max > dj_min) {
443
444
445
446
447
      first_pi--;
      /* Store the index of the particle if it is active. */
      if (part_is_active_no_debug(&parts_i[sort_i[first_pi].i], max_active_bin))
        active_id = first_pi;
    }
448

449
450
    /* Set the first active pi in range of any particle in cell j. */
    first_pi = active_id;
451

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

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

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

464
      /* Loop through particles in cell j until they are not in range of pi.
465
466
       * Make sure that temp stays between 0 and cj->hydro.count - 1.*/
      while (temp < cj->hydro.count - 1 && first_di > sort_j[temp].d) temp++;
467

468
      max_index_i[first_pi] = temp;
469

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

James Willis's avatar
James Willis committed
475
476
        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
477

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

481
482
483
484
        max_index_i[i] = temp;
      }
    } else {
      /* Make sure that max index is set to first particle in cj.*/
485
      max_index_i[ci->hydro.count - 1] = 0;
486
    }
487
  } else {
James Willis's avatar
James Willis committed
488
489
    /* Make sure that foreign cells are only read into the cache if the local
     * cell requires it.
490
     * Also ensure that it does not require any particles from cj. */
491
492
    first_pi = ci->hydro.count - 1;
    max_index_i[ci->hydro.count - 1] = 0;
493
494
  }

James Willis's avatar
James Willis committed
495
496
  /* Only populate max_index array for local actve cells. */
  if (active_cj) {
497
498
499
500
    /* Find the rightmost active particle in cell j that interacts with any
     * particle in cell i. */
    last_pj = -1;
    active_id = last_pj;
501
    while (last_pj < cj->hydro.count &&
502
           sort_j[last_pj + 1].d - h_max - dx_max < di_max) {
503
504
505
506
507
      last_pj++;
      /* Store the index of the particle if it is active. */
      if (part_is_active_no_debug(&parts_j[sort_j[last_pj].i], max_active_bin))
        active_id = last_pj;
    }
James Willis's avatar
James Willis committed
508

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

James Willis's avatar
James Willis committed
512
513
    /* Find the maximum index into cell i for each particle in range in cell j.
     */
514
    if (last_pj >= 0) {
Matthieu Schaller's avatar
Matthieu Schaller committed
515

516
      /* Start from the last particle in cell i. */
517
      temp = ci->hydro.count - 1;
518

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

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

526
      max_index_j[last_pj] = temp;
527

528
529
530
531
      /* 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];
532

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

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

538
539
540
541
        max_index_j[i] = temp;
      }
    } else {
      /* Make sure that max index is set to last particle in ci.*/
542
      max_index_j[0] = ci->hydro.count - 1;
543
    }
544
  } else {
James Willis's avatar
James Willis committed
545
546
    /* Make sure that foreign cells are only read into the cache if the local
     * cell requires it.
547
548
     * Also ensure that it does not require any particles from ci. */
    last_pj = 0;
549
    max_index_j[0] = ci->hydro.count - 1;
550
551
  }

James Willis's avatar
James Willis committed
552
553
  *init_pi = first_pi;
  *init_pj = last_pj;
554
}
555

556
/**
557
558
559
560
 * @brief Populates the array max_index_i with the maximum
 * index of
 * particles into the neighbouring cell. Also finds the first/last pj that
 * interacts with any particle in ci.
561
 *
562
563
564
565
566
567
 * @param count_i The number of particles in ci.
 * @param count_j The number of particles in cj.
 * @param parts_i The #part to interact with @c cj.
 * @param ind The list of indices of particles in @c ci to interact with.
 * @param total_ci_shift The shift vector to apply to the particles in ci.
 * @param dxj Maximum particle movement allowed in cell cj.
James Willis's avatar
James Willis committed
568
569
 * @param di_shift_correction The correction to di after the particles have been
 * shifted to the frame of cell ci.
570
571
572
 * @param runner_shift_x The runner_shift in the x direction.
 * @param runner_shift_y The runner_shift in the y direction.
 * @param runner_shift_z The runner_shift in the z direction.
573
 * @param sort_j #entry array for particle distance in cj
James Willis's avatar
James Willis committed
574
575
 * @param max_index_i array to hold the maximum distances of pi particles into
 * #cell cj
576
 * @param flipped Flag to check whether the cells have been flipped or not.
James Willis's avatar
James Willis committed
577
578
 * @return first_pj/last_pj first or last pj to interact with any particle in ci
 * depending whether the cells have been flipped or not.
579
580
 */
__attribute__((always_inline)) INLINE static int populate_max_index_subset(
James Willis's avatar
James Willis committed
581
582
583
584
585
586
587
    const int count_i, const int count_j, struct part *restrict parts_i,
    int *restrict ind, const double *total_ci_shift, const float dxj,
    const double di_shift_correction, const double runner_shift_x,
    const double runner_shift_y, const double runner_shift_z,
    const struct entry *restrict sort_j, int *max_index_i, const int flipped) {

  /* The cell is on the right so read the particles
588
   * into the cache from the start of the cell. */
James Willis's avatar
James Willis committed
589
590
  if (!flipped) {

591
592
593
    /* Find the rightmost particle in cell j that interacts with any
     * particle in cell i. */
    int last_pj = 0;
James Willis's avatar
James Willis committed
594

595
596
597
598
599
600
601
    for (int pid = 0; pid < count_i; pid++) {
      struct part *restrict pi = &parts_i[ind[pid]];
      const float pix = pi->x[0] - total_ci_shift[0];
      const float piy = pi->x[1] - total_ci_shift[1];
      const float piz = pi->x[2] - total_ci_shift[2];
      const float hi = pi->h;

602
      const double di = hi * kernel_gamma + dxj + pix * runner_shift_x +
James Willis's avatar
James Willis committed
603
604
605
606
607
608
                        piy * runner_shift_y + piz * runner_shift_z +
                        di_shift_correction;

      for (int pjd = last_pj; pjd < count_j && sort_j[pjd].d < di; pjd++)
        last_pj++;

609
610
611
612
      max_index_i[pid] = last_pj;
    }
    return last_pj;
  }
James Willis's avatar
James Willis committed
613
  /* The cell is on the left so read the particles
614
   * into the cache from the end of the cell. */
615
616
  else {

James Willis's avatar
James Willis committed
617
    int first_pj = count_j - 1;
618
619
620
621
622
623
624
625

    for (int pid = 0; pid < count_i; pid++) {
      struct part *restrict pi = &parts_i[ind[pid]];
      const float pix = pi->x[0] - total_ci_shift[0];
      const float piy = pi->x[1] - total_ci_shift[1];
      const float piz = pi->x[2] - total_ci_shift[2];
      const float hi = pi->h;

626
      const double di = -hi * kernel_gamma - dxj + pix * runner_shift_x +
James Willis's avatar
James Willis committed
627
628
629
                        piy * runner_shift_y + piz * runner_shift_z +
                        di_shift_correction;

James Willis's avatar
James Willis committed
630
      for (int pjd = first_pj; pjd > 0 && di < sort_j[pjd].d; pjd--) first_pj--;
James Willis's avatar
James Willis committed
631

632
633
634
635
636
637
      max_index_i[pid] = first_pj;
    }
    return first_pj;
  }
}

638
#endif /* WITH_VECTORIZATION && GADGET2_SPH */
639
640

/**
James Willis's avatar
James Willis committed
641
642
 * @brief Compute the cell self-interaction (non-symmetric) using vector
 * intrinsics with one particle pi at a time.
643
644
645
646
 *
 * @param r The #runner.
 * @param c The #cell.
 */
James Willis's avatar
James Willis committed
647
void runner_doself1_density_vec(struct runner *r, struct cell *restrict c) {
648

649
#if defined(WITH_VECTORIZATION) && defined(GADGET2_SPH)
650

Matthieu Schaller's avatar
Matthieu Schaller committed
651
652
  /* Get some local variables */
  const struct engine *e = r->e;
653
654
  struct part *restrict parts = c->hydro.parts;
  const int count = c->hydro.count;
James Willis's avatar
James Willis committed
655

Matthieu Schaller's avatar
Matthieu Schaller committed
656
  TIMER_TIC;
657

Matthieu Schaller's avatar
Matthieu Schaller committed
658
  /* Anything to do here? */
659
  if (!cell_is_active_hydro(c, e)) return;
660

661
  /* Check that everybody was drifted here */
662
  if (!cell_are_part_drifted(c, e)) error("Interacting undrifted cell.");
663

664
665
666
#ifdef SWIFT_DEBUG_CHECKS
  for (int i = 0; i < count; i++) {
    /* Check that particles have been drifted to the current time */
667
    if (parts[i].ti_drift != e->ti_current && !part_is_inhibited(&parts[i], e))
668
669
670
671
      error("Particle pi not drifted to current time");
  }
#endif

James Willis's avatar
James Willis committed
672
  /* Get the particle cache from the runner and re-allocate
673
   * the cache if it is not big enough for the cell. */
674
  struct cache *restrict cell_cache = &r->ci_cache;
675
  if (cell_cache->count < count) cache_init(cell_cache, count);
676

James Willis's avatar
James Willis committed
677
  /* Read the particles from the cell and store them locally in the cache. */
678
  const int count_align = cache_read_particles(c, cell_cache);
679

680
681
  /* Create secondary cache to store particle interactions. */
  struct c2_cache int_cache;
682
683
684
685
686

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

689
690
    /* Is the i^th particle active? */
    if (!part_is_active(pi, e)) continue;
691

James Willis's avatar
James Willis committed
692
    /* Fill particle pi vectors. */
693
694
695
    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]);
696
    const vector v_hi = vector_set1(cell_cache->h[pid]);
697
698
699
    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]);
700

701
702
    /* Some useful mulitples of h */
    const float hi = cell_cache->h[pid];
703
    const float hig2 = hi * hi * kernel_gamma2;
704
    const vector v_hig2 = vector_set1(hig2);
705
    const vector v_hi_inv = vec_reciprocal(v_hi);
James Willis's avatar
James Willis committed
706

707
708
709
710
711
712
713
714
715
    /* 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();
716

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

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

      /* Load 2 sets of vectors from the particle cache. */
726
727
728
      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]);
729

730
731
732
      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
733

734
      /* Compute the pairwise distance. */
James Willis's avatar
James Willis committed
735
      vector v_dx, v_dy, v_dz, v_r2;
736
737
      vector v_dx_2, v_dy_2, v_dz_2, v_r2_2;

James Willis's avatar
James Willis committed
738
      v_dx.v = vec_sub(v_pix.v, v_pjx.v);
Matthieu Schaller's avatar
Matthieu Schaller committed
739
      v_dx_2.v = vec_sub(v_pix.v, v_pjx2.v);
James Willis's avatar
James Willis committed
740
      v_dy.v = vec_sub(v_piy.v, v_pjy.v);
Matthieu Schaller's avatar
Matthieu Schaller committed
741
      v_dy_2.v = vec_sub(v_piy.v, v_pjy2.v);
James Willis's avatar
James Willis committed
742
      v_dz.v = vec_sub(v_piz.v, v_pjz.v);
James Willis's avatar
James Willis committed
743
      v_dz_2.v = vec_sub(v_piz.v, v_pjz2.v);
744
745

      v_r2.v = vec_mul(v_dx.v, v_dx.v);
Matthieu Schaller's avatar
Matthieu Schaller committed
746
      v_r2_2.v = vec_mul(v_dx_2.v, v_dx_2.v);
James Willis's avatar
James Willis committed
747
      v_r2.v = vec_fma(v_dy.v, v_dy.v, v_r2.v);
Matthieu Schaller's avatar
Matthieu Schaller committed
748
      v_r2_2.v = vec_fma(v_dy_2.v, v_dy_2.v, v_r2_2.v);
James Willis's avatar
James Willis committed
749
      v_r2.v = vec_fma(v_dz.v, v_dz.v, v_r2.v);
750
      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
751

James Willis's avatar
James Willis committed
752
      /* Form a mask from r2 < hig2 and r2 > 0.*/
753
      mask_t v_doi_mask, v_doi_mask2;
754
755
756
757
      mask_t v_doi_mask_self_check, 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()));
758
      vec_create_mask(v_doi_mask, vec_cmp_lt(v_r2.v, v_hig2.v));
759
760
761
762

      /* 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()));
763
      vec_create_mask(v_doi_mask2, vec_cmp_lt(v_r2_2.v, v_hig2.v));
764

765
766
767
768
769
      /* 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);
Matthieu Schaller's avatar
Matthieu Schaller committed
770

771
772
773
#ifdef SWIFT_DEBUG_CHECKS
      /* Verify that we have no inhibited particles in the interaction cache */
      for (int bit_index = 0; bit_index < VEC_SIZE; bit_index++) {
Matthieu Schaller's avatar
Matthieu Schaller committed
774
775
776
777
778
        if (doi_mask & (1 << bit_index)) {
          if (parts[pjd + bit_index].time_bin >= time_bin_inhibited) {
            error("Inhibited particle in interaction cache!");
          }
        }
779
780
      }
      for (int bit_index = 0; bit_index < VEC_SIZE; bit_index++) {
Matthieu Schaller's avatar
Matthieu Schaller committed
781
782
783
784
785
786
        if (doi_mask2 & (1 << bit_index)) {
          if (parts[pjd + VEC_SIZE + bit_index].time_bin >=
              time_bin_inhibited) {
            error("Inhibited particle in interaction cache2!");
          }
        }
787
788
      }
#endif
Matthieu Schaller's avatar
Matthieu Schaller committed
789

790
#ifdef DEBUG_INTERACTIONS_SPH
791
      for (int bit_index = 0; bit_index < VEC_SIZE; bit_index++) {
792
793
794
795
        if (doi_mask & (1 << bit_index)) {
          if (pi->num_ngb_density < MAX_NUM_OF_NEIGHBOURS)
            pi->ids_ngbs_density[pi->num_ngb_density] =
                parts[pjd + bit_index].id;
James Willis's avatar
James Willis committed
796
          ++pi->num_ngb_density;
797
        }
798

799
800
801
802
        if (doi_mask2 & (1 << bit_index)) {
          if (pi->num_ngb_density < MAX_NUM_OF_NEIGHBOURS)
            pi->ids_ngbs_density[pi->num_ngb_density] =
                parts[pjd + VEC_SIZE + bit_index].id;
James Willis's avatar
James Willis committed
803
          ++pi->num_ngb_density;
804
805
806
807
        }
      }
#endif

James Willis's avatar
James Willis committed
808
809
      /* If there are any interactions left pack interaction values into c2
       * cache. */
810
      if (doi_mask) {
James Willis's avatar
James Willis committed
811
        storeInteractions(doi_mask, pjd, &v_r2, &v_dx, &v_dy, &v_dz, cell_cache,
812
813
814
815
                          &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);
816
      }
James Willis's avatar
James Willis committed
817
818
      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
819
                          &v_dz_2, cell_cache, &int_cache, &icount, &v_rhoSum,
820
821
822
                          &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
823
      }
824
825
    }

James Willis's avatar
James Willis committed
826
    /* Perform padded vector remainder interactions if any are present. */
827
828
829
830
    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
831
832
833

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

838
    /* Perform interaction with NUM_VEC_PROC vectors. */
James Willis's avatar
James Willis committed
839
    for (int pjd = 0; pjd < icount_align; pjd += (NUM_VEC_PROC * VEC_SIZE)) {
James Willis's avatar
James Willis committed
840
841
842
843
      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],
844
845
846
          &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);
847
848
    }

849
    /* Perform horizontal adds on vector sums and store result in pi. */
James Willis's avatar
James Willis committed
850
851
852
853
854
855
856
857
    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]);
858
859
860
861
862

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

James Willis's avatar
James Willis committed
863
  TIMER_TOC(timer_doself_density);
864
865
866
867
868

#else

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

869
#endif /* WITH_VECTORIZATION */
870
871