diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 29ea90f4..c23505c6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -125,7 +125,7 @@ jobs: # Linux environment with development versions of numpy and scipy - name: pylatest_pip_dev - os : ubuntu-latest + os : ubuntu-24.04 PACKAGER: "pip-dev" PYTHON_VERSION: "*" CC_OUTER_LOOP: "gcc" @@ -134,7 +134,7 @@ jobs: # Linux + Python 3.10 and homogeneous runtime nesting. - name: py310_conda_openblas_clang_clang - os: ubuntu-latest + os: ubuntu-24.04 PACKAGER: "conda" PYTHON_VERSION: "3.10" BLAS: "openblas" @@ -145,7 +145,7 @@ jobs: # Linux environment with MKL and Clang (known to be unsafe for # threadpoolctl) to only test the warning from multiple OpenMP. - name: pylatest_conda_mkl_clang_gcc - os: ubuntu-latest + os: ubuntu-24.04 PYTHON_VERSION: "*" PACKAGER: "conda" BLAS: "mkl" @@ -154,7 +154,7 @@ jobs: TESTS: "libomp_libiomp_warning" # Linux environment with MKL, safe for threadpoolctl. - name: pylatest_conda_mkl_gcc_gcc - os: ubuntu-latest + os: ubuntu-24.04 PYTHON_VERSION: "*" PACKAGER: "conda" BLAS: "mkl" @@ -166,7 +166,7 @@ jobs: # Linux + Python 3.11 with numpy / scipy installed with pip from PyPI # and heterogeneous OpenMP runtimes. - name: py311_pip_openblas_gcc_clang - os: ubuntu-latest + os: ubuntu-24.04 PACKAGER: "pip" PYTHON_VERSION: "3.11" CC_OUTER_LOOP: "gcc" @@ -174,7 +174,7 @@ jobs: # Linux + conda-forge Python RC, extra deps from PyPI. # OpenCV is omitted: no wheels for the current RC ABI yet. - name: pyrc_pip - os: ubuntu-latest + os: ubuntu-24.04 PACKAGER: "pip" # Using the "*" marker does not select the latest RC release # from the conda-forge `python_rc` channel hence the need to pin @@ -185,7 +185,7 @@ jobs: CC_INNER_LOOP: "gcc" # Linux + conda-forge free-threaded Python RC, extra deps from PyPI. - name: pyrc_ft_pip - os: ubuntu-latest + os: ubuntu-24.04 PACKAGER: "pip" # Using the "*" marker does not select the latest RC release # from the conda-forge `python_rc` channel hence the need to pin @@ -197,7 +197,7 @@ jobs: CC_INNER_LOOP: "gcc" # Linux environment with numpy from conda-forge channel and openblas-openmp - name: pylatest_conda_forge - os: ubuntu-latest + os: ubuntu-24.04 PACKAGER: "conda-forge" PYTHON_VERSION: "*" BLAS: "openblas" @@ -207,7 +207,7 @@ jobs: CC_INNER_LOOP: "gcc" # Linux environment with no numpy and heterogeneous OpenMP runtimes. - name: pylatest_conda_nonumpy_gcc_clang - os: ubuntu-latest + os: ubuntu-24.04 PACKAGER: "conda" PYTHON_VERSION: "*" NO_NUMPY: "true" @@ -216,7 +216,7 @@ jobs: # Linux environments with numpy linked to BLIS - name: pylatest_blis_gcc_clang_openmp - os: ubuntu-latest + os: ubuntu-24.04 PYTHON_VERSION: "*" INSTALL_BLAS: "blis" BLIS_NUM_THREAEDS: "4" @@ -225,7 +225,7 @@ jobs: BLIS_CC: "clang-18" BLIS_ENABLE_THREADING: "openmp" - name: pylatest_blis_clang_gcc_pthreads - os: ubuntu-latest + os: ubuntu-24.04 PYTHON_VERSION: "*" INSTALL_BLAS: "blis" BLIS_NUM_THREADS: "4" @@ -234,7 +234,7 @@ jobs: BLIS_CC: "gcc-12" BLIS_ENABLE_THREADING: "pthreads" - name: pylatest_blis_no_threading - os: ubuntu-latest + os: ubuntu-24.04 PYTHON_VERSION: "*" INSTALL_BLAS: "blis" BLIS_NUM_THREADS: "1" @@ -245,7 +245,7 @@ jobs: # Linux env with FlexiBLAS - name: pylatest_flexiblas - os: ubuntu-latest + os: ubuntu-24.04 PYTHON_VERSION: "*" INSTALL_BLAS: "flexiblas" PLATFORM_SPECIFIC_PACKAGES: "mkl" @@ -254,7 +254,7 @@ jobs: # Python 3.14, free-threaded - name: py314_freethreaded_pip_openblas - os: ubuntu-latest + os: ubuntu-24.04 PACKAGER: "conda-forge" BLAS: "openblas" PYTHON_VERSION: "3.14" diff --git a/CHANGES.md b/CHANGES.md index eb539e0d..8f1bbc12 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,11 @@ -3.9.0 (under development) +3.8.1 (under development) ========================= +- Fix a number of race conditions in OpenBLAS-pthreads/win32 controlling thread + logic, that could result in unexpected number of threads being set. + https://github.com/joblib/threadpoolctl/pull/300 + + 3.8.0 (2026-11-05) ================== diff --git a/README.md b/README.md index ec393ffa..3d4a4905 100644 --- a/README.md +++ b/README.md @@ -297,10 +297,12 @@ with ( # APIs operate on a process-wide basis. Because it's a context manager, it # will restore the limits after the ``with`` is done. controller.limit(limits=1), - # Make sure each Python worker thread also calls threadpool_limits(). If - # you're using another thread pool class, you will need to do so some other - # way. - ThreadPoolExecutor(4, initializer=lambda: controller.limit(limits=1)) as pool, + # Make sure each Python worker thread also sets limits, using an API that + # is specifically for Python worker threads. If you're using another thread + # pool class, you will need to do so some other way. + ThreadPoolExecutor( + 4, initializer=lambda: controller.limit_in_worker_thread(limits=1) + ) as pool, ): # ... run some BLAS-using code in the thread pool ... pool.map(somefunc, someargs) @@ -309,7 +311,9 @@ with ( controller = ThreadpoolController() with ( controller.limit(limits=2), - ThreadPoolExecutor(4, initializer=lambda: controller.limit(limits=2)) as pool, + ThreadPoolExecutor( + 4, initializer=lambda: controller.limit_in_worker_thread(limits=2) + ) as pool, ): # ... run some BLAS-using code in the thread pool ... pool.map(somefunc, someargs) @@ -323,7 +327,9 @@ from threadpoolctl import ThreadpoolController controller = ThreadpoolController() try: limiter = controller.limit(limits=1) - with ThreadPoolExecutor(4, initializer=lambda: controller.limit(limits=1)) as pool: + with ThreadPoolExecutor( + 4, initializer=lambda: controller.limit_in_worker_thread(limits=1) + ) as pool: # ... run some BLAS-using code in the thread pool ... pool.map(somefunc, someargs) finally: @@ -350,7 +356,7 @@ with CONTROLLER.limit(limits=1): def limit_then_do_work(*args, **kwargs): # Set a limit on OpenMP in the current thread: - CONTROLLER.limit(limits=1) + CONTROLLER.limit_in_worker_thread(limits=1) # Do the actual work: return do_real_work_with_openmp(*args, **kwargs) @@ -366,7 +372,7 @@ with CONTROLLER.limit(limits=4): with CONTROLLER.limit(limits=2): def limit_then_do_work2(*args, **kwargs): - CONTROLLER.limit(limits=2) + CONTROLLER.limit_in_worker_thread(limits=2) return do_even_more_real_work_with_openmp(*args, **kwargs) results3 = POOL.map(limit_then_do_work2, results2) diff --git a/tests/test_threadpoolctl.py b/tests/test_threadpoolctl.py index 93c32292..86e7b84f 100644 --- a/tests/test_threadpoolctl.py +++ b/tests/test_threadpoolctl.py @@ -8,7 +8,7 @@ import subprocess import sys import time -from threading import Lock, Thread, current_thread +from threading import Event, Lock, Thread, current_thread import pytest @@ -21,6 +21,7 @@ OpenBLASController, ThreadpoolController, _determine_thread_limit_scope, + _ThreadpoolLimiter, get_cached_controller, reset_cached_controller, threadpool_info, @@ -873,7 +874,7 @@ def restore(): def test_openblas_controlling_thread_cleared_on_limiter_context_manager_exit(request): - libs, lib, orig_limit = _require_resizable_non_openmp_openblas() + libs, orig_limits = _require_resizable_non_openmp_openblas() request.addfinalizer(_restore_openblas_thread_counts(libs)) with threadpool_limits(limits=1): @@ -883,11 +884,11 @@ def test_openblas_controlling_thread_cleared_on_limiter_context_manager_exit(req assert _openblas_controlling_thread() is current_thread() assert _openblas_controlling_thread() is None - assert lib.get_num_threads() == orig_limit + assert [lib.get_num_threads() for lib in libs] == orig_limits def test_openblas_controlling_thread_cleared_on_decorator_exit_non_recursive(request): - libs, lib, orig_limit = _require_resizable_non_openmp_openblas() + libs, orig_limits = _require_resizable_non_openmp_openblas() request.addfinalizer(_restore_openblas_thread_counts(libs)) @threadpool_limits.wrap(limits=1) @@ -902,16 +903,15 @@ def outer(): outer() assert _openblas_controlling_thread() is None - assert lib.get_num_threads() == orig_limit + assert [lib.get_num_threads() for lib in libs] == orig_limits def _require_resizable_non_openmp_openblas(): libs = _non_openmp_openblas() - lib = libs[0] - orig = lib.get_num_threads() - if orig is None or orig < 2: + orig = [lib.get_num_threads() for lib in libs] + if not orig or max(orig) < 2: pytest.skip("Test requires a non-OpenMP OpenBLAS with at least 2 threads") - return libs, lib, orig + return libs, orig @pytest.mark.parametrize( @@ -921,8 +921,9 @@ def test_recursive_wrap_releases_the_shared_pool_claim(request, make_wrap): # One wrapped function calling itself must keep the limit between the inner # return and the outer return, then restore the original count and drop the # controlling thread. Another thread can then set the count. - libs, lib, orig_limit = _require_resizable_non_openmp_openblas() + libs, orig_limits = _require_resizable_non_openmp_openblas() request.addfinalizer(_restore_openblas_thread_counts(libs)) + lib = libs[0] # To avoid annoying libiomp/omp warning happening at import time. wrap = make_wrap() @@ -944,7 +945,7 @@ def recurse(depth): assert lib.get_num_threads() == 1 recurse(1) - assert lib.get_num_threads() == orig_limit + assert lib.get_num_threads() == orig_limits[0] observed = {} @@ -962,8 +963,9 @@ def other_thread(): def test_recursive_wrap_releases_the_shared_pool_claim_exception(request): - libs, lib, orig_limit = _require_resizable_non_openmp_openblas() + libs, orig_limits = _require_resizable_non_openmp_openblas() request.addfinalizer(_restore_openblas_thread_counts(libs)) + lib = libs[0] @threadpool_limits.wrap(limits=1, user_api="blas") def recurse(depth): @@ -977,7 +979,7 @@ def recurse(depth): with pytest.raises(ZeroDivisionError): recurse(1) - assert lib.get_num_threads() == orig_limit + assert lib.get_num_threads() == orig_limits[0] assert _openblas_controlling_thread() is None @@ -1060,19 +1062,198 @@ def test_openblas_controlling_thread_manual_increment_decrement_api(request): def test_restore_original_limits_keeps_openblas_controlling_thread(request): - libs, lib, orig = _require_resizable_non_openmp_openblas() + libs, orig_limits = _require_resizable_non_openmp_openblas() request.addfinalizer(_restore_openblas_thread_counts(libs)) - limiter = threadpool_limits(limits=1) - assert lib.get_num_threads() == 1 - assert lib._SHARED_STATE.controlling_thread is current_thread() + limiter = ThreadpoolController().select(internal_api="openblas").limit(limits=1) + for lib in libs: + assert lib.get_num_threads() == 1 + assert lib._SHARED_STATE.controlling_thread is current_thread() limiter.restore_original_limits() - assert lib.get_num_threads() == orig - assert lib._SHARED_STATE.controlling_thread is current_thread() + for lib, orig in zip(libs, orig_limits): + assert lib.get_num_threads() == orig + assert lib._SHARED_STATE.controlling_thread is current_thread() OpenBLASController.clear_controlling_thread() - assert lib._SHARED_STATE.controlling_thread is None + for lib in libs: + assert lib._SHARED_STATE.controlling_thread is None + + +@pytest.mark.parametrize("user_api", ["openmp", "blas", None]) +def test_limit_in_worker_thread_integer(user_api): + """Python worker thread API should not set limits on shared thread pools.""" + flexiblas_controller = ThreadpoolController().select(internal_api="flexiblas") + if flexiblas_controller: + pytest.skip( + "FlexiBLAS means there are two controllers for same OpenBLAS " + "(direct + FlexiBLAS) which causes test failures." + ) + + controller = ThreadpoolController() + original_limits = controller.info() + + limiter = controller.limit_in_worker_thread(limits=1, user_api=user_api) + assert isinstance(limiter, _ThreadpoolLimiter) + for lib, info in zip(controller.lib_controllers, original_limits): + # If this logic ever grows beyond OpenBLAS, this check should be made + # more general: + if isinstance(lib, OpenBLASController) and lib.threading_layer != "openmp": + # Should be unchanged: + assert lib.get_num_threads() == info["num_threads"] + else: + assert lib.get_num_threads() == 1 + limiter.restore_original_limits() + assert controller.info() == original_limits + + +@pytest.mark.parametrize("limit", [[], {}, "sequential_blas_under_openmp", None]) +def test_limit_in_worker_thread_not_integer(limit): + """Python worker thread API should not set limits on shared thread pools.""" + controller = ThreadpoolController() + original_limits = controller.info() + + with pytest.raises(TypeError, match="only int is supported"): + controller.limit_in_worker_thread(limits=limit) + assert controller.info() == original_limits + + +def _race_condition_skip_problem_platforms(): + flexiblas_controller = ThreadpoolController().select(internal_api="flexiblas") + + if sys.platform == "win32": + pytest.xfail("OpenMP on Windows has its own issues with overlapping setting") + if flexiblas_controller: + pytest.skip( + "FlexiBLAS means there are two controllers for same OpenBLAS " + "(direct + FlexiBLAS) which causes test failures." + ) + + +def test_overlapping_context_manager_restores_correctly(request): + """A thread that failed to set should also not restore.""" + _race_condition_skip_problem_platforms() + + libs, orig_limits = _require_resizable_non_openmp_openblas() + request.addfinalizer(_restore_openblas_thread_counts(libs)) + openmp = ThreadpoolController().select(user_api="openmp") + original_openmp_info = openmp.info() + + a_in, b_in, a_out = (Event() for _ in range(3)) + + a_limits_correct = [] + b_limits_correct = [] + + def A(): + with threadpool_limits(2): + a_in.set() + b_in.wait() + a_limits_correct.append(openmp.info() == original_openmp_info) + a_out.set() + + def B(): + a_in.wait() + with threadpool_limits(1): # snapshot taken while A's limit is active + b_in.set() + a_out.wait() + b_limits_correct.append(openmp.info() == original_openmp_info) + + ts = [Thread(target=A), Thread(target=B)] + for t in ts: + t.start() + for t in ts: + t.join() + + assert a_limits_correct == [True] + assert b_limits_correct == [True] + assert [lib.get_num_threads() for lib in libs] == orig_limits + assert openmp.info() == original_openmp_info + + +def test_overlapping_set_and_restore(request): + """A thread that failed to set should also not restore.""" + _race_condition_skip_problem_platforms() + + libs, orig_limits = _require_resizable_non_openmp_openblas() + request.addfinalizer(_restore_openblas_thread_counts(libs)) + openmp = ThreadpoolController().select(user_api="openmp") + original_openmp_info = openmp.info() + + a_in, b_in, a_out = (Event() for _ in range(3)) + + a_limits_correct = [] + b_limits_correct = [] + + def A(): + limiter = threadpool_limits(2) + a_in.set() + b_in.wait() + limiter.restore_original_limits() + a_limits_correct.append(openmp.info() == original_openmp_info) + a_out.set() + + def B(): + a_in.wait() + limiter = threadpool_limits(1) # snapshot taken while A's limit is active + b_in.set() + a_out.wait() + limiter.restore_original_limits() + b_limits_correct.append(openmp.info() == original_openmp_info) + + ts = [Thread(target=A), Thread(target=B)] + for t in ts: + t.start() + for t in ts: + t.join() + + assert a_limits_correct == [True] + assert b_limits_correct == [True] + assert [lib.get_num_threads() for lib in libs] == orig_limits + assert openmp.info() == original_openmp_info + + +def test_get_current_info_race_condition(request): + """Test for race condition in multi-threaded OpenBLAS-pthreads usage. + + 1. Thread A sets limit L. + 2. Thread B gets limit L in `_ThreadpoolLimiter.__init__`. + 3. Thread A restores to original limit O and releases control. + 4. Thread B continues `__init__`, setting a limit and taking control. + 5. Thread B restores to limit L. + """ + _race_condition_skip_problem_platforms() + + libs, orig_limits = _require_resizable_non_openmp_openblas() + request.addfinalizer(_restore_openblas_thread_counts(libs)) + + a_in, b_retrieved, a_out = (Event() for _ in range(3)) + + def A(): + with threadpool_limits(2): + a_in.set() + b_retrieved.wait() + a_out.set() + + class racy_threadpool_limits(threadpool_limits): + def _set_threadpool_limits(self): + # Tell A it can finish up: + b_retrieved.set() + # Wait until thread A is finished, so we can take control: + a_out.wait() + super()._set_threadpool_limits() + + def B(): + a_in.wait() + with racy_threadpool_limits(1): + pass + + ts = [Thread(target=A), Thread(target=B)] + for t in ts: + t.start() + for t in ts: + t.join() + + assert [lib.get_num_threads() for lib in libs] == orig_limits # skip test if not run in a azure pipelines job since it relies on a specific flexiblas diff --git a/threadpoolctl.py b/threadpoolctl.py index 40377c03..4f7861ac 100644 --- a/threadpoolctl.py +++ b/threadpoolctl.py @@ -201,6 +201,31 @@ def get_and_set() -> None: OpenBLASController.clear_controlling_thread(force=True) +# Returned from set_num_threads() to indicate that setting did not happen, +# typically because the current thread is not the controlling thread. +_DID_NOT_SET = object() + + +@dataclass +class _THIS_THREAD_TOOK_CONTROL: + """Indicates the current thread took control when calling ``set_num_threads()``. + + Includes number of threads set previously, right after control was released. + """ + + previous_num_threads: int + + +@dataclass +class _ALREADY_HAD_CONTROL: + """Indicates control was needed, but this thread already had it. + + Includes number of threads set previously. + """ + + previous_num_threads: int + + class LibController(ABC): """Abstract base class for the individual library controllers @@ -550,6 +575,9 @@ def clear_controlling_thread(cls, force: bool = False): cls._SHARED_STATE.controlling_thread = None def set_num_threads(self, num_threads): + self._set_num_threads_with_extra_info(num_threads) + + def _set_num_threads_with_extra_info(self, num_threads): if self.threading_layer == "openmp": # Per-thread limits, so no need to be constraining: return self._real_set_num_threads(num_threads) @@ -559,10 +587,13 @@ def set_num_threads(self, num_threads): this_thread = current_thread() controlling_thread = self._SHARED_STATE.controlling_thread if controlling_thread is this_thread: - # This thread controls OpenBLAS: - return self._real_set_num_threads(num_threads) + # This thread already controls OpenBLAS: + previous_threads = self.get_num_threads() + self._real_set_num_threads(num_threads) + return _ALREADY_HAD_CONTROL(previous_threads) elif controlling_thread is None or not controlling_thread.is_alive(): # No thread controls OpenBLAS, so try to take control: + previous_num_threads = None with self._SHARED_STATE.lock: # Another thread might have changed it, so ensure that's not # the case: @@ -571,12 +602,16 @@ def set_num_threads(self, num_threads): or not self._SHARED_STATE.controlling_thread.is_alive() ): self._SHARED_STATE.controlling_thread = this_thread - # Controlling thread should now be set, to either this thread or - # another, so try again: - return self.set_num_threads(num_threads) + previous_num_threads = self.get_num_threads() + if previous_num_threads is not None: + self._real_set_num_threads(num_threads) + return _THIS_THREAD_TOOK_CONTROL(previous_num_threads) + else: + # Another thread took control: + return _DID_NOT_SET else: # Some other thread controls OpenBLAS: do nothing. - return + return _DID_NOT_SET def _real_set_num_threads(self, num_threads): # The OpenBLAS limiting API is process-wide, and we want current thread @@ -656,6 +691,8 @@ def decrement(self): """Decrement ownership.""" self.count -= 1 if self.count <= 0: + # If we're not currently the controlling thread, this will do + # nothing, which is the desired behavior: OpenBLASController.clear_controlling_thread() @@ -1105,7 +1142,9 @@ def __init__(self, controller, *, limits=None, user_api=None): self._limits, self._user_api = _check_limiter_params( controller, limits, user_api ) - self._original_info = self._controller.info() + self._original_info = dict( + zip(self._controller.lib_controllers, self._controller.info()) + ) self._set_threadpool_limits() def __enter__(self): @@ -1125,9 +1164,7 @@ def wrap(cls, controller, *, limits=None, user_api=None): def restore_original_limits(self): """Set the limits back to their original values""" - for lib_controller, original_info in zip( - self._controller.lib_controllers, self._original_info - ): + for lib_controller, original_info in self._original_info.items(): lib_controller.set_num_threads(original_info["num_threads"]) # Alias of `restore_original_limits` for backward compatibility @@ -1144,7 +1181,7 @@ def get_original_num_threads(self): for user_api in self._user_api: limits = [ lib_info["num_threads"] - for lib_info in self._original_info + for lib_info in self._original_info.values() if lib_info["user_api"] == user_api ] limits = set(limits) @@ -1174,6 +1211,8 @@ def _set_threadpool_limits(self): if self._limits is None: return + took_control = False + for lib_controller in self._controller.lib_controllers: # self._limits is a dict {key: num_threads} where key is either a # filepath, a prefix, or a user_api. Libraries are matched in that @@ -1188,7 +1227,50 @@ def _set_threadpool_limits(self): continue if num_threads is not None: - lib_controller.set_num_threads(num_threads) + if ( + type(lib_controller) is not OpenBLASController + or lib_controller.threading_layer == "openmp" + ): + lib_controller.set_num_threads(num_threads) + continue + + # Special-case OpenBLASController API that returns additional + # information, only used in the pthreads/windows threading + # layer case: + result = lib_controller._set_num_threads_with_extra_info(num_threads) + + if result is _DID_NOT_SET: + # Didn't set, so shouldn't restore either. Lacking this, if + # this thread later became the controlling thread during + # restoration, it might restore the wrong value. + self._original_info.pop(lib_controller, None) + elif isinstance(result, _THIS_THREAD_TOOK_CONTROL): + # Now that we took control: + # + # 1. Recorded limits for OpenBLAS-pthreads might be wrong, + # since they're global and another thread may have been + # in control and changed them since they were recorded + # in self._original_info. + # 2. Since this thread is now in control, no other thread + # can change that behind our back anymore + # 3. There might be multiple OpenBLAS-pthreads loaded. + # + # So, first set update info for this one, and then set + # took_control to True so later sets of OpenBLAS-pthreads + # can also be updated. + self._original_info[lib_controller]["num_threads"] = ( + result.previous_num_threads + ) + took_control = True + elif took_control and isinstance(result, _ALREADY_HAD_CONTROL): + # This happens if there are multiple OpenBLAS-pthreads + # loaded, e.g. NumPy and SciPy installed from PyPI. Only + # want to do this if we took control, cause that is the + # only case where previously set value might have been + # changed since we recorded it originally. + self._original_info[lib_controller]["num_threads"] = ( + result.previous_num_threads + ) class _ThreadpoolLimiterDecorator(ContextDecorator): @@ -1456,6 +1538,55 @@ def limit(self, *, limits=None, user_api=None): """ return _ThreadpoolLimiter(self, limits=limits, user_api=user_api) + def limit_in_worker_thread(self, *, limits=None, user_api=None): + """Like ``limit()``, but for use in Python thread pool workers. + + This has the same behavior and similar arguments to ``limit()``, except + that ``limits`` may only be an integer, and it may skip setting limits + on some libraries that are process-wide and should therefore only have + their limit set in the main thread. + + Parameters + ---------- + limits : int + Sets the maximum number of threads to `limits` for each + library selected by `user_api`, except it will skip setting limits + on some libraries that are process-wide and should therefore only + have their limit set in the main thread. + + user_api : {USER_APIS} or None (default=None) + APIs of libraries to limit. Used only if `limits` is an int. + See limit() for more details. + """ + if not isinstance(limits, int): + raise TypeError( + "only int is supported as limit for limit_in_worker_thread()" + ) + + new_limits = [] + + for lib_controller in self.lib_controllers: + # If this logic grows beyond OpenBLAS, maybe add a new API to + # LibController to detect shared process pools: + if ( + isinstance(lib_controller, OpenBLASController) + and lib_controller.threading_layer != "openmp" + ): + # Worker threads shouldn't try to set OpenBLAS-pthreads global + # state in their initializer, because it might never get restored. + continue + + # "filepath" is for threadpoolctl 3.8+, "prefix" for backwards compat + # with older versions. + new_limits.append( + { + "filepath": lib_controller.filepath, + "num_threads": limits, + } + ) + + return self.limit(limits=new_limits, user_api=user_api) + @_format_docstring( USER_APIS=", ".join(f'"{api}"' for api in _ALL_USER_APIS), BLAS_LIBS=", ".join(_ALL_BLAS_LIBRARIES), @@ -1555,6 +1686,13 @@ def _load_libraries(self): # Non-Linux Unix platforms. self._find_libraries_with_dl_iterate_phdr() + # Make FlexiBLASController last, so some of the OpenBLAS special-casing + # we do gets overriden by FlexiBLAS if FlexiBLAS is using OpenBLAS: + for lib in self.lib_controllers[:]: + if isinstance(lib, FlexiBLASController): + self.lib_controllers.remove(lib) + self.lib_controllers.append(lib) + _PATH_RE = re.compile(rb" (/[^\n]+\.so[^\n^/]*)\n", re.MULTILINE) def _find_libraries_with_linux(self):