From ca5ff1a329a09decdce31d9b0730be6bc81ba95b Mon Sep 17 00:00:00 2001
From: loikki <loic.hausammann@protonmail.ch>
Date: Tue, 5 Feb 2019 09:44:25 +0100
Subject: [PATCH] Correct supers

---
 src/cell.c                | 20 ++++++------
 src/engine.c              |  4 +--
 src/engine_maketasks.c    | 64 +++++++++++++++++++-------------------
 src/engine_marktasks.c    | 65 +++++++++++++++------------------------
 src/runner_doiact_stars.h | 40 +++++++++++++++++-------
 src/scheduler.c           | 53 +++++++++++++++----------------
 src/space.c               | 10 ------
 src/space.h               |  2 --
 src/task.c                |  3 ++
 9 files changed, 124 insertions(+), 137 deletions(-)

diff --git a/src/cell.c b/src/cell.c
index 6f34739c02..8b60c668fc 100644
--- a/src/cell.c
+++ b/src/cell.c
@@ -2002,7 +2002,7 @@ void cell_activate_hydro_sorts(struct cell *c, int sid, struct scheduler *s) {
  */
 void cell_activate_stars_sorts_up(struct cell *c, struct scheduler *s) {
 
-  if (c == c->super) {
+  if (c == c->hydro.super) {
 #ifdef SWIFT_DEBUG_CHECKS
     if ((c->nodeID == engine_rank && c->stars.sorts_local == NULL) ||
         (c->nodeID != engine_rank && c->stars.sorts_foreign == NULL))
@@ -2023,18 +2023,18 @@ void cell_activate_stars_sorts_up(struct cell *c, struct scheduler *s) {
          parent != NULL && !parent->stars.do_sub_sort;
          parent = parent->parent) {
       parent->stars.do_sub_sort = 1;
-      if (parent == c->super) {
+      if (parent == c->hydro.super) {
 #ifdef SWIFT_DEBUG_CHECKS
-        if ((c->nodeID == engine_rank && parent->stars.sorts_local == NULL) ||
-            (c->nodeID != engine_rank && parent->stars.sorts_foreign == NULL))
+        if ((parent->nodeID == engine_rank && parent->stars.sorts_local == NULL) ||
+            (parent->nodeID != engine_rank && parent->stars.sorts_foreign == NULL))
           error("Trying to activate un-existing parents->stars.sorts");
 #endif
-        if (c->nodeID == engine_rank) {
+        if (parent->nodeID == engine_rank) {
           scheduler_activate(s, parent->stars.sorts_local);
           cell_activate_drift_part(parent, s);
           cell_activate_drift_spart(parent, s);
         }
-        if (c->nodeID != engine_rank) {
+        if (parent->nodeID != engine_rank) {
           scheduler_activate(s, parent->stars.sorts_foreign);
         }
         break;
@@ -2776,10 +2776,8 @@ void cell_activate_subcell_stars_tasks(struct cell *ci, struct cell *cj,
 
     /* Otherwise, activate the sorts and drifts. */
     else {
-      const int do_ci = cell_is_active_stars(ci, e);
-      const int do_cj = cell_is_active_stars(cj, e);
 
-      if (do_ci) {
+      if (cell_is_active_stars(ci, e)) {
         /* We are going to interact this pair, so store some values. */
         atomic_or(&cj->hydro.requires_sorts, 1 << sid);
         atomic_or(&ci->stars.requires_sorts, 1 << sid);
@@ -2796,7 +2794,7 @@ void cell_activate_subcell_stars_tasks(struct cell *ci, struct cell *cj,
         cell_activate_stars_sorts(ci, sid, s);
       }
 
-      if (do_cj) {
+      if (cell_is_active_stars(cj, e)) {
         /* We are going to interact this pair, so store some values. */
         atomic_or(&cj->stars.requires_sorts, 1 << sid);
         atomic_or(&ci->hydro.requires_sorts, 1 << sid);
@@ -3345,7 +3343,7 @@ int cell_unskip_stars_tasks(struct cell *c, struct scheduler *s) {
   struct engine *e = s->space->e;
   const int nodeID = e->nodeID;
   int rebuild = 0;
-
+  
   /* Un-skip the density tasks involved with this cell. */
   for (struct link *l = c->stars.density; l != NULL; l = l->next) {
     struct task *t = l->t;
diff --git a/src/engine.c b/src/engine.c
index 33f8de38b3..3c0895da5e 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -2684,7 +2684,8 @@ void engine_skip_force_and_kick(struct engine *e) {
         t->type == task_type_grav_long_range || t->type == task_type_grav_mm ||
         t->type == task_type_grav_down || t->type == task_type_cooling ||
         t->type == task_type_star_formation ||
-        t->type == task_type_extra_ghost || t->subtype == task_subtype_gradient)
+        t->type == task_type_extra_ghost || t->subtype == task_subtype_gradient ||
+	t->subtype == task_subtype_stars_feedback || t->type == task_type_stars_sort_foreign)
       t->skip = 1;
   }
 
@@ -2722,7 +2723,6 @@ void engine_skip_drift(struct engine *e) {
  * @param e The #engine.
  */
 void engine_launch(struct engine *e) {
-
   const ticks tic = getticks();
 
 #ifdef SWIFT_DEBUG_CHECKS
diff --git a/src/engine_maketasks.c b/src/engine_maketasks.c
index 02e3aa8cf0..feb0c999be 100644
--- a/src/engine_maketasks.c
+++ b/src/engine_maketasks.c
@@ -273,7 +273,7 @@ void engine_addtasks_send_stars(struct engine *e, struct cell *ci,
       scheduler_addunlock(s, t_feed, ci->super->end_force);
 
       /* Ghost before you send */
-      scheduler_addunlock(s, ci->super->stars.ghost_out, t_feed);
+      scheduler_addunlock(s, ci->hydro.super->stars.ghost_out, t_feed);
     }
 
     if (hydro == NULL) {
@@ -479,7 +479,7 @@ void engine_addtasks_recv_stars(struct engine *e, struct cell *c,
                                c->mpi.tag, 0, c, NULL);
 
     /* Need to sort task before feedback loop */
-    scheduler_addunlock(s, t_feed, c->super->stars.sorts_foreign);
+    scheduler_addunlock(s, t_feed, c->hydro.super->stars.sorts_foreign);
   }
 
   c->mpi.hydro.recv_xv = t_xv;
@@ -956,7 +956,7 @@ void engine_make_hierarchical_tasks_stars(struct engine *e, struct cell *c) {
   struct scheduler *s = &e->sched;
 
   /* Are we in a super-cell ? */
-  if (c->super == c) {
+  if (c->hydro.super == c) {
     /* Foreign tasks only */
     if (c->nodeID != e->nodeID) {
       c->stars.sorts_foreign = scheduler_addtask(
@@ -978,14 +978,8 @@ void engine_make_hierarchical_tasks_stars(struct engine *e, struct cell *c) {
       engine_add_stars_ghosts(e, c, c->stars.ghost_in, c->stars.ghost_out);
 
       /* Need to compute the gas density before moving to the feedback */
-      struct task *hydro_ghost = NULL;
-      if (c->hydro.super)
-	hydro_ghost = c->hydro.super->hydro.ghost_out;
-
-      if (hydro_ghost) {
-	scheduler_addunlock(s, hydro_ghost,
-			    c->super->stars.ghost_out);
-      }
+      scheduler_addunlock(s, c->hydro.super->hydro.ghost_out,
+			  c->hydro.super->stars.ghost_out);
     }
   } else { /* We are above the super-cell so need to go deeper */
 
@@ -1576,8 +1570,8 @@ static inline void engine_make_stars_loops_dependencies(struct scheduler *sched,
                                                         struct task *feedback,
                                                         struct cell *c) {
   /* density loop --> ghost --> feedback loop*/
-  scheduler_addunlock(sched, density, c->super->stars.ghost_in);
-  scheduler_addunlock(sched, c->super->stars.ghost_out, feedback);
+  scheduler_addunlock(sched, density, c->hydro.super->stars.ghost_in);
+  scheduler_addunlock(sched, c->hydro.super->stars.ghost_out, feedback);
 }
 
 /**
@@ -2046,7 +2040,7 @@ void engine_make_extra_starsloop_tasks_mapper(void *map_data, int num_elements,
 
       if (t->ci->nodeID == engine_rank) {
         scheduler_addunlock(sched, t->ci->super->grav.drift, t);
-        scheduler_addunlock(sched, t->ci->super->stars.sorts_local, t);
+        scheduler_addunlock(sched, t->ci->hydro.super->stars.sorts_local, t);
       }
 
       if (t->ci->hydro.super != t->cj->hydro.super) {
@@ -2055,11 +2049,13 @@ void engine_make_extra_starsloop_tasks_mapper(void *map_data, int num_elements,
         scheduler_addunlock(sched, t->cj->hydro.super->hydro.sorts, t);
       }
 
-      if (t->ci->super != t->cj->super) {
-        if (t->cj->nodeID == engine_rank) {
-          scheduler_addunlock(sched, t->cj->super->grav.drift, t);
-          scheduler_addunlock(sched, t->cj->super->stars.sorts_local, t);
+      if (t->cj->nodeID == engine_rank) {
+	if (t->ci->hydro.super != t->cj->hydro.super) {
+          scheduler_addunlock(sched, t->cj->hydro.super->stars.sorts_local, t);
         }
+	if (t->ci->super != t->cj->super) {
+          scheduler_addunlock(sched, t->cj->super->grav.drift, t);
+	}
       }
 
       /* Start by constructing the task for the second stars loop */
@@ -2069,11 +2065,11 @@ void engine_make_extra_starsloop_tasks_mapper(void *map_data, int num_elements,
 
       /* Add sort before feedback loop */
       if (t->ci->nodeID != engine_rank) {
-        scheduler_addunlock(sched, t->ci->super->stars.sorts_foreign, t2);
+        scheduler_addunlock(sched, t->ci->hydro.super->stars.sorts_foreign, t2);
       }
-      if (t->ci->super != t->cj->super) {
+      if (t->ci->hydro.super != t->cj->hydro.super) {
         if (t->cj->nodeID != engine_rank) {
-          scheduler_addunlock(sched, t->cj->super->stars.sorts_foreign, t2);
+          scheduler_addunlock(sched, t->cj->hydro.super->stars.sorts_foreign, t2);
         }
       }
 
@@ -2087,8 +2083,10 @@ void engine_make_extra_starsloop_tasks_mapper(void *map_data, int num_elements,
         scheduler_addunlock(sched, t2, t->ci->super->end_force);
       }
       if (t->cj->nodeID == nodeID) {
-        if (t->ci->super != t->cj->super) {
+        if (t->ci->hydro.super != t->cj->hydro.super) {
           engine_make_stars_loops_dependencies(sched, t, t2, t->cj);
+	}
+        if (t->ci->super != t->cj->super) {
           scheduler_addunlock(sched, t2, t->cj->super->end_force);
         }
       }
@@ -2103,7 +2101,7 @@ void engine_make_extra_starsloop_tasks_mapper(void *map_data, int num_elements,
       scheduler_addunlock(sched, t->ci->hydro.super->hydro.drift, t);
       scheduler_addunlock(sched, t->ci->hydro.super->hydro.sorts, t);
       scheduler_addunlock(sched, t->ci->super->grav.drift, t);
-      scheduler_addunlock(sched, t->ci->super->stars.sorts_local, t);
+      scheduler_addunlock(sched, t->ci->hydro.super->stars.sorts_local, t);
 
       /* Start by constructing the task for the second stars loop */
       struct task *t2 = scheduler_addtask(sched, task_type_sub_self,
@@ -2132,7 +2130,7 @@ void engine_make_extra_starsloop_tasks_mapper(void *map_data, int num_elements,
 
       if (t->cj->nodeID == engine_rank) {
         scheduler_addunlock(sched, t->cj->super->grav.drift, t);
-        scheduler_addunlock(sched, t->cj->super->stars.sorts_local, t);
+        scheduler_addunlock(sched, t->cj->hydro.super->stars.sorts_local, t);
       }
 
       if (t->ci->hydro.super != t->cj->hydro.super) {
@@ -2141,11 +2139,13 @@ void engine_make_extra_starsloop_tasks_mapper(void *map_data, int num_elements,
         scheduler_addunlock(sched, t->ci->hydro.super->hydro.sorts, t);
       }
 
-      if (t->ci->super != t->cj->super) {
-        if (t->ci->nodeID == engine_rank) {
+      if (t->ci->nodeID == engine_rank) {
+	if (t->ci->super != t->cj->super) {
           scheduler_addunlock(sched, t->ci->super->grav.drift, t);
-          scheduler_addunlock(sched, t->ci->super->stars.sorts_local, t);
-        }
+	}
+	if (t->ci->hydro.super != t->cj->hydro.super) {
+          scheduler_addunlock(sched, t->ci->hydro.super->stars.sorts_local, t);
+	}
       }
 
       /* Start by constructing the task for the second stars loop */
@@ -2155,11 +2155,11 @@ void engine_make_extra_starsloop_tasks_mapper(void *map_data, int num_elements,
 
       /* Add the sort before feedback */
       if (t->cj->nodeID != engine_rank) {
-        scheduler_addunlock(sched, t->cj->super->stars.sorts_foreign, t2);
+        scheduler_addunlock(sched, t->cj->hydro.super->stars.sorts_foreign, t2);
       }
-      if (t->ci->super != t->cj->super) {
+      if (t->ci->hydro.super != t->cj->hydro.super) {
         if (t->ci->nodeID != engine_rank) {
-          scheduler_addunlock(sched, t->ci->super->stars.sorts_foreign, t2);
+          scheduler_addunlock(sched, t->ci->hydro.super->stars.sorts_foreign, t2);
         }
       }
 
@@ -2173,7 +2173,7 @@ void engine_make_extra_starsloop_tasks_mapper(void *map_data, int num_elements,
         scheduler_addunlock(sched, t2, t->ci->super->end_force);
       }
       if (t->cj->nodeID == nodeID) {
-        if (t->ci->super != t->cj->super)
+        if (t->ci->hydro.super != t->cj->hydro.super)
           engine_make_stars_loops_dependencies(sched, t, t2, t->cj);
         if (t->ci->super != t->cj->super)
           scheduler_addunlock(sched, t2, t->cj->super->end_force);
diff --git a/src/engine_marktasks.c b/src/engine_marktasks.c
index fef065fa92..ce370f76e0 100644
--- a/src/engine_marktasks.c
+++ b/src/engine_marktasks.c
@@ -71,17 +71,21 @@ void engine_activate_stars_mpi(struct engine *e, struct scheduler *s,
 
     if (cj_active_stars) {
       scheduler_activate(s, ci->mpi.hydro.recv_xv);
+
+      /* If the local cell is active, more stuff will be needed. */
+      scheduler_activate_send(s, cj->mpi.stars.send, ci_nodeID);
+
+      /* If the local cell is active, send its ti_end values. */
+      scheduler_activate_send(s, cj->mpi.send_ti, ci_nodeID);
     }
+
     if (ci_active_stars) {
       scheduler_activate(s, ci->mpi.stars.recv);
-    }
-
-    /* If the foreign cell is active, we want its ti_end values. */
-    if (ci_active_stars) scheduler_activate(s, ci->mpi.recv_ti);
 
-    /* Is the foreign cell active and will need stuff from us? */
-    if (ci_active_stars) {
+      /* If the foreign cell is active, we want its ti_end values. */
+      scheduler_activate(s, ci->mpi.recv_ti);
 
+      /* Is the foreign cell active and will need stuff from us? */
       struct link *l =
           scheduler_activate_send(s, cj->mpi.hydro.send_xv, ci_nodeID);
 
@@ -90,50 +94,37 @@ void engine_activate_stars_mpi(struct engine *e, struct scheduler *s,
          itself. */
       cell_activate_drift_part(l->t->ci, s);
     }
-    /* If the local cell is active, more stuff will be needed. */
-    if (cj_active_stars) {
-      scheduler_activate_send(s, cj->mpi.stars.send, ci_nodeID);
-    }
-
-    /* If the local cell is active, send its ti_end values. */
-    if (cj_active_stars) {
-      scheduler_activate_send(s, cj->mpi.send_ti, ci_nodeID);
-    }
 
   } else if (cj_nodeID != nodeID) {
-    /* If the local cell is active, receive data from the foreign cell. */
+
     if (ci_active_stars) {
+      /* If the local cell is active, receive data from the foreign cell. */
       scheduler_activate(s, cj->mpi.hydro.recv_xv);
+
+      /* If the local cell is active, more stuff will be needed. */
+      scheduler_activate_send(s, ci->mpi.stars.send, cj_nodeID);
+
+      /* If the local cell is active, send its ti_end values. */
+      scheduler_activate_send(s, ci->mpi.send_ti, cj_nodeID);
     }
     if (cj_active_stars) {
       scheduler_activate(s, cj->mpi.stars.recv);
-    }
 
-    /* If the foreign cell is active, we want its ti_end values. */
-    if (cj_active_stars) scheduler_activate(s, cj->mpi.recv_ti);
-
-    /* Is the foreign cell active and will need stuff from us? */
-    if (cj_active_stars) {
+      /* If the foreign cell is active, we want its ti_end values. */
+      scheduler_activate(s, cj->mpi.recv_ti);
 
       struct link *l =
-          scheduler_activate_send(s, ci->mpi.hydro.send_xv, cj_nodeID);
+	scheduler_activate_send(s, ci->mpi.hydro.send_xv, cj_nodeID);
 
       /* 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 active, more stuff will be needed. */
-    if (ci_active_stars) {
-      scheduler_activate_send(s, ci->mpi.stars.send, cj_nodeID);
-    }
 
-    /* If the local cell is active, send its ti_end values. */
-    if (ci_active_stars) {
-      scheduler_activate_send(s, ci->mpi.send_ti, cj_nodeID);
     }
   }
 }
+
 #endif
 
 /**
@@ -341,15 +332,9 @@ void engine_marktasks_mapper(void *map_data, int num_elements,
       }
 
       /* Stars density and feedback */
-      const int stars_density =
-	t_subtype == task_subtype_stars_density;
-
-      const int stars_feedback =
-	t_subtype == task_subtype_stars_feedback;
-
-      const int active = ci_active_stars || cj_active_stars;
-
-      if ((stars_density || stars_feedback) && active) {
+      if ((t_subtype == task_subtype_stars_density ||
+	   t_subtype == task_subtype_stars_feedback) &&
+	  (ci_active_stars || cj_active_stars)) {
 
         scheduler_activate(s, t);
 
diff --git a/src/runner_doiact_stars.h b/src/runner_doiact_stars.h
index 8d8be307bb..24c920e06c 100644
--- a/src/runner_doiact_stars.h
+++ b/src/runner_doiact_stars.h
@@ -1385,14 +1385,34 @@ void DOSUB_PAIR1_STARS(struct runner *r, struct cell *ci, struct cell *cj,
       /* Do any of the cells need to be sorted first? */
       if (!(ci->stars.sorted & (1 << sid)) ||
           ci->stars.dx_max_sort_old > ci->dmin * space_maxreldx) {
-	cell_activate_stars_sorts(ci, sid, &r->e->sched);
-        error("Interacting unsorted cell (sparts). %p %p %i %i %i %i %i %i %i %i %i %i", ci->stars.density, cj->stars.density, ci->stars.sorted, 1 << sid, cj->nodeID, ci->nodeID, ci->super->stars.sorts_foreign->skip,
-	      ci->stars.count, ci->hydro.count, cj->stars.count, cj->hydro.count, ci->stars.do_sort);
+	message("%i %i %i %i %p %p", ci->cellID, cj->cellID,
+		ci->nodeID, cj->nodeID, ci->hydro.super, ci->super);
+	if (ci->hydro.super) {
+	  message("%p", ci->hydro.super->stars.sorts_foreign);
+	  if (ci->hydro.super->stars.sorts_foreign) {
+	    for (struct cell *parent = ci->parent;
+		 parent != NULL;
+		 parent = parent->parent) {
+	      message("%i", parent->stars.do_sub_sort);
+	      if (parent == ci->hydro.super)
+		break;
+	    }
+	    message("%i", ci->hydro.super->stars.sorts_foreign->skip);
+	    message("%i, %i", ci->hydro.super->stars.sorts_foreign->skip,
+		    ci->hydro.super->stars.do_sub_sort);
+	    message("%i %i", ci->stars.sorted, ci->hydro.super->stars.sorted);
+	    message("%p", ci->stars.sort[sid]);
+	    message("%p %p", ci->stars.density, ci->hydro.super->stars.density);
+	    message("%p %p", ci->stars.feedback, ci->hydro.super->stars.feedback);
+	  }
+	}
+	message("%i", sid);
+        error("Interacting unsorted cell (sparts).");
       }
 
       if (!(cj->hydro.sorted & (1 << sid)) ||
           cj->hydro.dx_max_sort_old > cj->dmin * space_maxreldx)
-        error("Interacting unsorted cell (parts). %i", cj->nodeID);
+        error("Interacting unsorted cell (parts).");
     }
 
     if (do_cj) {
@@ -1407,16 +1427,14 @@ void DOSUB_PAIR1_STARS(struct runner *r, struct cell *ci, struct cell *cj,
       /* Do any of the cells need to be sorted first? */
       if (!(ci->hydro.sorted & (1 << sid)) ||
           ci->hydro.dx_max_sort_old > ci->dmin * space_maxreldx) {
-	message("%i %i: %g %g %g", ci->hydro.sorted, 1 << sid, ci->hydro.dx_max_sort_old, ci->dmin, space_maxreldx);
-	message("%p %p", ci->super, cj->super);
-        error("Interacting unsorted cell (parts). %i %i sorts=%i %i %i %i %i %p", ci->nodeID, cj->nodeID, ci->super->hydro.sorts->skip, ci->hydro.count, ci->stars.count, cj->hydro.count,
-	      cj->stars.count, ci->super->stars.density);
+        error("Interacting unsorted cell (parts).");
       }
 
       if (!(cj->stars.sorted & (1 << sid)) ||
-          cj->stars.dx_max_sort_old > cj->dmin * space_maxreldx)
-        error("Interacting unsorted cell (sparts). %i %i %i %i %i %i %i", cj->nodeID, ci->nodeID, ci->stars.sorts_foreign->skip,
-	      ci->stars.count, ci->hydro.count, cj->stars.count, cj->hydro.count);
+          cj->stars.dx_max_sort_old > cj->dmin * space_maxreldx) {
+	message("%i %i %p %p", cj->cellID, ci->cellID, cj->hydro.super, cj->super);
+        error("Interacting unsorted cell (sparts).");
+      }
     }
 
     if (do_ci || do_cj) DOPAIR1_BRANCH_STARS(r, ci, cj);
diff --git a/src/scheduler.c b/src/scheduler.c
index bea084d9d6..ea0f829e67 100644
--- a/src/scheduler.c
+++ b/src/scheduler.c
@@ -1031,16 +1031,15 @@ static void scheduler_splittask_stars(struct task *t, struct scheduler *s) {
 
     /* Empty task? */
     /* Need defines in order to evaluate after check for t->ci == NULL */
-#define pair_no_part                                          \
-  t->type == task_type_pair &&                                \
-      (t->ci->stars.count == 0 || t->cj->hydro.count == 0) && \
-      (t->cj->stars.count == 0 || t->ci->hydro.count == 0)
-#define self_no_part           \
-  t->type == task_type_self && \
-      (t->ci->stars.count == 0 || t->ci->hydro.count == 0)
-
-    if ((t->ci == NULL) || (t->type == task_type_pair && t->cj == NULL) ||
-        (self_no_part) || (pair_no_part)) {
+    const int self = (t->ci != NULL) && t->type == task_type_self &&
+      t->ci->stars.count != 0 && t->ci->hydro.count != 0;
+
+    const int pair = (t->ci != NULL) && (t->cj != NULL) &&
+      t->type == task_type_pair &&
+      ((t->ci->stars.count != 0 && t->cj->hydro.count != 0) ||
+       (t->cj->stars.count != 0 && t->ci->hydro.count != 0));
+ 
+    if (!self && !pair) {
       t->type = task_type_none;
       t->subtype = task_subtype_none;
       t->cj = NULL;
@@ -1064,7 +1063,7 @@ static void scheduler_splittask_stars(struct task *t, struct scheduler *s) {
       if (cell_can_split_self_stars_task(ci)) {
 
         /* Make a sub? */
-        if (scheduler_dosub && ci->stars.count < space_subsize_self_stars) {
+        if (scheduler_dosub && ci->hydro.count < space_subsize_self_hydro) {
 
           /* convert to a self-subtask. */
           t->type = task_type_sub_self;
@@ -1080,7 +1079,8 @@ static void scheduler_splittask_stars(struct task *t, struct scheduler *s) {
           while (ci->progeny[first_child] == NULL) first_child++;
           t->ci = ci->progeny[first_child];
           for (int k = first_child + 1; k < 8; k++)
-            if (ci->progeny[k] != NULL && ci->progeny[k]->stars.count)
+            if (ci->progeny[k] != NULL && ci->progeny[k]->stars.count != 0 &&
+		ci->progeny[k]->hydro.count != 0)
               scheduler_splittask_stars(
                   scheduler_addtask(s, task_type_self, t->subtype, 0, 0,
                                     ci->progeny[k], NULL),
@@ -1088,11 +1088,11 @@ static void scheduler_splittask_stars(struct task *t, struct scheduler *s) {
 
           /* Make a task for each pair of progeny */
           for (int j = 0; j < 8; j++)
-            if (ci->progeny[j] != NULL &&
-                (ci->progeny[j]->stars.count || ci->progeny[j]->hydro.count))
+            if (ci->progeny[j] != NULL)
               for (int k = j + 1; k < 8; k++)
-                if (ci->progeny[k] != NULL && (ci->progeny[k]->stars.count ||
-                                               ci->progeny[k]->hydro.count))
+                if (ci->progeny[k] != NULL &&
+		    ((ci->progeny[k]->stars.count != 0 && ci->progeny[j]->hydro.count != 0) ||
+		     (ci->progeny[j]->stars.count != 0 && ci->progeny[k]->hydro.count != 0)))
                   scheduler_splittask_stars(
                       scheduler_addtask(s, task_type_pair, t->subtype,
                                         sub_sid_flag[j][k], 0, ci->progeny[j],
@@ -1127,24 +1127,19 @@ static void scheduler_splittask_stars(struct task *t, struct scheduler *s) {
               t->flags);
 #endif
 
-      /* Compute number of interactions */
-      const int ci_interaction = (ci->stars.count * cj->hydro.count);
-      const int cj_interaction = (cj->stars.count * ci->hydro.count);
-
-      const int number_interactions = (ci_interaction + cj_interaction);
-
       /* Should this task be split-up? */
       if (cell_can_split_pair_stars_task(ci, cj) &&
-          cell_can_split_pair_stars_task(ci, cj)) {
+          cell_can_split_pair_stars_task(cj, ci)) {
 
         /* Replace by a single sub-task? */
-        if (scheduler_dosub &&
-            number_interactions * sid_scale[sid] < space_subsize_pair_stars &&
-            !sort_is_corner(sid)) {
+	if (scheduler_dosub && /* Use division to avoid integer overflow. */
+	    ci->hydro.count * sid_scale[sid] <
+	    space_subsize_pair_hydro / cj->hydro.count &&
+	    !sort_is_corner(sid)) {
 
-          /* Make this task a sub task. */
+	  /* Make this task a sub task. */
           t->type = task_type_sub_pair;
-
+	
           /* Otherwise, split it. */
         } else {
           /* Take a step back (we're going to recycle the current task)... */
@@ -1487,7 +1482,7 @@ static void scheduler_splittask_stars(struct task *t, struct scheduler *s) {
 
         /* Otherwise, break it up if it is too large? */
       } else if (scheduler_doforcesplit && ci->split && cj->split &&
-                 (number_interactions > space_maxsize)) {
+                 (ci->hydro.count > space_maxsize / cj->hydro.count)) {
 
         /* Replace the current task. */
         t->type = task_type_none;
diff --git a/src/space.c b/src/space.c
index 4568f697c7..9068c5da60 100644
--- a/src/space.c
+++ b/src/space.c
@@ -67,8 +67,6 @@ int space_subsize_pair_hydro = space_subsize_pair_hydro_default;
 int space_subsize_self_hydro = space_subsize_self_hydro_default;
 int space_subsize_pair_grav = space_subsize_pair_grav_default;
 int space_subsize_self_grav = space_subsize_self_grav_default;
-int space_subsize_pair_stars = space_subsize_pair_stars_default;
-int space_subsize_self_stars = space_subsize_self_stars_default;
 int space_subdepth_diff_grav = space_subdepth_diff_grav_default;
 int space_maxsize = space_maxsize_default;
 
@@ -3811,12 +3809,6 @@ void space_init(struct space *s, struct swift_params *params,
   space_subsize_self_grav =
       parser_get_opt_param_int(params, "Scheduler:cell_sub_size_self_grav",
                                space_subsize_self_grav_default);
-  space_subsize_pair_stars =
-      parser_get_opt_param_int(params, "Scheduler:cell_sub_size_pair_stars",
-                               space_subsize_pair_stars_default);
-  space_subsize_self_stars =
-      parser_get_opt_param_int(params, "Scheduler:cell_sub_size_self_stars",
-                               space_subsize_self_stars_default);
   space_splitsize = parser_get_opt_param_int(
       params, "Scheduler:cell_split_size", space_splitsize_default);
   space_subdepth_diff_grav =
@@ -3837,8 +3829,6 @@ void space_init(struct space *s, struct swift_params *params,
             space_subsize_pair_hydro, space_subsize_self_hydro);
     message("sub_size_pair_grav set to %d, sub_size_self_grav set to %d",
             space_subsize_pair_grav, space_subsize_self_grav);
-    message("sub_size_pair_stars set to %d, sub_size_self_stars set to %d",
-            space_subsize_pair_stars, space_subsize_self_stars);
   }
 
   /* Apply h scaling */
diff --git a/src/space.h b/src/space.h
index 98ab252366..564a1d0992 100644
--- a/src/space.h
+++ b/src/space.h
@@ -53,8 +53,6 @@ struct cosmology;
 #define space_subsize_self_hydro_default 32000
 #define space_subsize_pair_grav_default 256000000
 #define space_subsize_self_grav_default 32000
-#define space_subsize_pair_stars_default 256000000
-#define space_subsize_self_stars_default 32000
 #define space_subdepth_diff_grav_default 4
 #define space_max_top_level_cells_default 12
 #define space_stretch 1.10f
diff --git a/src/task.c b/src/task.c
index a2768d4b48..642fb870a2 100644
--- a/src/task.c
+++ b/src/task.c
@@ -305,6 +305,9 @@ float task_overlap(const struct task *restrict ta,
     if (tb->ci != NULL) size_union += tb->ci->stars.count;
     if (tb->cj != NULL) size_union += tb->cj->stars.count;
 
+    if (size_union == 0)
+      return 0.f;
+
     /* Compute the intersection of the cell data. */
     const size_t size_intersect = task_cell_overlap_spart(ta->ci, tb->ci) +
                                   task_cell_overlap_spart(ta->ci, tb->cj) +
-- 
GitLab