runner_doiact.h 106 KB
Newer Older
1
/*******************************************************************************
2
 * This file is part of SWIFT.
3
 * Copyright (c) 2012 Pedro Gonnet (pedro.gonnet@durham.ac.uk)
4
 *               2016 Matthieu Schaller (matthieu.schaller@durham.ac.uk)
5
 *
6
7
8
9
 * 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.
10
 *
11
12
13
14
 * 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.
15
 *
16
17
 * 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/>.
18
 *
19
20
21
22
23
24
25
26
 ******************************************************************************/

/* Before including this file, define FUNCTION, which is the
   name of the interaction function. This creates the interaction functions
   runner_dopair_FUNCTION, runner_dopair_FUNCTION_naive, runner_doself_FUNCTION,
   and runner_dosub_FUNCTION calling the pairwise interaction function
   runner_iact_FUNCTION. */

27
#define PASTE(x, y) x##_##y
28

29
#define _DOPAIR1(f) PASTE(runner_dopair1, f)
30
#define DOPAIR1 _DOPAIR1(FUNCTION)
31

32
#define _DOPAIR2(f) PASTE(runner_dopair2, f)
33
#define DOPAIR2 _DOPAIR2(FUNCTION)
34

35
36
37
38
39
40
#define _DOPAIR1_NOSORT(f) PASTE(runner_dopair1_sort, f)
#define DOPAIR1_NOSORT _DOPAIR1_NOSORT(FUNCTION)

#define _DOPAIR2_NOSORT(f) PASTE(runner_dopair2_sort, f)
#define DOPAIR2_NOSORT _DOPAIR2_NOSORT(FUNCTION)

41
#define _DOPAIR_SUBSET(f) PASTE(runner_dopair_subset, f)
42
#define DOPAIR_SUBSET _DOPAIR_SUBSET(FUNCTION)
43

44
#define _DOPAIR_SUBSET_NAIVE(f) PASTE(runner_dopair_subset_naive, f)
Pedro Gonnet's avatar
Pedro Gonnet committed
45
46
#define DOPAIR_SUBSET_NAIVE _DOPAIR_SUBSET_NAIVE(FUNCTION)

47
#define _DOPAIR_NAIVE(f) PASTE(runner_dopair_naive, f)
48
#define DOPAIR_NAIVE _DOPAIR_NAIVE(FUNCTION)
49

50
#define _DOSELF_NAIVE(f) PASTE(runner_doself_naive, f)
51
#define DOSELF_NAIVE _DOSELF_NAIVE(FUNCTION)
52

53
#define _DOSELF1(f) PASTE(runner_doself1, f)
54
#define DOSELF1 _DOSELF1(FUNCTION)
55

56
#define _DOSELF2(f) PASTE(runner_doself2, f)
57
#define DOSELF2 _DOSELF2(FUNCTION)
58

59
#define _DOSELF_SUBSET(f) PASTE(runner_doself_subset, f)
60
#define DOSELF_SUBSET _DOSELF_SUBSET(FUNCTION)
61

62
63
64
65
66
67
68
69
#define _DOSUB_SELF1(f) PASTE(runner_dosub_self1, f)
#define DOSUB_SELF1 _DOSUB_SELF1(FUNCTION)

#define _DOSUB_PAIR1(f) PASTE(runner_dosub_pair1, f)
#define DOSUB_PAIR1 _DOSUB_PAIR1(FUNCTION)

#define _DOSUB_SELF2(f) PASTE(runner_dosub_self2, f)
#define DOSUB_SELF2 _DOSUB_SELF2(FUNCTION)
70

71
72
#define _DOSUB_PAIR2(f) PASTE(runner_dosub_pair2, f)
#define DOSUB_PAIR2 _DOSUB_PAIR2(FUNCTION)
73

74
#define _DOSUB_SUBSET(f) PASTE(runner_dosub_subset, f)
75
#define DOSUB_SUBSET _DOSUB_SUBSET(FUNCTION)
76

77
#define _IACT_NONSYM(f) PASTE(runner_iact_nonsym, f)
78
#define IACT_NONSYM _IACT_NONSYM(FUNCTION)
79

80
#define _IACT(f) PASTE(runner_iact, f)
81
#define IACT _IACT(FUNCTION)
82

83
84
85
86
87
88
#define _IACT_NONSYM_VEC(f) PASTE(runner_iact_nonsym_vec, f)
#define IACT_NONSYM_VEC _IACT_NONSYM_VEC(FUNCTION)

#define _IACT_VEC(f) PASTE(runner_iact_vec, f)
#define IACT_VEC _IACT_VEC(FUNCTION)

89
#define _TIMER_DOSELF(f) PASTE(timer_doself, f)
90
#define TIMER_DOSELF _TIMER_DOSELF(FUNCTION)
91

92
#define _TIMER_DOPAIR(f) PASTE(timer_dopair, f)
93
#define TIMER_DOPAIR _TIMER_DOPAIR(FUNCTION)
Pedro Gonnet's avatar
Pedro Gonnet committed
94

