Skip to content

Configure OpenBLAS to use scikit-learn's OpenMP threadpool #28883

Open

Description

@jeremiedbb

OpenBLAS v0.3.28 will have a new feature allowing OpenBLAS to use the threadpool chosen by the user, (see OpenMathLib/OpenBLAS#4577).

This is very interesting because it would solve a performance issue happening when there's a quick succession of BLAS calls and OpenMP (prange) calls. The issue happens when OpenBLAS and OpenMP don't share the same threadpool because both threadpools are in active wait mode when they're idle (see OpenMathLib/OpenBLAS#3187 for details), which is a current situation since numpy and scipy wheels are built against OpenBLAS with the pthreads threading layer.

This issue is currently impacting some estimators like KMeans (#20642), NMF (#16439), pairwise_distances (#26097), ...

Being able to configure OpenBLAS to use our OpenMP threadpool would allow to get rid of this issue even if numpy and scipy keep building their wheels against OpenBLAS pthreads (which is very likely).

I'm not sure yet if or how OpenMathLib/OpenBLAS#4577 would make this possible so I'm opening this issue to track the progress on this subject.

Activity

  1. ogrisel commented on Apr 25, 2024

    @ogrisel
    Member

    This is indeed interesting and might also be of interest for other people in the ecosystem, e.g. @rgommers and numpy/scipy developers interested in multithreading.

  2. thomasjpfan commented on Apr 29, 2024

    @thomasjpfan
    Member

    Wow, this is amazing! From looking through, OpenMathLib/OpenBLAS#4577, I think we'll need to write and register a callback that hooks up scikit-learn's vendered OpenMP with OpenBLAS.

    If this backend-specific callback code is useful for other projects, is there a way to share it through threadpoolctl? Concretely, something like threadpoolctl.register_openblas_backend("openmp"). Although, this does increase the scope of threadpoolctl, the feature feels related.

  3. ogrisel commented on May 7, 2024

    @ogrisel
    Member

    I am not sure we can do that as part of threadpoolctl itself or more precisely, if we do it as part of threadpoolctl, then openblas will use the openmp runtime linked against a native extension shipped with threadpoolctl but we would have no guarantee that this is the same runtime as the one linked to sklearn's Cython native extensions with prange loops.

  4. thomasjpfan commented on May 11, 2024

    @thomasjpfan
    Member

    if we do it as part of threadpoolctl, then openblas will use the openmp runtime linked against a native extension shipped with threadpoolctl

    I do not think threadpoolctl should ship any runtimes.

    I was thinking of using threadpoolctl to connect the openmp runtime loaded by sklearn with OpenBLAS. Concretely, Scikit-learn is responsible for calling threadpoolctl.register_openblas_backend("openmp"). If we want to be more verbose:

    PATH_TO_LIBOMP = ".../lib/python3.12/site-packages/sklearn/.dylibs/libomp.dylib"
    threadpoolctl.register_openblas_backend(PATH_TO_LIBOMP)
  5. added
    Parallelism and scalability
    free-threading
    on May 27, 2026
  6. itamarst commented on Jun 10, 2026

    @itamarst
    Contributor

    Getting to reading this now, and this could potentially solve one of the big problems in #34171 (undersubscription of OpenBLAS threads) so I'm going to look into how the API works and see it's workable.

  7. itamarst commented on Jun 10, 2026

    @itamarst
    Contributor

    To handle the undersaturation/oversaturation dillema, you don't actually need to use OpenMP's thread pool, you just need to match the OpenMP behavior of thread pool size being thread-local and spinning up a thread pool on demand. You would want to still control it in the same way as OpenMP, though.

    Thus: you could register a custom callback that has this behavior that isn't OpenMP-based, and then registration of this can go in threadpoolctl without worrying about which version of OpenMP is in use, and threadpoolctl can control it.

  8. thomasjpfan commented on Jun 11, 2026

    @thomasjpfan
    Member

    Thus: you could register a custom callback that has this behavior that isn't OpenMP-based, and then registration of this can go in threadpoolctl without worrying about which version of OpenMP is in use, and threadpoolctl can control it.

    Is this the solution proposed in #34171?

  9. itamarst commented on Jun 11, 2026

    @itamarst
    Contributor

    More like making #34171 better.

    Imagine a 10-core machine, and a sklearn Parallel that is limited to 5 cores.

    • Right now, with a pool of 5 processes, each process is configured to have 2 OpenMP threads and two BLAS threads. All is well.
    • In Proof of concept: Thread oversaturation mitigation #34171, with a pool of 5 threads, each thread is configured to have 2 OpenMP threads and that works; each prange() will start two threads, so you get 2*5 = 10 threads. So that's fine. But for OpenBLAS on pthreads, the number of threads you set is for a global thread pool for the whole process. If you set number of threads for OpenBLAS to 2, you only have 2 OpenBLAS threads for the whole process. If you set it to 10, now you have 10 OpenBLAS threads in addition to any concurrent operations, so e.g. if it's a mix of OpenMP and OpenBLAS you might get 18 threads on a 10-core machine (1 Python thread does OpenBLAS operation → 10 threads doing OpenBLAS, meanwhile the other 4 Python threads calling into prange() → 4 * 2 == 8 threads doing OpenMP, for a total of 18 threads doing computation despite only having 10 cores).

    The OpenBLAS API under discussion would allow switching OpenBLAS under pthreads to the same behavior as OpenBLAS that uses OpenMP: the thread pool size is thread-specific. Or to put it another way, you can switch from a process-specific global thread pool to a per-thread thread pool started on demand. This would allow #34171 to get the number of threads of working threads to match the number the cores.

    If this still doesn't make sense let me know and I'll draw some pictures.

  10. jeremiedbb commented on Jun 11, 2026

    @jeremiedbb
    MemberAuthor

    FYI I tried to do that in #29403 but it doesn't work as we would like to use it, see OpenMathLib/OpenBLAS#4770.

  11. itamarst commented on Jun 11, 2026

    @itamarst
    Contributor

    Ah, I missed that (sleep deprived, smoke alarm keeps going off in the middle of the night for no good reason for past few days).

    I am new to OpenMP, but I can see it maybe giving you insufficient control. But if my interpretation of "BLAS wants each iteration of the loop to be executed by a different thread otherwise it goes into infinite waiting" is correct, could be solvable by just launching a thread per job, instead of using an OpenMP for loop? Or, for another interpretation, starting a thread pool of size N and explicitly using thread i % N for job i.

  12. itamarst commented on Jun 11, 2026

    @itamarst
    Contributor

    I have created a sketch of a plug-in OpenBLAS thread pool, intended to cooperate (one way or another) with threadpoolctl: https://github.com/itamarst/openblas-tod

    The interesting bit is https://github.com/itamarst/openblas-tod/blob/main/src/lib.rs#L47

    I then ran the full sklearn test suite on branch from #34171, both with loky backend and with threading backend of joblib. All tests passed (except a couple that always break on threading backend, unrelated).

    Empirically seems like it always gives you a number of jobs smaller than or equal to the number of configured threads for OpenBLAS, so e.g. I've seen:

    STARTING CUSTOM THREAD POOL WITH 6 threads to run 6 jobs from OpenBLAS
    STARTING CUSTOM THREAD POOL WITH 12 threads to run 2 jobs from OpenBLA
    STARTING CUSTOM THREAD POOL WITH 12 threads to run 12 jobs from OpenBLAS
    

    (My CPU has 12 cores.)

    So: my conclusion is that this is a viable approach to enhance #34171 such that number of OpenBLAS pthreads version interacts with thread pools the same way it interacts with process pools (as is the case for OpenMP and therefore OpenMP version of OpenBLAS.)

  13. ogrisel commented on Jun 16, 2026

    @ogrisel
    Member

    This looks interesting. Have you tried to evaluate if starting new threadpool on demand can introduce extra overhead comparing to reusing a global pool, e.g. in cases where there is no nesting?

  14. itamarst commented on Jun 16, 2026

    @itamarst
    Contributor

    Not yet. But the general idea would be to only use this for the nested thread pool case, and for process pools stick to OpenBLAS' default threading.

  15. itamarst commented on Jun 16, 2026

    @itamarst
    Contributor

    I should also add that this was simplest possible implementation. There are probably more sophisticated approaches one could implement, e.g. have a process-level thread pool limited to number of cores (maximal) and then grab a subset of threads to run the jobs.

  16. itamarst commented on Jun 16, 2026

    @itamarst
    Contributor

    Also, while I do think this is worth pursuing long-term, in the short-term it seems that OpenBLAS is buggy when called from multiple threads; notice this fixes some but not all of the issues, and it's not released: OpenMathLib/OpenBLAS#5838

    So I would suggest keeping OpenBLAS in sequential mode, where it just runs in current thread, when using Python thread pools, at least for now.

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

    Parallelism and scalabilityPerformancefree-threading

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions