]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Make the set_thread_limit function readable by adding some commentary. 523/head
authorWolfgang Bangerth <bangerth@math.tamu.edu>
Wed, 11 Feb 2015 23:24:52 +0000 (17:24 -0600)
committerWolfgang Bangerth <bangerth@math.tamu.edu>
Thu, 12 Feb 2015 13:27:47 +0000 (07:27 -0600)
Also add some error checking.

source/base/multithread_info.cc

index 5b7c9d982ac87dc377cb5340e538d9333abb6058..e47136f1e56f95e1d4d4bf6912752288539d6371 100644 (file)
@@ -1,6 +1,6 @@
 // ---------------------------------------------------------------------
 //
-// Copyright (C) 2000 - 2014 by the deal.II authors
+// Copyright (C) 2000 - 2015 by the deal.II authors
 //
 // This file is part of the deal.II library.
 //
@@ -103,14 +103,42 @@ void MultithreadInfo::set_thread_limit(const unsigned int max_threads)
   Assert(n_max_threads==numbers::invalid_unsigned_int,
          ExcMessage("Calling set_thread_limit() more than once is not supported!"));
 
-  unsigned int max_threads_env = numbers::invalid_unsigned_int;
-  char *penv;
-  penv = getenv ("DEAL_II_NUM_THREADS");
-
-  if (penv!=NULL)
-    max_threads_env = Utilities::string_to_int(std::string(penv));
-
-  n_max_threads = std::min(max_threads, max_threads_env);
+  // set the maximal number of threads to the given value as specified
+  n_max_threads = max_threads;
+
+  // then also see if something was given in the environment
+  {
+    const char *penv = getenv ("DEAL_II_NUM_THREADS");
+    if (penv!=NULL)
+      {
+        unsigned int max_threads_env = numbers::invalid_unsigned_int;
+        try
+          {
+            max_threads_env = Utilities::string_to_int(std::string(penv));
+          }
+        catch (...)
+          {
+            AssertThrow (false,
+                         ExcMessage (std::string("When specifying the <DEAL_II_NUM_THREADS> environment "
+                                                 "variable, it needs to be something that can be interpreted "
+                                                 "as an integer. The text you have in the environment "
+                                                 "variable is <") + penv + ">"));
+          }
+
+        AssertThrow (max_threads_env>0,
+                     ExcMessage ("When specifying the <DEAL_II_NUM_THREADS> environment "
+                                 "variable, it needs to be a positive number."));
+
+        if (n_max_threads != numbers::invalid_unsigned_int)
+          n_max_threads = std::min(n_max_threads, max_threads_env);
+        else
+          n_max_threads = max_threads_env;
+      }
+  }
+  // finally see if we need to tell the TBB about this. if no value has been set
+  // (the if-branch), then simply set n_max_threads to what we get from the TBB
+  // but don't do anything further. otherwise (some value has been set), let the
+  // TBB use this value
   if (n_max_threads == numbers::invalid_unsigned_int)
     n_max_threads = tbb::task_scheduler_init::default_num_threads();
   else

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.