engine_maketasks.c 120 KB
Newer Older
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
/*******************************************************************************
 * 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 "atomic.h"
#include "cell.h"
#include "clocks.h"
#include "cycle.h"
#include "debug.h"
#include "error.h"
#include "proxy.h"
#include "timers.h"

55
56
57
extern int engine_max_parts_per_ghost;
extern int engine_max_sparts_per_ghost;

58
59
60
61
62
63
64
/**
 * @brief Add send tasks for the gravity pairs to a hierarchy of cells.
 *
 * @param e The #engine.
 * @param ci The sending #cell.
 * @param cj Dummy cell containing the nodeID of the receiving node.
 * @param t_grav The send_grav #task, if it has already been created.
65
 * @param t_ti The recv_ti_end #task, if it has already been created.
66
67
 */
void engine_addtasks_send_gravity(struct engine *e, struct cell *ci,
68
69
                                  struct cell *cj, struct task *t_grav,
                                  struct task *t_ti) {
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

#ifdef WITH_MPI
  struct link *l = NULL;
  struct scheduler *s = &e->sched;
  const int nodeID = cj->nodeID;

  /* Check if any of the gravity tasks are for the target node. */
  for (l = ci->grav.grav; l != NULL; l = l->next)
    if (l->t->ci->nodeID == nodeID ||
        (l->t->cj != NULL && l->t->cj->nodeID == nodeID))
      break;

  /* If so, attach send tasks. */
  if (l != NULL) {

    /* Create the tasks and their dependencies? */
    if (t_grav == NULL) {

Matthieu Schaller's avatar
Matthieu Schaller committed
88
89
      /* Make sure this cell is tagged. */
      cell_ensure_tagged(ci);
90
91
92
93

      t_grav = scheduler_addtask(s, task_type_send, task_subtype_gpart,
                                 ci->mpi.tag, 0, ci, cj);

94
95
96
      t_ti = scheduler_addtask(s, task_type_send, task_subtype_tend_gpart,
                               ci->mpi.tag, 0, ci, cj);

97
98
99
100
101
      /* The sends should unlock the down pass. */
      scheduler_addunlock(s, t_grav, ci->grav.super->grav.down);

      /* Drift before you send */
      scheduler_addunlock(s, ci->grav.super->grav.drift, t_grav);
102
103

      scheduler_addunlock(s, ci->super->timestep, t_ti);
104
105
106
    }

    /* Add them to the local cell. */
107
108
    engine_addlink(e, &ci->mpi.send, t_grav);
    engine_addlink(e, &ci->mpi.send, t_ti);
109
110
111
112
113
114
  }

  /* Recurse? */
  if (ci->split)
    for (int k = 0; k < 8; k++)
      if (ci->progeny[k] != NULL)
115
        engine_addtasks_send_gravity(e, ci->progeny[k], cj, t_grav, t_ti);
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

#else
  error("SWIFT was not compiled with MPI support.");
#endif
}

/**
 * @brief Add send tasks for the hydro pairs to a hierarchy of cells.
 *
 * @param e The #engine.
 * @param ci The sending #cell.
 * @param cj Dummy cell containing the nodeID of the receiving node.
 * @param t_xv The send_xv #task, if it has already been created.
 * @param t_rho The send_rho #task, if it has already been created.
 * @param t_gradient The send_gradient #task, if already created.
131
 * @param t_ti The recv_ti_end #task, if it has already been created.
132
133
134
 */
void engine_addtasks_send_hydro(struct engine *e, struct cell *ci,
                                struct cell *cj, struct task *t_xv,
135
136
                                struct task *t_rho, struct task *t_gradient,
                                struct task *t_ti) {
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154

#ifdef WITH_MPI
  struct link *l = NULL;
  struct scheduler *s = &e->sched;
  const int nodeID = cj->nodeID;

  /* Check if any of the density tasks are for the target node. */
  for (l = ci->hydro.density; l != NULL; l = l->next)
    if (l->t->ci->nodeID == nodeID ||
        (l->t->cj != NULL && l->t->cj->nodeID == nodeID))
      break;

  /* If so, attach send tasks. */
  if (l != NULL) {

    /* Create the tasks and their dependencies? */
    if (t_xv == NULL) {

Matthieu Schaller's avatar
Matthieu Schaller committed
155
156
      /* Make sure this cell is tagged. */
      cell_ensure_tagged(ci);
157
158
159
160
161

      t_xv = scheduler_addtask(s, task_type_send, task_subtype_xv, ci->mpi.tag,
                               0, ci, cj);
      t_rho = scheduler_addtask(s, task_type_send, task_subtype_rho,
                                ci->mpi.tag, 0, ci, cj);
162

163
164
165
166
167
#ifdef EXTRA_HYDRO_LOOP
      t_gradient = scheduler_addtask(s, task_type_send, task_subtype_gradient,
                                     ci->mpi.tag, 0, ci, cj);
#endif

168
169
170
      t_ti = scheduler_addtask(s, task_type_send, task_subtype_tend_part,
                               ci->mpi.tag, 0, ci, cj);

171
172
#ifdef EXTRA_HYDRO_LOOP

173
      scheduler_addunlock(s, t_gradient, ci->hydro.super->hydro.end_force);
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

      scheduler_addunlock(s, ci->hydro.super->hydro.extra_ghost, t_gradient);

      /* The send_rho task should unlock the super_hydro-cell's extra_ghost
       * task. */
      scheduler_addunlock(s, t_rho, ci->hydro.super->hydro.extra_ghost);

      /* The send_rho task depends on the cell's ghost task. */
      scheduler_addunlock(s, ci->hydro.super->hydro.ghost_out, t_rho);

      /* The send_xv task should unlock the super_hydro-cell's ghost task. */
      scheduler_addunlock(s, t_xv, ci->hydro.super->hydro.ghost_in);

#else
      /* The send_rho task should unlock the super_hydro-cell's kick task. */
189
      scheduler_addunlock(s, t_rho, ci->hydro.super->hydro.end_force);
190
191
192
193
194
195
196
197
198

      /* The send_rho task depends on the cell's ghost task. */
      scheduler_addunlock(s, ci->hydro.super->hydro.ghost_out, t_rho);

      /* The send_xv task should unlock the super_hydro-cell's ghost task. */
      scheduler_addunlock(s, t_xv, ci->hydro.super->hydro.ghost_in);

#endif

199
      scheduler_addunlock(s, ci->hydro.super->hydro.drift, t_rho);
200

201
202
      /* Drift before you send */
      scheduler_addunlock(s, ci->hydro.super->hydro.drift, t_xv);
203
204

      scheduler_addunlock(s, ci->super->timestep, t_ti);
205
206
207
    }

    /* Add them to the local cell. */
208
209
    engine_addlink(e, &ci->mpi.send, t_xv);
    engine_addlink(e, &ci->mpi.send, t_rho);
210
#ifdef EXTRA_HYDRO_LOOP
211
    engine_addlink(e, &ci->mpi.send, t_gradient);
212
#endif
213
    engine_addlink(e, &ci->mpi.send, t_ti);
214
215
216
217
218
219
220
  }

  /* Recurse? */
  if (ci->split)
    for (int k = 0; k < 8; k++)
      if (ci->progeny[k] != NULL)
        engine_addtasks_send_hydro(e, ci->progeny[k], cj, t_xv, t_rho,
221
                                   t_gradient, t_ti);
222
223
224
225
226
227

#else
  error("SWIFT was not compiled with MPI support.");
#endif
}

228
229
230
231
232
233
/**
 * @brief Add send tasks for the stars pairs to a hierarchy of cells.
 *
 * @param e The #engine.
 * @param ci The sending #cell.
 * @param cj Dummy cell containing the nodeID of the receiving node.
234
 * @param t_feedback The send_feed #task, if it has already been created.
Matthieu Schaller's avatar
Matthieu Schaller committed
235
 * @param t_sf_counts The send_sf_counts, if it has been created.
236
 * @param t_ti The recv_ti_end #task, if it has already been created.
Matthieu Schaller's avatar
Matthieu Schaller committed
237
 * @param with_star_formation Are we running with star formation on?
238
239
 */
void engine_addtasks_send_stars(struct engine *e, struct cell *ci,
240
                                struct cell *cj, struct task *t_feedback,
241
242
                                struct task *t_sf_counts, struct task *t_ti,
                                const int with_star_formation) {
243
244

#ifdef WITH_MPI
245

246
247
248
249
  struct link *l = NULL;
  struct scheduler *s = &e->sched;
  const int nodeID = cj->nodeID;

250
251
  if (t_sf_counts == NULL && with_star_formation && ci->hydro.count > 0) {
#ifdef SWIFT_DEBUG_CHECKS
252
253
254
255
256
    if (ci->depth != 0)
      error(
          "Attaching a sf_count task at a non-top level c->depth=%d "
          "c->count=%d",
          ci->depth, ci->hydro.count);
257
#endif
258
259
    t_sf_counts = scheduler_addtask(s, task_type_send, task_subtype_sf_counts,
                                    ci->mpi.tag, 0, ci, cj);
260
261
262
    scheduler_addunlock(s, ci->hydro.star_formation, t_sf_counts);
  }

263
  /* Check if any of the density tasks are for the target node. */
264
  for (l = ci->stars.density; l != NULL; l = l->next)
265
    if (l->t->ci->nodeID == nodeID ||
266
        (l->t->cj != NULL && l->t->cj->nodeID == nodeID))
267
268
269
270
      break;

  /* If so, attach send tasks. */
  if (l != NULL) {
271

272
    if (t_feedback == NULL) {
273

274
275
      /* Make sure this cell is tagged. */
      cell_ensure_tagged(ci);
276

277
      /* Create the tasks and their dependencies? */
278
      t_feedback = scheduler_addtask(s, task_type_send, task_subtype_spart,
279
280
                                     ci->mpi.tag, 0, ci, cj);

281
282
283
      t_ti = scheduler_addtask(s, task_type_send, task_subtype_tend_spart,
                               ci->mpi.tag, 0, ci, cj);

284
      /* The send_stars task should unlock the super_cell's kick task. */
285
      scheduler_addunlock(s, t_feedback, ci->hydro.super->stars.stars_out);
286

287
      /* Ghost before you send */
288
      scheduler_addunlock(s, ci->hydro.super->stars.ghost, t_feedback);
289
290
291

      /* Drift before you send */
      scheduler_addunlock(s, ci->hydro.super->stars.drift, t_feedback);
292
293
294
295

      scheduler_addunlock(s, ci->super->timestep, t_ti);
    }

296
297
    engine_addlink(e, &ci->mpi.send, t_feedback);
    engine_addlink(e, &ci->mpi.send, t_ti);
298
    if (with_star_formation && ci->hydro.count > 0) {
299
      engine_addlink(e, &ci->mpi.send, t_sf_counts);
300
    }
301
302
303
304
305
306
  }

  /* Recurse? */
  if (ci->split)
    for (int k = 0; k < 8; k++)
      if (ci->progeny[k] != NULL)
Matthieu Schaller's avatar
Matthieu Schaller committed
307
        engine_addtasks_send_stars(e, ci->progeny[k], cj, t_feedback,
308
                                   t_sf_counts, t_ti, with_star_formation);
309
310
311
312
313
314

#else
  error("SWIFT was not compiled with MPI support.");
#endif
}

315
316
317
318
319
320
/**
 * @brief Add send tasks for the black holes pairs to a hierarchy of cells.
 *
 * @param e The #engine.
 * @param ci The sending #cell.
 * @param cj Dummy cell containing the nodeID of the receiving node.
321
322
323
324
 * @param t_rho The density comm. task, if it has already been created.
 * @param t_swallow The BH swallow comm. task, if it has already been created.
 * @param t_gas_swallow The gas swallow comm. task, if it has already been
 * created.
325
326
327
328
 * @param t_feedback The send_feed #task, if it has already been created.
 * @param t_ti The recv_ti_end #task, if it has already been created.
 */
void engine_addtasks_send_black_holes(struct engine *e, struct cell *ci,
329
                                      struct cell *cj, struct task *t_rho,
330
                                      struct task *t_bh_merger,
331
332
                                      struct task *t_gas_swallow,
                                      struct task *t_feedback,
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
                                      struct task *t_ti) {

#ifdef WITH_MPI

  struct link *l = NULL;
  struct scheduler *s = &e->sched;
  const int nodeID = cj->nodeID;

  /* Check if any of the density tasks are for the target node. */
  for (l = ci->black_holes.density; l != NULL; l = l->next)
    if (l->t->ci->nodeID == nodeID ||
        (l->t->cj != NULL && l->t->cj->nodeID == nodeID))
      break;

  /* If so, attach send tasks. */
  if (l != NULL) {

350
    if (t_rho == NULL) {
351
352
353
354
355

      /* Make sure this cell is tagged. */
      cell_ensure_tagged(ci);

      /* Create the tasks and their dependencies? */
356
357
358
      t_rho = scheduler_addtask(s, task_type_send, task_subtype_bpart_rho,
                                ci->mpi.tag, 0, ci, cj);

359
360
      t_bh_merger = scheduler_addtask(
          s, task_type_send, task_subtype_bpart_merger, ci->mpi.tag, 0, ci, cj);
361
362
363
364
365
366
367

      t_gas_swallow = scheduler_addtask(
          s, task_type_send, task_subtype_part_swallow, ci->mpi.tag, 0, ci, cj);

      t_feedback =
          scheduler_addtask(s, task_type_send, task_subtype_bpart_feedback,
                            ci->mpi.tag, 0, ci, cj);
368
369
370
371

      t_ti = scheduler_addtask(s, task_type_send, task_subtype_tend_bpart,
                               ci->mpi.tag, 0, ci, cj);

372
373
      /* The send_black_holes task should unlock the super_cell's BH exit point
       * task. */
374
375
376
      scheduler_addunlock(s, t_feedback,
                          ci->hydro.super->black_holes.black_holes_out);

377
      scheduler_addunlock(s, ci->hydro.super->black_holes.swallow_ghost[2],
378
379
                          t_feedback);

380
      /* Ghost before you send */
381
382
383
      scheduler_addunlock(s, ci->hydro.super->black_holes.density_ghost, t_rho);
      scheduler_addunlock(s, t_rho,
                          ci->hydro.super->black_holes.swallow_ghost[0]);
384

385
386
387
388
      scheduler_addunlock(s, ci->hydro.super->black_holes.swallow_ghost[0],
                          t_bh_merger);
      scheduler_addunlock(s, t_bh_merger,
                          ci->hydro.super->black_holes.swallow_ghost[2]);
389

390
391
392
393
      scheduler_addunlock(s, ci->hydro.super->black_holes.swallow_ghost[0],
                          t_gas_swallow);
      scheduler_addunlock(s, t_gas_swallow,
                          ci->hydro.super->black_holes.swallow_ghost[1]);
394
395
396
397

      scheduler_addunlock(s, ci->super->timestep, t_ti);
    }

398
    engine_addlink(e, &ci->mpi.send, t_rho);
399
    engine_addlink(e, &ci->mpi.send, t_bh_merger);
400
    engine_addlink(e, &ci->mpi.send, t_gas_swallow);
401
402
    engine_addlink(e, &ci->mpi.send, t_feedback);
    engine_addlink(e, &ci->mpi.send, t_ti);
403
404
405
406
407
408
  }

  /* Recurse? */
  if (ci->split)
    for (int k = 0; k < 8; k++)
      if (ci->progeny[k] != NULL)
409
        engine_addtasks_send_black_holes(e, ci->progeny[k], cj, t_rho,
410
                                         t_bh_merger, t_gas_swallow, t_feedback,
411
412
413
414
415
416
417
                                         t_ti);

#else
  error("SWIFT was not compiled with MPI support.");
#endif
}

