Home > Backend Development > Python Tutorial > Why Does Multiprocessing Stall on a Single Core When NumPy Is Imported?

Why Does Multiprocessing Stall on a Single Core When NumPy Is Imported?

Patricia Arquette
Release: 2024-10-29 18:19:57
Original
277 people have browsed it

Why Does Multiprocessing Stall on a Single Core When NumPy Is Imported?

Why Multiprocessing Stalls on a Single Core When NumPy Is Imported

When attempting to parallelize a CPU-intensive loop using joblib, you may encounter an issue where all worker processes are assigned to the same core, leading to no performance gain. This occurs due to certain Python modules, including NumPy, manipulating core affinity on import.

Root Cause:

NumPy links against multithreaded OpenBLAS libraries, which interferes with core affinity settings.

Solution:

To resolve this issue, reset the task affinity using the following command:

<code class="python">os.system("taskset -p 0xff %d" % os.getpid())</code>
Copy after login

Place this line after importing the module where the issue arises. This forces the process to run on all available cores.

Alternative Solutions:

  • Environment Variable: Set the OPENBLAS_MAIN_FREE (or GOTOBLAS_MAIN_FREE) environment variable to 1 at run-time to disable the CPU affinity-resetting behavior of OpenBLAS.
  • Build-Time Modification: Disable CPU affinity resetting permanently by modifying the Makefile.rule to include the line NO_AFFINITY=1 when compiling OpenBLAS from source.

The above is the detailed content of Why Does Multiprocessing Stall on a Single Core When NumPy Is Imported?. 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