95
96
97
98
99
#define _TIMER_DOSUB_SELF(f) PASTE(timer_dosub_self, f)
#define TIMER_DOSUB_SELF _TIMER_DOSUB_SELF(FUNCTION)

#define _TIMER_DOSUB_PAIR(f) PASTE(timer_dosub_pair, f)
#define TIMER_DOSUB_PAIR _TIMER_DOSUB_PAIR(FUNCTION)
100

101
#define _TIMER_DOSELF_SUBSET(f) PASTE(timer_doself_subset, f)
102
103
#define TIMER_DOSELF_SUBSET _TIMER_DOSELF_SUBSET(FUNCTION)

104
#define _TIMER_DOPAIR_SUBSET(f) PASTE(timer_dopair_subset, f)
105
106
#define TIMER_DOPAIR_SUBSET _TIMER_DOPAIR_SUBSET(FUNCTION)

107
108
#include "runner_doiact_nosort.h"

109
110
111
112
113
114
115
/**
 * @brief Compute the interactions between a cell pair.
 *
 * @param r The #runner.
 * @param ci The first #cell.
 * @param cj The second #cell.
 */
116
117
118
void DOPAIR_NAIVE(struct runner *r, struct cell *restrict ci,
                  struct cell *restrict cj) {

119
120
121
122
  const struct engine *e = r->e;

  error("Don't use in actual runs ! Slow code !");

123
#ifdef WITH_VECTORIZATION
124
125
126
127
128
129
130
  int icount = 0;
  float r2q[VEC_SIZE] __attribute__((aligned(16)));
  float hiq[VEC_SIZE] __attribute__((aligned(16)));
  float hjq[VEC_SIZE] __attribute__((aligned(16)));
  float dxq[3 * VEC_SIZE] __attribute__((aligned(16)));
  struct part *piq[VEC_SIZE], *pjq[VEC_SIZE];
#endif
Matthieu Schaller's avatar
Matthieu Schaller committed
131
  TIMER_TIC;
132
133

  /* Anything to do here? */
134
  if (!cell_is_active(ci, e) && !cell_is_active(cj, e)) return;
135

136
137
138
139
140
  const int count_i = ci->count;
  const int count_j = cj->count;
  struct part *restrict parts_i = ci->parts;
  struct part *restrict parts_j = cj->parts;

141
  /* Get the relative distance between the pairs, wrapping. */
142
143
  double shift[3] = {0.0, 0.0, 0.0};
  for (int k = 0; k < 3; k++) {
144
145
146
147
148
149
150
    if (cj->loc[k] - ci->loc[k] < -e->s->dim[k] / 2)
      shift[k] = e->s->dim[k];
    else if (cj->loc[k] - ci->loc[k] > e->s->dim[k] / 2)
      shift[k] = -e->s->dim[k];
  }

  /* Loop over the parts in ci. */
151
  for (int pid = 0; pid < count_i; pid++) {
152
153

    /* Get a hold of the ith part in ci. */
154
155
156
157
158
159
    struct part *restrict pi = &parts_i[pid];
    const float hi = pi->h;

    double pix[3];
    for (int k = 0; k < 3; k++) pix[k] = pi->x[k] - shift[k];
    const float hig2 = hi * hi * kernel_gamma2;
160
161

    /* Loop over the parts in cj. */
162
    for (int pjd = 0; pjd < count_j; pjd++) {
163
164

      /* Get a pointer to the jth particle. */
165
      struct part *restrict pj = &parts_j[pjd];
166
167

      /* Compute the pairwise distance. */
168
169
170
      float r2 = 0.0f;
      float dx[3];
      for (int k = 0; k < 3; k++) {
171
172
173
174
175
176
177
        dx[k] = pix[k] - pj->x[k];
        r2 += dx[k] * dx[k];
      }

      /* Hit or miss? */
      if (r2 < hig2 || r2 < pj->h * pj->h * kernel_gamma2) {

178
#ifndef WITH_VECTORIZATION
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198

        IACT(r2, dx, hi, pj->h, pi, pj);

#else

        /* Add this interaction to the queue. */
        r2q[icount] = r2;
        dxq[3 * icount + 0] = dx[0];
        dxq[3 * icount + 1] = dx[1];
        dxq[3 * icount + 2] = dx[2];
        hiq[icount] = hi;
        hjq[icount] = pj->h;
        piq[icount] = pi;
        pjq[icount] = pj;
        icount += 1;

        /* Flush? */
        if (icount == VEC_SIZE) {
          IACT_VEC(r2q, dxq, hiq, hjq, piq, pjq);
          icount = 0;
199
200
        }

201
202
203
204
205
206
207
#endif
      }

    } /* loop over the parts in cj. */

  } /* loop over the parts in ci. */

208
#ifdef WITH_VECTORIZATION
209
210
  /* Pick up any leftovers. */
  if (icount > 0)
211
    for (int k = 0; k < icount; k++)
212
213
214
215
216
217
218
219
      IACT(r2q[k], &dxq[3 * k], hiq[k], hjq[k], piq[k], pjq[k]);
#endif

  TIMER_TOC(TIMER_DOPAIR);
}

