Home > Backend Development > Python Tutorial > Why Are My Joblib Workers Stuck on One Core After Importing NumPy?

Why Are My Joblib Workers Stuck on One Core After Importing NumPy?

Susan Sarandon
Release: 2024-10-29 19:43:29
Original
828 people have browsed it

Why Are My Joblib Workers Stuck on One Core After Importing NumPy?

Multiprocessing Core Affinity Issue

In certain scenarios, when importing the NumPy library and attempting to parallelize a CPU-intensive loop using the joblib module, it may be observed that the worker processes assigned to the loop are all confined to a single core, hindering performance gains.

This issue arises due to the interaction between NumPy and the underlying OpenBLAS libraries, which are often employed for multithreaded operations. By default, these libraries may interfere with the core affinity settings, leading to all worker processes being assigned to the same core.

Solution: Reset Task Affinity

A straightforward solution is to reset the task affinity using the os.system() function after importing the affected modules. By executing the command os.system("taskset -p 0xff %d" % os.getpid()), the core affinity is reset, allowing the worker processes to spread across all available cores.

Disable OpenBLAS CPU Affinity Reset

Alternatively, the CPU affinity-resetting behavior of OpenBLAS can be disabled either at run-time or at build-time. At run-time, setting the environment variable OPENBLAS_MAIN_FREE to 1 prevents the library from adjusting core affinity. Alternatively, during the OpenBLAS compilation process, adding NO_AFFINITY=1 to the Makefile.rule permanently disables this behavior.

The above is the detailed content of Why Are My Joblib Workers Stuck on One Core After Importing NumPy?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template