From 046189e7320524841c81e6e5de3be91da72c52d2 Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <matthieu.schaller@durham.ac.uk>
Date: Fri, 8 Apr 2016 12:51:11 +0100
Subject: [PATCH] Made the number of threads a command-line parameter again.

---
 README                               |  1 +
 examples/CosmoVolume/cosmoVolume.yml |  1 -
 examples/CosmoVolume/run.sh          |  2 +-
 examples/SedovBlast/run.sh           |  2 +-
 examples/SedovBlast/sedov.yml        |  1 -
 examples/SodShock/run.sh             |  2 +-
 examples/SodShock/sodShock.yml       |  1 -
 examples/UniformBox/run.sh           |  2 +-
 examples/UniformBox/uniformBox.yml   |  1 -
 examples/main.c                      | 18 ++++++++++++++++--
 examples/parameter_example.yml       |  1 -
 src/engine.c                         |  5 +++--
 src/engine.h                         |  2 +-
 13 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/README b/README
index 0c57e3f565..59c362415a 100644
--- a/README
+++ b/README
@@ -23,6 +23,7 @@ Valid options are:
   -g          Run with an external gravitational potential
   -G          Run with self-gravity
   -s          Run with SPH
+  -t    {int} The number of threads to use on each MPI rank. Defaults to 1 if not specified.
   -v     [12] Increase the level of verbosity 1: MPI-rank 0 writes 
               2: All MPI-ranks write
   -y    {int} Time-step frequency at which task graphs are dumped
diff --git a/examples/CosmoVolume/cosmoVolume.yml b/examples/CosmoVolume/cosmoVolume.yml
index 20d5febb28..2da4522104 100644
--- a/examples/CosmoVolume/cosmoVolume.yml
+++ b/examples/CosmoVolume/cosmoVolume.yml
@@ -9,7 +9,6 @@ UnitSystem:
 
 # Parameters for the task scheduling
 Scheduler:
-  nr_threads:       16        # The number of threads per MPI rank to use.
   nr_queues:        0        # The number of task queues to use. Use 0  to let the system decide.
   cell_max_size:    8000000  # Maximal number of interactions per task (this is the default value).
   cell_sub_size:    5000     # Maximal number of interactions per sub-task  (this is the default value).
diff --git a/examples/CosmoVolume/run.sh b/examples/CosmoVolume/run.sh
index a788a35c76..412456c3cb 100755
--- a/examples/CosmoVolume/run.sh
+++ b/examples/CosmoVolume/run.sh
@@ -7,4 +7,4 @@ then
     ./getIC.sh
 fi
 
-../swift -s cosmoVolume.yml
+../swift -s -t 16 cosmoVolume.yml
diff --git a/examples/SedovBlast/run.sh b/examples/SedovBlast/run.sh
index 58646cf42e..f71830eb6a 100755
--- a/examples/SedovBlast/run.sh
+++ b/examples/SedovBlast/run.sh
@@ -7,4 +7,4 @@ then
     python makeIC_fcc.py
 fi
 
-../swift -s sedov.yml
+../swift -s -t 16 sedov.yml
diff --git a/examples/SedovBlast/sedov.yml b/examples/SedovBlast/sedov.yml
index f354ef5679..55974b03b8 100644
--- a/examples/SedovBlast/sedov.yml
+++ b/examples/SedovBlast/sedov.yml
@@ -9,7 +9,6 @@ UnitSystem:
 
 # Parameters for the task scheduling
 Scheduler:
-  nr_threads:       16       # The number of threads per MPI rank to use.
   nr_queues:        0        # The number of task queues to use. Use 0  to let the system decide.
   cell_max_size:    8000000  # Maximal number of interactions per task (this is the default value).
   cell_sub_size:    5000     # Maximal number of interactions per sub-task  (this is the default value).
diff --git a/examples/SodShock/run.sh b/examples/SodShock/run.sh
index 646f1e3a33..b8141e5154 100755
--- a/examples/SodShock/run.sh
+++ b/examples/SodShock/run.sh
@@ -7,4 +7,4 @@ then
     python makeIC.py
 fi
 
-../swift -s sodShock.yml
+../swift -s -t 16 sodShock.yml
diff --git a/examples/SodShock/sodShock.yml b/examples/SodShock/sodShock.yml
index 5fe7be7b9f..ab43d6682b 100644
--- a/examples/SodShock/sodShock.yml
+++ b/examples/SodShock/sodShock.yml
@@ -9,7 +9,6 @@ UnitSystem:
 
 # Parameters for the task scheduling
 Scheduler:
-  nr_threads:       16        # The number of threads per MPI rank to use.
   nr_queues:        0        # The number of task queues to use. Use 0  to let the system decide.
   cell_max_size:    8000000  # Maximal number of interactions per task (this is the default value).
   cell_sub_size:    5000     # Maximal number of interactions per sub-task.
diff --git a/examples/UniformBox/run.sh b/examples/UniformBox/run.sh
index ca78b0ac04..0cb0a50591 100755
--- a/examples/UniformBox/run.sh
+++ b/examples/UniformBox/run.sh
@@ -7,4 +7,4 @@ then
     python makeIC.py 100
 fi
 
-../swift -s uniformBox.yml
+../swift -s -t 16 uniformBox.yml
diff --git a/examples/UniformBox/uniformBox.yml b/examples/UniformBox/uniformBox.yml
index 2d5512815b..0474b0f820 100644
--- a/examples/UniformBox/uniformBox.yml
+++ b/examples/UniformBox/uniformBox.yml
@@ -9,7 +9,6 @@ UnitSystem:
 
 # Parameters for the task scheduling
 Scheduler:
