Skip to content

Commit

Permalink
Merge topic 'AddThreadpoolOnAPI'
Browse files Browse the repository at this point in the history
0b6113b ENH: Expand interface for selecting threadpool
  • Loading branch information
hjmjohnson authored and kwrobot committed Dec 2, 2014
2 parents b35b8b7 + 0b6113b commit 1dea442
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
14 changes: 13 additions & 1 deletion Modules/Core/Common/include/itkMultiThreader.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,15 @@ class ITKCommon_EXPORT MultiThreader : public Object
* Therefore the caller of this method should check that the requested number
* of threads was accepted. */
static void SetGlobalMaximumNumberOfThreads(ThreadIdType val);

static ThreadIdType GetGlobalMaximumNumberOfThreads();

/** Set/Get whether to use the to use the thread pool
* implementation or the spawing implementation of
* starting threads.
*/
static void SetGlobalDefaultUseThreadPool( const bool GlobalDefaultUseThreadPool );
static bool GetGlobalDefaultUseThreadPool( );

/** Set/Get the value which is used to initialize the NumberOfThreads in the
* constructor. It will be clamped to the range [1, m_GlobalMaximumNumberOfThreads ].
* Therefore the caller of this method should check that the requested number
Expand Down Expand Up @@ -202,6 +208,12 @@ class ITKCommon_EXPORT MultiThreader : public Object
* ITK_MAX_THREADS and greater than zero. */
static ThreadIdType m_GlobalMaximumNumberOfThreads;

/** Global value to effect weather the threadpool implementation should
* be used. This defaults to the environmental variable "ITK_USE_THREADPOOL"
* if set, else it default to false and new threads are spawned.
*/
static bool m_GlobalDefaultUseThreadPool;

/* Global variable defining the default number of threads to set at
* construction time of a MultiThreader instance. The
* m_GlobalDefaultNumberOfThreads must always be less than or equal to the
Expand Down
53 changes: 43 additions & 10 deletions Modules/Core/Common/src/itkMultiThreader.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,47 @@
namespace itk
{

// GlobalDefaultUseThreadPoolIsInitialized is used only in this
// file to ensure that the ITK_USE_THREADPOOL environmenal variable
// is only used as a fall back option. If the SetGlobalDefaultUseThreadPool
// API is ever used by the developer, the developers choice is
// respected over the environmental variable.
static bool GlobalDefaultUseThreadPoolIsInitialized=false;
static SimpleFastMutexLock globalDefaultInitializerLock;

bool MultiThreader::m_GlobalDefaultUseThreadPool = false;

void MultiThreader::SetGlobalDefaultUseThreadPool( const bool GlobalDefaultUseThreadPool )
{
m_GlobalDefaultUseThreadPool = GlobalDefaultUseThreadPool;
GlobalDefaultUseThreadPoolIsInitialized=true;
}

bool MultiThreader::GetGlobalDefaultUseThreadPool( )
{
if( ! GlobalDefaultUseThreadPoolIsInitialized )
{
//
// look for runtime request to use thread pool
std::string use_threadpool;
if( itksys::SystemTools::GetEnv("ITK_USE_THREADPOOL",use_threadpool) )
{
MutexLockHolder< SimpleFastMutexLock > lock(globalDefaultInitializerLock);
use_threadpool = itksys::SystemTools::UpperCase(use_threadpool);
//NOTE: GlobalDefaultUseThreadPoolIsInitialized=true after this call
if(use_threadpool != "NO" && use_threadpool != "OFF" && use_threadpool != "FALSE")
{
MultiThreader::SetGlobalDefaultUseThreadPool( true );
}
else
{
MultiThreader::SetGlobalDefaultUseThreadPool( false );
}
}
}
return m_GlobalDefaultUseThreadPool;
}

// Initialize static member that controls global maximum number of threads.
ThreadIdType MultiThreader::m_GlobalMaximumNumberOfThreads = ITK_MAX_THREADS;

Expand Down Expand Up @@ -199,16 +240,8 @@ MultiThreader::MultiThreader() : m_ThreadPool(ThreadPool::GetInstance() ), m_Use
m_SingleMethod = ITK_NULLPTR;
m_SingleData = ITK_NULLPTR;
m_NumberOfThreads = this->GetGlobalDefaultNumberOfThreads();
//
// look for runtime request to use thread pool
std::string use_threadpool;
if( itksys::SystemTools::GetEnv("ITK_USE_THREADPOOL",use_threadpool) )
{
if(use_threadpool != "NO" && use_threadpool != "OFF")
{
this->SetUseThreadPool(true);
}
}

this->SetUseThreadPool( MultiThreader::GetGlobalDefaultUseThreadPool() );
}

MultiThreader::~MultiThreader()
Expand Down

0 comments on commit 1dea442

Please sign in to comment.