void DOSELF_NAIVE(struct runner *r, struct cell *restrict c) {

220
  const struct engine *e = r->e;
221
222
223

  error("Don't use in actual runs ! Slow code !");

224
#ifdef WITH_VECTORIZATION
225
226
227
228
229
230
231
  int icount = 0;
  float r2q[VEC_SIZE] __attribute__((aligned(16)));
  float hiq[VEC_SIZE] __attribute__((aligned(16)));
  float hjq[VEC_SIZE] __attribute__((aligned(16)));
  float dxq[3 * VEC_SIZE] __attribute__((aligned(16)));
  struct part *piq[VEC_SIZE], *pjq[VEC_SIZE];
#endif
232

Matthieu Schaller's avatar
Matthieu Schaller committed
233
  TIMER_TIC;
234
235

  /* Anything to do here? */
236
  if (!cell_is_active(c, e)) return;
237

238
239
  const int count = c->count;
  struct part *restrict parts = c->parts;
240
241

  /* Loop over the parts in ci. */
242
  for (int pid = 0; pid < count; pid++) {
243
244

    /* Get a hold of the ith part in ci. */
245
246
247
248
    struct part *restrict pi = &parts[pid];
    const double pix[3] = {pi->x[0], pi->x[1], pi->x[2]};
    const float hi = pi->h;
    const float hig2 = hi * hi * kernel_gamma2;
249

250
    /* Loop over the parts in cj. */
251
    for (int pjd = pid + 1; pjd < count; pjd++) {
252
253

      /* Get a pointer to the jth particle. */
254
      struct part *restrict pj = &parts[pjd];
255
256

      /* Compute the pairwise distance. */
257
258
259
      float r2 = 0.0f;
      float dx[3];
      for (int k = 0; k < 3; k++) {
260
261
262
263
264
265
266
        dx[k] = pix[k] - pj->x[k];
        r2 += dx[k] * dx[k];
      }

      /* Hit or miss? */
      if (r2 < hig2 || r2 < pj->h * pj->h * kernel_gamma2) {

267
#ifndef WITH_VECTORIZATION
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288

        IACT(r2, dx, hi, pj->h, pi, pj);

#else

        /* Add this interaction to the queue. */
        r2q[icount] = r2;
        dxq[3 * icount + 0] = dx[0];
        dxq[3 * icount + 1] = dx[1];
        dxq[3 * icount + 2] = dx[2];
        hiq[icount] = hi;
        hjq[icount] = pj->h;
        piq[icount] = pi;
        pjq[icount] = pj;
        icount += 1;

        /* Flush? */
        if (icount == VEC_SIZE) {
          IACT_VEC(r2q, dxq, hiq, hjq, piq, pjq);
          icount = 0;
        }
289

290
291
#endif
      }
292

293
    } /* loop over the parts in cj. */
294

295
296
  } /* loop over the parts in ci. */

297
#ifdef WITH_VECTORIZATION
298
299
  /* Pick up any leftovers. */
  if (icount > 0)
300
    for (int k = 0; k < icount; k++)
301
302
      IACT(r2q[k], &dxq[3 * k], hiq[k], hjq[k], piq[k], pjq[k]);
#endif
303

304
305
  TIMER_TOC(TIMER_DOSELF);
}
306

307
308
309
310
311
312
/**
 * @brief Compute the interactions between a cell pair, but only for the
 *      given indices in ci.
 *
 * @param r The #runner.
 * @param ci The first #cell.
313
 * @param parts_i The #part to interact with @c cj.
314
315
316
317
 * @param ind The list of indices of particles in @c ci to interact with.
 * @param count The number of particles in @c ind.
 * @param cj The second #cell.
 */
