Limit Threads for the whole host for the execution of multiple ITK processes

I have a tool written in Python and SimpleITK and start multiple instances of it at the same time.
I have limited the number of threads per process via the environment variable ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS . However, it can happen that the number of processes times the number of threads is more than the number of CPUs I have in the system. I wondered if there is a way to limit the number of total threads for the whole host? I.e., I would set ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS to 8 but set a global limit of 64 for the whole host. Thus, even if I would start 10 processes, I would not start 80 threads but only a maximum of 64?
There is a similar question here: Threading in ITK - #17 by pradeep but as far as I can tell, the variable ITK_USE_THREADPOOL is deprecated? Or is this something completely different?

Hello,

Please look at the SimpleITK ProcessObject. It contains a number of “GlobalDefault” variables that can be used to configure the threading behavior.

I am uncertain when you say “start multiple instances” if you are referring to starting separate processes or threads. There is no way for multiple ITK process to communicate with each other. The above “Global” variables only refer to the process, and not limits between processes. This is reasonable because threads are not shared between processes.

There are a lot of options with how to paralyzed image processing tasks, and the best way to do it depend on the size of the data and the algorithms run. And this can change between differenent parts of the algorithm. For example I have written algorithms which scale nearly perfect with large data and number of threads, but when run 90% of the time is spent single threaded in I/O.

So if you have a large number of datasets to process it may be most efficient to just set each processes to 2 threads, and run N processes as the number of physical cores ( not hyper threaded).

Addition: SimpleITK from PyPI is not distributed with the TBB threaded backend. It is available in some ( linux, windows?) condaforge SimpleITK distributions.

2 Likes

Processes. Okay, I actually already expected that answer :wink:

I usually do that, but I have to remember that before starting my tools :smiley: Reason I was asking was, that I started 10 jobs at the same time, where all were set to use 16 threads each…

ah okay, I’ll have a look! Does it make a huge difference? So far, I did not had any problems with large number of threads (the largest machine has 128 cores and I successfully ran some registrations there)

Yes, TBB does make a huge difference (makes execution several times faster) in special cases, mostly when without TBB you would create and delete hundreds of threads per second. However, it does not make any perceivable difference most of the time. It is hard to know without actually trying.

1 Like

Per the documentation in SimpleITK ProcessObject::SetGlobalDefaultThreader there are three options:

  • “POOL” itk::PoolMultiThreader
  • “TBB” itk::TBBThreader (optional)
  • “PLATFORM” itk::PlatformMultiThreader

The “POOL” threader would also have the same benefits of persistent threads, I believe that is the current default threader in SimpleITK and ITK.

1 Like