418
419
420
421
422
423
424
425
/**
 * @brief Add recv tasks for hydro pairs to a hierarchy of cells.
 *
 * @param e The #engine.
 * @param c The foreign #cell.
 * @param t_xv The recv_xv #task, if it has already been created.
 * @param t_rho The recv_rho #task, if it has already been created.
 * @param t_gradient The recv_gradient #task, if it has already been created.
426
 * @param t_ti The recv_ti_end #task, if it has already been created.
427
428
429
 */
void engine_addtasks_recv_hydro(struct engine *e, struct cell *c,
                                struct task *t_xv, struct task *t_rho,
430
                                struct task *t_gradient, struct task *t_ti) {
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451

#ifdef WITH_MPI
  struct scheduler *s = &e->sched;

  /* Have we reached a level where there are any hydro tasks ? */
  if (t_xv == NULL && c->hydro.density != NULL) {

#ifdef SWIFT_DEBUG_CHECKS
    /* Make sure this cell has a valid tag. */
    if (c->mpi.tag < 0) error("Trying to receive from untagged cell.");
#endif  // SWIFT_DEBUG_CHECKS

    /* Create the tasks. */
    t_xv = scheduler_addtask(s, task_type_recv, task_subtype_xv, c->mpi.tag, 0,
                             c, NULL);
    t_rho = scheduler_addtask(s, task_type_recv, task_subtype_rho, c->mpi.tag,
                              0, c, NULL);
#ifdef EXTRA_HYDRO_LOOP
    t_gradient = scheduler_addtask(s, task_type_recv, task_subtype_gradient,
                                   c->mpi.tag, 0, c, NULL);
#endif
452
453
454

    t_ti = scheduler_addtask(s, task_type_recv, task_subtype_tend_part,
                             c->mpi.tag, 0, c, NULL);
455
456
  }

457
458
459
460
461
462
463
  if (t_xv != NULL) {
    engine_addlink(e, &c->mpi.recv, t_xv);
    engine_addlink(e, &c->mpi.recv, t_rho);
#ifdef EXTRA_HYDRO_LOOP
    engine_addlink(e, &c->mpi.recv, t_gradient);
#endif
    engine_addlink(e, &c->mpi.recv, t_ti);
464

465
466
467
468
469
    /* Add dependencies. */
    if (c->hydro.sorts != NULL) {
      scheduler_addunlock(s, t_xv, c->hydro.sorts);
      scheduler_addunlock(s, c->hydro.sorts, t_rho);
    }
470

471
472
473
474
    for (struct link *l = c->hydro.density; l != NULL; l = l->next) {
      scheduler_addunlock(s, t_xv, l->t);
      scheduler_addunlock(s, l->t, t_rho);
    }
475
#ifdef EXTRA_HYDRO_LOOP
476
477
478
479
480
481
482
483
    for (struct link *l = c->hydro.gradient; l != NULL; l = l->next) {
      scheduler_addunlock(s, t_rho, l->t);
      scheduler_addunlock(s, l->t, t_gradient);
    }
    for (struct link *l = c->hydro.force; l != NULL; l = l->next) {
      scheduler_addunlock(s, t_gradient, l->t);
      scheduler_addunlock(s, l->t, t_ti);
    }
484
#else
485
486
487
488
    for (struct link *l = c->hydro.force; l != NULL; l = l->next) {
      scheduler_addunlock(s, t_rho, l->t);
      scheduler_addunlock(s, l->t, t_ti);
    }
489
490
#endif

491
492
493
494
495
    /* Make sure the density has been computed before the stars compute theirs.
     */
    for (struct link *l = c->stars.density; l != NULL; l = l->next) {
      scheduler_addunlock(s, t_rho, l->t);
    }
Matthieu Schaller's avatar
Matthieu Schaller committed
496

497
498
    /* Make sure the part have been received before the BHs compute their
     * accretion rates (depends on particles' rho). */
Matthieu Schaller's avatar
Matthieu Schaller committed
499
    for (struct link *l = c->black_holes.density; l != NULL; l = l->next) {
500
      scheduler_addunlock(s, t_rho, l->t);
Matthieu Schaller's avatar
Matthieu Schaller committed
501
    }
502
  }
503

504
505
506
507
  /* Recurse? */
  if (c->split)
    for (int k = 0; k < 8; k++)
      if (c->progeny[k] != NULL)
508
509
        engine_addtasks_recv_hydro(e, c->progeny[k], t_xv, t_rho, t_gradient,
                                   t_ti);
510
511
512
513
514
515

#else
  error("SWIFT was not compiled with MPI support.");
#endif
}

