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
OpenBLAS hangs when calling DGEMM after a Unix fork #294
Then calling GEMM with a 7x7 squared matrix in parent and child process works without issue:
$ ./openblas_fork 7
computing for size 7
c[0][0] is 7.000000
parent d[0][0] is 7.000000
child d[0][0] is 7.000000
Doing the same for a 8x8 matrix will cause the child process to never completes (while burning 100% of a CPU) until I kill it.
Might this be caused by some static initialization step in the OpenBLAS runtime? If so would it be possible during init to store the pid of the process at init time and then later on check that the current process pid still the same and if not reinit the runtime?
Atlas, Apple vecLib and Intel MKL have no trouble with Unix forks. Hence this limitation (bug?) prevents OpenBLAS to be used as a drop-in replacement for those guys. This is especially annoying when used with Python that uses Unix forks in its standard library via the multiprocessing module. As Python threads suffer from a locking issue, many project use multiprocessing as a work around.
Thanks, sorry I did not find it priori to reporting. I will have a deeper look.
While this is not the problem I reported here: we might want to be able to do blas calls in the parent process before and after the fork, so enabling openmp for openblas would be detrimental as this pattern is officially not supported by openmp.
$ CC=gcc-4.7 make TARGET="NEHALEM" NO_AFFINITY=1 USE_OPENMP=1 USE_SIMPLE_THREADED_LEVEL3=1 NO_WARMUP=1
$ make install
Then this programs hangs forever because of libgomp not supporting OpenMP to be used in the parent process before a fork and in the child process (after the fork).
It is my understanding that this OpenMP shortcoming is not implied by the standard itself but results from the specific implementation in libgomp.
In the mean time would it be possible to "fix" OpenBLAS built with USE_OPENMP=0 so as to have the openblas_fork_parent_init not crash nor hang? What change would be involved?
Both R and Python rely on forking for handling parallelization at their level due to limitations of their interpreter design. It would be great if imported libraries such as OpenBLAS would not crash in such a case.
If transparent fork-child detection is too complicated to implement in OpenBLAS itself, at least it would help to be able to have the functions blas_thread_shutdown and blas_thread_init exposed in the openblas dynlib, maybe under names such as openblas_thread_shutdown and openblas_thread_init so that the program doing the fork could call them around the fork.
Compiling statically is not really feasible in my use case: I have many libraries in my ecosystem who would need to compile statically against OpenBLAS: numpy, scipy, scikit-learn and maybe others. The library doing the fork would not to know about all the Python compiled extensions that could have been compiled statically against OpenBLAS. In scikit-learn alone there are at least several compiled extensions (.so files) that would be impacted. The library handling the parallelization using Python multiprocessing module would need to be aware about all of them. This is really not practical as they are considered private API.
I can't reproduce this (current head of the develop branch, Linux x86-64 running on Intel Core i7 (Sandy Bridge), clean build) -- I get a different bug instead where the child process seems to silently die during the DGEMM.
(Btw., dynamic linking is even more important because it should be possible to just drop in OpenBLAS without even recompiling. This is what the Debian/Ubuntu packages for BLAS, ATLAS and OpenBLAS do and it's a very easy way to get a speedup in NumPy and SciPy.)
@larsmans I have never observed a child crash silently: either it hangs with 100% CPU usage (when USE_OPENMP=0) or with 0% CPU usage (when USE_OPENMP=1). In both cases I have to kill the child process manually with pkill...
Alright, then it's probably that under Linux the default compiler is GCC and OpenMP is enabled by default by the OpenBLAS Makefile. This is not the case under OSX where the default C compiler is clang which does not support OpenMP yet.
make NO_LAPACK=1 NO_AFFINITY=1 USE_OPENMP=1 USE_SIMPLE_THREADED_LEVEL3=1 NO_WARMUP=1
seems to work. An additional
cd /opt/OpenBLAS/lib
ln -s libopenblas_sandybridgep-r0.2.8.so libblas.so.3
still gives a significant speedup in np.dot. With some care (avoiding the segfaulting error handling in OpenBLAS), I can even compute matrix products in a joblib.Parallel.
Even when you call a np.dot in the parent process prior to forking with joblib.Parallel? I doubt it. This is the situation I emulate in C with the openblas_fork_parent_init.c program.
CC=gcc-4.7 make NO_AFFINITY=1 USE_OPENMP=1 USE_SIMPLE_THREADED_LEVEL3=1 NO_WARMUP=1
make install PREFIX=/opt/OpenBLAS_with_OpenMP
Note that I did not disable LAPACK with NO_LAPACK=1 as numpy.dot does not work well otherwise (I do not understand why, but anyway LAPACK is required for scipy and scikit-learn so we have to have it enabled).
I then built numpy with this branch and the following site.cfg:
I have to kill the hung process with pkill -f numpy_fork.py.
I have also made another version version of the script numpy_fork_omp_aware.py to make it possible to call omp_set_num_threads(1) on the OpenBLAS lib prior to forking assuming that libgomp was statically linked into it (which is apparently the case by default):
That works, but then OpenBLAS cannot used with multiple threads in the children. That might be a good work around for joblib though as one could expect the size of the joblib process pool to match the number of physical cores of the host machine.
What I don't understand is why omp_get_num_threads() = 1 before I call omp_set_num_threads(1)...
I had not seen this. This is weird. The only difference is the NO_LAPACK=1 flag. It might be the culprit but we actually need LAPACK for other stuff in our case.
Sorry for the rebase noise... I have just opened PR #343 to submit a fix for this issue. I tested it with gcc 4.7 with and without OpenMP (to check that the fork test is skipped with OpenMP) and clang-500.2.79 with and without USE_THREAD=0 (to check that it still builds in sequential mode).
I have not tested it under Windows but the fork test should not be built under windows (and the problem does not exist there as Windows does not implement fork).
I leave the affinity stuff of another PR as I would like to write a test for this case as well but I don't have the time to do it now.
@cartazio sorry for that. I don't really understand how your build works but that might be caused buy the default OMP build that does not require the link to the pthread library. Here is a new PR to try and fix that: #345
Activity
xianyi commentedon Sep 5, 2013
This is same to #240
ogrisel commentedon Sep 5, 2013
Thanks, sorry I did not find it priori to reporting. I will have a deeper look.
While this is not the problem I reported here: we might want to be able to do blas calls in the parent process before and after the fork, so enabling openmp for openblas would be detrimental as this pattern is officially not supported by openmp.
ogrisel commentedon Sep 5, 2013
To make myself clearer, here is a new version of the program:
If I build OpenBLAS with:
Then this programs hangs forever because of libgomp not supporting OpenMP to be used in the parent process before a fork and in the child process (after the fork).
There is a bug on GCC here: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52303 unfortunately it was deemed invalid by the GCC developers some time ago. This shortcoming of OpenMP is also documented in this OpenMP howto: http://bisqwit.iki.fi/story/howto/openmp/#OpenmpAndFork
It is my understanding that this OpenMP shortcoming is not implied by the standard itself but results from the specific implementation in libgomp.
In the mean time would it be possible to "fix" OpenBLAS built with
USE_OPENMP=0so as to have theopenblas_fork_parent_initnot crash nor hang? What change would be involved?Both R and Python rely on forking for handling parallelization at their level due to limitations of their interpreter design. It would be great if imported libraries such as OpenBLAS would not crash in such a case.
ogrisel commentedon Sep 5, 2013
If transparent fork-child detection is too complicated to implement in OpenBLAS itself, at least it would help to be able to have the functions
blas_thread_shutdownandblas_thread_initexposed in the openblas dynlib, maybe under names such asopenblas_thread_shutdownandopenblas_thread_initso that the program doing the fork could call them around the fork.Compiling statically is not really feasible in my use case: I have many libraries in my ecosystem who would need to compile statically against OpenBLAS: numpy, scipy, scikit-learn and maybe others. The library doing the fork would not to know about all the Python compiled extensions that could have been compiled statically against OpenBLAS. In scikit-learn alone there are at least several compiled extensions (
.sofiles) that would be impacted. The library handling the parallelization using Python multiprocessing module would need to be aware about all of them. This is really not practical as they are considered private API.larsmans commentedon Sep 5, 2013
I can't reproduce this (current head of the
developbranch, Linux x86-64 running on Intel Core i7 (Sandy Bridge), clean build) -- I get a different bug instead where the child process seems to silently die during the DGEMM.(Btw., dynamic linking is even more important because it should be possible to just drop in OpenBLAS without even recompiling. This is what the Debian/Ubuntu packages for BLAS, ATLAS and OpenBLAS do and it's a very easy way to get a speedup in NumPy and SciPy.)
ogrisel commentedon Sep 5, 2013
@larsmans I have never observed a child crash silently: either it hangs with 100% CPU usage (when
USE_OPENMP=0) or with 0% CPU usage (whenUSE_OPENMP=1). In both cases I have to kill the child process manually withpkill...larsmans commentedon Sep 5, 2013
Correction, you're right, the child processes keep running in the background. I noticed when I added a
waitpidto the program.ogrisel commentedon Sep 5, 2013
Alright, then it's probably that under Linux the default compiler is GCC and OpenMP is enabled by default by the OpenBLAS Makefile. This is not the case under OSX where the default C compiler is clang which does not support OpenMP yet.
larsmans commentedon Sep 5, 2013
@ogrisel: @xianyi's suggestion of compiling with
seems to work. An additional
still gives a significant speedup in
np.dot. With some care (avoiding the segfaulting error handling in OpenBLAS), I can even compute matrix products in ajoblib.Parallel.ogrisel commentedon Sep 5, 2013
Even when you call a
np.dotin the parent process prior to forking withjoblib.Parallel? I doubt it. This is the situation I emulate in C with theopenblas_fork_parent_init.cprogram.larsmans commentedon Sep 5, 2013
Yes, that seems to work too.
ogrisel commentedon Sep 5, 2013
But the
openblas_fork_parent_init.cprogram is able to trigger the crash no?larsmans commentedon Sep 5, 2013
Not with those build flags.
ogrisel commentedon Sep 5, 2013
I just recompiled OpenBLAS with:
Note that I did not disable LAPACK with
NO_LAPACK=1asnumpy.dotdoes not work well otherwise (I do not understand why, but anyway LAPACK is required for scipy and scikit-learn so we have to have it enabled).I then built numpy with this branch and the following
site.cfg:Then when run this numpy_fork.py script I get a hung child process:
I have to kill the hung process with
pkill -f numpy_fork.py.I have also made another version version of the script numpy_fork_omp_aware.py to make it possible to call
omp_set_num_threads(1)on the OpenBLAS lib prior to forking assuming that libgomp was statically linked into it (which is apparently the case by default):That works, but then OpenBLAS cannot used with multiple threads in the children. That might be a good work around for
joblibthough as one could expect the size of the joblib process pool to match the number of physical cores of the host machine.What I don't understand is why
omp_get_num_threads() = 1before I callomp_set_num_threads(1)...ogrisel commentedon Sep 5, 2013
I had not seen this. This is weird. The only difference is the
NO_LAPACK=1flag. It might be the culprit but we actually need LAPACK for other stuff in our case.69 remaining items
ogrisel commentedon Feb 19, 2014
@njsmith agreed, let us postpone the affinity issue for now and open a new issue once #294 is fixed.
@xianyi I think that
__builtin_expectis supported by most compilers but I am not sure about Microsoft's compiler so I just removed them.I'll try to have a look at the
USE_THREAD=0case soon.xianyi commentedon Feb 19, 2014
@ogrisel , I like lazy init in
exec_blas_*.I prefer
unlikelymacro in https://gist.github.com/xianyi/9091495 .For affinity, in
init.c, you can usesched_setaffinity(0, sizeof(cpu_orig_mask), &cpu_orig_mask[0]);to restore the default affinity setting.ogrisel commentedon Feb 19, 2014
Sorry for the rebase noise... I have just opened PR #343 to submit a fix for this issue. I tested it with gcc 4.7 with and without OpenMP (to check that the fork test is skipped with OpenMP) and clang-500.2.79 with and without USE_THREAD=0 (to check that it still builds in sequential mode).
I have not tested it under Windows but the fork test should not be built under windows (and the problem does not exist there as Windows does not implement fork).
I leave the affinity stuff of another PR as I would like to write a test for this case as well but I don't have the time to do it now.
Merge pull request #343 from ogrisel/fix-294-fork-safe-pthread
xianyi commentedon Feb 19, 2014
@ogrisel ,
Thank you for the patch.
cartazio commentedon Feb 20, 2014
ummm, now my haskell bindings don't built any more!
https://travis-ci.org/wellposed/hOpenBLAS/jobs/19229988#L604
ogrisel commentedon Feb 20, 2014
@cartazio sorry for that. I don't really understand how your build works but that might be caused buy the default OMP build that does not require the link to the pthread library. Here is a new PR to try and fix that: #345
ogrisel commentedon Feb 20, 2014
@cartazio please feel free to comment on #345 if it fixes your build issue or not.