runner_doiact.h 103 KB
Newer Older
1
/*******************************************************************************
2
 * This file is part of SWIFT.
3
 * Coypright (c) 2012 Pedro Gonnet (pedro.gonnet@durham.ac.uk)
4
 *
5
6
7
8
 * 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.
9
 *
10
11
12
13
 * 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.
14
 *
15
16
 * 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/>.
17
 *
18
19
 ******************************************************************************/

20
21
22
/* Includes. */
#include "part.h"

23
24
25
26
27
28
/* 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. */

29
#define PASTE(x, y) x##_##y
30

31
#define _DOPAIR1(f) PASTE(runner_dopair1, f)
32
#define DOPAIR1 _DOPAIR1(FUNCTION)
33

34
#define _DOPAIR2(f) PASTE(runner_dopair2, f)
35
#define DOPAIR2 _DOPAIR2(FUNCTION)
36

37
#define _DOPAIR_SUBSET(f) PASTE(runner_dopair_subset, f)
38
#define DOPAIR_SUBSET _DOPAIR_SUBSET(FUNCTION)
39

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

43
#define _DOPAIR_NAIVE(f) PASTE(runner_dopair_naive, f)
44
#define DOPAIR_NAIVE _DOPAIR_NAIVE(FUNCTION)
45

46
#define _DOSELF_NAIVE(f) PASTE(runner_doself_naive, f)
47
#define DOSELF_NAIVE _DOSELF_NAIVE(FUNCTION)
48

49
#define _DOSELF1(f) PASTE(runner_doself1, f)
50
#define DOSELF1 _DOSELF1(FUNCTION)
51

52
#define _DOSELF2(f) PASTE(runner_doself2, f)
53
#define DOSELF2 _DOSELF2(FUNCTION)
54

55
#define _DOSELF_SUBSET(f) PASTE(runner_doself_subset, f)
56
#define DOSELF_SUBSET _DOSELF_SUBSET(FUNCTION)
57

58
#define _DOSUB1(f) PASTE(runner_dosub1, f)
59
#define DOSUB1 _DOSUB1(FUNCTION)
60

61
#define _DOSUB2(f) PASTE(runner_dosub2, f)
62
#define DOSUB2 _DOSUB2(FUNCTION)
63

64
#define _DOSUB_SUBSET(f) PASTE(runner_dosub_subset, f)
65
#define DOSUB_SUBSET _DOSUB_SUBSET(FUNCTION)
66

67
#define _IACT_NONSYM(f) PASTE(runner_iact_nonsym, f)
68
#define IACT_NONSYM _IACT_NONSYM(FUNCTION)
69

70
#define _IACT(f) PASTE(runner_iact, f)
71
#define IACT _IACT(FUNCTION)
72

73
#define _TIMER_DOSELF(f) PASTE(timer_doself, f)
74
#define TIMER_DOSELF _TIMER_DOSELF(FUNCTION)
75

76
#define _TIMER_DOPAIR(f) PASTE(timer_dopair, f)
77
#define TIMER_DOPAIR _TIMER_DOPAIR(FUNCTION)
Pedro Gonnet's avatar
Pedro Gonnet committed
78

79
#define _TIMER_DOSUB(f) PASTE(timer_dosub, f)
80
81
#define TIMER_DOSUB _TIMER_DOSUB(FUNCTION)

82
#define _TIMER_DOSELF_SUBSET(f) PASTE(timer_doself_subset, f)
83
84
#define TIMER_DOSELF_SUBSET _TIMER_DOSELF_SUBSET(FUNCTION)

85
#define _TIMER_DOPAIR_SUBSET(f) PASTE(timer_dopair_subset, f)
86
87
#define TIMER_DOPAIR_SUBSET _TIMER_DOPAIR_SUBSET(FUNCTION)

88
#define _IACT_NONSYM_VEC(f) PASTE(runner_iact_nonsym_vec, f)
89
90
#define IACT_NONSYM_VEC _IACT_NONSYM_VEC(FUNCTION)

