You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Numpy MKL causes hang with Python Subprocess and Multiprocessing modules. #11734
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:
importnumpyasnpimportsubprocessx=np.arange(10000) # 1000 shows no hang.xnorm=np.linalg.norm(x)
subprocess.run('ls')
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).
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.
Activity
mattip commentedon Aug 13, 2018
Duplicate of #10060, #11041 #10993, and maybe others.
Among the fixes that might work:
set OPENBLAS_NUM_THREADS=1Please let us know which of these, if any, helped so we can advise others.
chrisgmorton commentedon Aug 13, 2018
Correction, I am using l_mkl_2018.3.222 and m_mkl_2018.3.185 already. I updated my original post.
mattip commentedon Aug 13, 2018
What about the other workarounds?
Unfortunately we have no such connections.
chrisgmorton commentedon Aug 13, 2018
Using
KMP_INIT_AT_FORK=FALSEresolves 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.