scheduler.c 45.9 KB
Newer Older
1
2
/*******************************************************************************
 * This file is part of SWIFT.
3
 * Copyright (c) 2012 Pedro Gonnet (pedro.gonnet@durham.ac.uk)
4
 *                    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
 ******************************************************************************/

/* Config parameters. */
#include "../config.h"

/* Some standard headers. */
25
26
27
#include <limits.h>
#include <math.h>
#include <pthread.h>
28
29
30
31
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

32
33
/* MPI headers. */
#ifdef WITH_MPI
34
#include <mpi.h>
35
36
#endif

37
38
39
/* This object's header. */
#include "scheduler.h"

40
41
42
/* Local headers. */
#include "atomic.h"
#include "const.h"
43
44
#include "cycle.h"
#include "error.h"
45
#include "intrinsics.h"
46
#include "kernel_hydro.h"
47
#include "timers.h"
48

49
50
51
52
53
54
55
56
/**
 * @brief Add an unlock_task to the given task.
 *
 * @param s The #scheduler.
 * @param ta The unlocking #task.
 * @param tb The #task that will be unlocked.
 */

57
58
59
60
61
62
63
void scheduler_addunlock(struct scheduler *s, struct task *ta,
                         struct task *tb) {
  /* Get an index at which to store this unlock. */
  const int ind = atomic_inc(&s->nr_unlocks);

  /* Does the buffer need to be grown? */
  if (ind == s->size_unlocks) {
64
    /* Allocate the new buffer. */
65
66
67
68
    struct task **unlocks_new;
    int *unlock_ind_new;
    const int size_unlocks_new = s->size_unlocks * 2;
    if ((unlocks_new = (struct task **)malloc(
69
70
71
             sizeof(struct task *) *size_unlocks_new)) == NULL ||
        (unlock_ind_new = (int *)malloc(sizeof(int) * size_unlocks_new)) ==
            NULL)
72
      error("Failed to re-allocate unlocks.");
73

74
    /* Wait for all writes to the old buffer to complete. */
75
76
77
    while (s->completed_unlock_writes < ind)
      ;

78
    /* Copy the buffers. */
79
80
81
82
83
84
    memcpy(unlocks_new, s->unlocks, sizeof(struct task *) * ind);
    memcpy(unlock_ind_new, s->unlock_ind, sizeof(int) * ind);
    free(s->unlocks);
    free(s->unlock_ind);
    s->unlocks = unlocks_new;
    s->unlock_ind = unlock_ind_new;
85

86
    /* Publish the new buffer size. */
87
88
    s->size_unlocks = size_unlocks_new;
  }
89

90
  /* Wait for there to actually be space at my index. */
91
92
  while (ind > s->size_unlocks)
    ;
93
94
95
96

  /* Write the unlock to the scheduler. */
  s->unlocks[ind] = tb;
  s->unlock_ind[ind] = ta - s->tasks;
97
  atomic_inc(&s->completed_unlock_writes);
98
99
}

