From 3da8de63bdcf7f2ab828c8f59d01caa64333681d Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <matthieu.schaller@durham.ac.uk>
Date: Fri, 6 Oct 2017 16:08:19 +0100
Subject: [PATCH] Instead of listing local cells only, list cells that have a
 tasks associated with them on the local node.

---
 src/cell.c   | 18 ++++++++++++++++++
 src/cell.h   |  1 +
 src/engine.c |  3 +++
 src/space.c  | 23 +++++++++++++----------
 src/space.h  |  1 +
 5 files changed, 36 insertions(+), 10 deletions(-)

diff --git a/src/cell.c b/src/cell.c
index 006df3e9cf..70e0b54b68 100644
--- a/src/cell.c
+++ b/src/cell.c
@@ -2071,6 +2071,24 @@ void cell_set_super_mapper(void *map_data, int num_elements, void *extra_data) {
   }
 }
 
+int cell_has_tasks(struct cell *c) {
+
+#ifdef WITH_MPI
+  if (c->timestep != NULL || c->recv_ti != NULL) return 1;
+#else
+  if (c->timestep != NULL) return 1;
+#endif /* WITH_MPI */
+
+  if (c->split) {
+    int count = 0;
+    for (int k = 0; k < 8; ++k)
+      if (c->progeny[k] != NULL) count += cell_has_tasks(c->progeny[k]);
+    return count;
+  } else {
+    return 0;
+  }
+}
+
 /**
  * @brief Recursively drifts the #part in a cell hierarchy.
  *
diff --git a/src/cell.h b/src/cell.h
index efc90e9004..8b62e43620 100644
--- a/src/cell.h
+++ b/src/cell.h
@@ -462,6 +462,7 @@ void cell_activate_drift_gpart(struct cell *c, struct scheduler *s);
 void cell_activate_sorts(struct cell *c, int sid, struct scheduler *s);
 void cell_clear_drift_flags(struct cell *c, void *data);
 void cell_set_super_mapper(void *map_data, int num_elements, void *extra_data);
+int cell_has_tasks(struct cell *c);
 
 /* Inlined functions (for speed). */
 
diff --git a/src/engine.c b/src/engine.c
index f28739dc89..c65757e4ea 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -3070,6 +3070,9 @@ void engine_rebuild(struct engine *e, int clean_h_values) {
   /* Re-build the tasks. */
   engine_maketasks(e);
 
+  /* Make the list of top-level cells that have tasks */
+  space_list_cells_with_tasks(e->s);
+
 #ifdef SWIFT_DEBUG_CHECKS
   /* Check that all cells have been drifted to the current time.
    * That can include cells that have not
diff --git a/src/space.c b/src/space.c
index c914b6c2e6..502a691259 100644
--- a/src/space.c
+++ b/src/space.c
@@ -503,16 +503,6 @@ void space_regrid(struct space *s, int verbose) {
     }
 #endif /* WITH_MPI */
 
-    /* Let's rebuild the list of local top-level cells */
-    s->nr_local_cells = 0;
-    for (int i = 0; i < s->nr_cells; ++i)
-      if (s->cells_top[i].nodeID == s->nodeID) {
-        s->local_cells_top[s->nr_local_cells] = i;
-        ++s->nr_local_cells;
-      }
-    if (verbose)
-      message("Have %d local cells (total=%d)", s->nr_local_cells, s->nr_cells);
-
     // message( "rebuilding upper-level cells took %.3f %s." ,
     // clocks_from_ticks(double)(getticks() - tic), clocks_getunit());
 
@@ -2483,6 +2473,19 @@ void space_getcells(struct space *s, int nr_cells, struct cell **cells) {
   }
 }
 
+void space_list_cells_with_tasks(struct space *s) {
+
+  /* Let's rebuild the list of local top-level cells */
+  s->nr_local_cells = 0;
+  for (int i = 0; i < s->nr_cells; ++i)
+    if (cell_has_tasks(&s->cells_top[i])) {
+      s->local_cells_top[s->nr_local_cells] = i;
+      ++s->nr_local_cells;
+    }
+  if (s->e->verbose)
+    message("Have %d local cells (total=%d)", s->nr_local_cells, s->nr_cells);
+}
+
 void space_synchronize_particle_positions_mapper(void *map_data, int nr_gparts,
                                                  void *extra_data) {
   /* Unpack the data */
diff --git a/src/space.h b/src/space.h
index 0b1e8248b5..8307858d6e 100644
--- a/src/space.h
+++ b/src/space.h
@@ -213,6 +213,7 @@ void space_recycle_list(struct space *s, struct cell *cell_list_begin,
 void space_split(struct space *s, struct cell *cells, int nr_cells,
                  int verbose);
 void space_split_mapper(void *map_data, int num_elements, void *extra_data);
+void space_list_cells_with_tasks(struct space *s);
 void space_parts_get_cell_index(struct space *s, int *ind, struct cell *cells,
                                 int verbose);
 void space_gparts_get_cell_index(struct space *s, int *gind, struct cell *cells,
-- 
GitLab