engine_marktasks.c 31.9 KB
Newer Older
Pedro Gonnet's avatar
Pedro Gonnet committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*******************************************************************************
 * This file is part of SWIFT.
 * Copyright (c) 2012 Pedro Gonnet (pedro.gonnet@durham.ac.uk)
 *                    Matthieu Schaller (matthieu.schaller@durham.ac.uk)
 *               2015 Peter W. Draper (p.w.draper@durham.ac.uk)
 *                    Angus Lepper (angus.lepper@ed.ac.uk)
 *               2016 John A. Regan (john.a.regan@durham.ac.uk)
 *                    Tom Theuns (tom.theuns@durham.ac.uk)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 ******************************************************************************/

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

/* Some standard headers. */
#include <stdlib.h>
#include <unistd.h>

/* MPI headers. */
#ifdef WITH_MPI
#include <mpi.h>
#endif

/* Load the profiler header, if needed. */
#ifdef WITH_PROFILER
#include <gperftools/profiler.h>
#endif

/* This object's header. */
#include "engine.h"

/* Local headers. */
#include "active.h"
#include "atomic.h"
#include "cell.h"
#include "clocks.h"
#include "cycle.h"
#include "debug.h"
#include "error.h"
#include "proxy.h"
#include "timers.h"

/**
 * @brief Mark tasks to be un-skipped and set the sort flags accordingly.
 *        Threadpool mapper function.
 *
 * @param map_data pointer to the tasks
 * @param num_elements number of tasks
 * @param extra_data pointer to int that will define if a rebuild is needed.
 */