100
101
102
103
104
/**
 * @brief Split tasks that may be too large.
 *
 * @param s The #scheduler we are working in.
 */
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
void scheduler_splittasks_mapper(void *map_data, void *extra_data) {

  /* Static constants. */
  const static int pts[7][8] = {{-1, 12, 10, 9, 4, 3, 1, 0},
                                {-1, -1, 11, 10, 5, 4, 2, 1},
                                {-1, -1, -1, 12, 7, 6, 4, 3},
                                {-1, -1, -1, -1, 8, 7, 5, 4},
                                {-1, -1, -1, -1, -1, 12, 10, 9},
                                {-1, -1, -1, -1, -1, -1, 11, 10},
                                {-1, -1, -1, -1, -1, -1, -1, 12}};
  const static float sid_scale[13] = {0.1897, 0.4025, 0.1897, 0.4025, 0.5788,
                                      0.4025, 0.1897, 0.4025, 0.1897, 0.4025,
                                      0.5788, 0.4025, 0.5788};

  /* Extract the parameters. */
  struct scheduler *s = (struct scheduler *)extra_data;
  struct task *t = (struct task *)map_data;

  /* Iterate on this task until we're done with it. */
  int redo = 1;
  while (redo) {

    /* Reset the redo flag. */
    redo = 0;

    /* Non-splittable task? */
    if ((t->ci == NULL || (t->type == task_type_pair && t->cj == NULL)) ||
        ((t->type == task_type_kick) && t->ci->nodeID != s->nodeID) ||
        ((t->type == task_type_drift) && t->ci->nodeID != s->nodeID) ||
        ((t->type == task_type_init) && t->ci->nodeID != s->nodeID)) {
Matthieu Schaller's avatar
Matthieu Schaller committed
136
137
      t->type = task_type_none;
      t->skip = 1;
138
      return;
139
140
141
142
143
144
    }

    /* Self-interaction? */
    if (t->type == task_type_self) {

      /* Get a handle on the cell involved. */
Pedro Gonnet's avatar
Pedro Gonnet committed
145
      struct cell *ci = t->ci;
146
147
148
149

      /* Foreign task? */
      if (ci->nodeID != s->nodeID) {
        t->skip = 1;
150
        return;
151
152
153
154
155
156
157
158
159
160
161
      }

      /* Is this cell even split? */
      if (ci->split) {

        /* Make a sub? */
        if (scheduler_dosub && ci->count < space_subsize / ci->count) {

          /* convert to a self-subtask. */
          t->type = task_type_sub;

162
163
          /* Otherwise, make tasks explicitly. */
        } else {
164
165
166
167

          /* Take a step back (we're going to recycle the current task)... */
          redo = 1;

168
          /* Add the self tasks. */
Pedro Gonnet's avatar
Pedro Gonnet committed
169
170
171
172
          int first_child = 0;
          while (ci->progeny[first_child] == NULL) first_child++;
          t->ci = ci->progeny[first_child];
          for (int k = first_child + 1; k < 8; k++)
173
            if (ci->progeny[k] != NULL)
174
175
176
177
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_self, t->subtype, 0, 0,
                                    ci->progeny[k], NULL, 0),
                  s);
178
179

          /* Make a task for each pair of progeny. */
Pedro Gonnet's avatar
Pedro Gonnet committed
180
          for (int j = 0; j < 8; j++)
181
            if (ci->progeny[j] != NULL)
Pedro Gonnet's avatar
Pedro Gonnet committed
182
              for (int k = j + 1; k < 8; k++)
183
                if (ci->progeny[k] != NULL)
184
185
186
187
188
                  scheduler_splittasks_mapper(
                      scheduler_addtask(s, task_type_pair, t->subtype,
                                        pts[j][k], 0, ci->progeny[j],
                                        ci->progeny[k], 0),
                      s);
189
190
191
        }
      }

192
193
      /* Pair interaction? */
    } else if (t->type == task_type_pair) {
194
195

      /* Get a handle on the cells involved. */
Pedro Gonnet's avatar
Pedro Gonnet committed
196
197
198
199
      struct cell *ci = t->ci;
      struct cell *cj = t->cj;
      const double hi = ci->dmin;
      const double hj = cj->dmin;
200
201
202
203

      /* Foreign task? */
      if (ci->nodeID != s->nodeID && cj->nodeID != s->nodeID) {
        t->skip = 1;
204
        return;
205
206
207
208
      }

      /* Get the sort ID, use space_getsid and not t->flags
         to make sure we get ci and cj swapped if needed. */
Pedro Gonnet's avatar
Pedro Gonnet committed
209
210
      double shift[3];
      int sid = space_getsid(s->space, &ci, &cj, shift);
211
212
213
214
215
216
217
218
219
220
221
222
223
224

      /* Should this task be split-up? */
      if (ci->split && cj->split &&
          ci->h_max * kernel_gamma * space_stretch < hi / 2 &&
          cj->h_max * kernel_gamma * space_stretch < hj / 2) {

        /* Replace by a single sub-task? */
        if (scheduler_dosub &&
            ci->count * sid_scale[sid] < space_subsize / cj->count &&
            sid != 0 && sid != 2 && sid != 6 && sid != 8) {

          /* Make this task a sub task. */
          t->type = task_type_sub;

225
226
          /* Otherwise, split it. */
        } else {
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244

          /* Take a step back (we're going to recycle the current task)... */
          redo = 1;

          /* For each different sorting type... */
          switch (sid) {

            case 0: /* (  1 ,  1 ,  1 ) */
              t->ci = ci->progeny[7];
              t->cj = cj->progeny[0];
              t->flags = 0;
              break;

            case 1: /* (  1 ,  1 ,  0 ) */
              t->ci = ci->progeny[6];
              t->cj = cj->progeny[0];
              t->flags = 1;
              t->tight = 1;
245
246
247
248
249
250
251
252
253
254
255
256
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 1, 0,
                                    ci->progeny[7], cj->progeny[1], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 0, 0,
                                    ci->progeny[6], cj->progeny[1], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 2, 0,
                                    ci->progeny[7], cj->progeny[0], 1),
                  s);
257
258
259
260
261
262
263
264
265
266
267
268
269
270
              break;

            case 2: /* (  1 ,  1 , -1 ) */
              t->ci = ci->progeny[6];
              t->cj = cj->progeny[1];
              t->flags = 2;
              t->tight = 1;
              break;

            case 3: /* (  1 ,  0 ,  1 ) */
              t->ci = ci->progeny[5];
              t->cj = cj->progeny[0];
              t->flags = 3;
              t->tight = 1;
271
272
273
274
275
276
277
278
279
280
281
282
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 3, 0,
                                    ci->progeny[7], cj->progeny[2], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 0, 0,
                                    ci->progeny[5], cj->progeny[2], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 6, 0,
                                    ci->progeny[7], cj->progeny[0], 1),
                  s);
