### Install threadpoolctl via pip Source: https://github.com/joblib/threadpoolctl/blob/master/README.md Standard installation command for users to install the package from PyPI. ```bash pip install threadpoolctl ``` -------------------------------- ### Install development dependencies and run tests Source: https://github.com/joblib/threadpoolctl/blob/master/README.md Commands for contributors to set up the development environment and execute the test suite. ```bash pip install -r dev-requirements.txt flit install --symlink pytest ``` -------------------------------- ### Create Custom Library Controller with LibController Source: https://context7.com/joblib/threadpoolctl/llms.txt Illustrates how to extend the `LibController` base class to add threadpoolctl support for custom native libraries. This involves defining `user_api`, `internal_api`, `filename_prefixes`, and implementing methods to get and set thread counts, and optionally retrieve the library version. ```python from threadpoolctl import LibController, register import ctypes class MyCustomLibController(LibController): """Controller for a custom threaded library.""" # Required class attributes user_api = "custom" internal_api = "mylib" filename_prefixes = ("libmylib",) # Optional: symbols to check for library identification check_symbols = ("mylib_get_threads", "mylib_set_threads") def get_num_threads(self): """Return current thread limit.""" get_func = getattr(self.dynlib, "mylib_get_threads", lambda: 1) return get_func() def set_num_threads(self, num_threads): """Set the thread limit.""" set_func = getattr(self.dynlib, "mylib_set_threads", lambda n: None) return set_func(num_threads) def get_version(self): """Return library version string.""" get_ver = getattr(self.dynlib, "mylib_version", None) if get_ver: get_ver.restype = ctypes.c_char_p return get_ver().decode("utf-8") return None def set_additional_attributes(self): """Set extra attributes exposed in info().""" self.custom_attr = "value" # Register the controller with threadpoolctl register(MyCustomLibController) # Now threadpoolctl will detect and control your library from threadpoolctl import threadpool_info info = threadpool_info() # Includes your custom library if loaded ``` -------------------------------- ### CLI: Get Threadpool Info Source: https://github.com/joblib/threadpoolctl/blob/master/README.md Command line interface to retrieve a JSON description of thread-pools initialized by specific Python packages. ```APIDOC ## CLI: python -m threadpoolctl ### Description Executes a command line query to inspect thread-pools for specified packages. ### Method CLI ### Endpoint python -m threadpoolctl -i [packages...] ### Parameters #### Query Parameters - **-i** (string) - Required - List of package names to inspect (e.g., numpy, scipy). ### Response #### Success Response (200) - **JSON** (stdout) - A JSON array containing the configuration of thread-pools for the requested packages. ``` -------------------------------- ### GET /threadpool_info Source: https://github.com/joblib/threadpoolctl/blob/master/README.md Retrieves information about the thread-pools initialized by native libraries currently loaded in the Python runtime. ```APIDOC ## GET /threadpool_info ### Description Returns a list of dictionaries describing the thread-pools of loaded native libraries (e.g., BLAS, OpenMP). ### Method GET ### Endpoint threadpool_info() ### Parameters None ### Response #### Success Response (200) - **list** (array) - A list of objects containing library metadata. #### Response Example [ { "filepath": "/path/to/lib.so", "prefix": "libname", "user_api": "blas", "internal_api": "openblas", "version": "0.3.17", "num_threads": 4 } ] ``` -------------------------------- ### Introspect Native Thread Pools using threadpoolctl CLI Source: https://context7.com/joblib/threadpoolctl/llms.txt Provides examples of using the threadpoolctl command-line interface to inspect the thread pools of loaded Python modules. This is useful for debugging and understanding how libraries like NumPy and SciPy manage native threads. It supports introspection after importing specific modules or executing arbitrary Python code. ```bash # Introspect after importing numpy python -m threadpoolctl -i numpy # Output: # [ # { # "filepath": "/path/to/libopenblas.so", # "prefix": "libopenblas", # "user_api": "blas", # "internal_api": "openblas", # "version": "0.3.17", # "num_threads": 4, # "threading_layer": "pthreads", # "architecture": "Haswell" # } # ] # Introspect after importing multiple packages python -m threadpoolctl -i numpy scipy.linalg sklearn # Execute arbitrary Python code before introspection python -m threadpoolctl -c "import numpy; numpy.zeros(10)" ``` -------------------------------- ### Switching BLAS Backend with FlexiBLAS Source: https://github.com/joblib/threadpoolctl/blob/master/README.md Demonstrates how to switch the BLAS backend using FlexiBLAS by providing the path to a shared library. It also shows how to inspect the current controller information. ```python import threadpoolctl # Assuming flexiblas_controller and controller are already initialized # Example: flexiblas_controller = threadpoolctl.threadpool_info(user_api='blas', internal_api='flexiblas')[0] # Example: controller = threadpoolctl.threadpool_info()[0] # Or a specific controller flexiblas_controller.switch_backend("/home/jeremie/miniforge/envs/flexiblas_threadpoolctl/lib/libmkl_rt.so") print(controller.info()) ``` -------------------------------- ### Switch FlexiBLAS Backend at Runtime Source: https://github.com/joblib/threadpoolctl/blob/master/README.md Illustrates how to inspect available BLAS backends and switch between them using the FlexiBLAS controller. Note that this API is experimental and subject to change. ```python from threadpoolctl import ThreadpoolController import numpy as np controller = ThreadpoolController() # Retrieve the flexiblas controller flexiblas_ct = controller.select(internal_api="flexiblas").lib_controllers[0] # Switch the backend flexiblas_ct.switch_backend("OPENBLASPTHREAD") controller.info() ``` -------------------------------- ### Implementing a Custom Library Controller with threadpoolctl Source: https://github.com/joblib/threadpoolctl/blob/master/README.md Illustrates the process of creating a custom controller for a native library by subclassing `LibController` and registering it with `threadpoolctl`. This allows threadpoolctl to manage the thread limits of libraries not natively supported. ```python from threadpoolctl import LibController, register class MyCustomController(LibController): # Implement attributes and methods as described in LibController docstring # ... pass register(MyCustomController) # Now threadpoolctl can manage threads for libraries handled by MyCustomController ``` -------------------------------- ### Switch FlexiBLAS Backend at Runtime Source: https://context7.com/joblib/threadpoolctl/llms.txt Demonstrates the experimental API in threadpoolctl for switching the BLAS backend when using FlexiBLAS. This allows changing the underlying BLAS implementation (e.g., from OpenBLAS to MKL) dynamically without restarting the Python process, providing flexibility in performance tuning. ```python from threadpoolctl import ThreadpoolController import numpy as np controller = ThreadpoolController() # Check if FlexiBLAS is available flexiblas_controllers = controller.select(internal_api='flexiblas') if flexiblas_controllers.lib_controllers: flexiblas = flexiblas_controllers.lib_controllers[0] # View available backends print(f"Available backends: {flexiblas.available_backends}") # Output: ['NETLIB', 'OPENBLASPTHREAD', 'ATLAS'] print(f"Loaded backends: {flexiblas.loaded_backends}") print(f"Current backend: {flexiblas.current_backend}") # Switch to a different backend flexiblas.switch_backend("OPENBLASPTHREAD") # Or load a backend from a specific path # flexiblas.switch_backend("/path/to/libmkl_rt.so") # Verify the switch print(f"Now using: {flexiblas.current_backend}") ``` -------------------------------- ### Limit Thread-Pool Size with Context Managers Source: https://github.com/joblib/threadpoolctl/blob/master/README.md Demonstrates how to use threadpool_limits and ThreadpoolController to restrict the number of threads for specific APIs like BLAS within a code block. This ensures that heavy computations do not oversubscribe system resources. ```python from threadpoolctl import threadpool_limits import numpy as np with threadpool_limits(limits=1, user_api='blas'): a = np.random.randn(1000, 1000) a_squared = a @ a from threadpoolctl import ThreadpoolController controller = ThreadpoolController() with controller.limit(limits=1, user_api='blas'): a = np.random.randn(1000, 1000) a_squared = a @ a ``` -------------------------------- ### FlexiBLAS Backend Switching Source: https://github.com/joblib/threadpoolctl/blob/master/README.md Experimental API to switch the backend implementation of FlexiBLAS at runtime. ```APIDOC ## Method: switch_backend ### Description Switches the current BLAS backend for FlexiBLAS to a different available backend. ### Parameters - **backend_name** (str) - Required - The name of the backend to switch to (e.g., 'OPENBLASPTHREAD'). ### Request Example ```python flexiblas_ct = controller.select(internal_api="flexiblas").lib_controllers[0] flexiblas_ct.switch_backend("OPENBLASPTHREAD") ``` ``` -------------------------------- ### Retrieve Original Thread Limits with threadpool_limits Source: https://context7.com/joblib/threadpoolctl/llms.txt Shows how to use the `threadpool_limits` context manager to temporarily set thread limits and then retrieve the original thread counts that were active before the limits were applied. This is useful for debugging and ensuring proper state restoration. ```python from threadpoolctl import threadpool_limits with threadpool_limits(limits=1) as limiter: # Get original thread counts by user_api original = limiter.get_original_num_threads() print(f"Original BLAS threads: {original.get('blas')}") print(f"Original OpenMP threads: {original.get('openmp')}") # Output example: # Original BLAS threads: 4 # Original OpenMP threads: 8 # Limits are automatically restored here ``` -------------------------------- ### Introspect thread-pools via CLI Source: https://github.com/joblib/threadpoolctl/blob/master/README.md Use the command line interface to retrieve a JSON representation of initialized thread-pools for specific Python packages. ```bash python -m threadpoolctl -i numpy scipy.linalg ``` -------------------------------- ### Control Matrix Multiplication Threads with ThreadpoolController.wrap Source: https://context7.com/joblib/threadpoolctl/llms.txt Demonstrates how to use the ThreadpoolController.wrap decorator to limit the number of threads used by a specific function, in this case, matrix multiplication using BLAS. This allows for fine-grained control over resource usage for performance-critical operations. ```python from threadpoolctl import ThreadpoolController import numpy as np controller = ThreadpoolController() @controller.wrap(limits=2, user_api='blas') def limited_matrix_multiply(a, b): """Matrix multiplication limited to 2 BLAS threads.""" return a @ b a = np.random.randn(1000, 1000) b = np.random.randn(1000, 1000) result = limited_matrix_multiply(a, b) ``` -------------------------------- ### Restrict Thread Limits with Function Decorators Source: https://github.com/joblib/threadpoolctl/blob/master/README.md Shows how to apply thread limits to an entire function using the wrap method. This is useful for encapsulating thread-pool constraints within specific functional logic. ```python from threadpoolctl import ThreadpoolController, threadpool_limits import numpy as np controller = ThreadpoolController() @controller.wrap(limits=1, user_api='blas') def my_func(): a = np.random.randn(1000, 1000) a_squared = a @ a ``` -------------------------------- ### Apply Thread Limits via Decorators Source: https://context7.com/joblib/threadpoolctl/llms.txt Demonstrates using threadpool_limits.wrap as a function decorator to ensure specific functions execute with defined thread constraints. ```python from threadpoolctl import threadpool_limits import numpy as np @threadpool_limits.wrap(limits=1, user_api='blas') def single_threaded_computation(matrix): return np.linalg.svd(matrix) result = single_threaded_computation(np.random.randn(500, 500)) ``` -------------------------------- ### Programmatic thread-pool introspection Source: https://github.com/joblib/threadpoolctl/blob/master/README.md Use the Python API to inspect the state of loaded native libraries and their thread-pool configurations. ```python from threadpoolctl import threadpool_info from pprint import pprint import numpy pprint(threadpool_info()) ``` -------------------------------- ### Manage Thread Pools with ThreadpoolController Source: https://context7.com/joblib/threadpoolctl/llms.txt An object-oriented approach to thread pool management that scans libraries once and provides efficient methods for filtering, selecting, and limiting specific libraries. ```python from threadpoolctl import ThreadpoolController import numpy as np controller = ThreadpoolController() with controller.limit(limits=1, user_api='blas'): a = np.random.randn(1000, 1000) result = a @ a openblas_controller = controller.select(internal_api='openblas') with openblas_controller.limit(limits=2): pass ``` -------------------------------- ### Using ThreadpoolController API Source: https://github.com/joblib/threadpoolctl/blob/master/README.md Utilize the object-oriented ThreadpoolController API to retrieve thread-pool information programmatically. ```python from threadpoolctl import ThreadpoolController from pprint import pprint import numpy controller = ThreadpoolController() pprint(controller.info()) ``` -------------------------------- ### ThreadpoolController API Source: https://github.com/joblib/threadpoolctl/blob/master/README.md Object-oriented interface for managing thread pools and querying library information. ```APIDOC ## Class: ThreadpoolController ### Description Provides an object-oriented approach to manage thread limits and inspect loaded shared libraries. ### Methods - **limit(limits, user_api)**: Returns a context manager to limit threads. - **wrap(limits, user_api)**: Returns a decorator to limit threads for a function. - **info()**: Returns a list of dictionaries containing details about loaded libraries. - **select(internal_api)**: Filters the controller to specific internal APIs. ### Request Example ```python from threadpoolctl import ThreadpoolController controller = ThreadpoolController() with controller.limit(limits=1, user_api='blas'): # Execution ``` ``` -------------------------------- ### Sequential BLAS within OpenMP Parallel Region Source: https://github.com/joblib/threadpoolctl/blob/master/README.md Provides guidance on safely setting BLAS thread limits to sequential when nested within an OpenMP parallel region. It recommends using `limits="sequential_blas_under_openmp"` over `limits=1` for better compatibility with certain configurations like OpenBLAS. ```python from threadpoolctl import threadpool_limits with threadpool_limits(limits="sequential_blas_under_openmp", user_api="blas"): # Code that performs BLAS calls within an OpenMP parallel region pass ``` -------------------------------- ### Introspect Loaded Thread Pool Libraries with threadpool_info Source: https://context7.com/joblib/threadpoolctl/llms.txt Retrieves a list of dictionaries containing metadata about all detected thread pool-enabled libraries currently loaded in the process, such as API type, current thread limits, and version. ```python from threadpoolctl import threadpool_info from pprint import pprint import numpy info = threadpool_info() pprint(info) ``` -------------------------------- ### Limit Thread Pool Sizes with threadpool_limits Source: https://context7.com/joblib/threadpoolctl/llms.txt A context manager and callable to temporarily restrict the maximum number of threads for specific native libraries. It automatically restores original limits upon exiting the context. ```python from threadpoolctl import threadpool_limits import numpy as np with threadpool_limits(limits=1): a = np.random.randn(1000, 1000) result = a @ a with threadpool_limits(limits=2, user_api='blas'): a = np.random.randn(1000, 1000) result = np.linalg.svd(a) with threadpool_limits(limits={'blas': 1, 'openmp': 4}): pass ``` -------------------------------- ### threadpool_limits Context Manager Source: https://github.com/joblib/threadpoolctl/blob/master/README.md Limits the number of threads used by specific runtime libraries within a defined code block. ```APIDOC ## Context Manager: threadpool_limits ### Description Limits the number of threads for supported libraries (e.g., BLAS, MKL) within a Python 'with' block. ### Parameters - **limits** (int/dict) - Required - The maximum number of threads allowed. - **user_api** (str) - Optional - The API to target (e.g., 'blas'). ### Request Example ```python from threadpoolctl import threadpool_limits with threadpool_limits(limits=1, user_api='blas'): # Code block execution ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.