Commit adcaefb7 authored by Martin Reinecke's avatar Martin Reinecke
Browse files

allow changing default number of threads

parent c698770d
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
/* Copyright (C) 2019 Peter Bell, Max-Planck-Society /* Copyright (C) 2019-2020 Peter Bell, Max-Planck-Society
Authors: Peter Bell, Martin Reinecke */ Authors: Peter Bell, Martin Reinecke */
#ifndef MRUTIL_THREADING_H #ifndef MRUTIL_THREADING_H
...@@ -45,6 +45,18 @@ using namespace std; ...@@ -45,6 +45,18 @@ using namespace std;
#ifndef MRUTIL_NO_THREADING #ifndef MRUTIL_NO_THREADING
static const size_t max_threads_ = max(1u, thread::hardware_concurrency()); static const size_t max_threads_ = max(1u, thread::hardware_concurrency());
inline atomic<size_t> &default_nthreads()
{
static atomic<size_t> default_nthreads_=max_threads_;
return default_nthreads_;
}
inline size_t get_default_nthreads()
{ return default_nthreads(); }
inline void set_default_nthreads(size_t new_default_nthreads)
{ default_nthreads() = max<size_t>(1, new_default_nthreads); }
class latch class latch
{ {
atomic<size_t> num_left_; atomic<size_t> num_left_;
...@@ -229,7 +241,7 @@ class Distribution ...@@ -229,7 +241,7 @@ class Distribution
size_t nthreads, size_t chunksize, Func f) size_t nthreads, size_t chunksize, Func f)
{ {
mode = STATIC; mode = STATIC;
nthreads_ = (nthreads==0) ? max_threads_ : nthreads; nthreads_ = (nthreads==0) ? get_default_nthreads() : nthreads;
nwork_ = nwork; nwork_ = nwork;
chunksize_ = (chunksize<1) ? (nwork_+nthreads_-1)/nthreads_ chunksize_ = (chunksize<1) ? (nwork_+nthreads_-1)/nthreads_
: chunksize; : chunksize;
...@@ -243,7 +255,7 @@ class Distribution ...@@ -243,7 +255,7 @@ class Distribution
size_t nthreads, size_t chunksize_min, double fact_max, Func f) size_t nthreads, size_t chunksize_min, double fact_max, Func f)
{ {
mode = DYNAMIC; mode = DYNAMIC;
nthreads_ = (nthreads==0) ? max_threads_ : nthreads; nthreads_ = (nthreads==0) ? get_default_nthreads() : nthreads;
nwork_ = nwork; nwork_ = nwork;
chunksize_ = (chunksize_min<1) ? 1 : chunksize_min; chunksize_ = (chunksize_min<1) ? 1 : chunksize_min;
if (chunksize_*nthreads_>=nwork_) if (chunksize_*nthreads_>=nwork_)
...@@ -255,7 +267,7 @@ class Distribution ...@@ -255,7 +267,7 @@ class Distribution
template<typename Func> void execParallel(size_t nthreads, Func f) template<typename Func> void execParallel(size_t nthreads, Func f)
{ {
mode = STATIC; mode = STATIC;
nthreads_ = (nthreads==0) ? max_threads_ : nthreads; nthreads_ = (nthreads==0) ? get_default_nthreads() : nthreads;
nwork_ = nthreads_; nwork_ = nthreads_;
chunksize_ = 1; chunksize_ = 1;
thread_map(move(f)); thread_map(move(f));
...@@ -372,6 +384,12 @@ class Sched0 ...@@ -372,6 +384,12 @@ class Sched0
template<typename Func> void execParallel(size_t /*nthreads*/, Func f) template<typename Func> void execParallel(size_t /*nthreads*/, Func f)
{ f(); } { f(); }
inline size_t get_default_nthreads()
{ return 1; }
inline void set_default_nthreads(size_t /*new_default_nthreads*/)
{}
#endif #endif
namespace { namespace {
...@@ -413,6 +431,8 @@ inline size_t max_threads() ...@@ -413,6 +431,8 @@ inline size_t max_threads()
} // end of namespace detail } // end of namespace detail
using detail_threading::get_default_nthreads;
using detail_threading::set_default_nthreads;
using detail_threading::Scheduler; using detail_threading::Scheduler;
using detail_threading::execSingle; using detail_threading::execSingle;
using detail_threading::execStatic; using detail_threading::execStatic;
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
/* \file sharp_testsuite.c /* \file sharp_testsuite.c
* *
* Copyright (C) 2012-2019 Max-Planck-Society * Copyright (C) 2012-2020 Max-Planck-Society
* \author Martin Reinecke * \author Martin Reinecke
*/ */
...@@ -34,13 +34,14 @@ using std::complex; ...@@ -34,13 +34,14 @@ using std::complex;
#include "mr_util/error_handling.h" #include "mr_util/error_handling.h"
#include "mr_util/threading.h" #include "mr_util/threading.h"
#include "mr_util/math_utils.h" #include "mr_util/math_utils.h"
#include "mr_util/string_utils.h"
using namespace std; using namespace std;
using namespace mr; using namespace mr;
static void threading_status(void) static void threading_status(void)
{ {
cout << "Threading: " << mr::max_threads() << " threads active." << endl; cout << "Threading: " << mr::get_default_nthreads() << " threads active." << endl;
} }
static void MPI_status(void) static void MPI_status(void)
...@@ -536,6 +537,13 @@ static void sharp_test (int argc, const char **argv) ...@@ -536,6 +537,13 @@ static void sharp_test (int argc, const char **argv)
int main(int argc, const char **argv) int main(int argc, const char **argv)
{ {
const char *tmp = getenv("OMP_NUM_THREADS");
if (tmp!=nullptr)
{
string tmp2(tmp);
if (trim(tmp2)!="")
mr::set_default_nthreads(stringToData<size_t>(tmp));
}
mytask=0; ntasks=1; mytask=0; ntasks=1;
MR_assert(argc>=2,"need at least one command line argument"); MR_assert(argc>=2,"need at least one command line argument");
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment