From 085a9886b37b0c68a788b05e7403b2bcb95c4fe0 Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <matthieu.schaller@durham.ac.uk>
Date: Mon, 4 Dec 2017 22:41:57 +0000
Subject: [PATCH] Correct dependencies for the top-level FFT task and its
 ghosts

---
 src/cell.h      |  2 +-
 src/engine.c    | 18 +++++++++---------
 src/scheduler.c |  6 ++++--
 src/space.c     |  4 ++--
 src/task.c      |  2 +-
 src/task.h      |  3 ++-
 6 files changed, 19 insertions(+), 16 deletions(-)

diff --git a/src/cell.h b/src/cell.h
index f4921d3dc5..ab131148db 100644
--- a/src/cell.h
+++ b/src/cell.h
@@ -227,7 +227,7 @@ struct cell {
   struct task *timestep;
 
   /*! Task linking the FFT mesh to the rest of gravity tasks */
-  struct task *grav_ghost[2];
+  struct task *grav_ghost_in, *grav_ghost_out;
 
   /*! Task computing long range non-periodic gravity interactions */
   struct task *grav_long_range;
diff --git a/src/engine.c b/src/engine.c
index 1133924ab7..eb395e0a2d 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -232,8 +232,8 @@ void engine_make_hierarchical_tasks(struct engine *e, struct cell *c) {
         c->grav_down = scheduler_addtask(s, task_type_grav_down,
                                          task_subtype_none, 0, 0, c, NULL);
 
-        if (periodic) scheduler_addunlock(s, c->init_grav, c->grav_ghost[0]);
-        if (periodic) scheduler_addunlock(s, c->grav_ghost[0], c->grav_down);
+        if (periodic) scheduler_addunlock(s, c->init_grav, c->grav_ghost_in);
+        if (periodic) scheduler_addunlock(s, c->grav_ghost_out, c->grav_down);
         scheduler_addunlock(s, c->init_grav, c->grav_long_range);
         scheduler_addunlock(s, c->grav_long_range, c->grav_down);
         scheduler_addunlock(s, c->grav_down, c->kick2);
@@ -2150,8 +2150,8 @@ void engine_make_self_gravity_tasks_mapper(void *map_data, int num_elements,
     const int ghost_id = cell_getid(cdim_ghost, i / 4, j / 4, k / 4);
     if (ghost_id > n_ghosts) error("Invalid ghost_id");
     if (periodic) {
-      ci->grav_ghost[0] = ghosts[2 * ghost_id + 0];
-      ci->grav_ghost[1] = ghosts[2 * ghost_id + 1];
+      ci->grav_ghost_in = ghosts[2 * ghost_id + 0];
+      ci->grav_ghost_out = ghosts[2 * ghost_id + 1];
     }
 
     /* Recover the multipole information */
@@ -2236,9 +2236,9 @@ void engine_make_self_gravity_tasks(struct engine *e) {
     /* Make the ghosts implicit and add the dependencies */
     for (int n = 0; n < n_ghosts / 2; ++n) {
       ghosts[2 * n + 0] = scheduler_addtask(
-          sched, task_type_grav_ghost, task_subtype_none, 0, 1, NULL, NULL);
+          sched, task_type_grav_ghost_in, task_subtype_none, 0, 1, NULL, NULL);
       ghosts[2 * n + 1] = scheduler_addtask(
-          sched, task_type_grav_ghost, task_subtype_none, 0, 1, NULL, NULL);
+          sched, task_type_grav_ghost_out, task_subtype_none, 0, 1, NULL, NULL);
       scheduler_addunlock(sched, ghosts[2 * n + 0], s->grav_top_level);
       scheduler_addunlock(sched, s->grav_top_level, ghosts[2 * n + 1]);
     }
@@ -2254,10 +2254,10 @@ void engine_make_self_gravity_tasks(struct engine *e) {
     for (int i = 0; i < s->nr_cells; ++i) {
       const struct cell *c = &s->cells_top[i];
       if (c->nodeID == engine_rank &&
-          (c->grav_ghost[0] == NULL || c->grav_ghost[0] == NULL))
+          (c->grav_ghost_in == NULL || c->grav_ghost_out == NULL))
         error("Invalid gravity_ghost for local cell");
       if (c->nodeID != engine_rank &&
-          (c->grav_ghost[0] != NULL || c->grav_ghost[0] != NULL))
+          (c->grav_ghost_in != NULL || c->grav_ghost_out != NULL))
         error("Invalid gravity_ghost for foreign cell");
     }
 #endif
@@ -3900,7 +3900,7 @@ void engine_skip_force_and_kick(struct engine *e) {
         t->type == task_type_timestep || t->subtype == task_subtype_force ||
         t->subtype == task_subtype_grav ||
         t->type == task_type_grav_long_range ||
-        t->type == task_type_grav_ghost ||
+        t->type == task_type_grav_ghost_in || t->type == task_type_grav_ghost_out ||
         t->type == task_type_grav_top_level || t->type == task_type_grav_down ||
         t->type == task_type_cooling || t->type == task_type_sourceterms)
       t->skip = 1;
diff --git a/src/scheduler.c b/src/scheduler.c
index e4f4710fd7..eabc3a5139 100644
--- a/src/scheduler.c
+++ b/src/scheduler.c
@@ -826,7 +826,8 @@ void scheduler_splittasks_mapper(void *map_data, int num_elements,
     } else if (t->subtype == task_subtype_grav) {
       scheduler_splittask_gravity(t, s);
     } else if (t->type == task_type_grav_top_level ||
-               t->type == task_type_grav_ghost) {
+               t->type == task_type_grav_ghost_in || 
+	       t->type == task_type_grav_ghost_out) {
       /* For future use */
     } else {
       error("Unexpected task sub-type");
@@ -1321,7 +1322,8 @@ void scheduler_start(struct scheduler *s) {
 
       /* Don't check the FFT task */
       if (t->type == task_type_grav_top_level ||
-          t->type == task_type_grav_ghost)
+          t->type == task_type_grav_ghost_in ||
+	  t->type == task_type_grav_ghost_out)
         continue;
 
       if (ci == NULL && cj == NULL) {
diff --git a/src/space.c b/src/space.c
index 7e7f32bb22..b4f2eb0ad8 100644
--- a/src/space.c
+++ b/src/space.c
@@ -228,8 +228,8 @@ void space_rebuild_recycle_mapper(void *map_data, int num_elements,
     c->drift_gpart = NULL;
     c->cooling = NULL;
     c->sourceterms = NULL;
-    c->grav_ghost[0] = NULL;
-    c->grav_ghost[1] = NULL;
+    c->grav_ghost_in = NULL;
+    c->grav_ghost_out = NULL;
     c->grav_long_range = NULL;
     c->grav_down = NULL;
     c->super = c;
diff --git a/src/task.c b/src/task.c
index 43da1d3568..a00693467c 100644
--- a/src/task.c
+++ b/src/task.c
@@ -54,7 +54,7 @@ const char *taskID_names[task_type_count] = {
     "drift_part", "drift_gpart",    "kick1",
     "kick2",      "timestep",       "send",
     "recv",       "grav_top_level", "grav_long_range",
-    "grav_ghost", "grav_mm",        "grav_down",
+    "grav_ghost_in", "grav_ghost_out", "grav_mm",        "grav_down",
     "cooling",    "sourceterms"};
 
 /* Sub-task type names. */
diff --git a/src/task.h b/src/task.h
index dee888c9f1..644ccdd74f 100644
--- a/src/task.h
+++ b/src/task.h
@@ -57,7 +57,8 @@ enum task_types {
   task_type_recv,
   task_type_grav_top_level,
   task_type_grav_long_range,
-  task_type_grav_ghost,
+  task_type_grav_ghost_in,
+  task_type_grav_ghost_out,
   task_type_grav_mm,
   task_type_grav_down,
   task_type_cooling,
-- 
GitLab