91
#define _IACT_VEC(f) PASTE(runner_iact_vec, f)
92
#define IACT_VEC _IACT_VEC(FUNCTION)
Pedro Gonnet's avatar
Pedro Gonnet committed
93

94
95
96
97
98
99
100
/**
 * @brief Compute the interactions between a cell pair.
 *
 * @param r The #runner.
 * @param ci The first #cell.
 * @param cj The second #cell.
 */
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186

void DOPAIR_NAIVE(struct runner *r, struct cell *restrict ci,
                  struct cell *restrict cj) {

  struct engine *e = r->e;
  int pid, pjd, k, count_i = ci->count, count_j = cj->count;
  double shift[3] = {0.0, 0.0, 0.0};
  struct part *restrict parts_i = ci->parts, *restrict parts_j = cj->parts;
  struct part *restrict pi, *restrict pj;
  double pix[3];
  float dx[3], hi, hig2, r2;
  float dt_step = e->dt_step;
#ifdef VECTORIZE
  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
  TIMER_TIC

  /* Anything to do here? */
  if (ci->dt_min > dt_step && cj->dt_min > dt_step) return;

  /* Get the relative distance between the pairs, wrapping. */
  for (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];
  }

  /* printf( "runner_dopair_naive: doing pair [ %g %g %g ]/[ %g %g %g ] with
  %i/%i parts and shift = [ %g %g %g ].\n" ,
      ci->loc[0] , ci->loc[1] , ci->loc[2] , cj->loc[0] , cj->loc[1] ,
  cj->loc[2] ,
      ci->count , cj->count , shift[0] , shift[1] , shift[2] ); fflush(stdout);
  tic = getticks(); */

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

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

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

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

      /* Compute the pairwise distance. */
      r2 = 0.0f;
      for (k = 0; k < 3; k++) {
        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) {

#ifndef VECTORIZE

        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;
187
188
        }

189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#endif
      }

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

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

#ifdef VECTORIZE
  /* Pick up any leftovers. */
  if (icount > 0)
    for (k = 0; k < icount; k++)
      IACT(r2q[k], &dxq[3 * k], hiq[k], hjq[k], piq[k], pjq[k]);
#endif

#ifdef TIMER_VERBOSE
  printf(
      "runner_dopair_naive[%02i]: %i/%i parts at depth %i (r_max=%.3f/%.3f) "
      "took %.3f ms.\n",
      r->id, count_i, count_j, ci->depth, ci->h_max, cj->h_max,
      ((double)TIMER_TOC(TIMER_DOPAIR)) / CPU_TPS * 1000);
#else
  TIMER_TOC(TIMER_DOPAIR);
#endif
}

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

  int pid, pjd, k, count = c->count;
  struct part *restrict parts = c->parts;
  struct part *restrict pi, *restrict pj;
  double pix[3] = {0.0, 0.0, 0.0};
  float dx[3], hi, hig2, r2;
  float dt_step = r->e->dt_step;
#ifdef VECTORIZE
  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
  TIMER_TIC

  /* Anything to do here? */
  if (c->dt_min > dt_step) return;

  /* printf( "runner_dopair_naive: doing pair [ %g %g %g ]/[ %g %g %g ] with
  %i/%i parts and shift = [ %g %g %g ].\n" ,
      ci->loc[0] , ci->loc[1] , ci->loc[2] , cj->loc[0] , cj->loc[1] ,
  cj->loc[2] ,
      ci->count , cj->count , shift[0] , shift[1] , shift[2] ); fflush(stdout);
  tic = getticks(); */

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

    /* Get a hold of the ith part in ci. */
    pi = &parts[pid];
    pix[0] = pi->x[0];
    pix[1] = pi->x[1];
    pix[2] = pi->x[2];
    hi = pi->h;
    hig2 = hi * hi * kernel_gamma2;
252

253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
    /* Loop over the parts in cj. */
    for (pjd = pid + 1; pjd < count; pjd++) {

      /* Get a pointer to the jth particle. */
      pj = &parts[pjd];

      /* Compute the pairwise distance. */
      r2 = 0.0f;
      for (k = 0; k < 3; k++) {
        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) {

#ifndef VECTORIZE

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

292
293
#endif
      }
294

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

297
298
299
300
301
302
303
304
  } /* loop over the parts in ci. */

#ifdef VECTORIZE
  /* Pick up any leftovers. */
  if (icount > 0)
    for (k = 0; k < icount; k++)
      IACT(r2q[k], &dxq[3 * k], hiq[k], hjq[k], piq[k], pjq[k]);
#endif
305

306
307
308
309
310
311
312
#ifdef TIMER_VERBOSE
  printf("runner_doself[%02i]: %i parts at depth %i took %.3f ms.\n", r->id,
         count, c->depth, ((double)TIMER_TOC(TIMER_DOSELF)) / CPU_TPS * 1000);
#else
  TIMER_TOC(TIMER_DOSELF);
#endif
}
313

314
315
316
317
318
319
/**
 * @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.
320
 * @param parts_i The #part to interact with @c cj.
321
322
323
324
 * @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.
 */
325
326
327
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
364
365
366
367
368
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
397
398
399
400
401
402
403
404

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;
  int pid, pjd, sid, k, count_j = cj->count, flipped;
  double shift[3] = {0.0, 0.0, 0.0};
  struct part *restrict pi, *restrict pj, *restrict parts_j = cj->parts;
  double pix[3];
  float dx[3], hi, hig2, r2, di, dxj;
  struct entry *sort_j;
#ifdef VECTORIZE
  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
  TIMER_TIC

  /* Get the relative distance between the pairs, wrapping. */
  for (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];
  }

  /* Get the sorting index. */
  for (sid = 0, k = 0; k < 3; k++)
    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? */
  flipped = runner_flip[sid];
  sid = sortlistID[sid];

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

  /* printf( "runner_dopair_naive: doing pair [ %g %g %g ]/[ %g %g %g ] with
  %i/%i parts and shift = [ %g %g %g ].\n" ,
      ci->loc[0] , ci->loc[1] , ci->loc[2] , cj->loc[0] , cj->loc[1] ,
  cj->loc[2] ,
      ci->count , cj->count , shift[0] , shift[1] , shift[2] ); fflush(stdout);
  tic = getticks(); */

  /* Pick-out the sorted lists. */
  sort_j = &cj->sort[sid * (cj->count + 1)];
  dxj = cj->dx_max;

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

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

      /* Get a hold of the ith part in ci. */
      pi = &parts_i[ind[pid]];
      for (k = 0; k < 3; k++) pix[k] = pi->x[k] - shift[k];
      hi = pi->h;
      hig2 = hi * hi * kernel_gamma2;
      di = hi * kernel_gamma + dxj + pix[0] * runner_shift[3 * sid + 0] +
           pix[1] * runner_shift[3 * sid + 1] +
           pix[2] * runner_shift[3 * sid + 2];

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

        /* Get a pointer to the jth particle. */
        pj = &parts_j[sort_j[pjd].i];

        /* Compute the pairwise distance. */
        r2 = 0.0f;
        for (k = 0; k < 3; k++) {
          dx[k] = pix[k] - pj->x[k];
          r2 += dx[k] * dx[k];
405
        }
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433

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

#ifndef VECTORIZE

          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
434
        }
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467

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

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

  }

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

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

      /* Get a hold of the ith part in ci. */
      pi = &parts_i[ind[pid]];
      for (k = 0; k < 3; k++) pix[k] = pi->x[k] - shift[k];
      hi = pi->h;
      hig2 = hi * hi * kernel_gamma2;
      di = -hi * kernel_gamma - dxj + pix[0] * runner_shift[3 * sid + 0] +
           pix[1] * runner_shift[3 * sid + 1] +
           pix[2] * runner_shift[3 * sid + 2];

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

        /* Get a pointer to the jth particle. */
        pj = &parts_j[sort_j[pjd].i];

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

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

