Skip to content

Slower without OMP_NUM_THREADS=1 than with OMP_NUM_THREADS=1 #1

Closed

Description

@paugier

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

  1. jeremiedbb commented on Mar 27, 2019

    @jeremiedbb
    Contributor

    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 the OMP_NUM_THREADS env var to 1 or 2 ?

  2. jeremiedbb commented on Mar 27, 2019

    @jeremiedbb
    Contributor

    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:

    from scipy import linalg 
    from threadpoolctl import get_threadpool_limits, threadpool_limits 
    import time 
    
    print(get_threadpool_limits())
     
    for i in range(100): 
        t = time.time() 
        with threadpool_limits(limits=1): 
            pass 
        print(time.time() -t)

    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 ?

  3. jeremiedbb commented on Mar 27, 2019

    @jeremiedbb
    Contributor

    Also it's not much but it would save a little bit of time if you do

    with threadpool_limits(1, user_api=None):
        for _ in range(4):
            tsp.search('search', nb_run=nb_run, dated=True)

    instead.

  4. tomMoral commented on Mar 27, 2019

    @tomMoral
    Contributor

    The number of loaded library should not change when setting OMP_NUM_THREADS=1 so 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 ldd on the .so generated with pythran? Just to make sure we are not just missing a filename_prefix.

  5. paugier commented on Mar 27, 2019

    @paugier
    Author

    Also it's not much but it would save a little bit of time if you do

    with threadpool_limits(1, user_api=None):
        for _ in range(4):
            tsp.search('search', nb_run=nb_run, dated=True)

    instead.

    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).

  6. jeremiedbb commented on Mar 27, 2019

    @jeremiedbb
    Contributor

    How many cpu cores do you have ?
    Can you also try with OMP_NUM_THREADS=2 ?

  7. paugier commented on Mar 27, 2019

    @paugier
    Author

    For the case without OpenMP:

    ldd __pythran__/tsp_ff7dfc4b1a826450385fbb27d8f1763a.cpython-37m-x86_64-linux-gnu.so 
    	linux-vdso.so.1 (0x00007fffb9fd3000)
    	libcblas.so.3 => /usr/lib/libcblas.so.3 (0x00007f8223ba6000)
    	libblas.so.3 => /usr/lib/libblas.so.3 (0x00007f8223948000)
    	libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f82235c6000)
    	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f82232c2000)
    	libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f82230ab000)
    	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f8222e8e000)
    	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f8222aef000)
    	libatlas.so.3 => /usr/lib/libatlas.so.3 (0x00007f822255c000)
    	libgfortran.so.3 => /usr/lib/x86_64-linux-gnu/libgfortran.so.3 (0x00007f8222236000)
    	libopenblas.so.0 => /usr/lib/libopenblas.so.0 (0x00007f82200db000)
    	/lib64/ld-linux-x86-64.so.2 (0x00007f8223fd5000)
    	libquadmath.so.0 => /usr/lib/x86_64-linux-gnu/libquadmath.so.0 (0x00007f821fe9c000)
    
  8. jeremiedbb commented on Mar 27, 2019

    @jeremiedbb
    Contributor
    libcblas.so.3 => /usr/lib/libcblas.so.3 (0x00007f8223ba6000)
    libblas.so.3 => /usr/lib/libblas.so.3 (0x00007f8223948000)
    libopenblas.so.0 => /usr/lib/libopenblas.so.0 (0x00007f82200db000)
    

    It's strange to see that all these blas are linked. It's hard to tell which blas is used at runtime

  9. paugier commented on Mar 27, 2019

    @paugier
    Author

    It's strange to see that all these blas are linked. It's hard to tell which blas is used at runtime

    Indeed

  10. paugier commented on Mar 27, 2019

    @paugier
    Author

    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?

    Yes, with python run-test-omp.py (and OMP_NUM_THREADS=2 python run-test-omp.py, and OMP_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.

    How many cpu cores do you have ?

    lscpu 
    Architecture:          x86_64
    CPU op-mode(s):        32-bit, 64-bit
    Byte Order:            Little Endian
    CPU(s):                4
    On-line CPU(s) list:   0-3
    Thread(s) per core:    1
    Core(s) per socket:    4
    
  11. jeremiedbb commented on Mar 27, 2019

    @jeremiedbb
    Contributor

    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 ?

  12. paugier commented on Mar 27, 2019

    @paugier
    Author

    More comprehensive benchmarks...

    Pythran using OpenMP

    $ rm -rf __pythran__/
    $ transonic tsp.py -pf "-march=native -DUSE_XSIMD -fopenmp"
    WARNING  1 files created or updated needs to be pythranized
    Pythranizing /home/users/augier3pi/Dev/tsp-pythran/__pythran__/tsp.py
    File /home/users/augier3pi/Dev/tsp-pythran/__pythran__/tsp_ff7dfc4b1a826450385fbb27d8f1763a.cpython-37m-x86_64-linux-gnu.so created by Pythran
    

    without threadpool_limits(1, user_api=None)

    We see acceleration due to OpenMP

    $ python run-test-omp.py
    best of 8: 0.49 s
    $ OMP_NUM_THREADS=1 python run-test-omp.py
    best of 8: 0.93 s
    $ OMP_NUM_THREADS=2 python run-test-omp.py
    best of 8: 0.76 s
    $ OMP_NUM_THREADS=4 python run-test-omp.py
    best of 8: 0.48 s
    

    with threadpool_limits(1, user_api=None)

    It would be nice to get the same result than with OMP_NUM_THREADS=1.

    $ python run-test-omp.py
    best of 8: 1.19 s
    $ OMP_NUM_THREADS=1 python run-test-omp.py
    best of 8: 0.92 s
    $ OMP_NUM_THREADS=2 python run-test-omp.py
    best of 8: 1.18 s
    $ OMP_NUM_THREADS=4 python run-test-omp.py
    best of 8: 1.19 s
    

    Pythran not using OpenMP

    $ rm -rf __pythran__/
    $ transonic tsp.py -pf "-march=native -DUSE_XSIMD"
    WARNING  1 files created or updated needs to be pythranized
    Pythranizing /home/users/augier3pi/Dev/tsp-pythran/__pythran__/tsp.py
    File /home/users/augier3pi/Dev/tsp-pythran/__pythran__/tsp_ff7dfc4b1a826450385fbb27d8f1763a.cpython-37m-x86_64-linux-gnu.so created by Pythran
    

    without threadpool_limits(1, user_api=None)

    $ python run-test-omp.py
    best of 8: 1.08 s
    $ OMP_NUM_THREADS=1 python run-test-omp.py
    best of 8: 0.86 s
    $ OMP_NUM_THREADS=2 python run-test-omp.py
    best of 8: 1.08 s
    $ OMP_NUM_THREADS=4 python run-test-omp.py
    best of 8: 1.08 s
    

    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.

    $ python run-test-omp.py
    best of 8: 1.10 s
    $ OMP_NUM_THREADS=1 python run-test-omp.py
    best of 8: 0.87 s
    $ OMP_NUM_THREADS=2 python run-test-omp.py
    best of 8: 1.08 s
    $ OMP_NUM_THREADS=4 python run-test-omp.py
    best of 8: 1.08 s
    

    Same results as without threadpool_limits(1, user_api=None).

  13. tomMoral commented on Mar 27, 2019

    @tomMoral
    Contributor

    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_limits or not.

    Let us know if you feel like there is still some issue.

  14. paugier commented on Mar 27, 2019

    @paugier
    Author

    Thus, this seems to be an issue not related to this library no?

    Tell me if I'm wrong, but with threadpool_limits(1, user_api=None), we should get the same results as with OMP_NUM_THREADS=1. It seems that there is something that still tries to use multithreading even when threadpool_limits(1) has been called.

    Isn't it a problem for threadpoolctl?

  15. ogrisel commented on Mar 27, 2019

    @ogrisel
    Contributor

    OpenBLAS is probably doing something smarter (e.g. static and not dynamic) when OMP_NUM_THREADS=1 globally. 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_limits call overhead.

  16. paugier commented on Mar 28, 2019

    @paugier
    Author

    OpenBLAS is probably doing something smarter (e.g. static and not dynamic) when OMP_NUM_THREADS=1 globally.

    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 with OMP_NUM_THREADS=1. It's not huge but not negligible neither.

  17. ogrisel commented on Mar 28, 2019

    @ogrisel
    Contributor

    Maybe you can write a simpler BLAS oriented code to diagnose what's happening with Pythran?

    The problem is that OMP_NUM_THREADS is 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_THREADS and 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.

  18. serge-sans-paille commented on Mar 28, 2019

    @serge-sans-paille

    @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_THREADS can 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.

  19. tomMoral commented on Mar 28, 2019

    @tomMoral
    Contributor

    But I don't believe threadpoolctl can do anything in this case.

    I tend to agree but I am not sure. It looks like openBLAS relies on OMP_NUM_THREADS=1 here but they don't seem to be checking the omp_get_max_threads to disable the mapping. I did not investigate enough to see if it could be set programatically.

  20. added a commit that references this issue on Apr 16, 2019
  21. ogrisel commented on Apr 19, 2019

    @ogrisel
    Contributor

    I think this disable_mapping switch is probably the source of the speed-up observed with using OMP_NUM_THREADS=1 or OPENBLAS_NUM_THREADS=1 and that cannot be reproduced with the dynamic *set_num_threads methods.

    So this is kind of expected. I don't see how we can do anything about it. Let's close.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions