-
Notifications
You must be signed in to change notification settings - Fork 36
Thread limit introspection, part 2: Empirical observation in CI #224
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
13
commits into
joblib:master
Choose a base branch
from
itamarst:214-limit-semantics-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.
+140
−0
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e44c88d
Script to check number of threads
itamarst ba8d196
Make it more useful for CI
itamarst 4d7b7cc
Run in CI
itamarst f90b5d5
Need psutil too
itamarst 11c9cc0
Handle missing NumPy case
itamarst 8eddb9e
Try adjusting numbers so oversaturation on limited CPU CI doesn't giv…
itamarst aeb1488
Reformat
itamarst 7b5e120
Can't run both checks in same process
itamarst f027303
Reformat
itamarst 60df598
Better variable name
itamarst 997f52c
Apply batched suggestions from code review
itamarst 0450224
Merge remote-tracking branch 'origin/214-limit-semantics-introspectio…
itamarst d86367c
Use time everywhere, bit better output.
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
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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| flit | ||
| coverage | ||
| psutil | ||
| pytest | ||
| pytest-cov | ||
| cython | ||
|
|
||
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,135 @@ | ||
| """ | ||
| Run this script to empirically determine if OpenMP and BLAS limiting API set | ||
| the size of a shared process-wide thread pool or a per-thread limit. | ||
| """ | ||
|
|
||
| import sys | ||
| from concurrent.futures import ThreadPoolExecutor | ||
| from os import cpu_count | ||
| from pprint import pprint | ||
| from time import time | ||
| from threading import Thread | ||
| from typing import Callable | ||
|
|
||
| import threadpoolctl | ||
| import psutil | ||
|
|
||
|
|
||
| def start_counting_threads() -> Callable[[], int]: | ||
| max_num_threads = 0 | ||
| stop = False | ||
|
|
||
| def in_thread(): | ||
| nonlocal max_num_threads, stop | ||
| process = psutil.Process() | ||
| while not stop: | ||
| max_num_threads = max(max_num_threads, process.num_threads()) | ||
|
|
||
| thread = Thread(target=in_thread) | ||
| thread.start() | ||
|
|
||
| def finish(): | ||
| nonlocal stop, max_num_threads | ||
| stop = True | ||
| thread.join() | ||
| return max_num_threads | ||
|
|
||
| return finish | ||
|
|
||
|
|
||
| def set_limits(): | ||
| threadpoolctl.threadpool_limits(2) | ||
| print("== Library info ==") | ||
| pprint(threadpoolctl.threadpool_info()) | ||
| print() | ||
|
|
||
|
|
||
| def blas(num_python_threads): | ||
| try: | ||
| import numpy as np | ||
| except ImportError: | ||
| print("BLAS not available") | ||
| return False | ||
|
|
||
| set_limits() | ||
| A = np.ones((1024, 1024)) | ||
|
|
||
| def blas_math(_): | ||
| A.dot(A) | ||
|
|
||
| with ThreadPoolExecutor(num_python_threads) as pool: | ||
| t0 = time() | ||
| while time() - t0 < 5: | ||
| for _ in pool.map(blas_math, range(40)): | ||
| pass | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| def openmp(num_python_threads): | ||
| try: | ||
| from tests._openmp_test_helper.openmp_helpers_inner import ( | ||
| check_openmp_num_threads, | ||
| ) | ||
| except ImportError: | ||
| print("OpenMP not available") | ||
| return False | ||
|
|
||
| set_limits() | ||
|
|
||
| def run_openmp(_): | ||
| check_openmp_num_threads(1000) | ||
|
|
||
| with ThreadPoolExecutor(num_python_threads) as pool: | ||
| t0 = time() | ||
| while time() - t0 < 5: | ||
| for _ in pool.map(run_openmp, range(40)): | ||
| pass | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| def run(which: str) -> None: | ||
| if which == "blas": | ||
| func = blas | ||
| else: | ||
| func = openmp | ||
|
|
||
| num_python_threads = 10 * cpu_count() | ||
| count_threads = start_counting_threads() | ||
| if not func(num_python_threads): | ||
| count_threads() | ||
| return | ||
| max_num_threads = count_threads() | ||
|
|
||
| # We're creating os.cpu_count() * 10 Python threads, and asking for 2 | ||
| # native threads. If number is close to number of Python threads, we'll | ||
| # assume process-wide shared thread pool. If the number is close to double | ||
| # the number of Python threads, we'll assume a thread pool per thread. | ||
| if max_num_threads < num_python_threads * 1.25: | ||
| scope = "process-wide shared thread pool" | ||
| elif max_num_threads > num_python_threads * 1.75: | ||
| scope = "thread pool per thread" | ||
| else: | ||
| scope = "not sure" | ||
|
|
||
| print(f"== Observed behavior: {which} ==") | ||
| print( | ||
| f"10x{cpu_count()} Python threads each running a parallel " | ||
| "workload with a 2 thread limit." | ||
| ) | ||
| print( | ||
| f"Maximum number of observed threads (includes Python and {which} threads):", | ||
| max_num_threads, | ||
| ) | ||
| print("Presumed API scope:", scope) | ||
| print() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| if sys.argv[1] == "openmp": | ||
| run("openmp") | ||
| elif sys.argv[1] == "blas": | ||
| run("blas") | ||
| else: | ||
| raise SystemExit("Bad argument") | ||
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.
Uh oh!
There was an error while loading. Please reload this page.