From cbdd02930f1ca3320c00f73c290dc1517db7d53b Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <schaller@strw.leidenuniv.nl>
Date: Mon, 5 Nov 2018 13:10:03 +0100
Subject: [PATCH] Replace the absolute depth criterion for gravity tasks with a
 relative criterion based on the local depth of the tree.

---
 examples/parameter_example.yml | 6 +++---
 src/cell.h                     | 8 ++++----
 src/engine_maketasks.c         | 5 +++--
 src/runner.c                   | 2 +-
 src/space.c                    | 9 +++++----
 src/space.h                    | 4 ++--
 6 files changed, 18 insertions(+), 16 deletions(-)

diff --git a/examples/parameter_example.yml b/examples/parameter_example.yml
index 63a61af3e7..9ae5fef665 100644
--- a/examples/parameter_example.yml
+++ b/examples/parameter_example.yml
@@ -59,10 +59,10 @@ Scheduler:
   cell_sub_size_self_hydro:  32000     # (Optional) Maximal number of interactions per sub-self hydro task  (this is the default value).
   cell_sub_size_pair_grav:   256000000 # (Optional) Maximal number of interactions per sub-pair gravity task  (this is the default value).
   cell_sub_size_self_grav:   32000     # (Optional) Maximal number of interactions per sub-self gravity task  (this is the default value).
-  cell_sub_size_pair_stars:   256000000 # (Optional) Maximal number of interactions per sub-pair stars task  (this is the default value).
-  cell_sub_size_self_stars:   32000     # (Optional) Maximal number of interactions per sub-self stars task  (this is the default value).
+  cell_sub_size_pair_stars:  256000000 # (Optional) Maximal number of interactions per sub-pair stars task  (this is the default value).
+  cell_sub_size_self_stars:  32000     # (Optional) Maximal number of interactions per sub-self stars task  (this is the default value).
   cell_split_size:           400       # (Optional) Maximal number of particles per cell (this is the default value).
-  cell_subdepth_grav:        2         # (Optional) Maximal depth the gravity tasks can be pushed down (this is the default value).
+  cell_subdepth_diff_grav:   4         # (Optional) Maximal depth difference between leaves and a cell that gravity tasks can be pushed down to (this is the default value).
   max_top_level_cells:       12        # (Optional) Maximal number of top-level cells in any dimension. The number of top-level cells will be the cube of this (this is the default value).
   tasks_per_cell:            0         # (Optional) The average number of tasks per cell. If not large enough the simulation will fail (means guess...).
   mpi_message_limit:         4096      # (Optional) Maximum MPI task message size to send non-buffered, KB.
diff --git a/src/cell.h b/src/cell.h
index 89f7c954c2..6203c0ec5a 100644
--- a/src/cell.h
+++ b/src/cell.h
@@ -808,8 +808,8 @@ __attribute__((always_inline)) INLINE static int cell_can_split_self_stars_task(
 __attribute__((always_inline)) INLINE static int
 cell_can_split_pair_gravity_task(const struct cell *c) {
 
-  /* Is the cell split ? */
-  return c->split && c->depth < space_subdepth_grav;
+  /* Is the cell split and still far from the leaves ? */
+  return c->split && ((c->maxdepth - c->depth) > space_subdepth_diff_grav);
 }
 
 /**
@@ -821,8 +821,8 @@ cell_can_split_pair_gravity_task(const struct cell *c) {
 __attribute__((always_inline)) INLINE static int
 cell_can_split_self_gravity_task(const struct cell *c) {
 
-  /* Is the cell split ? */
-  return c->split && c->depth < space_subdepth_grav;
+  /* Is the cell split and still far from the leaves ? */
+  return c->split && ((c->maxdepth - c->depth) > space_subdepth_diff_grav);
 }
 
 /**
diff --git a/src/engine_maketasks.c b/src/engine_maketasks.c
index e4431a8cf1..270b1d3868 100644
--- a/src/engine_maketasks.c
+++ b/src/engine_maketasks.c
@@ -568,7 +568,8 @@ void engine_make_hierarchical_tasks_gravity(struct engine *e, struct cell *c) {
   }
 
   /* We are below the super-cell but not below the maximal splitting depth */
