runner_doiact.h 102 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
#define _DOPAIR_SUBSET(f) PASTE(runner_dopair_subset, f)
36
#define DOPAIR_SUBSET _DOPAIR_SUBSET(FUNCTION)
37

38
#define _DOPAIR_SUBSET_NAIVE(f) PASTE(runner_dopair_subset_naive, f)
Pedro Gonnet's avatar
Pedro Gonnet committed
39
40
#define DOPAIR_SUBSET_NAIVE _DOPAIR_SUBSET_NAIVE(FUNCTION)

41
#define _DOPAIR_NAIVE(f) PASTE(runner_dopair_naive, f)
42
#define DOPAIR_NAIVE _DOPAIR_NAIVE(FUNCTION)
43

44
#define _DOSELF_NAIVE(f) PASTE(runner_doself_naive, f)
45
#define DOSELF_NAIVE _DOSELF_NAIVE(FUNCTION)
46

47
#define _DOSELF1(f) PASTE(runner_doself1, f)
48
#define DOSELF1 _DOSELF1(FUNCTION)
49

50
#define _DOSELF2(f) PASTE(runner_doself2, f)
51
#define DOSELF2 _DOSELF2(FUNCTION)
52

53
#define _DOSELF_SUBSET(f) PASTE(runner_doself_subset, f)
54
#define DOSELF_SUBSET _DOSELF_SUBSET(FUNCTION)
55

56
57
58
59
60
61
62
63
#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)
64

65
66
#define _DOSUB_PAIR2(f) PASTE(runner_dosub_pair2, f)
#define DOSUB_PAIR2 _DOSUB_PAIR2(FUNCTION)
67

68
#define _DOSUB_SUBSET(f) PASTE(runner_dosub_subset, f)
69
#define DOSUB_SUBSET _DOSUB_SUBSET(FUNCTION)
70

71
#define _IACT_NONSYM(f) PASTE(runner_iact_nonsym, f)
72
#define IACT_NONSYM _IACT_NONSYM(FUNCTION)
73

74
#define _IACT(f) PASTE(runner_iact, f)
75
#define IACT _IACT(FUNCTION)
76

77
78
79
80
81
82
#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)

83
#define _TIMER_DOSELF(f) PASTE(timer_doself, f)
84
#define TIMER_DOSELF _TIMER_DOSELF(FUNCTION)
85

86
#define _TIMER_DOPAIR(f) PASTE(timer_dopair, f)
87
#define TIMER_DOPAIR _TIMER_DOPAIR(FUNCTION)
Pedro Gonnet's avatar
Pedro Gonnet committed
88

89
90
91
92
93
#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)
94

95
#define _TIMER_DOSELF_SUBSET(f) PASTE(timer_doself_subset, f)
96
97
#define TIMER_DOSELF_SUBSET _TIMER_DOSELF_SUBSET(FUNCTION)

98
#define _TIMER_DOPAIR_SUBSET(f) PASTE(timer_dopair_subset, f)
99
100
#define TIMER_DOPAIR_SUBSET _TIMER_DOPAIR_SUBSET(FUNCTION)

101
102
103
104
105
106
107
/**
 * @brief Compute the interactions between a cell pair.
 *
 * @param r The #runner.
 * @param ci The first #cell.
 * @param cj The second #cell.
 */
