-
Notifications
You must be signed in to change notification settings - Fork 37
Thread limit introspection API, part 1: API scope #213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
itamarst
wants to merge
25
commits into
joblib:master
Choose a base branch
from
itamarst:211-thread-limit-introspection-api
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+237
−14
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
28bd246
Sketch of determine_api_scope.
itamarst 128c4ef
Add API scope to info.
itamarst 8637248
Reformat
itamarst 6946867
Reformat.
itamarst 31bbcf5
Try to work with older Python.
itamarst f27e6f2
Better names.
itamarst 0d4dde5
Test for expected API scopes for different libraries.
itamarst 60dd11c
Be more lenient.
itamarst 2c985b6
Convert into test to check _determine_api_scope(), rather than of exp…
itamarst bb7b528
Don't include API scope in info by default, since it might have side-…
itamarst 7fee2dc
Minor cleanups.
itamarst 1c6ae79
Improve docs.
itamarst 9ad8382
Match command-line behavior
itamarst d306c55
Give up on live testing, it's too inconsistent.
itamarst 8740d13
Cleanups from code review
itamarst 12a9d96
Review comments: better names, don't raise exception.
itamarst 95d77a0
Try to reintroduce a test that checks real libraries.
itamarst 3c831ad
Better explanation.
itamarst db35024
There's another option
itamarst b076937
Document better the limitations of this parameter.
itamarst a686a9b
Correct filtering logic.
itamarst fa3be78
Black
itamarst 0e44183
New architecture that showed up from a CI run
itamarst 586809d
FlexiBLAS support.
itamarst 5899f47
Correct default
itamarst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| """ | ||
| Tests for get/set number of threads API introspection. | ||
| """ | ||
|
|
||
| import sys | ||
| from threading import local as threadlocal | ||
| from typing import Callable | ||
|
|
||
| import pytest | ||
|
|
||
| from threadpoolctl import ( | ||
| _ThreadLimitScope, | ||
| LibController, | ||
| _determine_thread_limit_scope, | ||
| ThreadpoolController, | ||
| ) | ||
|
|
||
| # Make sure we have some BLAS libraries loaded: | ||
| from . import utils as _ | ||
|
|
||
|
|
||
| class FakeThreadLocalAPI(threadlocal): | ||
| """Thread-local num threads setting API.""" | ||
|
|
||
| def get(self) -> int: | ||
| return getattr(self, "num_threads", 17) | ||
|
|
||
| def set(self, n: int) -> None: | ||
| self.num_threads = n | ||
|
|
||
|
|
||
| class FakeProcesswideAPI: | ||
| """Process-wide num threads setting API.""" | ||
|
|
||
| def __init__(self, num_threads: int): | ||
| self.num_threads = num_threads | ||
|
|
||
| def get(self) -> int: | ||
| return self.num_threads | ||
|
|
||
| def set(self, n: int) -> None: | ||
| self.num_threads = n | ||
|
|
||
|
|
||
| def test_determine_thread_limit_scope_thread_local() -> None: | ||
| """ | ||
| Check ``_determine_thread_limit_scope()`` can correctly diagnose a trivial | ||
| thread-local implementation. | ||
| """ | ||
| api = FakeThreadLocalAPI() | ||
| assert ( | ||
| _determine_thread_limit_scope(api.get, api.set) | ||
| == _ThreadLimitScope.CURRENT_THREAD | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("default", [1, 17]) | ||
| def test_determine_thread_limit_scope_processwide(default: int) -> None: | ||
| """ | ||
| Check ``_determine_thread_limit_scope()`` can correctly diagnose a trivial | ||
| process-wide implementation. | ||
| """ | ||
| api = FakeProcesswideAPI(default) | ||
| assert _determine_thread_limit_scope(api.get, api.set) == _ThreadLimitScope.PROCESS | ||
|
|
||
|
|
||
| @pytest.mark.skipif( | ||
| sys.platform != "linux", reason="Non-Linux OpenMP might be different" | ||
| ) | ||
| @pytest.mark.parametrize( | ||
| ["select_filter", "expected_thread_limit_scope", "extra_check"], | ||
| [ | ||
| ( | ||
| {"internal_api": "openblas"}, | ||
| _ThreadLimitScope.PROCESS, | ||
| lambda lib: lib.threading_layer == "pthreads", | ||
| ), | ||
| ({"user_api": "openmp"}, _ThreadLimitScope.CURRENT_THREAD, lambda _lib: True), | ||
| ], | ||
| ) | ||
| def test_api_scope( | ||
| select_filter: dict[str, str], | ||
| expected_thread_limit_scope: str, | ||
| extra_check: Callable[[LibController], bool], | ||
| ) -> None: | ||
| """ | ||
| Check ``_determine_thread_limit_scope()`` against libraries with known | ||
| properties, to make sure it detects them correctly. The test is intended | ||
| to be of the function's behavior, not of the libraries. | ||
| """ | ||
| controller = ThreadpoolController().select(**select_filter) | ||
| if not controller.lib_controllers: | ||
| pytest.skip(f"{select_filter} controller not found") | ||
|
|
||
| for lib in controller.lib_controllers: | ||
| if not extra_check(lib): | ||
| pytest.skip("extra check returned false") | ||
| assert ( | ||
| _determine_thread_limit_scope(lib.get_num_threads, lib.set_num_threads) | ||
| == expected_thread_limit_scope | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about it a bit more, we could make this inspection side effect free by calling this function in an isolated via
subprocess.runif we really wanted.The problem would be to make sure that the same native threadpool libraries that are loaded in the current process at the time of the inspection call are also loaded in the subprocess. To do so we could manually call
ctypes.CDLLbut that might be a bit brittle.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking out loud... also relevant to the "should we hardcode this if introspection isn't available" question...
My current thought is that introspection is a useful debugging tool, but not something that would actually be used. Whatever it does, the library is stuck with it. And so I think it's OK if it's just opt-in, mostly for CLI users doing bug reports or when doing performance debugging.
And so something simpler and minimalist seems sufficient.
I may be wrong, it may be that knowing which it is will be helpful. And if so we can maybe go for more elaborate approaches like subprocess later on.