283
284
285
286
287
288
289
              break;

            case 4: /* (  1 ,  0 ,  0 ) */
              t->ci = ci->progeny[4];
              t->cj = cj->progeny[0];
              t->flags = 4;
              t->tight = 1;
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
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
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 5, 0,
                                    ci->progeny[5], cj->progeny[0], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 7, 0,
                                    ci->progeny[6], cj->progeny[0], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 8, 0,
                                    ci->progeny[7], cj->progeny[0], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 3, 0,
                                    ci->progeny[4], cj->progeny[1], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 4, 0,
                                    ci->progeny[5], cj->progeny[1], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 6, 0,
                                    ci->progeny[6], cj->progeny[1], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 7, 0,
                                    ci->progeny[7], cj->progeny[1], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 1, 0,
                                    ci->progeny[4], cj->progeny[2], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 2, 0,
                                    ci->progeny[5], cj->progeny[2], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 4, 0,
                                    ci->progeny[6], cj->progeny[2], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 5, 0,
                                    ci->progeny[7], cj->progeny[2], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 0, 0,
                                    ci->progeny[4], cj->progeny[3], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 1, 0,
                                    ci->progeny[5], cj->progeny[3], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 3, 0,
                                    ci->progeny[6], cj->progeny[3], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 4, 0,
                                    ci->progeny[7], cj->progeny[3], 1),
                  s);
350
351
352
353
354
355
356
              break;

            case 5: /* (  1 ,  0 , -1 ) */
              t->ci = ci->progeny[4];
              t->cj = cj->progeny[1];
              t->flags = 5;
              t->tight = 1;
357
358
359
360
361
362
363
364
365
366
367
368
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 5, 0,
                                    ci->progeny[6], cj->progeny[3], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 2, 0,
                                    ci->progeny[4], cj->progeny[3], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 8, 0,
                                    ci->progeny[6], cj->progeny[1], 1),
                  s);
369
370
371
372
373
374
375
376
377
378
379
380
381
382
              break;

            case 6: /* (  1 , -1 ,  1 ) */
              t->ci = ci->progeny[5];
              t->cj = cj->progeny[2];
              t->flags = 6;
              t->tight = 1;
              break;

            case 7: /* (  1 , -1 ,  0 ) */
              t->ci = ci->progeny[4];
              t->cj = cj->progeny[3];
              t->flags = 6;
              t->tight = 1;
383
384
385
386
387
388
389
390
391
392
393
394
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 8, 0,
                                    ci->progeny[5], cj->progeny[2], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 7, 0,
                                    ci->progeny[4], cj->progeny[2], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 7, 0,
                                    ci->progeny[5], cj->progeny[3], 1),
                  s);