318
319
320
321
322
323
324
325
void DOPAIR_SUBSET_NAIVE(struct runner *r, struct cell *restrict ci,
                         struct part *restrict parts_i, int *restrict ind,
                         int count, struct cell *restrict cj) {

  struct engine *e = r->e;

  error("Don't use in actual runs ! Slow code !");

326
#ifdef WITH_VECTORIZATION
327
328
329
330
331
332
333
334
  int icount = 0;
  float r2q[VEC_SIZE] __attribute__((aligned(16)));
  float hiq[VEC_SIZE] __attribute__((aligned(16)));
  float hjq[VEC_SIZE] __attribute__((aligned(16)));
  float dxq[3 * VEC_SIZE] __attribute__((aligned(16)));
  struct part *piq[VEC_SIZE], *pjq[VEC_SIZE];
#endif

Matthieu Schaller's avatar
Matthieu Schaller committed
335
  TIMER_TIC;
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371

  const int count_j = cj->count;
  struct part *restrict parts_j = cj->parts;

  /* Get the relative distance between the pairs, wrapping. */
  double shift[3] = {0.0, 0.0, 0.0};
  for (int k = 0; k < 3; k++) {
    if (cj->loc[k] - ci->loc[k] < -e->s->dim[k] / 2)
      shift[k] = e->s->dim[k];
    else if (cj->loc[k] - ci->loc[k] > e->s->dim[k] / 2)
      shift[k] = -e->s->dim[k];
  }

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

    /* Get a hold of the ith part in ci. */
    struct part *restrict pi = &parts_i[ind[pid]];
    double pix[3];
    for (int k = 0; k < 3; k++) pix[k] = pi->x[k] - shift[k];
    const float hi = pi->h;
    const float hig2 = hi * hi * kernel_gamma2;

    /* Loop over the parts in cj. */
    for (int pjd = 0; pjd < count_j; pjd++) {

      /* Get a pointer to the jth particle. */
      struct part *restrict pj = &parts_j[pjd];

      /* Compute the pairwise distance. */
      float r2 = 0.0f;
      float dx[3];
      for (int k = 0; k < 3; k++) {
        dx[k] = pix[k] - pj->x[k];
        r2 += dx[k] * dx[k];
      }
372

373
374
375
      /* Hit or miss? */
      if (r2 < hig2) {

376
#ifndef WITH_VECTORIZATION
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404

        IACT_NONSYM(r2, dx, hi, pj->h, pi, pj);

#else

        /* Add this interaction to the queue. */
        r2q[icount] = r2;
        dxq[3 * icount + 0] = dx[0];
        dxq[3 * icount + 1] = dx[1];
        dxq[3 * icount + 2] = dx[2];
        hiq[icount] = hi;
        hjq[icount] = pj->h;
        piq[icount] = pi;
        pjq[icount] = pj;
        icount += 1;

        /* Flush? */
        if (icount == VEC_SIZE) {
          IACT_NONSYM_VEC(r2q, dxq, hiq, hjq, piq, pjq);
          icount = 0;
        }

#endif
      }

    } /* loop over the parts in cj. */

  } /* loop over the parts in ci. */
405

406
#ifdef WITH_VECTORIZATION
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
  /* Pick up any leftovers. */
  if (icount > 0)
    for (int k = 0; k < icount; k++)
      IACT_NONSYM(r2q[k], &dxq[3 * k], hiq[k], hjq[k], piq[k], pjq[k]);
#endif

  TIMER_TOC(timer_dopair_subset);
}

/**
 * @brief Compute the interactions between a cell pair, but only for the
 *      given indices in ci.
 *
 * @param r The #runner.
 * @param ci The first #cell.
 * @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 count The number of particles in @c ind.
 * @param cj The second #cell.
 */
