ITK icon indicating copy to clipboard operation
ITK copied to clipboard

GlobalDefaultNumberOfThreads should not be greater than GlobalMaximumNumberOfThreads

Open N-Dekker opened this issue 1 year ago • 0 comments

Description

Users typically call MultiThreaderBase::SetGlobalMaximumNumberOfThreads when they want to reduce (limit) the number of threads used by a process. However, when a user sets the GlobalMaximumNumberOfThreads while leaving the GlobalDefaultNumberOfThreads unspecified, this "default number" may end up becoming larger than the "maximum number"!

Steps to Reproduce

#include <itkMultiThreaderBase.h>
#include <iostream>

int main()
{
  itk::MultiThreaderBase::SetGlobalMaximumNumberOfThreads(3);

  std::cout
    << "Maximum: " << itk::MultiThreaderBase::GetGlobalMaximumNumberOfThreads() << '\n'
    << "Default: " << itk::MultiThreaderBase::GetGlobalDefaultNumberOfThreads() << '\n';
}

Expected behavior

With this example, both Maximum and Default should end up being 3 (at least, if the machine supports having 3 simultaneous threads).

Actual behavior

Output on my (LKEB/LUMC) multi-core machine:

Maximum: 3 Default: 12

Versions

ITK 5.4.0

Environment

Windows

Additional Information

GlobalDefaultNumberOfThreads is being used when constructing the ThreadPool:

https://github.com/InsightSoftwareConsortium/ITK/blob/01e133c6b550786c63fe2886fe4252ecefa19567/Modules/Core/Common/src/itkThreadPool.cxx#L114

So the number of threads held by the thread pool appears to also occasionally exceed GlobalMaximumNumberOfThreads!


  • Triggered by research by Sebastian (@Svdvoort) (related to ITKElastix issue https://github.com/InsightSoftwareConsortium/ITKElastix/issues/160) and by Gonzalo Mosquera Rojas and Juancito van Leeuwen.

N-Dekker avatar Jul 10 '24 16:07 N-Dekker