395
396
397
398
399
400
401
402
403
404
405
406
407
408
              break;

            case 8: /* (  1 , -1 , -1 ) */
              t->ci = ci->progeny[4];
              t->cj = cj->progeny[3];
              t->flags = 8;
              t->tight = 1;
              break;

            case 9: /* (  0 ,  1 ,  1 ) */
              t->ci = ci->progeny[3];
              t->cj = cj->progeny[0];
              t->flags = 9;
              t->tight = 1;
409
410
411
412
413
414
415
416
417
418
419
420
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 9, 0,
                                    ci->progeny[7], cj->progeny[4], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 0, 0,
                                    ci->progeny[3], cj->progeny[4], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 8, 0,
                                    ci->progeny[7], cj->progeny[0], 1),
                  s);
421
422
423
424
425
426
427
              break;

            case 10: /* (  0 ,  1 ,  0 ) */
              t->ci = ci->progeny[2];
              t->cj = cj->progeny[0];
              t->flags = 10;
              t->tight = 1;
428
429
430
431
432
433
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 11, 0,
                                    ci->progeny[3], cj->progeny[0], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 7, 0,
                                    ci->progeny[6], cj->progeny[0], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 6, 0,
                                    ci->progeny[7], cj->progeny[0], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 9, 0,
                                    ci->progeny[2], cj->progeny[1], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 10, 0,
                                    ci->progeny[3], cj->progeny[1], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 8, 0,
                                    ci->progeny[6], cj->progeny[1], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 7, 0,
                                    ci->progeny[7], cj->progeny[1], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 1, 0,
                                    ci->progeny[2], cj->progeny[4], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 2, 0,
                                    ci->progeny[3], cj->progeny[4], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 10, 0,
                                    ci->progeny[6], cj->progeny[4], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 11, 0,
                                    ci->progeny[7], cj->progeny[4], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 0, 0,
                                    ci->progeny[2], cj->progeny[5], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 1, 0,
                                    ci->progeny[3], cj->progeny[5], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 9, 0,
                                    ci->progeny[6], cj->progeny[5], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 10, 0,
                                    ci->progeny[7], cj->progeny[5], 1),
                  s);
488
489
490
491
492
493
494
              break;

            case 11: /* (  0 ,  1 , -1 ) */
              t->ci = ci->progeny[2];
              t->cj = cj->progeny[1];
              t->flags = 11;
              t->tight = 1;
495
496
497
498
499
500
501
502
503
504
505
506
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 11, 0,
                                    ci->progeny[6], cj->progeny[5], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 2, 0,
                                    ci->progeny[2], cj->progeny[5], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 6, 0,
                                    ci->progeny[6], cj->progeny[1], 1),
                  s);
507
508
509
510
511
512
513
              break;

            case 12: /* (  0 ,  0 ,  1 ) */
              t->ci = ci->progeny[1];
              t->cj = cj->progeny[0];
              t->flags = 12;
              t->tight = 1;
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
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
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 11, 0,
                                    ci->progeny[3], cj->progeny[0], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 5, 0,
                                    ci->progeny[5], cj->progeny[0], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 2, 0,
                                    ci->progeny[7], cj->progeny[0], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 9, 0,
                                    ci->progeny[1], cj->progeny[2], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 12, 0,
                                    ci->progeny[3], cj->progeny[2], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 8, 0,
                                    ci->progeny[5], cj->progeny[2], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 5, 0,
                                    ci->progeny[7], cj->progeny[2], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 3, 0,
                                    ci->progeny[1], cj->progeny[4], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 6, 0,
                                    ci->progeny[3], cj->progeny[4], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 12, 0,
                                    ci->progeny[5], cj->progeny[4], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 11, 0,
                                    ci->progeny[7], cj->progeny[4], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 0, 0,
                                    ci->progeny[1], cj->progeny[6], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 3, 0,
                                    ci->progeny[3], cj->progeny[6], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 9, 0,
                                    ci->progeny[5], cj->progeny[6], 1),
                  s);
              scheduler_splittasks_mapper(
                  scheduler_addtask(s, task_type_pair, t->subtype, 12, 0,
                                    ci->progeny[7], cj->progeny[6], 1),
                  s);
574
              break;
575
          } /* switch(sid) */
576
577
        }