427
428
429
430
431
void DOPAIR_SUBSET(struct runner *r, struct cell *restrict ci,
                   struct part *restrict parts_i, int *restrict ind, int count,
                   struct cell *restrict cj) {

  struct engine *e = r->e;
432

433
#ifdef WITH_VECTORIZATION
434
435
436
437
438
439
440
  int icount = 0;
  float r2q[VEC_SIZE] __attribute__((aligned(16)));
  float hiq[VEC_SIZE] __attribute__((aligned(16)));
  float hjq[VEC_SIZE] __attribute__((aligned(16)));
  float dxq[3 * VEC_SIZE] __attribute__((aligned(16)));
  struct part *piq[VEC_SIZE], *pjq[VEC_SIZE];
#endif
441

Matthieu Schaller's avatar
Matthieu Schaller committed
442
  TIMER_TIC;
443

444
445
446
  const int count_j = cj->count;
  struct part *restrict parts_j = cj->parts;

447
  /* Get the relative distance between the pairs, wrapping. */
448
449
  double shift[3] = {0.0, 0.0, 0.0};
  for (int k = 0; k < 3; k++) {
450
451
452
453
454
455
456
    if (cj->loc[k] - ci->loc[k] < -e->s->dim[k] / 2)
      shift[k] = e->s->dim[k];
    else if (cj->loc[k] - ci->loc[k] > e->s->dim[k] / 2)
      shift[k] = -e->s->dim[k];
  }

  /* Get the sorting index. */
457
458
  int sid = 0;
  for (int k = 0; k < 3; k++)
459
460
461
462
463
    sid = 3 * sid + ((cj->loc[k] - ci->loc[k] + shift[k] < 0)
                         ? 0
                         : (cj->loc[k] - ci->loc[k] + shift[k] > 0) ? 2 : 1);

  /* Switch the cells around? */
464
  const int flipped = runner_flip[sid];
465
466
467
468
469
470
  sid = sortlistID[sid];

  /* Have the cells been sorted? */
  if (!(cj->sorted & (1 << sid))) error("Trying to interact unsorted cells.");

  /* Pick-out the sorted lists. */
471
472
  const struct entry *restrict sort_j = &cj->sort[sid * (cj->count + 1)];
  const float dxj = cj->dx_max;
473
474
475
476
477

  /* Parts are on the left? */
  if (!flipped) {

    /* Loop over the parts_i. */
478
    for (int pid = 0; pid < count; pid++) {
479
480

      /* Get a hold of the ith part in ci. */
481
482
483
484
485
486
487
488
489
      struct part *restrict pi = &parts_i[ind[pid]];
      double pix[3];
      for (int k = 0; k < 3; k++) pix[k] = pi->x[k] - shift[k];

      const float hi = pi->h;
      const float hig2 = hi * hi * kernel_gamma2;
      const float di = hi * kernel_gamma + dxj + pix[0] * runner_shift[sid][0] +
                       pix[1] * runner_shift[sid][1] +
                       pix[2] * runner_shift[sid][2];
490
491

      /* Loop over the parts in cj. */
492
      for (int pjd = 0; pjd < count_j && sort_j[pjd].d < di; pjd++) {
493
494

        /* Get a pointer to the jth particle. */
495
        struct part *restrict pj = &parts_j[sort_j[pjd].i];
496
497

        /* Compute the pairwise distance. */
498
499
500
        float r2 = 0.0f;
        float dx[3];
        for (int k = 0; k < 3; k++) {
501
502
          dx[k] = pix[k] - pj->x[k];
          r2 += dx[k] * dx[k];
503
        }
504
505
506
507

        /* Hit or miss? */
        if (r2 < hig2) {

508
#ifndef WITH_VECTORIZATION
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531

          IACT_NONSYM(r2, dx, hi, pj->h, pi, pj);

#else

          /* Add this interaction to the queue. */
          r2q[icount] = r2;
          dxq[3 * icount + 0] = dx[0];
          dxq[3 * icount + 1] = dx[1];
          dxq[3 * icount + 2] = dx[2];
          hiq[icount] = hi;
          hjq[icount] = pj->h;
          piq[icount] = pi;
          pjq[icount] = pj;
          icount += 1;

          /* Flush? */
          if (icount == VEC_SIZE) {
            IACT_NONSYM_VEC(r2q, dxq, hiq, hjq, piq, pjq);
            icount = 0;
          }

#endif
532
        }
533
534
535
536
537
538
539
540
541
542
543

      } /* loop over the parts in cj. */

    } /* loop over the parts in ci. */

  }

  /* Parts are on the right. */
  else {

    /* Loop over the parts_i. */
544
    for (int pid = 0; pid < count; pid++) {
545
546

      /* Get a hold of the ith part in ci. */
547
548
549
550
551
552
553
554
      struct part *restrict pi = &parts_i[ind[pid]];
      double pix[3];
      for (int k = 0; k < 3; k++) pix[k] = pi->x[k] - shift[k];
      const float hi = pi->h;
      const float hig2 = hi * hi * kernel_gamma2;
      const float di =
          -hi * kernel_gamma - dxj + pix[0] * runner_shift[sid][0] +
          pix[1] * runner_shift[sid][1] + pix[2] * runner_shift[sid][2];
555
556

      /* Loop over the parts in cj. */
557
      for (int pjd = count_j - 1; pjd >= 0 && di < sort_j[pjd].d; pjd--) {
558
559

        /* Get a pointer to the jth particle. */
560
        struct part *restrict pj = &parts_j[sort_j[pjd].i];
561
562

        /* Compute the pairwise distance. */
563
564
565
        float r2 = 0.0f;
        float dx[3];
        for (int k = 0; k < 3; k++) {
566
567
          dx[k] = pix[k] - pj->x[k];
          r2 += dx[k] * dx[k];
568
        }
569

570
571
        /* Hit or miss? */
        if (r2 < hig2) {
572

573
#ifndef WITH_VECTORIZATION
574
575

          IACT_NONSYM(r2, dx, hi, pj->h, pi, pj);
576

577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#else

          /* Add this interaction to the queue. */
          r2q[icount] = r2;
          dxq[3 * icount + 0] = dx[0];
          dxq[3 * icount + 1] = dx[1];
          dxq[3 * icount + 2] = dx[2];
          hiq[icount] = hi;
          hjq[icount] = pj->h;
          piq[icount] = pi;
          pjq[icount] = pj;
          icount += 1;

          /* Flush? */
          if (icount == VEC_SIZE) {
            IACT_NONSYM_VEC(r2q, dxq, hiq, hjq, piq, pjq);
            icount = 0;
          }

#endif
        }

      } /* loop over the parts in cj. */

    } /* loop over the parts in ci. */
  }

604
#ifdef WITH_VECTORIZATION
605
606
  /* Pick up any leftovers. */
  if (icount > 0)
607
    for (int k = 0; k < icount; k++)
608
609
      IACT_NONSYM(r2q[k], &dxq[3 * k], hiq[k], hjq[k], piq[k], pjq[k]);
#endif
Pedro Gonnet's avatar
Pedro Gonnet committed
610

611
612
  TIMER_TOC(timer_dopair_subset);
}
Pedro Gonnet's avatar
Pedro Gonnet committed
613

614
615
616
617
618
619
/**
 * @brief Compute the interactions between a cell pair, but only for the
 *      given indices in ci.
 *
 * @param r The #runner.
 * @param ci The first #cell.
620
 * @param parts The #part to interact.
621
622
623
 * @param ind The list of indices of particles in @c ci to interact with.
 * @param count The number of particles in @c ind.
 */