473
474
475
#ifndef VECTORIZE

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

477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#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. */
  }

#ifdef VECTORIZE
  /* Pick up any leftovers. */
  if (icount > 0)
    for (k = 0; k < icount; k++)
      IACT_NONSYM(r2q[k], &dxq[3 * k], hiq[k], hjq[k], piq[k], pjq[k]);
#endif

#ifdef TIMER_VERBOSE
  printf(
      "runner_dopair_subset[%02i]: %i/%i parts at depth %i (r_max=%.3f/%.3f) "
      "took %.3f ms.\n",
      r->id, count, count_j, ci->depth, ci->h_max, cj->h_max,
      ((double)TIMER_TOC(TIMER_DOPAIR)) / CPU_TPS * 1000);
#else
  TIMER_TOC(timer_dopair_subset);
#endif
}
521

Pedro Gonnet's avatar
Pedro Gonnet committed
522
523
524
525
526
527
/**
 * @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.
528
 * @param parts_i The #part to interact with @c cj.
Pedro Gonnet's avatar
Pedro Gonnet committed
529
530
531
532
533
 * @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.
 */

534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
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;
  int pid, pjd, k, count_j = cj->count;
  double shift[3] = {0.0, 0.0, 0.0};
  struct part *restrict pi, *restrict pj, *restrict parts_j = cj->parts;
  double pix[3];
  float dx[3], hi, hig2, r2;
#ifdef VECTORIZE
  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
  TIMER_TIC

  /* Get the relative distance between the pairs, wrapping. */
  for (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];
  }

  /* printf( "runner_dopair_naive: doing pair [ %g %g %g ]/[ %g %g %g ] with
  %i/%i parts and shift = [ %g %g %g ].\n" ,
      ci->loc[0] , ci->loc[1] , ci->loc[2] , cj->loc[0] , cj->loc[1] ,
  cj->loc[2] ,
      ci->count , cj->count , shift[0] , shift[1] , shift[2] ); fflush(stdout);
  tic = getticks(); */

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

    /* Get a hold of the ith part in ci. */
    pi = &parts_i[ind[pid]];
    for (k = 0; k < 3; k++) pix[k] = pi->x[k] - shift[k];
    hi = pi->h;
    hig2 = hi * hi * kernel_gamma2;
Pedro Gonnet's avatar
Pedro Gonnet committed
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
604
605
606
607
608
609
610
611
612
613
614
615
    /* Loop over the parts in cj. */
    for (pjd = 0; pjd < count_j; pjd++) {

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

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

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

#ifndef VECTORIZE

        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;
        }
Pedro Gonnet's avatar
Pedro Gonnet committed
616

617
618
#endif
      }
Pedro Gonnet's avatar
Pedro Gonnet committed
619

620
    } /* loop over the parts in cj. */
Pedro Gonnet's avatar
Pedro Gonnet committed
621

622
  } /* loop over the parts in ci. */
Pedro Gonnet's avatar
Pedro Gonnet committed
623

624
625
626
627
628
629
#ifdef VECTORIZE
  /* Pick up any leftovers. */
  if (icount > 0)
    for (k = 0; k < icount; k++)
      IACT_NONSYM(r2q[k], &dxq[3 * k], hiq[k], hjq[k], piq[k], pjq[k]);
#endif
Pedro Gonnet's avatar
Pedro Gonnet committed
630

631
632
633
634
635
636
637
638
639
640
#ifdef TIMER_VERBOSE
  printf(
      "runner_dopair_subset[%02i]: %i/%i parts at depth %i (r_max=%.3f/%.3f) "
      "took %.3f ms.\n",
      r->id, count, count_j, ci->depth, ci->h_max, cj->h_max,
      ((double)TIMER_TOC(TIMER_DOPAIR)) / CPU_TPS * 1000);
#else
  TIMER_TOC(timer_dopair_subset);
#endif
}
Pedro Gonnet's avatar
Pedro Gonnet committed
641