516
517
518
519
520
/**
 * @brief Add recv tasks for stars pairs to a hierarchy of cells.
 *
 * @param e The #engine.
 * @param c The foreign #cell.
Matthieu Schaller's avatar
Matthieu Schaller committed
521
 * @param t_feedback The recv_feed #task, if it has already been created.
Matthieu Schaller's avatar
Matthieu Schaller committed
522
 * @param t_sf_counts The recv_sf_counts, if it has been created.
523
 * @param t_ti The recv_ti_end #task, if it has already been created.
Matthieu Schaller's avatar
Matthieu Schaller committed
524
 * @param with_star_formation Are we running with star formation on?
525
526
 */
void engine_addtasks_recv_stars(struct engine *e, struct cell *c,
527
528
529
                                struct task *t_feedback,
                                struct task *t_sf_counts, struct task *t_ti,
                                const int with_star_formation) {
530
531
532
533

#ifdef WITH_MPI
  struct scheduler *s = &e->sched;

534
535
  if (t_sf_counts == NULL && with_star_formation && c->hydro.count > 0) {
#ifdef SWIFT_DEBUG_CHECKS
536
537
538
539
540
    if (c->depth != 0)
      error(
          "Attaching a sf_count task at a non-top level c->depth=%d "
          "c->count=%d",
          c->depth, c->hydro.count);
541
542
#endif
    t_sf_counts = scheduler_addtask(s, task_type_recv, task_subtype_sf_counts,
543
                                    c->mpi.tag, 0, c, NULL);
544
  }
545

546
547
  /* Have we reached a level where there are any stars tasks ? */
  if (t_feedback == NULL && c->stars.density != NULL) {
548
549
550
551
552
553
554

#ifdef SWIFT_DEBUG_CHECKS
    /* Make sure this cell has a valid tag. */
    if (c->mpi.tag < 0) error("Trying to receive from untagged cell.");
#endif  // SWIFT_DEBUG_CHECKS

    /* Create the tasks. */
555
    t_feedback = scheduler_addtask(s, task_type_recv, task_subtype_spart,
556
                                   c->mpi.tag, 0, c, NULL);
557
558
559

    t_ti = scheduler_addtask(s, task_type_recv, task_subtype_tend_spart,
                             c->mpi.tag, 0, c, NULL);
560
561
562
563
564
565

    if (with_star_formation && c->hydro.count > 0) {

      /* Receive the stars only once the counts have been received */
      scheduler_addunlock(s, t_sf_counts, t_feedback);
    }
566
567
  }

568
569
570
  if (t_feedback != NULL) {
    engine_addlink(e, &c->mpi.recv, t_feedback);
    engine_addlink(e, &c->mpi.recv, t_ti);
Matthieu Schaller's avatar
Matthieu Schaller committed
571
572
573
    if (with_star_formation && c->hydro.count > 0) {
      engine_addlink(e, &c->mpi.recv, t_sf_counts);
    }
574

575
#ifdef SWIFT_DEBUG_CHECKS
576
    if (c->nodeID == e->nodeID) error("Local cell!");
577
#endif
578
579
    if (c->stars.sorts != NULL)
      scheduler_addunlock(s, t_feedback, c->stars.sorts);
580

581
582
583
    for (struct link *l = c->stars.density; l != NULL; l = l->next) {
      scheduler_addunlock(s, l->t, t_feedback);
    }
584

585
586
587
588
    for (struct link *l = c->stars.feedback; l != NULL; l = l->next) {
      scheduler_addunlock(s, t_feedback, l->t);
      scheduler_addunlock(s, l->t, t_ti);
    }
589
590
  }

591
592
593
594
  /* Recurse? */
  if (c->split)
    for (int k = 0; k < 8; k++)
      if (c->progeny[k] != NULL)
Matthieu Schaller's avatar
Matthieu Schaller committed
595
        engine_addtasks_recv_stars(e, c->progeny[k], t_feedback, t_sf_counts,
596
                                   t_ti, with_star_formation);
597
598
599
600
601
602

#else
  error("SWIFT was not compiled with MPI support.");
#endif
}

603
604
605
606
607
/**
 * @brief Add recv tasks for black_holes pairs to a hierarchy of cells.
 *
 * @param e The #engine.
 * @param c The foreign #cell.
608
609
610
611
 * @param t_rho The density comm. task, if it has already been created.
 * @param t_swallow The BH swallow comm. task, if it has already been created.
 * @param t_gas_swallow The gas swallow comm. task, if it has already been
 * created.
612
613
614
615
 * @param t_feedback The recv_feed #task, if it has already been created.
 * @param t_ti The recv_ti_end #task, if it has already been created.
 */