624
625
626
void DOSELF_SUBSET(struct runner *r, struct cell *restrict ci,
                   struct part *restrict parts, int *restrict ind, int count) {

627
#ifdef WITH_VECTORIZATION
628
629
630
631
632
633
634
  int icount = 0;
  float r2q[VEC_SIZE] __attribute__((aligned(16)));
  float hiq[VEC_SIZE] __attribute__((aligned(16)));
  float hjq[VEC_SIZE] __attribute__((aligned(16)));
  float dxq[3 * VEC_SIZE] __attribute__((aligned(16)));
  struct part *piq[VEC_SIZE], *pjq[VEC_SIZE];
#endif
635

Matthieu Schaller's avatar
Matthieu Schaller committed
636
  TIMER_TIC;
637

638
639
  const int count_i = ci->count;
  struct part *restrict parts_j = ci->parts;
640
641

  /* Loop over the parts in ci. */
642
  for (int pid = 0; pid < count; pid++) {
643
644

    /* Get a hold of the ith part in ci. */
645
646
647
648
    struct part *restrict pi = &parts[ind[pid]];
    const double pix[3] = {pi->x[0], pi->x[1], pi->x[2]};
    const float hi = pi->h;
    const float hig2 = hi * hi * kernel_gamma2;
649

650
    /* Loop over the parts in cj. */
651
    for (int pjd = 0; pjd < count_i; pjd++) {
652
653

      /* Get a pointer to the jth particle. */
654
      struct part *restrict pj = &parts_j[pjd];
655
656

      /* Compute the pairwise distance. */
657
658
659
      float r2 = 0.0f;
      float dx[3];
      for (int k = 0; k < 3; k++) {
660
661
662
663
664
665
666
        dx[k] = pix[k] - pj->x[k];
        r2 += dx[k] * dx[k];
      }

      /* Hit or miss? */
      if (r2 > 0.0f && r2 < hig2) {

667
#ifndef WITH_VECTORIZATION
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695

        IACT_NONSYM(r2, dx, hi, pj->h, pi, pj);

#else

        /* Add this interaction to the queue. */
        r2q[icount] = r2;
        dxq[3 * icount + 0] = dx[0];
        dxq[3 * icount + 1] = dx[1];
        dxq[3 * icount + 2] = dx[2];
        hiq[icount] = hi;
        hjq[icount] = pj->h;
        piq[icount] = pi;
        pjq[icount] = pj;
        icount += 1;

        /* Flush? */
        if (icount == VEC_SIZE) {
          IACT_NONSYM_VEC(r2q, dxq, hiq, hjq, piq, pjq);
          icount = 0;
        }

#endif
      }

    } /* loop over the parts in cj. */

  } /* loop over the parts in ci. */
696

697
#ifdef WITH_VECTORIZATION
698
699
  /* Pick up any leftovers. */
  if (icount > 0)
700
    for (int k = 0; k < icount; k++)
701
702
703
704
705
      IACT_NONSYM(r2q[k], &dxq[3 * k], hiq[k], hjq[k], piq[k], pjq[k]);
#endif

  TIMER_TOC(timer_dopair_subset);
}
706

707
/**
708
 * @brief Compute the interactions between a cell pair (non-symmetric).
709
710
711
712
713
 *
 * @param r The #runner.
 * @param ci The first #cell.
 * @param cj The second #cell.
 */