-  else if (c->grav.super != NULL && c->depth < space_subdepth_grav) {
+  else if ((c->grav.super != NULL) &&
+           ((c->maxdepth - c->depth) >= space_subdepth_diff_grav)) {
 
     /* Local tasks only... */
     if (c->nodeID == e->nodeID) {
@@ -588,7 +589,7 @@ void engine_make_hierarchical_tasks_gravity(struct engine *e, struct cell *c) {
   }
 
   /* Recurse but not below the maximal splitting depth */
-  if (c->split && c->depth <= space_subdepth_grav)
+  if (c->split && ((c->maxdepth - c->depth) >= space_subdepth_diff_grav))
     for (int k = 0; k < 8; k++)
       if (c->progeny[k] != NULL)
         engine_make_hierarchical_tasks_gravity(e, c->progeny[k]);
diff --git a/src/runner.c b/src/runner.c
index cf5492dad4..0026d833b9 100644
--- a/src/runner.c
+++ b/src/runner.c
@@ -1353,7 +1353,7 @@ static void runner_do_unskip_gravity(struct cell *c, struct engine *e) {
   if (!cell_is_active_gravity(c, e)) return;
 
   /* Recurse */
-  if (c->split && c->depth < space_subdepth_grav) {
+  if (c->split && ((c->maxdepth - c->depth) >= space_subdepth_diff_grav)) {
     for (int k = 0; k < 8; k++) {
       if (c->progeny[k] != NULL) {
         struct cell *cp = c->progeny[k];
diff --git a/src/space.c b/src/space.c
index bda2f48825..32b7718c24 100644
--- a/src/space.c
+++ b/src/space.c
@@ -68,7 +68,7 @@ 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_grav = space_subdepth_grav_default;
+int space_subdepth_diff_grav = space_subdepth_diff_grav_default;
 int space_maxsize = space_maxsize_default;
 #ifdef SWIFT_DEBUG_CHECKS
 int last_cell_id;
@@ -3159,13 +3159,14 @@ void space_init(struct space *s, struct swift_params *params,
                                space_subsize_self_stars_default);
   space_splitsize = parser_get_opt_param_int(
       params, "Scheduler:cell_split_size", space_splitsize_default);
-  space_subdepth_grav = parser_get_opt_param_int(
-      params, "Scheduler:cell_subdepth_grav", space_subdepth_grav_default);
+  space_subdepth_diff_grav =
+      parser_get_opt_param_int(params, "Scheduler:cell_subdepth_diff_grav",
+                               space_subdepth_diff_grav_default);
 
   if (verbose) {
     message("max_size set to %d split_size set to %d", space_maxsize,
             space_splitsize);
-    message("subdepth_grav set to %d", space_subdepth_grav);
+    message("subdepth_grav set to %d", space_subdepth_diff_grav);
     message("sub_size_pair_hydro set to %d, sub_size_self_hydro set to %d",
             space_subsize_pair_hydro, space_subsize_self_hydro);
     message("sub_size_pair_grav set to %d, sub_size_self_grav set to %d",
diff --git a/src/space.h b/src/space.h
index d0ec372d6f..e6d774200b 100644
--- a/src/space.h
+++ b/src/space.h
@@ -50,7 +50,7 @@ struct cosmology;
 #define space_subsize_self_grav_default 32000
 #define space_subsize_pair_stars_default 256000000
 #define space_subsize_self_stars_default 32000
-#define space_subdepth_grav_default 2
+#define space_subdepth_diff_grav_default 4
 #define space_max_top_level_cells_default 12
 #define space_stretch 1.10f
 #define space_maxreldx 0.1f
@@ -67,7 +67,7 @@ extern int space_subsize_pair_grav;
 extern int space_subsize_self_grav;
 extern int space_subsize_pair_stars;
 extern int space_subsize_self_stars;
-extern int space_subdepth_grav;
+extern int space_subdepth_diff_grav;
 
 /**
  * @brief The space in which the cells and particles reside.
-- 
GitLab