Description
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
ogrisel commentedon Apr 25, 2024
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.
thomasjpfan commentedon Apr 29, 2024
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 likethreadpoolctl.register_openblas_backend("openmp"). Although, this does increase the scope ofthreadpoolctl, the feature feels related.ogrisel commentedon May 7, 2024
I am not sure we can do that as part of
threadpoolctlitself or more precisely, if we do it as part of threadpoolctl, then openblas will use the openmp runtime linked against a native extension shipped withthreadpoolctlbut we would have no guarantee that this is the same runtime as the one linked tosklearn's Cython native extensions withprangeloops.thomasjpfan commentedon May 11, 2024
I do not think
threadpoolctlshould ship any runtimes.I was thinking of using
threadpoolctlto connect the openmp runtime loaded bysklearnwithOpenBLAS. Concretely, Scikit-learn is responsible for callingthreadpoolctl.register_openblas_backend("openmp"). If we want to be more verbose:itamarst commentedon Jun 10, 2026
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.
itamarst commentedon Jun 10, 2026
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
threadpoolctlwithout worrying about which version of OpenMP is in use, andthreadpoolctlcan control it.thomasjpfan commentedon Jun 11, 2026
Is this the solution proposed in #34171?
itamarst commentedon Jun 11, 2026
More like making #34171 better.
Imagine a 10-core machine, and a sklearn
Parallelthat is limited to 5 cores.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 intoprange()→ 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.
jeremiedbb commentedon Jun 11, 2026
FYI I tried to do that in #29403 but it doesn't work as we would like to use it, see OpenMathLib/OpenBLAS#4770.
itamarst commentedon Jun 11, 2026
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 % Nfor jobi.itamarst commentedon Jun 11, 2026
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-todThe 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
lokybackend and withthreadingbackend of joblib. All tests passed (except a couple that always break onthreadingbackend, 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:
(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.)
ogrisel commentedon Jun 16, 2026
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?
itamarst commentedon Jun 16, 2026
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.
itamarst commentedon Jun 16, 2026
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.
itamarst commentedon Jun 16, 2026
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.