void engine_marktasks_mapper(void *map_data, int num_elements,
                             void *extra_data) {
  /* Unpack the arguments. */
  struct task *tasks = (struct task *)map_data;
  size_t *rebuild_space = &((size_t *)extra_data)[1];
  struct scheduler *s = (struct scheduler *)(((size_t *)extra_data)[2]);
  struct engine *e = (struct engine *)((size_t *)extra_data)[0];
  const int nodeID = e->nodeID;
72
  const int with_limiter = e->policy & engine_policy_limiter;
Pedro Gonnet's avatar
Pedro Gonnet committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86

  for (int ind = 0; ind < num_elements; ind++) {

    /* Get basic task information */
    struct task *t = &tasks[ind];
    const enum task_types t_type = t->type;
    const enum task_subtypes t_subtype = t->subtype;

    /* Single-cell task? */
    if (t_type == task_type_self || t_type == task_type_sub_self) {

      /* Local pointer. */
      struct cell *ci = t->ci;

87
      if (ci->nodeID != nodeID) error("Non-local self task found");
Pedro Gonnet's avatar
Pedro Gonnet committed
88
89
90
91
92
93

      /* Activate the hydro drift */
      if (t_type == task_type_self && t_subtype == task_subtype_density) {
        if (cell_is_active_hydro(ci, e)) {
          scheduler_activate(s, t);
          cell_activate_drift_part(ci, s);
94
          if (with_limiter) cell_activate_limiter(ci, s);
Pedro Gonnet's avatar
Pedro Gonnet committed
95
96
97
98
99
100
101
102
103
        }
      }

      /* Store current values of dx_max and h_max. */
      else if (t_type == task_type_sub_self &&
               t_subtype == task_subtype_density) {
        if (cell_is_active_hydro(ci, e)) {
          scheduler_activate(s, t);
          cell_activate_subcell_hydro_tasks(ci, NULL, s);
104
          if (with_limiter) cell_activate_limiter(ci, s);
Pedro Gonnet's avatar
Pedro Gonnet committed
105
106
107
108
109
110
111
112
113
114
115
116
        }
      }

      else if (t_type == task_type_self && t_subtype == task_subtype_force) {
        if (cell_is_active_hydro(ci, e)) scheduler_activate(s, t);
      }

      else if (t_type == task_type_sub_self &&
               t_subtype == task_subtype_force) {
        if (cell_is_active_hydro(ci, e)) scheduler_activate(s, t);
      }

117
118
119
120
121
122
123
124
125
126
      else if (t->type == task_type_self &&
               t->subtype == task_subtype_limiter) {
        if (cell_is_active_hydro(ci, e)) scheduler_activate(s, t);
      }

      else if (t->type == task_type_sub_self &&
               t->subtype == task_subtype_limiter) {
        if (cell_is_active_hydro(ci, e)) scheduler_activate(s, t);
      }

Pedro Gonnet's avatar
Pedro Gonnet committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
      else if (t_type == task_type_self && t_subtype == task_subtype_gradient) {
        if (cell_is_active_hydro(ci, e)) scheduler_activate(s, t);
      }

      else if (t_type == task_type_sub_self &&
               t_subtype == task_subtype_gradient) {
        if (cell_is_active_hydro(ci, e)) scheduler_activate(s, t);
      }

      /* Activate the star density */
      else if (t_type == task_type_self &&
               t_subtype == task_subtype_stars_density) {
        if (cell_is_active_stars(ci, e)) {
          scheduler_activate(s, t);
          cell_activate_drift_part(ci, s);
Loic Hausammann's avatar
Loic Hausammann committed
142
          cell_activate_drift_spart(ci, s);
Pedro Gonnet's avatar
Pedro Gonnet committed
143
144
145
146
147
148
149
150
151
152
153
154
        }
      }

      /* Store current values of dx_max and h_max. */
      else if (t_type == task_type_sub_self &&
               t_subtype == task_subtype_stars_density) {
        if (cell_is_active_stars(ci, e)) {
          scheduler_activate(s, t);
          cell_activate_subcell_stars_tasks(ci, NULL, s);
        }
      }

Alexei Borissov's avatar
Alexei Borissov committed
155
156
157
158
159
160
161
162
163
      else if (t_type == task_type_self &&
               t_subtype == task_subtype_stars_feedback) {
        if (cell_is_active_stars(ci, e)) {
          scheduler_activate(s, t);
        }
      }

      else if (t_type == task_type_sub_self &&
               t_subtype == task_subtype_stars_feedback) {
164
        if (cell_is_active_stars(ci, e)) scheduler_activate(s, t);
Alexei Borissov's avatar
Alexei Borissov committed
165
166
      }

167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
      /* Activate the black hole density */
      else if (t_type == task_type_self &&
               t_subtype == task_subtype_bh_density) {
        if (cell_is_active_black_holes(ci, e)) {
          scheduler_activate(s, t);
          cell_activate_drift_part(ci, s);
          cell_activate_drift_bpart(ci, s);
        }
      }

      /* Store current values of dx_max and h_max. */
      else if (t_type == task_type_sub_self &&
               t_subtype == task_subtype_bh_density) {
        if (cell_is_active_black_holes(ci, e)) {
          scheduler_activate(s, t);
182
          cell_activate_subcell_black_holes_tasks(ci, NULL, s);
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
        }
      }

      else if (t_type == task_type_self &&
               t_subtype == task_subtype_bh_feedback) {
        if (cell_is_active_black_holes(ci, e)) {
          scheduler_activate(s, t);
        }
      }

      else if (t_type == task_type_sub_self &&
               t_subtype == task_subtype_bh_feedback) {
        if (cell_is_active_black_holes(ci, e)) scheduler_activate(s, t);
      }

Pedro Gonnet's avatar
Pedro Gonnet committed
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
      /* Activate the gravity drift */
      else if (t_type == task_type_self && t_subtype == task_subtype_grav) {
        if (cell_is_active_gravity(ci, e)) {
          scheduler_activate(s, t);
          cell_activate_subcell_grav_tasks(t->ci, NULL, s);
        }
      }

      /* Activate the gravity drift */
      else if (t_type == task_type_self &&
               t_subtype == task_subtype_external_grav) {
        if (cell_is_active_gravity(ci, e)) {
          scheduler_activate(s, t);
          cell_activate_drift_gpart(t->ci, s);
        }
      }

#ifdef SWIFT_DEBUG_CHECKS
      else {
        error("Invalid task type / sub-type encountered");
      }
#endif
    }

    /* Pair? */
    else if (t_type == task_type_pair || t_type == task_type_sub_pair) {

      /* Local pointers. */
      struct cell *ci = t->ci;
      struct cell *cj = t->cj;
#ifdef WITH_MPI
      const int ci_nodeID = ci->nodeID;
      const int cj_nodeID = cj->nodeID;
#else
      const int ci_nodeID = nodeID;
      const int cj_nodeID = nodeID;
#endif
      const int ci_active_hydro = cell_is_active_hydro(ci, e);
      const int cj_active_hydro = cell_is_active_hydro(cj, e);
237

Pedro Gonnet's avatar
Pedro Gonnet committed
238
239
      const int ci_active_gravity = cell_is_active_gravity(ci, e);
      const int cj_active_gravity = cell_is_active_gravity(cj, e);
240

241
242
      const int ci_active_stars = cell_is_active_stars(ci, e);
      const int cj_active_stars = cell_is_active_stars(cj, e);
Pedro Gonnet's avatar
Pedro Gonnet committed
243

244
245
246
      const int ci_active_black_holes = cell_is_active_black_holes(ci, e);
      const int cj_active_black_holes = cell_is_active_black_holes(cj, e);

Pedro Gonnet's avatar
Pedro Gonnet committed
247
248
249
      /* Only activate tasks that involve a local active cell. */
      if ((t_subtype == task_subtype_density ||
           t_subtype == task_subtype_gradient ||
250
           t_subtype == task_subtype_limiter ||
Pedro Gonnet's avatar
Pedro Gonnet committed
251
252
253
254
255
256
257
           t_subtype == task_subtype_force) &&
          ((ci_active_hydro && ci_nodeID == nodeID) ||
           (cj_active_hydro && cj_nodeID == nodeID))) {

        scheduler_activate(s, t);

        /* Set the correct sorting flags */
Loic Hausammann's avatar
Loic Hausammann committed
258
        if (t_type == task_type_pair && t_subtype == task_subtype_density) {
Pedro Gonnet's avatar
Pedro Gonnet committed
259
260
261
262
263
264
265
266
267
268
269

          /* Store some values. */
          atomic_or(&ci->hydro.requires_sorts, 1 << t->flags);
          atomic_or(&cj->hydro.requires_sorts, 1 << t->flags);
          ci->hydro.dx_max_sort_old = ci->hydro.dx_max_sort;
          cj->hydro.dx_max_sort_old = cj->hydro.dx_max_sort;

          /* Activate the hydro drift tasks. */
          if (ci_nodeID == nodeID) cell_activate_drift_part(ci, s);
          if (cj_nodeID == nodeID) cell_activate_drift_part(cj, s);

270
          /* And the limiter */
271
272
          if (ci_nodeID == nodeID && with_limiter) cell_activate_limiter(ci, s);
          if (cj_nodeID == nodeID && with_limiter) cell_activate_limiter(cj, s);
273

Pedro Gonnet's avatar
Pedro Gonnet committed
274
          /* Check the sorts and activate them if needed. */
275
276
          cell_activate_hydro_sorts(ci, t->flags, s);
          cell_activate_hydro_sorts(cj, t->flags, s);
Pedro Gonnet's avatar
Pedro Gonnet committed
277
278
279
280
281

        }

        /* Store current values of dx_max and h_max. */
        else if (t_type == task_type_sub_pair &&
Loic Hausammann's avatar
Loic Hausammann committed
282
                 t_subtype == task_subtype_density) {
Pedro Gonnet's avatar
Pedro Gonnet committed
283
284
285
286
          cell_activate_subcell_hydro_tasks(t->ci, t->cj, s);
        }
      }

287
288
      /* Stars density */
      else if ((t_subtype == task_subtype_stars_density) &&
289
290
291
               (ci_active_stars || cj_active_stars) &&
               (ci_nodeID == nodeID || cj_nodeID == nodeID)) {

loikki's avatar
loikki committed
292
        scheduler_activate(s, t);
293

Pedro Gonnet's avatar
Pedro Gonnet committed
294
295
296
        /* Set the correct sorting flags */
        if (t_type == task_type_pair) {

Loic Hausammann's avatar
Loic Hausammann committed
297
          /* Do ci */
loikki's avatar
loikki committed
298
          if (ci_active_stars) {
299

300
            /* stars for ci */
301
            atomic_or(&ci->stars.requires_sorts, 1 << t->flags);
302
            ci->stars.dx_max_sort_old = ci->stars.dx_max_sort;
Loic Hausammann's avatar
Loic Hausammann committed
303

304
            /* hydro for cj */
305
            atomic_or(&cj->hydro.requires_sorts, 1 << t->flags);
306
            cj->hydro.dx_max_sort_old = cj->hydro.dx_max_sort;
Pedro Gonnet's avatar
Pedro Gonnet committed
307

308
            /* Activate the drift tasks. */
309
310
            if (ci_nodeID == nodeID) cell_activate_drift_spart(ci, s);
            if (cj_nodeID == nodeID) cell_activate_drift_part(cj, s);
Pedro Gonnet's avatar
Pedro Gonnet committed
311

312
313
314
315
            /* Check the sorts and activate them if needed. */
            cell_activate_hydro_sorts(cj, t->flags, s);
            cell_activate_stars_sorts(ci, t->flags, s);
          }
Loic Hausammann's avatar
Loic Hausammann committed
316
317

          /* Do cj */
loikki's avatar
loikki committed
318
          if (cj_active_stars) {
Loic Hausammann's avatar
Loic Hausammann committed
319

320
            /* hydro for ci */
321
            atomic_or(&ci->hydro.requires_sorts, 1 << t->flags);
322
            ci->hydro.dx_max_sort_old = ci->hydro.dx_max_sort;
323

324
            /* stars for cj */
325
            atomic_or(&cj->stars.requires_sorts, 1 << t->flags);
326
            cj->stars.dx_max_sort_old = cj->stars.dx_max_sort;
Loic Hausammann's avatar
Loic Hausammann committed
327

328
            /* Activate the drift tasks. */
329
330
            if (ci_nodeID == nodeID) cell_activate_drift_part(ci, s);
            if (cj_nodeID == nodeID) cell_activate_drift_spart(cj, s);
Loic Hausammann's avatar
Loic Hausammann committed
331

332
333
334
335
            /* Check the sorts and activate them if needed. */
            cell_activate_hydro_sorts(ci, t->flags, s);
            cell_activate_stars_sorts(cj, t->flags, s);
          }
Pedro Gonnet's avatar
Pedro Gonnet committed
336
337
338
        }

        /* Store current values of dx_max and h_max. */
339
        else if (t_type == task_type_sub_pair &&
340
                 t_subtype == task_subtype_stars_density) {
341
          cell_activate_subcell_stars_tasks(ci, cj, s);
Pedro Gonnet's avatar
Pedro Gonnet committed
342
343
344
        }
      }

345
      /* Stars feedback */
346
      else if (t_subtype == task_subtype_stars_feedback) {
347

348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
        /* We only want to activate the task if the cell is active and is
           going to update some gas on the *local* node */
        if ((ci_nodeID == nodeID && cj_nodeID == nodeID) &&
            (ci_active_stars || cj_active_stars)) {

          scheduler_activate(s, t);

        } else if ((ci_nodeID == nodeID && cj_nodeID != nodeID) &&
                   (cj_active_stars)) {

          scheduler_activate(s, t);

        } else if ((ci_nodeID != nodeID && cj_nodeID == nodeID) &&
                   (ci_active_stars)) {

          scheduler_activate(s, t);
        }
365
      }
366

367
368
369
370
371
372
373
374
375
376
377
378
379
380
      /* Black_Holes density */
      else if ((t_subtype == task_subtype_bh_density) &&
               (ci_active_black_holes || cj_active_black_holes) &&
               (ci_nodeID == nodeID || cj_nodeID == nodeID)) {

        scheduler_activate(s, t);

        /* Set the correct sorting flags */
        if (t_type == task_type_pair) {

          /* Do ci */
          if (ci_active_black_holes) {

            /* Activate the drift tasks. */
381
            if (ci_nodeID == nodeID) cell_activate_drift_bpart(ci, s);
382
383
384
385
386
387
388
389
            if (cj_nodeID == nodeID) cell_activate_drift_part(cj, s);
          }

          /* Do cj */
          if (cj_active_black_holes) {

            /* Activate the drift tasks. */
            if (ci_nodeID == nodeID) cell_activate_drift_part(ci, s);
390
            if (cj_nodeID == nodeID) cell_activate_drift_bpart(cj, s);
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
          }
        }

        /* Store current values of dx_max and h_max. */
        else if (t_type == task_type_sub_pair &&
                 t_subtype == task_subtype_bh_density) {
          cell_activate_subcell_black_holes_tasks(ci, cj, s);
        }
      }

      /* Black_Holes feedback */
      else if (t_subtype == task_subtype_bh_feedback) {

        /* We only want to activate the task if the cell is active and is
           going to update some gas on the *local* node */
        if ((ci_nodeID == nodeID && cj_nodeID == nodeID) &&
            (ci_active_black_holes || cj_active_black_holes)) {

          scheduler_activate(s, t);

        } else if ((ci_nodeID == nodeID && cj_nodeID != nodeID) &&
                   (cj_active_black_holes)) {

          scheduler_activate(s, t);

        } else if ((ci_nodeID != nodeID && cj_nodeID == nodeID) &&
                   (ci_active_black_holes)) {

          scheduler_activate(s, t);
        }
      }

423
      /* Gravity */
424
      else if ((t_subtype == task_subtype_grav) &&
425
426
               ((ci_active_gravity && ci_nodeID == nodeID) ||
                (cj_active_gravity && cj_nodeID == nodeID))) {
Pedro Gonnet's avatar
Pedro Gonnet committed
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446

        scheduler_activate(s, t);

        if (t_type == task_type_pair && t_subtype == task_subtype_grav) {
          /* Activate the gravity drift */
          cell_activate_subcell_grav_tasks(t->ci, t->cj, s);
        }

#ifdef SWIFT_DEBUG_CHECKS
        else if (t_type == task_type_sub_pair &&
                 t_subtype == task_subtype_grav) {
          error("Invalid task sub-type encountered");
        }
#endif
      }

      /* Only interested in density tasks as of here. */
      if (t_subtype == task_subtype_density) {

        /* Too much particle movement? */
Alexei Borissov's avatar
Alexei Borissov committed
447
        if (cell_need_rebuild_for_hydro_pair(ci, cj)) *rebuild_space = 1;
Pedro Gonnet's avatar
Pedro Gonnet committed
448
449
450
451
452
453
454

#ifdef WITH_MPI
        /* Activate the send/recv tasks. */
        if (ci_nodeID != nodeID) {

          /* If the local cell is active, receive data from the foreign cell. */
          if (cj_active_hydro) {
455
            scheduler_activate_recv(s, ci->mpi.recv, task_subtype_xv);
Pedro Gonnet's avatar
Pedro Gonnet committed
456
            if (ci_active_hydro) {
457
              scheduler_activate_recv(s, ci->mpi.recv, task_subtype_rho);
Pedro Gonnet's avatar
Pedro Gonnet committed
458
#ifdef EXTRA_HYDRO_LOOP
459
              scheduler_activate_recv(s, ci->mpi.recv, task_subtype_gradient);
Pedro Gonnet's avatar
Pedro Gonnet committed
460
461
462
463
464
#endif
            }
          }

          /* If the foreign cell is active, we want its ti_end values. */
465
466
          if (ci_active_hydro)
            scheduler_activate_recv(s, ci->mpi.recv, task_subtype_tend_part);
Pedro Gonnet's avatar
Pedro Gonnet committed
467
468
469

          /* Is the foreign cell active and will need stuff from us? */
          if (ci_active_hydro) {
470
            struct link *l = scheduler_activate_send(
471
                s, cj->mpi.send, task_subtype_xv, ci_nodeID);
Pedro Gonnet's avatar
Pedro Gonnet committed
472
473
474
475
476
477
478
479

            /* Drift the cell which will be sent at the level at which it is
               sent, i.e. drift the cell specified in the send task (l->t)
               itself. */
            cell_activate_drift_part(l->t->ci, s);

            /* If the local cell is also active, more stuff will be needed. */
            if (cj_active_hydro) {
480
481
              scheduler_activate_send(s, cj->mpi.send, task_subtype_rho,
                                      ci_nodeID);
Pedro Gonnet's avatar
Pedro Gonnet committed
482
483

#ifdef EXTRA_HYDRO_LOOP
484
485
              scheduler_activate_send(s, cj->mpi.send, task_subtype_gradient,
                                      ci_nodeID);
Pedro Gonnet's avatar
Pedro Gonnet committed
486
487
488
489
490
491
#endif
            }
          }

          /* If the local cell is active, send its ti_end values. */
          if (cj_active_hydro)
492
493
            scheduler_activate_send(s, cj->mpi.send, task_subtype_tend_part,
                                    ci_nodeID);
Pedro Gonnet's avatar
Pedro Gonnet committed
494
495
496
497
498

        } else if (cj_nodeID != nodeID) {

          /* If the local cell is active, receive data from the foreign cell. */
          if (ci_active_hydro) {
499

500
            scheduler_activate_recv(s, cj->mpi.recv, task_subtype_xv);
Pedro Gonnet's avatar
Pedro Gonnet committed
501
            if (cj_active_hydro) {
502
              scheduler_activate_recv(s, cj->mpi.recv, task_subtype_rho);
Pedro Gonnet's avatar
Pedro Gonnet committed
503
#ifdef EXTRA_HYDRO_LOOP
504
              scheduler_activate_recv(s, cj->mpi.recv, task_subtype_gradient);
Pedro Gonnet's avatar
Pedro Gonnet committed
505
506
507
508
509
#endif
            }
          }

          /* If the foreign cell is active, we want its ti_end values. */
510
511
          if (cj_active_hydro)
            scheduler_activate_recv(s, cj->mpi.recv, task_subtype_tend_part);
Pedro Gonnet's avatar
Pedro Gonnet committed
512
513
514
515

          /* Is the foreign cell active and will need stuff from us? */
          if (cj_active_hydro) {

516
            struct link *l = scheduler_activate_send(
517
                s, ci->mpi.send, task_subtype_xv, cj_nodeID);
Pedro Gonnet's avatar
Pedro Gonnet committed
518
519
520
521
522
523
524
525
526

            /* Drift the cell which will be sent at the level at which it is
               sent, i.e. drift the cell specified in the send task (l->t)
               itself. */
            cell_activate_drift_part(l->t->ci, s);

            /* If the local cell is also active, more stuff will be needed. */
            if (ci_active_hydro) {

527
528
              scheduler_activate_send(s, ci->mpi.send, task_subtype_rho,
                                      cj_nodeID);
Pedro Gonnet's avatar
Pedro Gonnet committed
529
530

#ifdef EXTRA_HYDRO_LOOP
531
532
              scheduler_activate_send(s, ci->mpi.send, task_subtype_gradient,
                                      cj_nodeID);
Pedro Gonnet's avatar
Pedro Gonnet committed
533
534
535
536
537
538
#endif
            }
          }

          /* If the local cell is active, send its ti_end values. */
          if (ci_active_hydro)
539
540
            scheduler_activate_send(s, ci->mpi.send, task_subtype_tend_part,
                                    cj_nodeID);
Pedro Gonnet's avatar
Pedro Gonnet committed
541
542
543
544
545
        }
#endif
      }

      /* Only interested in stars_density tasks as of here. */
546
      else if (t->subtype == task_subtype_stars_density) {
547
548
549
550
551

        /* Too much particle movement? */
        if (cell_need_rebuild_for_stars_pair(ci, cj)) *rebuild_space = 1;
        if (cell_need_rebuild_for_stars_pair(cj, ci)) *rebuild_space = 1;

552
#ifdef WITH_MPI
553
554
555
556
        /* Activate the send/recv tasks. */
        if (ci_nodeID != nodeID) {

          if (cj_active_stars) {
557
            scheduler_activate_recv(s, ci->mpi.recv, task_subtype_xv);
558
            scheduler_activate_recv(s, ci->mpi.recv, task_subtype_rho);
559
560

            /* If the local cell is active, more stuff will be needed. */
561
            scheduler_activate_send(s, cj->mpi.send, task_subtype_spart,
562
                                    ci_nodeID);
563
564
565
            cell_activate_drift_spart(cj, s);

            /* If the local cell is active, send its ti_end values. */
566
567
            scheduler_activate_send(s, cj->mpi.send, task_subtype_tend_spart,
                                    ci_nodeID);
568
569
570
          }

          if (ci_active_stars) {
571
            scheduler_activate_recv(s, ci->mpi.recv, task_subtype_spart);
572
573

            /* If the foreign cell is active, we want its ti_end values. */
574
            scheduler_activate_recv(s, ci->mpi.recv, task_subtype_tend_spart);
575
576

            /* Is the foreign cell active and will need stuff from us? */
577
            scheduler_activate_send(s, cj->mpi.send, task_subtype_xv,
578
                                    ci_nodeID);
579
            scheduler_activate_send(s, cj->mpi.send, task_subtype_rho,
580
                                    ci_nodeID);
581
582
583
584
585
586
587
588
589
590

            /* Drift the cell which will be sent; note that not all sent
               particles will be drifted, only those that are needed. */
            cell_activate_drift_part(cj, s);
          }

        } else if (cj_nodeID != nodeID) {

          /* If the local cell is active, receive data from the foreign cell. */
          if (ci_active_stars) {
591
            scheduler_activate_recv(s, cj->mpi.recv, task_subtype_xv);
592
            scheduler_activate_recv(s, cj->mpi.recv, task_subtype_rho);
593
594

            /* If the local cell is active, more stuff will be needed. */
595
            scheduler_activate_send(s, ci->mpi.send, task_subtype_spart,
596
                                    cj_nodeID);
597
598
599
            cell_activate_drift_spart(ci, s);

            /* If the local cell is active, send its ti_end values. */
600
601
            scheduler_activate_send(s, ci->mpi.send, task_subtype_tend_spart,
                                    cj_nodeID);
602
603
604
          }

          if (cj_active_stars) {
605
            scheduler_activate_recv(s, cj->mpi.recv, task_subtype_spart);
606
607

            /* If the foreign cell is active, we want its ti_end values. */
608
            scheduler_activate_recv(s, cj->mpi.recv, task_subtype_tend_spart);
609
610

            /* Is the foreign cell active and will need stuff from us? */
611
            scheduler_activate_send(s, ci->mpi.send, task_subtype_xv,
612
                                    cj_nodeID);
613
            scheduler_activate_send(s, ci->mpi.send, task_subtype_rho,
614
                                    cj_nodeID);
615
616
617
618
619
620

            /* Drift the cell which will be sent; note that not all sent
               particles will be drifted, only those that are needed. */
            cell_activate_drift_part(ci, s);
          }
        }
621
#endif
Pedro Gonnet's avatar
Pedro Gonnet committed
622
      }
623

624
      /* Only interested in black hole density tasks as of here. */
625
626
627
628
629
630
631
632
633
634
635
      else if (t->subtype == task_subtype_bh_density) {

        /* Too much particle movement? */
        if (cell_need_rebuild_for_black_holes_pair(ci, cj)) *rebuild_space = 1;
        if (cell_need_rebuild_for_black_holes_pair(cj, ci)) *rebuild_space = 1;

#ifdef WITH_MPI
        /* Activate the send/recv tasks. */
        if (ci_nodeID != nodeID) {

          if (cj_active_black_holes) {
636
637
            scheduler_activate_recv(s, ci->mpi.recv, task_subtype_xv);
            scheduler_activate_recv(s, ci->mpi.recv, task_subtype_rho);
638
639

            /* If the local cell is active, more stuff will be needed. */
640
641
            scheduler_activate_send(s, cj->mpi.send, task_subtype_bpart,
                                    ci_nodeID);
642
            cell_activate_drift_bpart(cj, s);
643
644

            /* If the local cell is active, send its ti_end values. */
645
646
            scheduler_activate_send(s, cj->mpi.send, task_subtype_tend_bpart,
                                    ci_nodeID);
647
648
649
          }

          if (ci_active_black_holes) {
650
            scheduler_activate_recv(s, ci->mpi.recv, task_subtype_bpart);
651
652

            /* If the foreign cell is active, we want its ti_end values. */
653
            scheduler_activate_recv(s, ci->mpi.recv, task_subtype_tend_bpart);
654
655

            /* Is the foreign cell active and will need stuff from us? */
656
657
658
659
            scheduler_activate_send(s, cj->mpi.send, task_subtype_xv,
                                    ci_nodeID);
            scheduler_activate_send(s, cj->mpi.send, task_subtype_rho,
                                    ci_nodeID);
660
661
662
663
664
665
666
667
668
669

            /* Drift the cell which will be sent; note that not all sent
               particles will be drifted, only those that are needed. */
            cell_activate_drift_part(cj, s);
          }

        } else if (cj_nodeID != nodeID) {

          /* If the local cell is active, receive data from the foreign cell. */
          if (ci_active_black_holes) {
670
671
            scheduler_activate_recv(s, cj->mpi.recv, task_subtype_xv);
            scheduler_activate_recv(s, cj->mpi.recv, task_subtype_rho);
672
673

            /* If the local cell is active, more stuff will be needed. */
674
675
            scheduler_activate_send(s, ci->mpi.send, task_subtype_bpart,
                                    cj_nodeID);
676
            cell_activate_drift_bpart(ci, s);
677
678

            /* If the local cell is active, send its ti_end values. */
679
680
            scheduler_activate_send(s, ci->mpi.send, task_subtype_tend_bpart,
                                    cj_nodeID);
681
682
683
          }

          if (cj_active_black_holes) {
684
            scheduler_activate_recv(s, cj->mpi.recv, task_subtype_bpart);
685
686

            /* If the foreign cell is active, we want its ti_end values. */
687
            scheduler_activate_recv(s, cj->mpi.recv, task_subtype_tend_bpart);
688
689

            /* Is the foreign cell active and will need stuff from us? */
690
691
692
693
            scheduler_activate_send(s, ci->mpi.send, task_subtype_xv,
                                    cj_nodeID);
            scheduler_activate_send(s, ci->mpi.send, task_subtype_rho,
                                    cj_nodeID);
694
695
696
697
698
699
700
701
702

            /* Drift the cell which will be sent; note that not all sent
               particles will be drifted, only those that are needed. */
            cell_activate_drift_part(ci, s);
          }
        }
#endif
      }

Pedro Gonnet's avatar
Pedro Gonnet committed
703
      /* Only interested in gravity tasks as of here. */
704
      else if (t_subtype == task_subtype_grav) {
Pedro Gonnet's avatar
Pedro Gonnet committed
705
706
707
708
709
710

#ifdef WITH_MPI
        /* Activate the send/recv tasks. */
        if (ci_nodeID != nodeID) {

          /* If the local cell is active, receive data from the foreign cell. */
711
712
          if (cj_active_gravity)
            scheduler_activate_recv(s, ci->mpi.recv, task_subtype_gpart);
Pedro Gonnet's avatar
Pedro Gonnet committed
713
714

          /* If the foreign cell is active, we want its ti_end values. */
715
716
          if (ci_active_gravity)
            scheduler_activate_recv(s, ci->mpi.recv, task_subtype_tend_gpart);
Pedro Gonnet's avatar
Pedro Gonnet committed
717
718
719
720

          /* Is the foreign cell active and will need stuff from us? */
          if (ci_active_gravity) {

721
            struct link *l = scheduler_activate_send(
722
                s, cj->mpi.send, task_subtype_gpart, ci_nodeID);
Pedro Gonnet's avatar
Pedro Gonnet committed
723
724
725
726
727
728
729
730
731

            /* Drift the cell which will be sent at the level at which it is
               sent, i.e. drift the cell specified in the send task (l->t)
               itself. */
            cell_activate_drift_gpart(l->t->ci, s);
          }

          /* If the local cell is active, send its ti_end values. */
          if (cj_active_gravity)
732
733
            scheduler_activate_send(s, cj->mpi.send, task_subtype_tend_gpart,
                                    ci_nodeID);
Pedro Gonnet's avatar
Pedro Gonnet committed
734
735
736
737

        } else if (cj_nodeID != nodeID) {

          /* If the local cell is active, receive data from the foreign cell. */
738
739
          if (ci_active_gravity)
            scheduler_activate_recv(s, cj->mpi.recv, task_subtype_gpart);
Pedro Gonnet's avatar
Pedro Gonnet committed
740
741

          /* If the foreign cell is active, we want its ti_end values. */
742
743
          if (cj_active_gravity)
            scheduler_activate_recv(s, cj->mpi.recv, task_subtype_tend_gpart);
Pedro Gonnet's avatar
Pedro Gonnet committed
744
745
746
747

          /* Is the foreign cell active and will need stuff from us? */
          if (cj_active_gravity) {

748
            struct link *l = scheduler_activate_send(
749
                s, ci->mpi.send, task_subtype_gpart, cj_nodeID);
Pedro Gonnet's avatar
Pedro Gonnet committed
750
751
752
753
754
755
756
757
758

            /* Drift the cell which will be sent at the level at which it is
               sent, i.e. drift the cell specified in the send task (l->t)
               itself. */
            cell_activate_drift_gpart(l->t->ci, s);
          }

          /* If the local cell is active, send its ti_end values. */
          if (ci_active_gravity)
759
760
            scheduler_activate_send(s, ci->mpi.send, task_subtype_tend_gpart,
                                    cj_nodeID);
Pedro Gonnet's avatar
Pedro Gonnet committed
761
762
763
764
765
        }
#endif
      }
    }

766
767
    /* End force for hydro ? */
    else if (t_type == task_type_end_hydro_force) {
Pedro Gonnet's avatar
Pedro Gonnet committed
768

769
770
771
772
773
774
775
      if (cell_is_active_hydro(t->ci, e)) scheduler_activate(s, t);
    }

    /* End force for gravity ? */
    else if (t_type == task_type_end_grav_force) {

      if (cell_is_active_gravity(t->ci, e)) scheduler_activate(s, t);
Pedro Gonnet's avatar
Pedro Gonnet committed
776
777
778
779
780
    }

    /* Kick ? */
    else if (t_type == task_type_kick1 || t_type == task_type_kick2) {

781
782
783
      if (cell_is_active_hydro(t->ci, e) || cell_is_active_gravity(t->ci, e) ||
          cell_is_active_stars(t->ci, e) ||
          cell_is_active_black_holes(t->ci, e))
Pedro Gonnet's avatar
Pedro Gonnet committed
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
        scheduler_activate(s, t);
    }

    /* Hydro ghost tasks ? */
    else if (t_type == task_type_ghost || t_type == task_type_extra_ghost ||
             t_type == task_type_ghost_in || t_type == task_type_ghost_out) {
      if (cell_is_active_hydro(t->ci, e)) scheduler_activate(s, t);
    }

    /* logger tasks ? */
    else if (t->type == task_type_logger) {
      if (cell_is_active_hydro(t->ci, e) || cell_is_active_gravity(t->ci, e) ||
          cell_is_active_stars(t->ci, e))
        scheduler_activate(s, t);
    }

    /* Gravity stuff ? */
    else if (t_type == task_type_grav_down || t_type == task_type_grav_mesh ||
             t_type == task_type_grav_long_range ||
             t_type == task_type_init_grav ||
             t_type == task_type_init_grav_out ||
805
             t_type == task_type_drift_gpart_out ||
Pedro Gonnet's avatar
Pedro Gonnet committed
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
             t_type == task_type_grav_down_in) {
      if (cell_is_active_gravity(t->ci, e)) scheduler_activate(s, t);
    }

    /* Multipole - Multipole interaction task */
    else if (t_type == task_type_grav_mm) {

      /* Local pointers. */
      const struct cell *ci = t->ci;
      const struct cell *cj = t->cj;
#ifdef WITH_MPI
      const int ci_nodeID = ci->nodeID;
      const int cj_nodeID = (cj != NULL) ? cj->nodeID : -1;
#else
      const int ci_nodeID = nodeID;
      const int cj_nodeID = nodeID;
#endif
      const int ci_active_gravity = cell_is_active_gravity_mm(ci, e);
      const int cj_active_gravity = cell_is_active_gravity_mm(cj, e);

      if ((ci_active_gravity && ci_nodeID == nodeID) ||
          (cj_active_gravity && cj_nodeID == nodeID))
        scheduler_activate(s, t);
    }

    /* Star ghost tasks ? */
832
833
834
835
836
837
    else if (t_type == task_type_stars_ghost) {
      if (cell_is_active_stars(t->ci, e)) scheduler_activate(s, t);
    }

    /* Feedback implicit tasks? */
    else if (t_type == task_type_stars_in || t_type == task_type_stars_out) {
Pedro Gonnet's avatar
Pedro Gonnet committed
838
839
840
      if (cell_is_active_stars(t->ci, e)) scheduler_activate(s, t);
    }

841
842
843
844
845
846
847
848
849
850
    /* Black hole ghost tasks ? */
    else if (t_type == task_type_bh_ghost) {
      if (cell_is_active_black_holes(t->ci, e)) scheduler_activate(s, t);
    }

    /* Black holes implicit tasks? */
    else if (t_type == task_type_bh_in || t_type == task_type_bh_out) {
      if (cell_is_active_black_holes(t->ci, e)) scheduler_activate(s, t);
    }

Pedro Gonnet's avatar
Pedro Gonnet committed
851
852
853
854
855
    /* Time-step? */
    else if (t_type == task_type_timestep) {
      t->ci->hydro.updated = 0;
      t->ci->grav.updated = 0;
      t->ci->stars.updated = 0;
856
857
858
859
      t->ci->black_holes.updated = 0;
      if (cell_is_active_hydro(t->ci, e) || cell_is_active_gravity(t->ci, e) ||
          cell_is_active_stars(t->ci, e) ||
          cell_is_active_black_holes(t->ci, e))
Pedro Gonnet's avatar
Pedro Gonnet committed
860
861
862
        scheduler_activate(s, t);
    }

863
    /* Subgrid tasks: cooling */
Pedro Gonnet's avatar
Pedro Gonnet committed
864
    else if (t_type == task_type_cooling) {
865
866
867
868
869
870
      if (cell_is_active_hydro(t->ci, e)) scheduler_activate(s, t);
    }

    /* Subgrid tasks: star formation */
    else if (t_type == task_type_star_formation) {
      if (cell_is_active_hydro(t->ci, e)) {
Pedro Gonnet's avatar
Pedro Gonnet committed
871
        scheduler_activate(s, t);
872
        cell_activate_super_spart_drifts(t->ci, s);
873
      }
Pedro Gonnet's avatar
Pedro Gonnet committed
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
    }
  }
}

/**
 * @brief Mark tasks to be un-skipped and set the sort flags accordingly.
 *
 * @return 1 if the space has to be rebuilt, 0 otherwise.
 */
int engine_marktasks(struct engine *e) {

  struct scheduler *s = &e->sched;
  const ticks tic = getticks();
  int rebuild_space = 0;

  /* Run through the tasks and mark as skip or not. */
  size_t extra_data[3] = {(size_t)e, (size_t)rebuild_space, (size_t)&e->sched};
  threadpool_map(&e->threadpool, engine_marktasks_mapper, s->tasks, s->nr_tasks,
                 sizeof(struct task), 0, extra_data);
  rebuild_space = extra_data[1];

  if (e->verbose)
    message("took %.3f %s.", clocks_from_ticks(getticks() - tic),
            clocks_getunit());

  /* All is well... */
  return rebuild_space;
}