void engine_addtasks_recv_black_holes(struct engine *e, struct cell *c,
616
                                      struct task *t_rho,
617
                                      struct task *t_bh_merger,
618
                                      struct task *t_gas_swallow,
619
620
621
622
623
624
625
                                      struct task *t_feedback,
                                      struct task *t_ti) {

#ifdef WITH_MPI
  struct scheduler *s = &e->sched;

  /* Have we reached a level where there are any black_holes tasks ? */
626
  if (t_rho == NULL && c->black_holes.density != NULL) {
627
628
629
630
631
632
633

#ifdef SWIFT_DEBUG_CHECKS
    /* Make sure this cell has a valid tag. */
    if (c->mpi.tag < 0) error("Trying to receive from untagged cell.");
#endif  // SWIFT_DEBUG_CHECKS

    /* Create the tasks. */
634
635
636
    t_rho = scheduler_addtask(s, task_type_recv, task_subtype_bpart_rho,
                              c->mpi.tag, 0, c, NULL);

637
638
    t_bh_merger = scheduler_addtask(
        s, task_type_recv, task_subtype_bpart_merger, c->mpi.tag, 0, c, NULL);
639
640
641
642
643
644

    t_gas_swallow = scheduler_addtask(
        s, task_type_recv, task_subtype_part_swallow, c->mpi.tag, 0, c, NULL);

    t_feedback = scheduler_addtask(
        s, task_type_recv, task_subtype_bpart_feedback, c->mpi.tag, 0, c, NULL);
645
646
647
648
649

    t_ti = scheduler_addtask(s, task_type_recv, task_subtype_tend_bpart,
                             c->mpi.tag, 0, c, NULL);
  }

650
651
  if (t_rho != NULL) {
    engine_addlink(e, &c->mpi.recv, t_rho);
652
    engine_addlink(e, &c->mpi.recv, t_bh_merger);
653
    engine_addlink(e, &c->mpi.recv, t_gas_swallow);
654
655
    engine_addlink(e, &c->mpi.recv, t_feedback);
    engine_addlink(e, &c->mpi.recv, t_ti);
656
657

#ifdef SWIFT_DEBUG_CHECKS
658
    if (c->nodeID == e->nodeID) error("Local cell!");
659
660
#endif

661
    for (struct link *l = c->black_holes.density; l != NULL; l = l->next) {
662
      scheduler_addunlock(s, l->t, t_rho);
663
    }
664

665
666
667
668
669
670
671
    for (struct link *l = c->hydro.force; l != NULL; l = l->next) {
      scheduler_addunlock(s, l->t, t_gas_swallow);
    }

    for (struct link *l = c->black_holes.swallow; l != NULL; l = l->next) {
      scheduler_addunlock(s, t_rho, l->t);
      scheduler_addunlock(s, l->t, t_gas_swallow);
672
      scheduler_addunlock(s, l->t, t_bh_merger);
673
    }
674
675
    for (struct link *l = c->black_holes.do_gas_swallow; l != NULL;
         l = l->next) {
676
      scheduler_addunlock(s, t_gas_swallow, l->t);
677
678
    }
    for (struct link *l = c->black_holes.do_bh_swallow; l != NULL;
679
         l = l->next) {
680
      scheduler_addunlock(s, t_bh_merger, l->t);
681
682
      scheduler_addunlock(s, l->t, t_feedback);
    }
683
684
685
686
    for (struct link *l = c->black_holes.feedback; l != NULL; l = l->next) {
      scheduler_addunlock(s, t_feedback, l->t);
      scheduler_addunlock(s, l->t, t_ti);
    }
687
688
689
690
691
692
  }

  /* Recurse? */
  if (c->split)
    for (int k = 0; k < 8; k++)
      if (c->progeny[k] != NULL)
693
        engine_addtasks_recv_black_holes(e, c->progeny[k], t_rho, t_bh_merger,
694
                                         t_gas_swallow, t_feedback, t_ti);
695
696
697
698
699
700

#else
  error("SWIFT was not compiled with MPI support.");
#endif
}

701
702
703
704
705
706
/**
 * @brief Add recv tasks for gravity pairs to a hierarchy of cells.
 *
 * @param e The #engine.
 * @param c The foreign #cell.
 * @param t_grav The recv_gpart #task, if it has already been created.
707
 * @param t_ti The recv_ti_end #task, if it has already been created.
708
709
 */
void engine_addtasks_recv_gravity(struct engine *e, struct cell *c,
710
                                  struct task *t_grav, struct task *t_ti) {
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726

#ifdef WITH_MPI
  struct scheduler *s = &e->sched;

  /* Have we reached a level where there are any gravity tasks ? */
  if (t_grav == NULL && c->grav.grav != NULL) {

#ifdef SWIFT_DEBUG_CHECKS
    /* Make sure this cell has a valid tag. */
    if (c->mpi.tag < 0) error("Trying to receive from untagged cell.");
#endif  // SWIFT_DEBUG_CHECKS

    /* Create the tasks. */
    t_grav = scheduler_addtask(s, task_type_recv, task_subtype_gpart,
                               c->mpi.tag, 0, c, NULL);

727
728
    t_ti = scheduler_addtask(s, task_type_recv, task_subtype_tend_gpart,
                             c->mpi.tag, 0, c, NULL);
729
730
  }

731
732
733
734
  /* If we have tasks, link them. */
  if (t_grav != NULL) {
    engine_addlink(e, &c->mpi.recv, t_grav);
    engine_addlink(e, &c->mpi.recv, t_ti);
735

736
737
738
739
    for (struct link *l = c->grav.grav; l != NULL; l = l->next) {
      scheduler_addunlock(s, t_grav, l->t);
      scheduler_addunlock(s, l->t, t_ti);
    }
740
  }
741

742
743
744
745
  /* Recurse? */
  if (c->split)
    for (int k = 0; k < 8; k++)
      if (c->progeny[k] != NULL)
746
        engine_addtasks_recv_gravity(e, c->progeny[k], t_grav, t_ti);
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767

#else
  error("SWIFT was not compiled with MPI support.");
#endif
}