578
579
580
        /* Otherwise, break it up if it is too large? */
      } else if (scheduler_doforcesplit && ci->split && cj->split &&
                 (ci->count > space_maxsize / cj->count)) {
581
582
583
584
585
586
587

        // message( "force splitting pair with %i and %i parts." , ci->count ,
        // cj->count );

        /* Replace the current task. */
        t->type = task_type_none;

Pedro Gonnet's avatar
Pedro Gonnet committed
588
        for (int j = 0; j < 8; j++)
589
          if (ci->progeny[j] != NULL)
Pedro Gonnet's avatar
Pedro Gonnet committed
590
            for (int k = 0; k < 8; k++)
591
              if (cj->progeny[k] != NULL) {
592
593
594
595
                scheduler_splittasks_mapper(
                    scheduler_addtask(s, task_type_pair, t->subtype, 0, 0,
                                      ci->progeny[j], cj->progeny[k], 0),
                    s);
596
597
598
                t->flags = space_getsid(s->space, &t->ci, &t->cj, shift);
              }

599
600
        /* Otherwise, if not spilt, stitch-up the sorting. */
      } else {
601
602

        /* Create the sort for ci. */
603
        lock_lock( &ci->lock );
604
605
606
607
608
        if (ci->sorts == NULL)
          ci->sorts =
              scheduler_addtask(s, task_type_sort, 0, 1 << sid, 0, ci, NULL, 0);
        else
          ci->sorts->flags |= (1 << sid);
609
        lock_unlock_blind( &ci->lock );
610
611
612
        scheduler_addunlock(s, ci->sorts, t);

        /* Create the sort for cj. */
613
        lock_lock( &cj->lock );
614
615
616
617
618
        if (cj->sorts == NULL)
          cj->sorts =
              scheduler_addtask(s, task_type_sort, 0, 1 << sid, 0, cj, NULL, 0);
        else
          cj->sorts->flags |= (1 << sid);
619
        lock_unlock_blind( &cj->lock );
620
621
622
623
624
        scheduler_addunlock(s, cj->sorts, t);
      }

    } /* pair interaction? */

625
626
  } /* iterate over the current task. */
}
627

628
void scheduler_splittasks(struct scheduler *s) {
629

630
631
632
  /* Call the mapper on each current task. */
  threadpool_map(s->threadpool, scheduler_splittasks_mapper, s->tasks,
                 s->nr_tasks, sizeof(struct task), s);
633
634
}

635
636
637
638
639
640
641
/**
 * @brief Add a #task to the #scheduler.
 *
 * @param s The #scheduler we are working in.
 * @param type The type of the task.
 * @param subtype The sub-type of the task.
 * @param flags The flags of the task.
642
 * @param wait
643
644
645
646
647
 * @param ci The first cell to interact.
 * @param cj The second cell to interact.
 * @param tight
 */

648
649
650
651
652
struct task *scheduler_addtask(struct scheduler *s, int type, int subtype,
                               int flags, int wait, struct cell *ci,
                               struct cell *cj, int tight) {

  /* Get the next free task. */
Pedro Gonnet's avatar
Pedro Gonnet committed
653
  const int ind = atomic_inc(&s->tasks_next);
Matthieu Schaller's avatar
Matthieu Schaller committed
654

655
656
657
658
  /* Overflow? */
  if (ind >= s->size) error("Task list overflow.");

  /* Get a pointer to the new task. */
Pedro Gonnet's avatar
Pedro Gonnet committed
659
  struct task *t = &s->tasks[ind];
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675

  /* Copy the data. */
  t->type = type;
  t->subtype = subtype;
  t->flags = flags;
  t->wait = wait;
  t->ci = ci;
  t->cj = cj;
  t->skip = 0;
  t->tight = tight;
  t->implicit = 0;
  t->weight = 0;
  t->rank = 0;
  t->tic = 0;
  t->toc = 0;
  t->nr_unlock_tasks = 0;
676
677
  t->rid = -1;
  t->last_rid = -1;
678
679
680
681
682
683
684
685
686
687
688
689

  /* Init the lock. */
  lock_init(&t->lock);

  /* Add an index for it. */
  // lock_lock( &s->lock );
  s->tasks_ind[atomic_inc(&s->nr_tasks)] = ind;
  // lock_unlock_blind( &s->lock );

  /* Return a pointer to the new task. */
  return t;
}
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
734
735
736
737
738
/**
 * @brief Set the unlock pointers in each task.
 *
 * @param s The #scheduler.
 */