108
109
110
void DOPAIR_NAIVE(struct runner *r, struct cell *restrict ci,
                  struct cell *restrict cj) {

111
112
113
114
  const struct engine *e = r->e;

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

115
#ifdef WITH_VECTORIZATION
116
117
118
119
120
121
122
  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
123
  TIMER_TIC;
124
125

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

128
129
130
131
132
  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;

133
  /* Get the relative distance between the pairs, wrapping. */
134
135
  double shift[3] = {0.0, 0.0, 0.0};
  for (int k = 0; k < 3; k++) {
136
137
138
139
140
141
142
    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. */
143
  for (int pid = 0; pid < count_i; pid++) {
144
145

    /* Get a hold of the ith part in ci. */
146
147
148
149
150
151
    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;
152
153

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

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

      /* Compute the pairwise distance. */
160
161
162
      float r2 = 0.0f;
      float dx[3];
      for (int k = 0; k < 3; k++) {
163
164
165
166
167
168
169
        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) {

170
#ifndef WITH_VECTORIZATION
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190

        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;
191
192
        }

193
194
195
196
197
198
199
#endif
      }

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

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

200
#ifdef WITH_VECTORIZATION
201
202
  /* Pick up any leftovers. */
  if (icount > 0)
203
    for (int k = 0; k < icount; k++)
204
205
206
207
208
209
210
211
      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) {

212
  const struct engine *e = r->e;
213
214
215

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

216
#ifdef WITH_VECTORIZATION
217
218
219
220
221
222
223
  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
224

Matthieu Schaller's avatar
Matthieu Schaller committed
225
  TIMER_TIC;
226
227

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

230
231
  const int count = c->count;
  struct part *restrict parts = c->parts;
232
233

  /* Loop over the parts in ci. */
234
  for (int pid = 0; pid < count; pid++) {
235
236

    /* Get a hold of the ith part in ci. */
237
238
239
240
    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;
241

242
    /* Loop over the parts in cj. */
243
    for (int pjd = pid + 1; pjd < count; pjd++) {
244
245

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

      /* Compute the pairwise distance. */
249
250
251
      float r2 = 0.0f;
      float dx[3];
      for (int k = 0; k < 3; k++) {
252
253
254
255
256
257
258
        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) {

259
#ifndef WITH_VECTORIZATION
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280

        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;
        }
281

282
283
#endif
      }
284

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

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

289
#ifdef WITH_VECTORIZATION
290
291
  /* Pick up any leftovers. */
  if (icount > 0)
292
    for (int k = 0; k < icount; k++)
293
294
      IACT(r2q[k], &dxq[3 * k], hiq[k], hjq[k], piq[k], pjq[k]);
#endif
295

296
297
  TIMER_TOC(TIMER_DOSELF);
}
298

299
300
301
302
303
304
/**
 * @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.
305
 * @param parts_i The #part to interact with @c cj.
306
307
308
309
 * @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.
 */
310
311
312
313
314
315
316
317
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 !");

318
#ifdef WITH_VECTORIZATION
319
320
321
322
323
324
325
326
  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
327
  TIMER_TIC;
328
329
330
331
332
333
334
335
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

  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];
      }
364

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

368
#ifndef WITH_VECTORIZATION
369
370
371
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

        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. */
397

398
#ifdef WITH_VECTORIZATION
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
  /* 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.
 */
419
420
421
422
423
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;
424

425
#ifdef WITH_VECTORIZATION
426
427
428
429
430
431
432
  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
433

Matthieu Schaller's avatar
Matthieu Schaller committed
434
  TIMER_TIC;
435

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

439
  /* Get the relative distance between the pairs, wrapping. */
440
441
  double shift[3] = {0.0, 0.0, 0.0};
  for (int k = 0; k < 3; k++) {
442
443
444
445
446
447
448
    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. */
449
450
  int sid = 0;
  for (int k = 0; k < 3; k++)
451
452
453
454
455
    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? */
456
  const int flipped = runner_flip[sid];
457
458
459
460
461
462
  sid = sortlistID[sid];

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

  /* Pick-out the sorted lists. */
463
464
  const struct entry *restrict sort_j = &cj->sort[sid * (cj->count + 1)];
  const float dxj = cj->dx_max;
465
466
467
468
469

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

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

      /* Get a hold of the ith part in ci. */
473
474
475
476
477
478
479
480
481
      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];
482
483

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

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

        /* Compute the pairwise distance. */
490
491
492
        float r2 = 0.0f;
        float dx[3];
        for (int k = 0; k < 3; k++) {
493
494
          dx[k] = pix[k] - pj->x[k];
          r2 += dx[k] * dx[k];
495
        }
496
497
498
499

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

500
#ifndef WITH_VECTORIZATION
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523

          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
524
        }
525
526
527
528
529
530
531
532
533
534
535

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

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

  }

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

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

      /* Get a hold of the ith part in ci. */
539
540
541
542
543
544
545
546
      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];
547
548

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

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

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

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

565
#ifndef WITH_VECTORIZATION
566
567

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

569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
#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. */
  }

596
#ifdef WITH_VECTORIZATION
597
598
  /* Pick up any leftovers. */
  if (icount > 0)
599
    for (int k = 0; k < icount; k++)
600
601
      IACT_NONSYM(r2q[k], &dxq[3 * k], hiq[k], hjq[k], piq[k], pjq[k]);
#endif
Pedro Gonnet's avatar
Pedro Gonnet committed
602

603
604
  TIMER_TOC(timer_dopair_subset);
}
Pedro Gonnet's avatar
Pedro Gonnet committed
605

606
607
608
609
610
611
/**
 * @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.
612
 * @param parts The #part to interact.
613
614
615
 * @param ind The list of indices of particles in @c ci to interact with.
 * @param count The number of particles in @c ind.
 */