/**
 * @brief Generate the hydro hierarchical tasks for a hierarchy of cells -
 * i.e. all the O(Npart) tasks -- timestep version
 *
 * Tasks are only created here. The dependencies will be added later on.
 *
 * Note that there is no need to recurse below the super-cell. Note also
 * that we only add tasks if the relevant particles are present in the cell.
 *
 * @param e The #engine.
 * @param c The #cell.
 */
void engine_make_hierarchical_tasks_common(struct engine *e, struct cell *c) {

  struct scheduler *s = &e->sched;
768
  const int with_limiter = (e->policy & engine_policy_limiter);
769
770
771
772
773
774
775
776
777
778
  const int with_star_formation = (e->policy & engine_policy_star_formation);

  /* Are we at the top-level? */
  if (c->top == c && c->nodeID == e->nodeID) {

    if (with_star_formation && c->hydro.count > 0) {
      c->hydro.star_formation = scheduler_addtask(
          s, task_type_star_formation, task_subtype_none, 0, 0, c, NULL);
    }
  }
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801

  /* Are we in a super-cell ? */
  if (c->super == c) {

    /* Local tasks only... */
    if (c->nodeID == e->nodeID) {

      /* Add the two half kicks */
      c->kick1 = scheduler_addtask(s, task_type_kick1, task_subtype_none, 0, 0,
                                   c, NULL);

#if defined(WITH_LOGGER)
      c->logger = scheduler_addtask(s, task_type_logger, task_subtype_none, 0,
                                    0, c, NULL);
#endif

      c->kick2 = scheduler_addtask(s, task_type_kick2, task_subtype_none, 0, 0,
                                   c, NULL);

      /* Add the time-step calculation task and its dependency */
      c->timestep = scheduler_addtask(s, task_type_timestep, task_subtype_none,
                                      0, 0, c, NULL);

802
      scheduler_addunlock(s, c->kick2, c->timestep);
803
804
      scheduler_addunlock(s, c->timestep, c->kick1);

805
806
807
808
809
810
      /* Subgrid tasks: star formation */
      if (with_star_formation && c->hydro.count > 0) {
        scheduler_addunlock(s, c->kick2, c->top->hydro.star_formation);
        scheduler_addunlock(s, c->top->hydro.star_formation, c->timestep);
      }

811
812
813
814
815
816
817
818
819
820
      /* Time-step limiting */
      if (with_limiter) {
        c->timestep_limiter = scheduler_addtask(
            s, task_type_timestep_limiter, task_subtype_none, 0, 0, c, NULL);

        /* Make sure it is not run before kick2 */
        scheduler_addunlock(s, c->timestep, c->timestep_limiter);
        scheduler_addunlock(s, c->timestep_limiter, c->kick1);
      }

821
822
823
824
825
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
#if defined(WITH_LOGGER)
      scheduler_addunlock(s, c->kick1, c->logger);
#endif
    }
  } else { /* We are above the super-cell so need to go deeper */

    /* Recurse. */
    if (c->split)
      for (int k = 0; k < 8; k++)
        if (c->progeny[k] != NULL)
          engine_make_hierarchical_tasks_common(e, c->progeny[k]);
  }
}

/**
 * @brief Generate the hydro hierarchical tasks for a hierarchy of cells -
 * i.e. all the O(Npart) tasks -- gravity version
 *
 * Tasks are only created here. The dependencies will be added later on.
 *
 * Note that there is no need to recurse below the super-cell. Note also
 * that we only add tasks if the relevant particles are present in the cell.
 *
 * @param e The #engine.
 * @param c The #cell.
 */