-  nr_threads:       16        # The number of threads per MPI rank to use.
   nr_queues:        0        # The number of task queues to use. Use 0  to let the system decide.
   cell_max_size:    8000000  # Maximal number of interactions per task (this is the default value).
   cell_sub_size:    5000     # Maximal number of interactions per sub-task  (this is the default value).
diff --git a/examples/main.c b/examples/main.c
index 5cfae5efba..9c98eaf1da 100644
--- a/examples/main.c
+++ b/examples/main.c
@@ -67,6 +67,9 @@ void print_help_message() {
          "Run with an external gravitational potential");
   printf("  %2s %8s %s\n", "-G", "", "Run with self-gravity");
   printf("  %2s %8s %s\n", "-s", "", "Run with SPH");
+  printf("  %2s %8s %s\n", "-t", "{int}",
+         "The number of threads to use on each MPI rank. Defaults to 1 if not "
+         "specified.");
   printf("  %2s %8s %s\n", "-v", "[12]",
          "Increase the level of verbosity 1: MPI-rank 0 writes ");
   printf("  %2s %8s %s\n", "", "", "2: All MPI-ranks write");
@@ -141,12 +144,13 @@ int main(int argc, char *argv[]) {
   int with_hydro = 0;
   int with_fp_exceptions = 0;
   int verbose = 0;
+  int nr_threads = 1;
   char paramFileName[200] = "";
   unsigned long long cpufreq = 0;
 
   /* Parse the parameters */
   int c;
-  while ((c = getopt(argc, argv, "cdef:gGhsv:y")) != -1) switch (c) {
+  while ((c = getopt(argc, argv, "cdef:gGhst:v:y")) != -1) switch (c) {
       case 'c':
         with_cosmology = 1;
         break;
@@ -175,6 +179,15 @@ int main(int argc, char *argv[]) {
       case 's':
         with_hydro = 1;
         break;
+      case 't':
+        if (sscanf(optarg, "%d", &nr_threads) != 1) {
+          if (myrank == 0)
+            printf("Error parsing the number of threads (-t).\n");
+          if (myrank == 0) print_help_message();
+          return 1;
+        }
+        with_hydro = 1;
+        break;
       case 'v':
         if (sscanf(optarg, "%d", &verbose) != 1) {
           if (myrank == 0) printf("Error parsing verbosity level (-v).\n");
@@ -383,7 +396,8 @@ int main(int argc, char *argv[]) {
   /* Initialize the engine with the space and policies. */
   if (myrank == 0) clocks_gettime(&tic);
   struct engine e;
-  engine_init(&e, &s, params, nr_nodes, myrank, engine_policies, talking);
+  engine_init(&e, &s, params, nr_nodes, myrank, nr_threads, engine_policies,
+              talking);
   if (myrank == 0) {
     clocks_gettime(&toc);
     message("engine_init took %.3f %s.", clocks_diff(&tic, &toc),
diff --git a/examples/parameter_example.yml b/examples/parameter_example.yml
index b91e99baf3..e3cd5b209c 100644
--- a/examples/parameter_example.yml
+++ b/examples/parameter_example.yml
@@ -9,7 +9,6 @@ UnitSystem:
 
 # Parameters for the task scheduling
 Scheduler:
-  nr_threads:       2        # The number of threads per MPI rank to use.
   nr_queues:        0        # The number of task queues to use. Use 0  to let the system decide.
   cell_max_size:    8000000  # Maximal number of interactions per task (this is the default value).
   cell_sub_size:    8000000  # Maximal number of interactions per sub-task  (this is the default value).
diff --git a/src/engine.c b/src/engine.c
index 5e60979811..7287f47b40 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -2323,20 +2323,21 @@ static bool hyperthreads_present(void) {
  * @param params The parsed parameter file.
  * @param nr_nodes The number of MPI ranks.
  * @param nodeID The MPI rank of this node.
+ * @param nr_threads The number of threads per MPI rank.
  * @param policy The queuing policy to use.
  * @param verbose Is this #engine talkative ?
  */
 
 void engine_init(struct engine *e, struct space *s,
                  const struct swift_params *params, int nr_nodes, int nodeID,
-                 int policy, int verbose) {
+                 int nr_threads, int policy, int verbose) {
 
   /* Clean-up everything */
   bzero(e, sizeof(struct engine));
 
   /* Store the values. */
   e->s = s;
-  e->nr_threads = parser_get_param_int(params, "Scheduler:nr_threads");
+  e->nr_threads = nr_threads;
   e->policy = policy;
   e->step = 0;
   e->nr_nodes = nr_nodes;
diff --git a/src/engine.h b/src/engine.h
index e1c3f61d12..c8b9d7a46f 100644
--- a/src/engine.h
+++ b/src/engine.h
@@ -170,7 +170,7 @@ struct engine {
 void engine_barrier(struct engine *e, int tid);
 void engine_init(struct engine *e, struct space *s,
                  const struct swift_params *params, int nr_nodes, int nodeID,
-                 int policy, int verbose);
+                 int nr_threads, int policy, int verbose);
 void engine_launch(struct engine *e, int nr_runners, unsigned int mask,
                    unsigned int submask);
 void engine_prepare(struct engine *e);
-- 
GitLab