616
617
618
void DOSELF_SUBSET(struct runner *r, struct cell *restrict ci,
                   struct part *restrict parts, int *restrict ind, int count) {

619
#ifdef WITH_VECTORIZATION
620
621
622
623
624
625
626
  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
627

Matthieu Schaller's avatar
Matthieu Schaller committed
628
  TIMER_TIC;
629

630
631
  const int count_i = ci->count;
  struct part *restrict parts_j = ci->parts;
632
633

  /* Loop over the parts in ci. */
634
  for (int pid = 0; pid < count; pid++) {
635
636

    /* Get a hold of the ith part in ci. */
637
638
639
640
    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;
641

642
    /* Loop over the parts in cj. */
643
    for (int pjd = 0; pjd < count_i; pjd++) {
644
645

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

      /* Compute the pairwise distance. */
649
650
651
      float r2 = 0.0f;
      float dx[3];
      for (int k = 0; k < 3; k++) {
652
653
654
655
656
657
658
        dx[k] = pix[k] - pj->x[k];
        r2 += dx[k] * dx[k];
      }

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

659
#ifndef WITH_VECTORIZATION
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687

        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. */
688

689
#ifdef WITH_VECTORIZATION
690
691
  /* Pick up any leftovers. */
  if (icount > 0)
692
    for (int k = 0; k < icount; k++)
693
694
695
696
697
      IACT_NONSYM(r2q[k], &dxq[3 * k], hiq[k], hjq[k], piq[k], pjq[k]);
#endif

  TIMER_TOC(timer_dopair_subset);
}
698

699
/**
700
 * @brief Compute the interactions between a cell pair (non-symmetric).
701
702
703
704
705
 *
 * @param r The #runner.
 * @param ci The first #cell.
 * @param cj The second #cell.
 */
706
707
void DOPAIR1(struct runner *r, struct cell *ci, struct cell *cj) {

708
  const struct engine *restrict e = r->e;
709

710
#ifdef WITH_VECTORIZATION
711
712
713
714
715
716
717
  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
718

Matthieu Schaller's avatar
Matthieu Schaller committed
719
  TIMER_TIC;
720
721

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

724
725
726
727
728
#ifdef SWIFT_DEBUG_CHECKS
  cell_is_drifted(ci, e);
  cell_is_drifted(cj, e);
#endif

729
  /* Get the sort ID. */
730
731
  double shift[3] = {0.0, 0.0, 0.0};
  const int sid = space_getsid(e->s, &ci, &cj, shift);
732
733
734
735
736
737

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

  /* Get the cutoff shift. */
738
739
  double rshift = 0.0;
  for (int k = 0; k < 3; k++) rshift += shift[k] * runner_shift[sid][k];
740
741

  /* Pick-out the sorted lists. */
742
743
  const struct entry *restrict sort_i = &ci->sort[sid * (ci->count + 1)];
  const struct entry *restrict sort_j = &cj->sort[sid * (cj->count + 1)];
744
745

  /* Get some other useful values. */
746
747
748
749
750
751
752
753
754
  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);
755
756

  /* Loop over the parts in ci. */
757
758
  for (int pid = count_i - 1;
       pid >= 0 && sort_i[pid].d + hi_max + dx_max > dj_min; pid--) {
759
760

    /* Get a hold of the ith part in ci. */
761
    struct part *restrict pi = &parts_i[sort_i[pid].i];
762
    if (!part_is_active(pi, e)) continue;
763
764
    const float hi = pi->h;
    const double di = sort_i[pid].d + hi * kernel_gamma + dx_max - rshift;
765
766
    if (di < dj_min) continue;

767
768
769
    double pix[3];
    for (int k = 0; k < 3; k++) pix[k] = pi->x[k] - shift[k];
    const float hig2 = hi * hi * kernel_gamma2;
770
771

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

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

      /* Compute the pairwise distance. */
778
779
780
      float r2 = 0.0f;
      float dx[3];
      for (int k = 0; k < 3; k++) {
781
782
783
784
785
786
787
        dx[k] = pix[k] - pj->x[k];
        r2 += dx[k] * dx[k];
      }

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

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

        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. */
819
  for (int pjd = 0; pjd < count_j && sort_j[pjd].d - hj_max - dx_max < di_max;
820
821
822
       pjd++) {

    /* Get a hold of the jth part in cj. */
823
    struct part *restrict pj = &parts_j[sort_j[pjd].i];
824
    if (!part_is_active(pj, e)) continue;
825
826
    const float hj = pj->h;
    const double dj = sort_j[pjd].d - hj * kernel_gamma - dx_max - rshift;
827
828
    if (dj > di_max) continue;

829
830
831
    double pjx[3];
    for (int k = 0; k < 3; k++) pjx[k] = pj->x[k] + shift[k];
    const float hjg2 = hj * hj * kernel_gamma2;
832
833

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

      /* Get a pointer to the jth particle. */
837
      struct part *restrict pi = &parts_i[sort_i[pid].i];
838
839

      /* Compute the pairwise distance. */
840
841
842
      float r2 = 0.0f;
      float dx[3];
      for (int k = 0; k < 3; k++) {
843
844
845
846
847
848
849
        dx[k] = pjx[k] - pi->x[k];
        r2 += dx[k] * dx[k];
      }

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

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

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

880
#ifdef WITH_VECTORIZATION
881
882
  /* Pick up any leftovers. */
  if (icount > 0)
883
    for (int k = 0; k < icount; k++)
884
885
886
887
888
889
      IACT_NONSYM(r2q[k], &dxq[3 * k], hiq[k], hjq[k], piq[k], pjq[k]);
#endif

  TIMER_TOC(TIMER_DOPAIR);
}

890
891
892
893
894
895
896
/**
 * @brief Compute the interactions between a cell pair (symmetric)
 *
 * @param r The #runner.
 * @param ci The first #cell.
 * @param cj The second #cell.
 */
897
898
899
void DOPAIR2(struct runner *r, struct cell *ci, struct cell *cj) {

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

901
#ifdef WITH_VECTORIZATION
902
903
904
905
906
907
908
909
910
911
912
913
914
  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
915

Matthieu Schaller's avatar
Matthieu Schaller committed
916
  TIMER_TIC;
917
918

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

921
922
923
924
925
#ifdef SWIFT_DEBUG_CHECKS
  cell_is_drifted(ci, e);
  cell_is_drifted(cj, e);
#endif

926
  /* Get the shift ID. */
927
928
  double shift[3] = {0.0, 0.0, 0.0};
  const int sid = space_getsid(e->s, &ci, &cj, shift);
929
930
931
932
933
934

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

  /* Get the cutoff shift. */
935
936
  double rshift = 0.0;
  for (int k = 0; k < 3; k++) rshift += shift[k] * runner_shift[sid][k];
937
938

  /* Pick-out the sorted lists. */
939
940
  struct entry *restrict sort_i = &ci->sort[sid * (ci->count + 1)];
  struct entry *restrict sort_j = &cj->sort[sid * (cj->count + 1)];
941
942

  /* Get some other useful values. */
943
944
945
946
947
948
949
950
951
  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);
952
953

  /* Collect the number of parts left and right below dt. */
954
955
  int countdt_i = 0, countdt_j = 0;
  struct entry *restrict sortdt_i = NULL, *restrict sortdt_j = NULL;
956
  if (cell_is_all_active(ci, e)) {
957
958
    sortdt_i = sort_i;
    countdt_i = count_i;
959
  } else if (cell_is_active(ci, e)) {
960
961
    if (posix_memalign((void *)&sortdt_i, VEC_SIZE * sizeof(float),
                       sizeof(struct entry) * count_i) != 0)
962
      error("Failed to allocate dt sortlists.");
963
    for (int k = 0; k < count_i; k++)
964
      if (part_is_active(&parts_i[sort_i[k].i], e)) {
965
966
967
968
        sortdt_i[countdt_i] = sort_i[k];
        countdt_i += 1;
      }
  }
969
  if (cell_is_all_active(cj, e)) {
970
971
    sortdt_j = sort_j;
    countdt_j = count_j;
972
  } else if (cell_is_active(cj, e)) {
973
974
    if (posix_memalign((void *)&sortdt_j, VEC_SIZE * sizeof(float),
                       sizeof(struct entry) * count_j) != 0)
975
      error("Failed to allocate dt sortlists.");
976
    for (int k = 0; k < count_j; k++)
977
      if (part_is_active(&parts_j[sort_j[k].i], e)) {
978
979
980
981
982
983
        sortdt_j[countdt_j] = sort_j[k];
        countdt_j += 1;
      }
  }

  /* Loop over the parts in ci. */
984
985
  for (int pid = count_i - 1;
       pid >= 0 && sort_i[pid].d + hi_max + dx_max > dj_min; pid--) {
986
987

    /* Get a hold of the ith part in ci. */
988
989
990
    struct part *restrict pi = &parts_i[sort_i[pid].i];
    const float hi = pi->h;
    const double di = sort_i[pid].d + hi * kernel_gamma + dx_max - rshift;
991
992
    if (di < dj_min) continue;

993
994
995
    double pix[3];
    for (int k = 0; k < 3; k++) pix[k] = pi->x[k] - shift[k];
    const float hig2 = hi * hi * kernel_gamma2;
996
997

    /* Look at valid dt parts only? */
998
    if (!part_is_active(pi, e)) {
999
1000

      /* Loop over the parts in cj within dt. */
For faster browsing, not all history is shown. View entire blame