642
643
644
645
646
647
/**
 * @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.
648
 * @param parts The #part to interact.
649
650
651
652
 * @param ind The list of indices of particles in @c ci to interact with.
 * @param count The number of particles in @c ind.
 */

653
654
655
656
657
658
659
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
void DOSELF_SUBSET(struct runner *r, struct cell *restrict ci,
                   struct part *restrict parts, int *restrict ind, int count) {

  int pid, pjd, k, count_i = ci->count;
  struct part *restrict parts_j = ci->parts;
  struct part *restrict pi, *restrict pj;
  double pix[3] = {0.0, 0.0, 0.0};
  float dx[3], hi, hig2, r2;
#ifdef VECTORIZE
  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
  TIMER_TIC

  /* printf( "runner_dopair_naive: doing pair [ %g %g %g ]/[ %g %g %g ] with
  %i/%i parts and shift = [ %g %g %g ].\n" ,
      ci->loc[0] , ci->loc[1] , ci->loc[2] , cj->loc[0] , cj->loc[1] ,
  cj->loc[2] ,
      ci->count , cj->count , shift[0] , shift[1] , shift[2] ); fflush(stdout);
  tic = getticks(); */

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

    /* Get a hold of the ith part in ci. */
    pi = &parts[ind[pid]];
    pix[0] = pi->x[0];
    pix[1] = pi->x[1];
    pix[2] = pi->x[2];
    hi = pi->h;
    hig2 = hi * hi * kernel_gamma2;
688

689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
    /* Loop over the parts in cj. */
    for (pjd = 0; pjd < count_i; pjd++) {

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

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

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

#ifndef VECTORIZE

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

735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
#ifdef VECTORIZE
  /* Pick up any leftovers. */
  if (icount > 0)
    for (k = 0; k < icount; k++)
      IACT_NONSYM(r2q[k], &dxq[3 * k], hiq[k], hjq[k], piq[k], pjq[k]);
#endif

#ifdef TIMER_VERBOSE
  printf("runner_doself_subset[%02i]: %i/%i parts at depth %i took %.3f ms.\n",
         r->id, count, ci->count, ci->depth,
         ((double)TIMER_TOC(TIMER_DOSELF)) / CPU_TPS * 1000);
#else
  TIMER_TOC(timer_dopair_subset);
#endif
}
750

751
752
753
754
755
756
757
/**
 * @brief Compute the interactions between a cell pair.
 *
 * @param r The #runner.
 * @param ci The first #cell.
 * @param cj The second #cell.
 */
758

759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
void DOPAIR1(struct runner *r, struct cell *ci, struct cell *cj) {

  struct engine *restrict e = r->e;
  int pid, pjd, k, sid;
  double rshift, shift[3] = {0.0, 0.0, 0.0};
  struct entry *restrict sort_i, *restrict sort_j;
  struct part *restrict pi, *restrict pj, *restrict parts_i, *restrict parts_j;
  double pix[3], pjx[3], di, dj;
  float dx[3], hi, hig2, hj, hjg2, r2, dx_max;
  double hi_max, hj_max;
  double di_max, dj_min;
  int count_i, count_j;
  float dt_step = e->dt_step;
#ifdef VECTORIZE
  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
  TIMER_TIC

  /* Anything to do here? */
  if (ci->dt_min > dt_step && cj->dt_min > dt_step) return;

  /* Get the sort ID. */
  sid = space_getsid(e->s, &ci, &cj, shift);

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

  /* Get the cutoff shift. */
  for (rshift = 0.0, k = 0; k < 3; k++)
    rshift += shift[k] * runner_shift[3 * sid + k];

  /* Pick-out the sorted lists. */
  sort_i = &ci->sort[sid * (ci->count + 1)];
  sort_j = &cj->sort[sid * (cj->count + 1)];

  /* Get some other useful values. */
  hi_max = ci->h_max * kernel_gamma - rshift;
  hj_max = cj->h_max * kernel_gamma;
  count_i = ci->count;
  count_j = cj->count;
  parts_i = ci->parts;
  parts_j = cj->parts;
  di_max = sort_i[count_i - 1].d - rshift;
  dj_min = sort_j[0].d;
  dx_max = (ci->dx_max + cj->dx_max);

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

    /* Get a hold of the ith part in ci. */
    pi = &parts_i[sort_i[pid].i];
    if (pi->dt > dt_step) continue;
    hi = pi->h;
    di = sort_i[pid].d + hi * kernel_gamma + dx_max - rshift;
    if (di < dj_min) continue;

    hig2 = hi * hi * kernel_gamma2;
    for (k = 0; k < 3; k++) pix[k] = pi->x[k] - shift[k];
824
825

    /* Loop over the parts in cj. */
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
    for (pjd = 0; pjd < count_j && sort_j[pjd].d < di; pjd++) {

      /* Get a pointer to the jth particle. */
      pj = &parts_j[sort_j[pjd].i];

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

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

#ifndef VECTORIZE

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

  /* printf( "runner_dopair: first half took %.3f ms...\n" ,
  ((double)(getticks() - tic)) / CPU_TPS * 1000 );
  tic = getticks(); */

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

    /* Get a hold of the jth part in cj. */
    pj = &parts_j[sort_j[pjd].i];
    if (pj->dt > dt_step) continue;
    hj = pj->h;
    dj = sort_j[pjd].d - hj * kernel_gamma - dx_max - rshift;
    if (dj > di_max) continue;

    for (k = 0; k < 3; k++) pjx[k] = pj->x[k] + shift[k];
    hjg2 = hj * hj * kernel_gamma2;

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

      /* Get a pointer to the jth particle. */
      pi = &parts_i[sort_i[pid].i];

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

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

#ifndef VECTORIZE

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

#ifdef VECTORIZE
  /* Pick up any leftovers. */
  if (icount > 0)
    for (k = 0; k < icount; k++)
      IACT_NONSYM(r2q[k], &dxq[3 * k], hiq[k], hjq[k], piq[k], pjq[k]);
#endif

#ifdef TIMER_VERBOSE
  printf(
      "runner_dopair[%02i]: %i/%i parts at depth %i (r_max=%.3f/%.3f, h=%.3f) "
      "took %.3f ms.\n",
      r->id, count_i, count_j, ci->depth, ci->h_max, cj->h_max,
      fmax(ci->h[0], fmax(ci->h[1], ci->h[2])),
      ((double)(TIMER_TOC(TIMER_DOPAIR))) / CPU_TPS * 1000);
#else
  TIMER_TOC(TIMER_DOPAIR);
#endif
}

void DOPAIR2(struct runner *r, struct cell *ci, struct cell *cj) {

  struct engine *restrict e = r->e;
  int pid, pjd, k, sid;
  double rshift, shift[3] = {0.0, 0.0, 0.0};
  struct entry *restrict sort_i, *restrict sort_j;
  struct entry *restrict sortdt_i = NULL, *restrict sortdt_j = NULL;
  int countdt_i = 0, countdt_j = 0;
  struct part *restrict pi, *restrict pj, *restrict parts_i, *restrict parts_j;
  double pix[3], pjx[3], di, dj;
  float dx[3], hi, hig2, hj, hjg2, r2, dx_max;
  double hi_max, hj_max;
  double di_max, dj_min;
  int count_i, count_j;
  float dt_step = e->dt_step;
#ifdef VECTORIZE
  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
  TIMER_TIC

  /* Anything to do here? */
  if (ci->dt_min > dt_step && cj->dt_min > dt_step) return;

  /* Get the shift ID. */
  sid = space_getsid(e->s, &ci, &cj, shift);

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

  /* Get the cutoff shift. */
  for (rshift = 0.0, k = 0; k < 3; k++)
    rshift += shift[k] * runner_shift[3 * sid + k];

  /* Pick-out the sorted lists. */
  sort_i = &ci->sort[sid * (ci->count + 1)];
For faster browsing, not all history is shown. View entire blame