Skip to content

Numpy MKL causes hang with Python Subprocess and Multiprocessing modules. #11734

Closed

Description

@chrisgmorton

This appears to be a defect that is specific to Intel's Math Kernel Library and the resolution will likely require changes therein. I'm submitting here as I believe the NumPy team has some relationships/connections with the Intel team and there have been some efforts to make MKL work well with NumPy (and SciPy). I am using Python 3.6.4, built from source on OSX 10.13.4 and Linux 16.04 (WSL). NumPy is built from source using gcc 7.1.0 on Mac and gcc 7.2 on Linux.

When executed with python3 this script will hang the terminal on OSX and Linux. No tests have been performed on Win64. If the size of the vector, x, is reduced by a factor of 10 to 1000, no hang is observed and the script executes correctly. The hang is specific to the call to linalg.norm. It is likely caused by multi-threaded execution of the norm() method using MKL's infrastructure, leaving the system in a state that interferes with Python execution of subprocess.run(). I also get a hang when using Python's Multiprocessing module process creation in place of subprocess.run().

This is important for a few reasons 1) Jupyter QtConsole & Notebook and IPython use subprocess.run() to execute certain os commands (such as ls). Hence we get a hang when working in all these applications. 2) If you want to spawn or fork to subprocesses to execute cpu intensive tasks you'll get a hang if you've previously called np.linalg.norm() with a sufficiently large vector (and perhaps many other NumPy and SciPy calls).

Reproducing code example:

import numpy as np
import subprocess

x = np.arange(10000) # 1000 shows no hang.
xnorm = np.linalg.norm(x)

subprocess.run('ls')

Error message:

No error message, just a hang.

Numpy/Python version information:

NumPy 1.14.3
Python 3.6.4
MKL: l_mkl_2018.3.222, m_mkl_2018.3.185

Activity

  1. mattip commented on Aug 13, 2018

    @mattip
    Member

    Duplicate of #10060, #11041 #10993, and maybe others.
    Among the fixes that might work:

    # work-around 1:
    KMP_INIT_AT_FORK=FALSE python script.py
    # work-around 2:
    MKL_THREADING_LAYER=TBB python script.py` 
    

    Please let us know which of these, if any, helped so we can advise others.

  2. chrisgmorton commented on Aug 13, 2018

    @chrisgmorton
    Author

    Correction, I am using l_mkl_2018.3.222 and m_mkl_2018.3.185 already. I updated my original post.

  3. mattip commented on Aug 13, 2018

    @mattip
    Member

    What about the other workarounds?

    I believe the NumPy team has some relationships/connections with the Intel team and there have been some efforts to make MKL work well with NumPy (and SciPy).

    Unfortunately we have no such connections.

  4. chrisgmorton commented on Aug 13, 2018

    @chrisgmorton
    Author

    Using KMP_INIT_AT_FORK=FALSE resolves this issue on OSX and Linux. Looking at the system install of MKL on Linux and OSX, it appears that TBB is included as standard. Since MKL is huge, I was copying only shared libraries I thought I needed into my distribution and was not including the tbb libraries. I assume OpenMP had to be the threading layer used in both cases as setting the aforementioned environmental variable resolves the hang on both os platforms. When pointing to the system install of MKL, using the TBB threading layer also fixes the issue on both platforms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions