-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
Enforce threading in parallel pairwise #13310
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
Merged
GaelVaroquaux
merged 7 commits into
scikit-learn:master
from
pierreglaser:enforce-threading-in_parallel_pairwise
Feb 28, 2019
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f7dc7ef
ENH enforce threading backend in _parallel_pairwise
pierreglaser 7361f64
OPT write inplace to the matrix in the workers
pierreglaser 18d7bf7
CLN cosmetics
pierreglaser ed86308
CLN renaming, cosmetics
pierreglaser 1d9dbca
CLN renaming
pierreglaser 99b93f0
DOC whats_new
pierreglaser 63a6346
DOC rephrasing
pierreglaser 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 |
---|---|---|
|
@@ -1049,6 +1049,11 @@ def distance_metrics(): | |
return PAIRWISE_DISTANCE_FUNCTIONS | ||
|
||
|
||
def _dist_wrapper(dist_func, dist_matrix, slice_, *args, **kwargs): | ||
"""Write in-place to a slice of a distance matrix""" | ||
dist_matrix[:, slice_] = dist_func(*args, **kwargs) | ||
|
||
|
||
def _parallel_pairwise(X, Y, func, n_jobs, **kwds): | ||
"""Break the pairwise matrix in n_jobs even slices | ||
and compute them in parallel""" | ||
|
@@ -1059,13 +1064,14 @@ def _parallel_pairwise(X, Y, func, n_jobs, **kwds): | |
if effective_n_jobs(n_jobs) == 1: | ||
return func(X, Y, **kwds) | ||
|
||
# TODO: in some cases, backend='threading' may be appropriate | ||
fd = delayed(func) | ||
ret = Parallel(n_jobs=n_jobs, verbose=0)( | ||
fd(X, Y[s], **kwds) | ||
# enforce a threading backend to prevent data communication overhead | ||
fd = delayed(_dist_wrapper) | ||
ret = np.empty((X.shape[0], Y.shape[0]), dtype=X.dtype, order='F') | ||
Parallel(backend="threading", n_jobs=n_jobs)( | ||
fd(func, ret, s, X, Y[s], **kwds) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting that it goes faster, I would have expected to access the shared array and writing to a shared array be limiting with respect to GIL. |
||
for s in gen_even_slices(_num_samples(Y), effective_n_jobs(n_jobs))) | ||
|
||
return np.hstack(ret) | ||
return ret | ||
|
||
|
||
def _pairwise_callable(X, Y, metric, **kwds): | ||
|
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.
we should probably not enforce backend like this be instead use the
prefer="threads"
hinting mechanism. That is using the**_joblib_parallel_args(prefer='threads')
trick to keep backward compat with joblib 0.11.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.
I was wondering about that, but say when you are using Dask, do we really want it to be able to change the backend here? I can't think of a use case where we want to use process-based backend for Euclidean distances which probably is the most frequent case.
Other metrics might require more fine-grained treatment ...