714
715
void DOPAIR1(struct runner *r, struct cell *ci, struct cell *cj) {

716
  const struct engine *restrict e = r->e;
717

718
719
720
721
722
723
724
#ifdef WITH_MPI
  if(ci->nodeID != cj->nodeID) {
    DOPAIR1_NOSORT(r, ci, cj);
    return;
  }
#endif

725
#ifdef WITH_VECTORIZATION
726
727
728
729
730
731
732
  int icount = 0;
  float r2q[VEC_SIZE] __attribute__((aligned(16)));
  float hiq[VEC_SIZE] __attribute__((aligned(16)));
  float hjq[VEC_SIZE] __attribute__((aligned(16)));
  float dxq[3 * VEC_SIZE] __attribute__((aligned(16)));
  struct part *piq[VEC_SIZE], *pjq[VEC_SIZE];
#endif
733

Matthieu Schaller's avatar
Matthieu Schaller committed
734
  TIMER_TIC;
735
736

  /* Anything to do here? */
737
  if (!cell_is_active(ci, e) && !cell_is_active(cj, e)) return;
738

739
740
  if (!cell_is_drifted(ci, e)) cell_drift(ci, e);
  if (!cell_is_drifted(cj, e)) cell_drift(cj, e);
741

742
  /* Get the sort ID. */
743
744
  double shift[3] = {0.0, 0.0, 0.0};
  const int sid = space_getsid(e->s, &ci, &cj, shift);
745
746
747
748
749
750

  /* Have the cells been sorted? */
  if (!(ci->sorted & (1 << sid)) || !(cj->sorted & (1 << sid)))
    error("Trying to interact unsorted cells.");

  /* Get the cutoff shift. */
751
752
  double rshift = 0.0;
  for (int k = 0; k < 3; k++) rshift += shift[k] * runner_shift[sid][k];
753
754

  /* Pick-out the sorted lists. */
755
756
  const struct entry *restrict sort_i = &ci->sort[sid * (ci->count + 1)];
  const struct entry *restrict sort_j = &cj->sort[sid * (cj->count + 1)];
757
758

  /* Get some other useful values. */
759
760
761
762
763
764
765
766
767
  const double hi_max = ci->h_max * kernel_gamma - rshift;
  const double hj_max = cj->h_max * kernel_gamma;
  const int count_i = ci->count;
  const int count_j = cj->count;
  struct part *restrict parts_i = ci->parts;
  struct part *restrict parts_j = cj->parts;
  const double di_max = sort_i[count_i - 1].d - rshift;
  const double dj_min = sort_j[0].d;
  const float dx_max = (ci->dx_max + cj->dx_max);
768
769

  /* Loop over the parts in ci. */
770
771
  for (int pid = count_i - 1;
       pid >= 0 && sort_i[pid].d + hi_max + dx_max > dj_min; pid--) {
772
773

    /* Get a hold of the ith part in ci. */
774
    struct part *restrict pi = &parts_i[sort_i[pid].i];
775
    if (!part_is_active(pi, e)) continue;
776
777
    const float hi = pi->h;
    const double di = sort_i[pid].d + hi * kernel_gamma + dx_max - rshift;
778
779
    if (di < dj_min) continue;

780
781
782
    double pix[3];
    for (int k = 0; k < 3; k++) pix[k] = pi->x[k] - shift[k];
    const float hig2 = hi * hi * kernel_gamma2;
783
784

    /* Loop over the parts in cj. */
785
    for (int pjd = 0; pjd < count_j && sort_j[pjd].d < di; pjd++) {
786
787

      /* Get a pointer to the jth particle. */
788
      struct part *restrict pj = &parts_j[sort_j[pjd].i];
789
790

      /* Compute the pairwise distance. */
791
792
793
      float r2 = 0.0f;
      float dx[3];
      for (int k = 0; k < 3; k++) {
794
795
796
797
        dx[k] = pix[k] - pj->x[k];
        r2 += dx[k] * dx[k];
      }

798
799
800
801
802
803
804
805
#ifdef SWIFT_DEBUG_CHECKS
      /* 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");
      if (pj->ti_drift != e->ti_current)
        error("Particle pj not drifted to current time");
#endif

806
807
808
      /* Hit or miss? */
      if (r2 < hig2) {

809
#ifndef WITH_VECTORIZATION
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

        IACT_NONSYM(r2, dx, hi, pj->h, pi, pj);

#else

        /* Add this interaction to the queue. */
        r2q[icount] = r2;
        dxq[3 * icount + 0] = dx[0];
        dxq[3 * icount + 1] = dx[1];
        dxq[3 * icount + 2] = dx[2];
        hiq[icount] = hi;
        hjq[icount] = pj->h;
        piq[icount] = pi;
        pjq[icount] = pj;
        icount += 1;

        /* Flush? */
        if (icount == VEC_SIZE) {
          IACT_NONSYM_VEC(r2q, dxq, hiq, hjq, piq, pjq);
          icount = 0;
        }

#endif
      }

    } /* loop over the parts in cj. */

  } /* loop over the parts in ci. */

  /* Loop over the parts in cj. */
840
  for (int pjd = 0; pjd < count_j && sort_j[pjd].d - hj_max - dx_max < di_max;
841
842
843
       pjd++) {

    /* Get a hold of the jth part in cj. */
844
    struct part *restrict pj = &parts_j[sort_j[pjd].i];
845
    if (!part_is_active(pj, e)) continue;
846
847
    const float hj = pj->h;
    const double dj = sort_j[pjd].d - hj * kernel_gamma - dx_max - rshift;
848
849
    if (dj > di_max) continue;

850
851
852
    double pjx[3];
    for (int k = 0; k < 3; k++) pjx[k] = pj->x[k] + shift[k];
    const float hjg2 = hj * hj * kernel_gamma2;
853
854

    /* Loop over the parts in ci. */
855
    for (int pid = count_i - 1; pid >= 0 && sort_i[pid].d > dj; pid--) {
856
857

      /* Get a pointer to the jth particle. */
858
      struct part *restrict pi = &parts_i[sort_i[pid].i];
859
860

      /* Compute the pairwise distance. */
861
862
863
      float r2 = 0.0f;
      float dx[3];
      for (int k = 0; k < 3; k++) {
864
865
866
867
        dx[k] = pjx[k] - pi->x[k];
        r2 += dx[k] * dx[k];
      }

868
869
870
871
872
873
874
875
#ifdef SWIFT_DEBUG_CHECKS
      /* 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");
      if (pj->ti_drift != e->ti_current)
        error("Particle pj not drifted to current time");
#endif

876
877
878
      /* Hit or miss? */
      if (r2 < hjg2) {

879
#ifndef WITH_VECTORIZATION
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

        IACT_NONSYM(r2, dx, hj, pi->h, pj, pi);

#else

        /* Add this interaction to the queue. */
        r2q[icount] = r2;
        dxq[3 * icount + 0] = dx[0];
        dxq[3 * icount + 1] = dx[1];
        dxq[3 * icount + 2] = dx[2];
        hiq[icount] = hj;
        hjq[icount] = pi->h;
        piq[icount] = pj;
        pjq[icount] = pi;
        icount += 1;

        /* Flush? */
        if (icount == VEC_SIZE) {
          IACT_NONSYM_VEC(r2q, dxq, hiq, hjq, piq, pjq);
          icount = 0;
        }

#endif
      }

    } /* loop over the parts in cj. */

  } /* loop over the parts in ci. */

909
#ifdef WITH_VECTORIZATION
910
911
  /* Pick up any leftovers. */
  if (icount > 0)
912
    for (int k = 0; k < icount; k++)
913
914
915
916
917
918
      IACT_NONSYM(r2q[k], &dxq[3 * k], hiq[k], hjq[k], piq[k], pjq[k]);
#endif

  TIMER_TOC(TIMER_DOPAIR);
}

