From a8ac1a020afb9e28fbbbf0e47b8c4e9ee12ed01e Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <schaller@strw.leidenuniv.nl>
Date: Sun, 17 Nov 2019 16:19:56 +0100
Subject: [PATCH] Simplify the recursive activation of limiting and
 synchronization tasks.

---
 src/cell.c             | 38 ++++++++++++++++++++++----------------
 src/cell.h             |  6 ++++--
 src/engine_marktasks.c | 11 +++++++----
 3 files changed, 33 insertions(+), 22 deletions(-)

diff --git a/src/cell.c b/src/cell.c
index 992bc5e65b..96e4e03b13 100644
--- a/src/cell.c
+++ b/src/cell.c
@@ -2910,12 +2910,12 @@ void cell_activate_stars_sorts(struct cell *c, int sid, struct scheduler *s) {
  * @param ci The first #cell we recurse in.
  * @param cj The second #cell we recurse in.
  * @param s The task #scheduler.
+ * @param with_timestep_limiter Are we running with time-step limiting on?
  */
 void cell_activate_subcell_hydro_tasks(struct cell *ci, struct cell *cj,
-                                       struct scheduler *s) {
+                                       struct scheduler *s,
+                                       const int with_timestep_limiter) {
   const struct engine *e = s->space->e;
-  const int with_timestep_limiter =
-      (e->policy & engine_policy_timestep_limiter);
 
   /* Store the current dx_max and h_max values. */
   ci->hydro.dx_max_part_old = ci->hydro.dx_max_part;
@@ -2936,11 +2936,12 @@ void cell_activate_subcell_hydro_tasks(struct cell *ci, struct cell *cj,
       /* Loop over all progenies and pairs of progenies */
       for (int j = 0; j < 8; j++) {
         if (ci->progeny[j] != NULL) {
-          cell_activate_subcell_hydro_tasks(ci->progeny[j], NULL, s);
+          cell_activate_subcell_hydro_tasks(ci->progeny[j], NULL, s,
+                                            with_timestep_limiter);
           for (int k = j + 1; k < 8; k++)
             if (ci->progeny[k] != NULL)
               cell_activate_subcell_hydro_tasks(ci->progeny[j], ci->progeny[k],
-                                                s);
+                                                s, with_timestep_limiter);
         }
       }
     } else {
@@ -2969,7 +2970,7 @@ void cell_activate_subcell_hydro_tasks(struct cell *ci, struct cell *cj,
         const int pjd = csp->pairs[k].pjd;
         if (ci->progeny[pid] != NULL && cj->progeny[pjd] != NULL)
           cell_activate_subcell_hydro_tasks(ci->progeny[pid], cj->progeny[pjd],
-                                            s);
+                                            s, with_timestep_limiter);
       }
     }
 
@@ -3006,12 +3007,13 @@ void cell_activate_subcell_hydro_tasks(struct cell *ci, struct cell *cj,
  * @param cj The second #cell we recurse in.
  * @param s The task #scheduler.
  * @param with_star_formation Are we running with star formation switched on?
+ * @param with_timestep_sync Are we running with time-step synchronization on?
  */
 void cell_activate_subcell_stars_tasks(struct cell *ci, struct cell *cj,
                                        struct scheduler *s,
-                                       const int with_star_formation) {
+                                       const int with_star_formation,
+                                       const int with_timestep_sync) {
   const struct engine *e = s->space->e;
-  const int with_timestep_sync = (e->policy & engine_policy_timestep_sync);
 
   /* Store the current dx_max and h_max values. */
   ci->stars.dx_max_part_old = ci->stars.dx_max_part;
@@ -3042,12 +3044,13 @@ void cell_activate_subcell_stars_tasks(struct cell *ci, struct cell *cj,
       /* Loop over all progenies and pairs of progenies */
       for (int j = 0; j < 8; j++) {
         if (ci->progeny[j] != NULL) {
-          cell_activate_subcell_stars_tasks(ci->progeny[j], NULL, s,
-                                            with_star_formation);
+          cell_activate_subcell_stars_tasks(
+              ci->progeny[j], NULL, s, with_star_formation, with_timestep_sync);
           for (int k = j + 1; k < 8; k++)
             if (ci->progeny[k] != NULL)
               cell_activate_subcell_stars_tasks(ci->progeny[j], ci->progeny[k],
-                                                s, with_star_formation);
+                                                s, with_star_formation,
+                                                with_timestep_sync);
         }
       }
     } else {
@@ -3083,7 +3086,8 @@ void cell_activate_subcell_stars_tasks(struct cell *ci, struct cell *cj,
         const int pjd = csp->pairs[k].pjd;
         if (ci->progeny[pid] != NULL && cj->progeny[pjd] != NULL)
           cell_activate_subcell_stars_tasks(ci->progeny[pid], cj->progeny[pjd],
-                                            s, with_star_formation);
+                                            s, with_star_formation,
+                                            with_timestep_sync);
       }
     }
 
@@ -3442,12 +3446,12 @@ int cell_unskip_hydro_tasks(struct cell *c, struct scheduler *s) {
 
       /* Store current values of dx_max and h_max. */
       else if (t->type == task_type_sub_self) {
-        cell_activate_subcell_hydro_tasks(ci, NULL, s);
+        cell_activate_subcell_hydro_tasks(ci, NULL, s, with_timestep_limiter);
       }
 
       /* Store current values of dx_max and h_max. */
       else if (t->type == task_type_sub_pair) {
-        cell_activate_subcell_hydro_tasks(ci, cj, s);
+        cell_activate_subcell_hydro_tasks(ci, cj, s, with_timestep_limiter);
       }
     }
 
@@ -3885,11 +3889,13 @@ int cell_unskip_stars_tasks(struct cell *c, struct scheduler *s,
       }
 
       else if (t->type == task_type_sub_self) {
-        cell_activate_subcell_stars_tasks(ci, NULL, s, with_star_formation);
+        cell_activate_subcell_stars_tasks(ci, NULL, s, with_star_formation,
+                                          with_timestep_sync);
       }
 
       else if (t->type == task_type_sub_pair) {
-        cell_activate_subcell_stars_tasks(ci, cj, s, with_star_formation);
+        cell_activate_subcell_stars_tasks(ci, cj, s, with_star_formation,
+                                          with_timestep_sync);
       }
     }
 
diff --git a/src/cell.h b/src/cell.h
index 13621f0cd2..abaced2b9d 100644
--- a/src/cell.h
+++ b/src/cell.h
@@ -891,12 +891,14 @@ void cell_set_star_resort_flag(struct cell *c);
 void cell_activate_star_formation_tasks(struct cell *c, struct scheduler *s,
                                         const int with_feedback);
 void cell_activate_subcell_hydro_tasks(struct cell *ci, struct cell *cj,
-                                       struct scheduler *s);
+                                       struct scheduler *s,
+                                       const int with_timestep_limiter);
 void cell_activate_subcell_grav_tasks(struct cell *ci, struct cell *cj,
                                       struct scheduler *s);
 void cell_activate_subcell_stars_tasks(struct cell *ci, struct cell *cj,
                                        struct scheduler *s,
-                                       const int with_star_formation);
+                                       const int with_star_formation,
+                                       const int with_timestep_sync);
 void cell_activate_subcell_black_holes_tasks(struct cell *ci, struct cell *cj,
                                              struct scheduler *s);
 void cell_activate_subcell_external_grav_tasks(struct cell *ci,
diff --git a/src/engine_marktasks.c b/src/engine_marktasks.c
index 0229c04320..3309b649a3 100644
--- a/src/engine_marktasks.c
+++ b/src/engine_marktasks.c
@@ -111,7 +111,7 @@ void engine_marktasks_mapper(void *map_data, int num_elements,
                t_subtype == task_subtype_density) {
         if (ci_active_hydro) {
           scheduler_activate(s, t);
-          cell_activate_subcell_hydro_tasks(ci, NULL, s);
+          cell_activate_subcell_hydro_tasks(ci, NULL, s, with_timestep_limiter);
           if (with_timestep_limiter) cell_activate_limiter(ci, s);
         }
       }
@@ -160,7 +160,8 @@ void engine_marktasks_mapper(void *map_data, int num_elements,
                t_subtype == task_subtype_stars_density) {
         if (ci_active_stars) {
           scheduler_activate(s, t);
-          cell_activate_subcell_stars_tasks(ci, NULL, s, with_star_formation);
+          cell_activate_subcell_stars_tasks(ci, NULL, s, with_star_formation,
+                                            with_timestep_sync);
         }
       }
 
@@ -332,7 +333,8 @@ void engine_marktasks_mapper(void *map_data, int num_elements,
         /* Store current values of dx_max and h_max. */
         else if (t_type == task_type_sub_pair &&
                  t_subtype == task_subtype_density) {
-          cell_activate_subcell_hydro_tasks(t->ci, t->cj, s);
+          cell_activate_subcell_hydro_tasks(t->ci, t->cj, s,
+                                            with_timestep_limiter);
         }
       }
 
@@ -394,7 +396,8 @@ void engine_marktasks_mapper(void *map_data, int num_elements,
         /* Store current values of dx_max and h_max. */
         else if (t_type == task_type_sub_pair &&
                  t_subtype == task_subtype_stars_density) {
-          cell_activate_subcell_stars_tasks(ci, cj, s, with_star_formation);
+          cell_activate_subcell_stars_tasks(ci, cj, s, with_star_formation,
+                                            with_timestep_sync);
         }
       }
 
-- 
GitLab