Repository navigation
Description
I tried with threadpool_limits(1, user_api=None): with a not so simple case : https://gitlab.com/paugier/tsp-pythran (branch threadpoolctl) on Debian.
The case uses Pythran (through Transonic but I don't see how it could change anything for this) to get an extension accelerated with OpenMP. Pythran uses the system OpenBlas library.
To reproduce (sorry, I don't use Git):
hg clone https://gitlab.com/paugier/tsp-pythran.git
cd tsp-pythran
hg up threadpoolctl
# compile the extension with openmp
transonic tsp.py -pf "-march=native -DUSE_XSIMD -fopenmp"
# wait to get the extension ready
python run-test-omp.py
OMP_NUM_THREADS=1 python run-test-omp.py
The good news is that threadpoolctl manages to reduce the number of threads used with OpenMP. However, I get something strange that I don't understand:
I'm not sure it's an issue, but I get something slower with python run-test-omp.py (or OMP_NUM_THREADS=2 python run-test-omp.py) than with OMP_NUM_THREADS=1 python run-test-omp.py.
I actually get the same behavior if the extension is built without OpenMP, i.e. just with transonic tsp.py.
OMP_NUM_THREADS=1 python run-test-omp.py
[{'filename_prefixes': ('libopenblas',),
'internal_api': 'openblas',
'module_path': '/home/users/augier3pi/.pyenv/versions/3.7.2/lib/python3.7/site-packages/numpy/.libs/libopenblasp-r0-382c8f3a.3.5.dev.so',
'n_thread': 1,
'prefix': 'libopenblas',
'user_api': 'blas',
'version': '0.3.5.dev'},
{'filename_prefixes': ('libiomp', 'libgomp', 'libomp', 'vcomp'),
'internal_api': 'openmp',
'module_path': '/usr/lib/x86_64-linux-gnu/libgomp.so.1',
'n_thread': 1,
'prefix': 'libgomp',
'user_api': 'openmp',
'version': None},
{'filename_prefixes': ('libopenblas',),
'internal_api': 'openblas',
'module_path': '/home/users/augier3pi/.pyenv/versions/3.7.2/lib/python3.7/site-packages/scipy/.libs/libopenblasp-r0-8dca6697.3.0.dev.so',
'n_thread': 1,
'prefix': 'libopenblas',
'user_api': 'blas',
'version': None},
{'filename_prefixes': ('libopenblas',),
'internal_api': 'openblas',
'module_path': '/usr/lib/libopenblas.so.0',
'n_thread': 1,
'prefix': 'libopenblas',
'user_api': 'blas',
'version': None}]
start search
run time = 0.43 s
start search
run time = 0.43 s
start search
run time = 0.44 s
start search
run time = 0.46 s
python run-test-omp.py
[{'filename_prefixes': ('libopenblas',),
'internal_api': 'openblas',
'module_path': '/home/users/augier3pi/.pyenv/versions/3.7.2/lib/python3.7/site-packages/numpy/.libs/libopenblasp-r0-382c8f3a.3.5.dev.so',
'n_thread': 4,
'prefix': 'libopenblas',
'user_api': 'blas',
'version': '0.3.5.dev'},
{'filename_prefixes': ('libiomp', 'libgomp', 'libomp', 'vcomp'),
'internal_api': 'openmp',
'module_path': '/usr/lib/x86_64-linux-gnu/libgomp.so.1',
'n_thread': 4,
'prefix': 'libgomp',
'user_api': 'openmp',
'version': None},
{'filename_prefixes': ('libopenblas',),
'internal_api': 'openblas',
'module_path': '/home/users/augier3pi/.pyenv/versions/3.7.2/lib/python3.7/site-packages/scipy/.libs/libopenblasp-r0-8dca6697.3.0.dev.so',
'n_thread': 4,
'prefix': 'libopenblas',
'user_api': 'blas',
'version': None},
{'filename_prefixes': ('libopenblas',),
'internal_api': 'openblas',
'module_path': '/usr/lib/libopenblas.so.0',
'n_thread': 4,
'prefix': 'libopenblas',
'user_api': 'blas',
'version': None}]
start search
run time = 0.57 s
start search
run time = 0.59 s
start search
run time = 0.58 s
start search
run time = 0.58 s
Activity
jeremiedbb commentedon Mar 27, 2019
I'm not sure to understand what you do. In the code you use the context manager with
with threadpool_limits(1, user_api=None)and the result is different whether you also set theOMP_NUM_THREADSenv var to 1 or 2 ?jeremiedbb commentedon Mar 27, 2019
We're wondering if it could be the overhead of setting the limits with the context manager (which loops over all linked libs). Here's a small script to benchmark it:
The output times are of order 15ms, but if it's run inside a program with lot of loaded libs maybe it would become significant.
Could you try to run it it tsp.py ?
jeremiedbb commentedon Mar 27, 2019
Also it's not much but it would save a little bit of time if you do
instead.
tomMoral commentedon Mar 27, 2019
The number of loaded library should not change when setting
OMP_NUM_THREADS=1so I am not sure this is the cause here.@paugier when you run your program, do you see multiple threads been run and does the number seems different when using the env variable or not?
Also, could you post here the output of
lddon the.sogenerated withpythran? Just to make sure we are not just missing afilename_prefix.paugier commentedon Mar 27, 2019
I tried that and the execution times are the same (because I was anyway timing inside the function and not the loop containing
threadpool_limits).jeremiedbb commentedon Mar 27, 2019
How many cpu cores do you have ?
Can you also try with
OMP_NUM_THREADS=2?paugier commentedon Mar 27, 2019
For the case without OpenMP:
jeremiedbb commentedon Mar 27, 2019
It's strange to see that all these blas are linked. It's hard to tell which blas is used at runtime
paugier commentedon Mar 27, 2019
Indeed
paugier commentedon Mar 27, 2019
Yes, with
python run-test-omp.py(andOMP_NUM_THREADS=2 python run-test-omp.py, andOMP_NUM_THREADS=8 python run-test-omp.py), I see 4 threads (3 doing nearly nothing).With
OMP_NUM_THREADS=1 python run-test-omp.py, there is only one thread and the computation is faster.jeremiedbb commentedon Mar 27, 2019
Sorry to ask you something again but what do you observe if you remove the thread context manager ?
with and without setting
OMP_NUM_THREADS?paugier commentedon Mar 27, 2019
More comprehensive benchmarks...
Pythran using OpenMP
without
threadpool_limits(1, user_api=None)We see acceleration due to OpenMP
with
threadpool_limits(1, user_api=None)It would be nice to get the same result than with
OMP_NUM_THREADS=1.Pythran not using OpenMP
without
threadpool_limits(1, user_api=None)It seems that something uses threads in an inefficient way.
with
threadpool_limits(1, user_api=None)It would be nice to get the same result than with
OMP_NUM_THREADS=1.Same results as without
threadpool_limits(1, user_api=None).ENH improve threadpool_limits ctx manager overhead (#2)
tomMoral commentedon Mar 27, 2019
Thus, this seems to be an issue not related to this library no?
Note that we just reduced the overhead of the context manager so the results should be even closer now when using
threadpool_limitsor not.Let us know if you feel like there is still some issue.
paugier commentedon Mar 27, 2019
Tell me if I'm wrong, but with
threadpool_limits(1, user_api=None), we should get the same results as withOMP_NUM_THREADS=1. It seems that there is something that still tries to use multithreading even whenthreadpool_limits(1)has been called.Isn't it a problem for
threadpoolctl?ogrisel commentedon Mar 27, 2019
OpenBLAS is probably doing something smarter (e.g. static and not dynamic) when
OMP_NUM_THREADS=1globally. I don't think we can trigger that with a dynamic control of the pool sizes.BTW I found another optim in #4 to remove most of the
threadpool_limitscall overhead.paugier commentedon Mar 28, 2019
Yes it's a reasonable guess.
Note that I don't get this difference when Pythran is not used (in this case, the code only uses Python / Numpy). Without Pythran, it's just very inefficient (17 times slower compared to Pythran without OpenMP) but it is as inefficient with and without
OMP_NUM_THREADS=1.One can assume that the problem is specific to Pythran (CC @serge-sans-paille), but until the problem is not really understood, we can also think that it can appear with any extensions (and maybe also in some cases, with Numpy / Scipy (?)), so it seems to me that it is an issue for threadpoolctl / joblib and other tools that would need threadpoolctl to dynamically disable multi-threading and get efficient sequential computations.
In the case presented in this issue, sequential computations are 25% slower with
threadpool_limits(1)than withOMP_NUM_THREADS=1. It's not huge but not negligible neither.ogrisel commentedon Mar 28, 2019
Maybe you can write a simpler BLAS oriented code to diagnose what's happening with Pythran?
The problem is that
OMP_NUM_THREADSis probably controlling the behavior of OpenBLAS even when the latter has not been built with OpenMP (and uses an internally managed libpthread threadpool instead).Maybe just running experiments with then impact of
OPENBLAS_NUM_THREADSand pythran code that involves only linear algebra operations without any for loop would help understand what is the issue.But I don't believe threadpoolctl can do anything in this case.
serge-sans-paille commentedon Mar 28, 2019
@paugier : OpenBLAS tends to spawn thread even if not used (as part of the library startup) and that can end up disturbing the computation. As suggested by @ogrisel
OPENBLAS_NUM_THREADScan be of help here.Your test case is creating lists and making calls to random inside a parallel section, and both operations involve locks or atomics. In order to find the culprit, I would try your setup with an embarrassingly parallel, lockless openmp program compiled by pythran, and check if the problem is reproducible.
tomMoral commentedon Mar 28, 2019
I tend to agree but I am not sure. It looks like openBLAS relies on
OMP_NUM_THREADS=1here but they don't seem to be checking theomp_get_max_threadsto disable the mapping. I did not investigate enough to see if it could be set programatically.Merge pull request joblib#1 from jeremiedbb/nested-openmp-tests
ogrisel commentedon Apr 19, 2019
I think this
disable_mappingswitch is probably the source of the speed-up observed with usingOMP_NUM_THREADS=1orOPENBLAS_NUM_THREADS=1and that cannot be reproduced with the dynamic*set_num_threadsmethods.So this is kind of expected. I don't see how we can do anything about it. Let's close.