919
920
921
922
923
924
925
/**
 * @brief Compute the interactions between a cell pair (symmetric)
 *
 * @param r The #runner.
 * @param ci The first #cell.
 * @param cj The second #cell.
 */
926
927
928
void DOPAIR2(struct runner *r, struct cell *ci, struct cell *cj) {

  struct engine *restrict e = r->e;
929

930
931
932
933
934
935
936
#ifdef WITH_MPI
  if(ci->nodeID != cj->nodeID) {
    DOPAIR2_NOSORT(r, ci, cj);
    return;
  }
#endif

937
#ifdef WITH_VECTORIZATION
938
939
940
941
942
943
944
945
946
947
948
949
950
  int icount1 = 0;
  float r2q1[VEC_SIZE] __attribute__((aligned(16)));
  float hiq1[VEC_SIZE] __attribute__((aligned(16)));
  float hjq1[VEC_SIZE] __attribute__((aligned(16)));
  float dxq1[3 * VEC_SIZE] __attribute__((aligned(16)));
  struct part *piq1[VEC_SIZE], *pjq1[VEC_SIZE];
  int icount2 = 0;
  float r2q2[VEC_SIZE] __attribute__((aligned(16)));
  float hiq2[VEC_SIZE] __attribute__((aligned(16)));
  float hjq2[VEC_SIZE] __attribute__((aligned(16)));
  float dxq2[3 * VEC_SIZE] __attribute__((aligned(16)));
  struct part *piq2[VEC_SIZE], *pjq2[VEC_SIZE];
#endif
951

Matthieu Schaller's avatar
Matthieu Schaller committed
952
  TIMER_TIC;
953
954

  /* Anything to do here? */
955
  if (!cell_is_active(ci, e) && !cell_is_active(cj, e)) return;
956

957
958
959
  if (!cell_is_drifted(ci, e)) error("Cell ci not drifted");
  if (!cell_is_drifted(cj, e)) error("Cell cj not drifted");

960
  /* Get the shift ID. */
961
962
  double shift[3] = {0.0, 0.0, 0.0};
  const int sid = space_getsid(e->s, &ci, &cj, shift);
963
964
965
966
967
968

  /* Have the cells been sorted? */
  if (!(ci->sorted & (1 << sid)) || !(cj->sorted & (1 << sid)))
    error("Trying to interact unsorted cells.");

  /* Get the cutoff shift. */
969
970
  double rshift = 0.0;
  for (int k = 0; k < 3; k++) rshift += shift[k] * runner_shift[sid][k];
971
972

  /* Pick-out the sorted lists. */
973
974
  struct entry *restrict sort_i = &ci->sort[sid * (ci->count + 1)];
  struct entry *restrict sort_j = &cj->sort[sid * (cj->count + 1)];
975
976

  /* Get some other useful values. */
977
978
979
980
981
982
983
984
985
  const double hi_max = ci->h_max * kernel_gamma - rshift;
  const double hj_max = cj->h_max * kernel_gamma;
  const int count_i = ci->count;
  const int count_j = cj->count;
  struct part *restrict parts_i = ci->parts;
  struct part *restrict parts_j = cj->parts;
  const double di_max = sort_i[count_i - 1].d - rshift;
  const double dj_min = sort_j[0].d;
  const double dx_max = (ci->dx_max + cj->dx_max);
986
987

  /* Collect the number of parts left and right below dt. */
988
989
  int countdt_i = 0, countdt_j = 0;
  struct entry *restrict sortdt_i = NULL, *restrict sortdt_j = NULL;
990
  if (cell_is_all_active(ci, e)) {
991
992
    sortdt_i = sort_i;
    countdt_i = count_i;
993
  } else if (cell_is_active(ci, e)) {
994
995
    if (posix_memalign((void *)&sortdt_i, VEC_SIZE * sizeof(float),
                       sizeof(struct entry) * count_i) != 0)
996
      error("Failed to allocate dt sortlists.");
997
    for (int k = 0; k < count_i; k++)
998
      if (part_is_active(&parts_i[sort_i[k].i], e)) {
999
1000
        sortdt_i[countdt_i] = sort_i[k];
        countdt_i += 1;
For faster browsing, not all history is shown. View entire blame