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

**URL:** https://discourse.itk.org/t/limit-threads-for-the-whole-host-for-the-execution-of-multiple-itk-processes/7276
**Category:** Engineering
**Tags:** simpleitk
**Created:** [October 22, 2024, 11:24am UTC](https://discourse.itk.org/t/limit-threads-for-the-whole-host-for-the-execution-of-multiple-itk-processes/7276 "2024-10-22T11:24:26Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![reox](https://discourse.itk.org/user_avatar/discourse.itk.org/reox/32/4490_2.png) [@reox](https://discourse.itk.org/u/reox)
#### Post date: [October 22, 2024, 11:24am UTC](https://discourse.itk.org/t/limit-threads-for-the-whole-host-for-the-execution-of-multiple-itk-processes/7276/1 "2024-10-22T11:24:26Z")

</div>

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](https://discourse.itk.org/t/threading-in-itk/782/17) but as far as I can tell, the variable `ITK_USE_THREADPOOL` is deprecated? Or is this something completely different?

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [October 22, 2024, 1:10pm UTC](https://discourse.itk.org/t/limit-threads-for-the-whole-host-for-the-execution-of-multiple-itk-processes/7276/2 "2024-10-22T13:10:35Z")

</div>

Hello,

Please look at the SimpleITK [ProcessObject](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1ProcessObject.html). 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.

---

<div class="post-metadata">

### Author: ![reox](https://discourse.itk.org/user_avatar/discourse.itk.org/reox/32/4490_2.png) [@reox](https://discourse.itk.org/u/reox)
#### Post date: [October 22, 2024, 1:42pm UTC](https://discourse.itk.org/t/limit-threads-for-the-whole-host-for-the-execution-of-multiple-itk-processes/7276/3 "2024-10-22T13:42:48Z")

</div>

> [@blowekamp](#):
>
> 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.

Processes. Okay, I actually already expected that answer 😉

> [@blowekamp](#):
>
> 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).

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

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

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)

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)
#### Post date: [October 22, 2024, 2:50pm UTC](https://discourse.itk.org/t/limit-threads-for-the-whole-host-for-the-execution-of-multiple-itk-processes/7276/4 "2024-10-22T14:50:26Z")

</div>

> [@reox](#):
>
> > [@blowekamp](#):
> >
> > Addition: SimpleITK from PyPI is not distributed with the TBB threaded backend. It is available in some ( linux, windows?) condaforge SimpleITK distributions.
> 
> 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.

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [October 22, 2024, 2:57pm UTC](https://discourse.itk.org/t/limit-threads-for-the-whole-host-for-the-execution-of-multiple-itk-processes/7276/5 "2024-10-22T14:57:42Z")

</div>

Per the documentation in SimpleITK [ProcessObject::SetGlobalDefaultThreader](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1ProcessObject.html#a709f1c6f88d214826f2e864aff85abd4) there are three options:

- “POOL” [itk::PoolMultiThreader](https://www.itk.org/Doxygen/html/classitk_1_1PoolMultiThreader.html)
- “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.
