From 0eb2abd80a46b47a8b62e6a20028587d3c6218bb Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <matthieu.schaller@durham.ac.uk>
Date: Thu, 7 Apr 2016 23:19:39 +0100
Subject: [PATCH] Separate the creation and the linking of the gravity tasks

---
 src/engine.c | 34 ++++++++++++++++++++--------------
 1 file changed, 20 insertions(+), 14 deletions(-)

diff --git a/src/engine.c b/src/engine.c
index e99254d94d..5b85238cec 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -105,6 +105,9 @@ void engine_make_ghost_tasks(struct engine *e, struct cell *c,
                              struct cell *super) {
 
   struct scheduler *s = &e->sched;
+  const int is_with_external_gravity =
+      (e->policy & engine_policy_external_gravity) ==
+      engine_policy_external_gravity;
 
   /* Am I the super-cell? */
   if (super == NULL && (c->count > 0 || c->gcount > 0)) {
@@ -115,12 +118,6 @@ void engine_make_ghost_tasks(struct engine *e, struct cell *c,
     /* Local tasks only... */
     if (c->nodeID == e->nodeID) {
 
-      if (c->count > 0) {
-        /* Generate the ghost task. */
-        c->ghost = scheduler_addtask(s, task_type_ghost, task_subtype_none, 0,
-                                     0, c, NULL, 0);
-      }
-
       /* Add the init task. */
       c->init = scheduler_addtask(s, task_type_init, task_subtype_none, 0, 0, c,
                                   NULL, 0);
@@ -133,16 +130,19 @@ void engine_make_ghost_tasks(struct engine *e, struct cell *c,
       c->kick = scheduler_addtask(s, task_type_kick, task_subtype_none, 0, 0, c,
                                   NULL, 0);
 
-      if (c->gcount > 0) {
-        /* Add the gravity tasks */
-        c->grav_external = scheduler_addtask(
-            s, task_type_grav_external, task_subtype_none, 0, 0, c, NULL, 0);
+      if (c->count > 0) {
 
-        /* Enforce gravity calculated before kick  */
-        scheduler_addunlock(s, c->grav_external, c->kick);
+        /* Generate the ghost task. */
+        c->ghost = scheduler_addtask(s, task_type_ghost, task_subtype_none, 0,
+                                     0, c, NULL, 0);
+      }
 
-        /* Enforce gravity calculated after init */
-        scheduler_addunlock(s, c->init, c->grav_external);
+      if (c->gcount > 0) {
+
+        /* Add the external gravity tasks */
+        if (is_with_external_gravity)
+          c->grav_external = scheduler_addtask(
+              s, task_type_grav_external, task_subtype_none, 0, 0, c, NULL, 0);
       }
     }
   }
@@ -1297,6 +1297,12 @@ void engine_make_extra_hydroloop_tasks(struct engine *e) {
     /* /\* Kick tasks should rely on the grav_down tasks of their cell. *\/ */
     /* else if (t->type == task_type_kick && t->ci->grav_down != NULL) */
     /*   scheduler_addunlock(sched, t->ci->grav_down, t); */
+
+    /* External gravity tasks should depend on init and unlock the kick */
+    else if (t->type == task_type_grav_external) {
+      scheduler_addunlock(sched, t->ci->init, t);
+      scheduler_addunlock(sched, t, t->ci->kick);
+    }
   }
 }
 
-- 
GitLab