void scheduler_set_unlocks(struct scheduler *s) {

  /* Store the counts for each task. */
  int *counts;
  if ((counts = (int *)malloc(sizeof(int) * s->nr_tasks)) == NULL)
    error("Failed to allocate temporary counts array.");
  bzero(counts, sizeof(int) * s->nr_tasks);
  for (int k = 0; k < s->nr_unlocks; k++) counts[s->unlock_ind[k]] += 1;

  /* Compute the offset for each unlock block. */
  int *offsets;
  if ((offsets = (int *)malloc(sizeof(int) * (s->nr_tasks + 1))) == NULL)
    error("Failed to allocate temporary offsets array.");
  offsets[0] = 0;
  for (int k = 0; k < s->nr_tasks; k++) offsets[k + 1] = offsets[k] + counts[k];

  /* Create and fill a temporary array with the sorted unlocks. */
  struct task **unlocks;
  if ((unlocks = (struct task **)malloc(sizeof(struct task *) *
                                        s->size_unlocks)) == NULL)
    error("Failed to allocate temporary unlocks array.");
  for (int k = 0; k < s->nr_unlocks; k++) {
    const int ind = s->unlock_ind[k];
    unlocks[offsets[ind]] = s->unlocks[k];
    offsets[ind] += 1;
  }

  /* Swap the unlocks. */
  free(s->unlocks);
  s->unlocks = unlocks;

  /* Re-set the offsets. */
  offsets[0] = 0;
  for (int k = 1; k < s->nr_tasks; k++)
    offsets[k] = offsets[k - 1] + counts[k - 1];

  /* Set the unlocks in the tasks. */
  for (int k = 0; k < s->nr_tasks; k++) {
    struct task *t = &s->tasks[k];
    t->nr_unlock_tasks = counts[k];
    t->unlock_tasks = &s->unlocks[offsets[k]];
  }
739

740
741
742
743
744
745
746
747
748
749
  /* Verify that there are no duplicate unlocks. */
  /* for (int k = 0; k < s->nr_tasks; k++) {
    struct task *t = &s->tasks[k];
    for (int i = 0; i < t->nr_unlock_tasks; i++) {
      for (int j = i + 1; j < t->nr_unlock_tasks; j++) {
        if (t->unlock_tasks[i] == t->unlock_tasks[j])
          error("duplicate unlock!");
      }
    }
  } */
750
751
752
753
754
755

  /* Clean up. */
  free(counts);
  free(offsets);
}

756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
/**
 * @brief #threadpool_map function which runs through the task
 *        graph and re-computes the task wait counters.
 */

void scheduler_simple_rewait_mapper(void *map_data, void *extra_data) {

  struct task *t = (struct task *)map_data;

  /* Increment the waits of the dependances */
  for (int k = 0; k < t->nr_unlock_tasks; k++) {
    struct task *u = t->unlock_tasks[k];
    atomic_inc(&u->wait);
  }
}
771
/**
772
773
774
775
 * @brief Sort the tasks in topological order over all queues.
 *
 * @param s The #scheduler.
 */
776
777
778

void scheduler_ranktasks(struct scheduler *s) {

Pedro Gonnet's avatar
Pedro Gonnet committed
779
780
781
  struct task *tasks = s->tasks;
  int *tid = s->tasks_ind;
  const int nr_tasks = s->nr_tasks;
782

783
  /* Run through the tasks and get all the waits right. */
784
785
  threadpool_map(s->threadpool, scheduler_simple_rewait_mapper, tasks, nr_tasks,
                 sizeof(struct task), NULL);
786

787
788
789
790
791
792
793
794
  /* Load the tids of tasks with no waits. */
  int left = 0;
  for (int k = 0; k < nr_tasks; k++)
    if (tasks[k].wait == 0) {
      tid[left] = k;
      left += 1;
    }

795
  /* Main loop. */
796
  for (int j = 0, rank = 0; left < nr_tasks; rank++) {
797
798
799

    /* Did we get anything? */
    if (j == left) error("Unsatisfiable task dependencies detected.");
800
    const int left_old = left;
801
802

    /* Unlock the next layer of tasks. */
803
804
    for (; j < left_old; j++) {
      struct task *t = &tasks[tid[j]];
805
806
807
808
      t->rank = rank;
      /* message( "task %i of type %s has rank %i." , i ,
          (t->type == task_type_self) ? "self" : (t->type == task_type_pair) ?
         "pair" : "sort" , rank ); */
809
810
811
812
813
814
815
      for (int k = 0; k < t->nr_unlock_tasks; k++) {
        struct task *u = t->unlock_tasks[k];
        if (--u->wait == 0) {
          tid[left] = u - tasks;
          left += 1;
        }
      }
816
817
    }

818
819
    /* Move back to the old left (like Sanders). */
    j = left_old;
820
821
822
  }

  /* Verify that the tasks were ranked correctly. */
823
  /* for (int k = 1; k < s->nr_tasks; k++)
824
    if (tasks[tid[k - 1]].rank > tasks[tid[k - 1]].rank)
825
      error("Task ranking failed."); */
826
}
827
828
829
830
831
832
833
834

