# How to set OMP\_NUM\_THREADS for distruted training?

**URL:** <https://discuss.pytorch.org/t/how-to-set-omp-num-threads-for-distruted-training/143234>\
**Category:** Uncategorized\
**Created:** [February 3, 2022, 11:16pm UTC](https://discuss.pytorch.org/t/how-to-set-omp-num-threads-for-distruted-training/143234 "2022-02-03T23:16:20Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![MinSnz](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/minsnz/32/76345_2.png) [@MinSnz](https://discuss.pytorch.org/u/MinSnz)\
**Post date:** [March 17, 2025, 2:10pm UTC](https://discuss.pytorch.org/t/how-to-set-omp-num-threads-for-distruted-training/143234/7 "2025-03-17T14:10:22Z")

</div>

`OMP_NUM_THREADS` should be set to `#PhysicalCores / #Processes`.

- `os.process_cpu_count`, as @handaru mentioned and was [added in Python 3.13](https://docs.python.org/3/library/os.html#os.process_cpu_count), as well as `nproc` and `htop`, gives the number of _logical_ CPUs. Please use `psutil.cpu_count(logical=False)` to get the number of **physical** CPUs.
- Don’t forget to divide it by `nproc_per_node` as @Kenzo mentioned, or you’ll have many processes competing for each core.

---

_[View the full topic](https://discuss.pytorch.org/t/how-to-set-omp-num-threads-for-distruted-training/143234)._