void engine_make_hierarchical_tasks_gravity(struct engine *e, struct cell *c) {

  struct scheduler *s = &e->sched;
  const int periodic = e->s->periodic;
  const int is_self_gravity = (e->policy & engine_policy_self_gravity);

  /* Are we in a super-cell ? */
  if (c->grav.super == c) {

    /* Local tasks only... */
    if (c->nodeID == e->nodeID) {

      c->grav.drift = scheduler_addtask(s, task_type_drift_gpart,
                                        task_subtype_none, 0, 0, c, NULL);

862
863
864
865
866
      c->grav.end_force = scheduler_addtask(s, task_type_end_grav_force,
                                            task_subtype_none, 0, 0, c, NULL);

      scheduler_addunlock(s, c->grav.end_force, c->super->kick2);

867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
      if (is_self_gravity) {

        /* Initialisation of the multipoles */
        c->grav.init = scheduler_addtask(s, task_type_init_grav,
                                         task_subtype_none, 0, 0, c, NULL);

        /* Gravity non-neighbouring pm calculations */
        c->grav.long_range = scheduler_addtask(
            s, task_type_grav_long_range, task_subtype_none, 0, 0, c, NULL);

        /* Gravity recursive down-pass */
        c->grav.down = scheduler_addtask(s, task_type_grav_down,
                                         task_subtype_none, 0, 0, c, NULL);

        /* Implicit tasks for the up and down passes */
882
883
        c->grav.drift_out = scheduler_addtask(s, task_type_drift_gpart_out,
                                              task_subtype_none, 0, 1, c, NULL);
884
885
886
887
888
889
890
891
892
893
894
895
896
897
        c->grav.init_out = scheduler_addtask(s, task_type_init_grav_out,
                                             task_subtype_none, 0, 1, c, NULL);
        c->grav.down_in = scheduler_addtask(s, task_type_grav_down_in,
                                            task_subtype_none, 0, 1, c, NULL);

        /* Gravity mesh force propagation */
        if (periodic)
          c->grav.mesh = scheduler_addtask(s, task_type_grav_mesh,
                                           task_subtype_none, 0, 0, c, NULL);

        if (periodic) scheduler_addunlock(s, c->grav.drift, c->grav.mesh);
        if (periodic) scheduler_addunlock(s, c->grav.mesh, c->grav.down);
        scheduler_addunlock(s, c->grav.init, c->grav.long_range);
        scheduler_addunlock(s, c->grav.long_range, c->grav.down);
898
        scheduler_addunlock(s, c->grav.down, c->grav.super->grav.end_force);
899
900
901

        /* Link in the implicit tasks */
        scheduler_addunlock(s, c->grav.init, c->grav.init_out);
902
        scheduler_addunlock(s, c->grav.drift, c->grav.drift_out);
903
904
905
906
907
908
        scheduler_addunlock(s, c->grav.down_in, c->grav.down);
      }
    }
  }

  /* We are below the super-cell but not below the maximal splitting depth */
909
  else if ((c->grav.super != NULL) &&
910
           ((c->maxdepth - c->depth) >= space_subdepth_diff_grav)) {
911
912
913
914
915
916

    /* Local tasks only... */
    if (c->nodeID == e->nodeID) {

      if (is_self_gravity) {

917
        c->grav.drift_out = scheduler_addtask(s, task_type_drift_gpart_out,
918
                                              task_subtype_none, 0, 1, c, NULL);
919

920
921
922
923
924
925
926
        c->grav.init_out = scheduler_addtask(s, task_type_init_grav_out,
                                             task_subtype_none, 0, 1, c, NULL);

        c->grav.down_in = scheduler_addtask(s, task_type_grav_down_in,
                                            task_subtype_none, 0, 1, c, NULL);

        scheduler_addunlock(s, c->parent->grav.init_out, c->grav.init_out);
927
        scheduler_addunlock(s, c->parent->grav.drift_out, c->grav.drift_out);
928
929
930
931
932
933
        scheduler_addunlock(s, c->grav.down_in, c->parent->grav.down_in);
      }
    }
  }

  /* Recurse but not below the maximal splitting depth */
934
  if (c->split && ((c->maxdepth - c->depth) >= space_subdepth_diff_grav))
935
936
937
938
939
940
941
942
943
944
945
    for (int k = 0; k < 8; k++)
      if (c->progeny[k] != NULL)
        engine_make_hierarchical_tasks_gravity(e, c->progeny[k]);
}

/**
 * @brief Recursively add non-implicit ghost tasks to a cell hierarchy.
 */
void engine_add_ghosts(struct engine *e, struct cell *c, struct task *ghost_in,
                       struct task *ghost_out) {

946
947
948
  /* Abort as there are no hydro particles here? */
  if (c->hydro.count_total == 0) return;

949
  /* If we have reached the leaf OR have to few particles to play with*/
950
  if (!c->split || c->hydro.count_total < engine_max_parts_per_ghost) {
951
952
953
954
955
956
957

    /* Add the ghost task and its dependencies */
    struct scheduler *s = &e->sched;
    c->hydro.ghost =
        scheduler_addtask(s, task_type_ghost, task_subtype_none, 0, 0, c, NULL);
    scheduler_addunlock(s, ghost_in, c->hydro.ghost);
    scheduler_addunlock(s, c->hydro.ghost, ghost_out);
958

959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
  } else {
    /* Keep recursing */
    for (int k = 0; k < 8; k++)
      if (c->progeny[k] != NULL)
        engine_add_ghosts(e, c->progeny[k], ghost_in, ghost_out);
  }
}

/**
 * @brief Generate the hydro hierarchical tasks for a hierarchy of cells -
 * i.e. all the O(Npart) tasks -- hydro version
 *
 * Tasks are only created here. The dependencies will be added later on.
 *
 * Note that there is no need to recurse below the super-cell. Note also
 * that we only add tasks if the relevant particles are present in the cell.
 *
 * @param e The #engine.
 * @param c The #cell.
 */
void engine_make_hierarchical_tasks_hydro(struct engine *e, struct cell *c) {

  struct scheduler *s = &e->sched;
982
  const int with_stars = (e->policy & engine_policy_stars);
983
  const int with_feedback = (e->policy & engine_policy_feedback);
984
985
  const int with_cooling = (e->policy & engine_policy_cooling);
  const int with_star_formation = (e->policy & engine_policy_star_formation);
986
  const int with_black_holes = (e->policy & engine_policy_black_holes);
987
988
989
990
991
992
993
994

  /* Are we in a super-cell ? */
  if (c->hydro.super == c) {

    /* Add the sort task. */
    c->hydro.sorts =
        scheduler_addtask(s, task_type_sort, task_subtype_none, 0, 0, c, NULL);

995
    if (with_feedback) {
996
997
      c->stars.sorts = scheduler_addtask(s, task_type_stars_sort,
                                         task_subtype_none, 0, 0, c, NULL);
998
    }
999

1000
    if (with_black_holes) {