/**
 * @brief (Re)allocate the task arrays.
 *
 * @param s The #scheduler.
 * @param size The maximum number of tasks in the #scheduler.
 */

835
void scheduler_reset(struct scheduler *s, int size) {
836

837
838
  /* Do we need to re-allocate? */
  if (size > s->size) {
839

840
    /* Free existing task lists if necessary. */
841
842
    if (s->tasks != NULL) free(s->tasks);
    if (s->tasks_ind != NULL) free(s->tasks_ind);
843

844
845
846
847
848
    /* Allocate the new lists. */
    if ((s->tasks = (struct task *)malloc(sizeof(struct task) *size)) == NULL ||
        (s->tasks_ind = (int *)malloc(sizeof(int) * size)) == NULL)
      error("Failed to allocate task lists.");
  }
849

850
851
852
853
854
  /* Reset the counters. */
  s->size = size;
  s->nr_tasks = 0;
  s->tasks_next = 0;
  s->waiting = 0;
855
  s->mask = 0;
856
  s->submask = 0;
857
  s->nr_unlocks = 0;
858
  s->completed_unlock_writes = 0;
859
860

  /* Set the task pointers in the queues. */
Pedro Gonnet's avatar
Pedro Gonnet committed
861
  for (int k = 0; k < s->nr_queues; k++) s->queues[k].tasks = s->tasks;
862
}
863
864

/**
865
 * @brief Compute the task weights
866
867
868
 *
 * @param s The #scheduler.
 */
869

