From b6792606a5d1fef3a6556e73a905efccfa2b4d50 Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Wed, 11 Feb 2015 17:24:52 -0600 Subject: [PATCH] Make the set_thread_limit function readable by adding some commentary. Also add some error checking. --- source/base/multithread_info.cc | 46 ++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/source/base/multithread_info.cc b/source/base/multithread_info.cc index 5b7c9d982a..e47136f1e5 100644 --- a/source/base/multithread_info.cc +++ b/source/base/multithread_info.cc @@ -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 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 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 -- 2.39.5