870
871
void scheduler_reweight(struct scheduler *s) {

Pedro Gonnet's avatar
Pedro Gonnet committed
872
873
874
875
  const int nr_tasks = s->nr_tasks;
  int *tid = s->tasks_ind;
  struct task *tasks = s->tasks;
  const int nodeID = s->nodeID;
Pedro Gonnet's avatar
Pedro Gonnet committed
876
877
878
  const float sid_scale[13] = {0.1897, 0.4025, 0.1897, 0.4025, 0.5788,
                               0.4025, 0.1897, 0.4025, 0.1897, 0.4025,
                               0.5788, 0.4025, 0.5788};
Pedro Gonnet's avatar
Pedro Gonnet committed
879
  const float wscale = 0.001;
880
881
  // ticks tic;

882
  /* Run through the tasks backwards and set their waits and
883
884
     weights. */
  // tic = getticks();
Pedro Gonnet's avatar
Pedro Gonnet committed
885
886
  for (int k = nr_tasks - 1; k >= 0; k--) {
    struct task *t = &tasks[tid[k]];
887
    t->weight = 0;
Pedro Gonnet's avatar
Pedro Gonnet committed
888
    for (int j = 0; j < t->nr_unlock_tasks; j++)
889
890
891
892
893
894
895
      if (t->unlock_tasks[j]->weight > t->weight)
        t->weight = t->unlock_tasks[j]->weight;
    if (!t->implicit && t->tic > 0)
      t->weight += wscale * (t->toc - t->tic);
    else
      switch (t->type) {
        case task_type_sort:
896
897
          t->weight += wscale * intrinsics_popcount(t->flags) * t->ci->count *
                       (sizeof(int) * 8 - intrinsics_clz(t->ci->count));
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
          break;
        case task_type_self:
          t->weight += 1 * t->ci->count * t->ci->count;
          break;
        case task_type_pair:
          if (t->ci->nodeID != nodeID || t->cj->nodeID != nodeID)
            t->weight +=
                3 * wscale * t->ci->count * t->cj->count * sid_scale[t->flags];
          else
            t->weight +=
                2 * wscale * t->ci->count * t->cj->count * sid_scale[t->flags];
          break;
        case task_type_sub:
          if (t->cj != NULL) {
            if (t->ci->nodeID != nodeID || t->cj->nodeID != nodeID) {
              if (t->flags < 0)
                t->weight += 3 * wscale * t->ci->count * t->cj->count;
              else
                t->weight += 3 * wscale * t->ci->count * t->cj->count *
                             sid_scale[t->flags];
            } else {
              if (t->flags < 0)
                t->weight += 2 * wscale * t->ci->count * t->cj->count;
              else
                t->weight += 2 * wscale * t->ci->count * t->cj->count *
                             sid_scale[t->flags];
            }
          } else
            t->weight += 1 * wscale * t->ci->count * t->ci->count;
          break;
        case task_type_ghost:
          if (t->ci == t->ci->super) t->weight += wscale * t->ci->count;
          break;
Matthieu Schaller's avatar
Matthieu Schaller committed
931
932
933
934
935
936
937
        case task_type_kick:
          t->weight += wscale * t->ci->count;
          break;
        case task_type_drift:
          t->weight += wscale * t->ci->count;
          break;
        case task_type_init:
938
939
940
941
942
943
944
945
          t->weight += wscale * t->ci->count;
          break;
        default:
          break;
      }
    if (t->type == task_type_send) t->weight = INT_MAX / 8;
    if (t->type == task_type_recv) t->weight *= 1.41;
  }
946
  // message( "weighting tasks took %.3f %s." ,
Matthieu Schaller's avatar
Matthieu Schaller committed
947
  // clocks_from_ticks( getticks() - tic ), clocks_getunit());
948
949
950
951
952
953
954
955
956

  /* int min = tasks[0].weight, max = tasks[0].weight;
  for ( k = 1 ; k < nr_tasks ; k++ )
      if ( tasks[k].weight < min )
          min = tasks[k].weight;
      else if ( tasks[k].weight > max )
          max = tasks[k].weight;
  message( "task weights are in [ %i , %i ]." , min , max ); */
}
957

Pedro Gonnet's avatar
Pedro Gonnet committed
958
959
960
961
/**
 * @brief #threadpool_map function which runs through the task
 *        graph and re-computes the task wait counters.
 */
962

Pedro Gonnet's avatar
Pedro Gonnet committed
963
964
965
966
967
968
969
970
void scheduler_rewait_mapper(void *map_data, void *extra_data) {

  struct scheduler *s = (struct scheduler *)extra_data;
  struct task *t = (struct task *)map_data;

  if (t->skip) return;

  /* Skip tasks not in the mask */
971
  if (!((1 << t->type) & s->mask) || !((1 << t->subtype) & s->submask)) return;
Pedro Gonnet's avatar
Pedro Gonnet committed
972
973
974
975
976
977
978
979
980
981
982

  /* Skip sort tasks that have already been performed */
  if (t->type == task_type_sort && t->flags == 0) return;

  /* Sets the waits of the dependances */
  for (int k = 0; k < t->nr_unlock_tasks; k++) {
    struct task *u = t->unlock_tasks[k];
    atomic_inc(&u->wait);
  }
}

983
984
985
986
987
/**
 * @brief Start the scheduler, i.e. fill the queues with ready tasks.
 *
 * @param s The #scheduler.
 * @param mask The task types to enqueue.
988
 * @param submask The sub-task types to enqueue.
989
 */
990

991
992
void scheduler_start(struct scheduler *s, unsigned int mask,
                     unsigned int submask) {
993

Pedro Gonnet's avatar
Pedro Gonnet committed
994
995
996
  const int nr_tasks = s->nr_tasks;
  int *tid = s->tasks_ind;
  struct task *tasks = s->tasks;
997
  // ticks tic;
998

999
  /* Store the masks */
Pedro Gonnet's avatar
Pedro Gonnet committed
1000
  s->mask = mask | (1 << task_type_comm_root);
For faster browsing, not all history is shown. View entire blame