Repository navigation
Handle nested parallelism in distributed.joblib #1705
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
TomAugspurger
merged 6 commits into
dask:master
from
TomAugspurger:distributed-joblib-nested
Jan 25, 2018
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
390ba2b
Handle nested parallelism in distributed.joblib
TomAugspurger f7f3eb4
Change joblib backend to use globally created client
mrocklin f3a9c39
Client doesn't close given loop
mrocklin 8ab747c
Serialize DaskDistributedBackend without client
mrocklin f6fba7c
Avoid rejoining
TomAugspurger 7cd398d
add rejoin back into retrieval context
mrocklin 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
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,9 +1,12 @@ | ||
| from __future__ import print_function, division, absolute_import | ||
| import os | ||
| from distutils.version import LooseVersion | ||
|
|
||
| import pytest | ||
| from random import random | ||
| from time import sleep | ||
|
|
||
| from distributed import Client | ||
| from distributed.utils_test import cluster, inc | ||
| from distributed.utils_test import loop # flake8: noqa | ||
|
|
||
|
|
@@ -18,28 +21,24 @@ def slow_raise_value_error(condition, duration=0.05): | |
| raise ValueError("condition evaluated to True") | ||
|
|
||
|
|
||
| @pytest.mark.skip(reason="intermittent blocking failures") | ||
| @pytest.mark.parametrize('joblib', joblibs) | ||
| def test_simple(loop, joblib): | ||
| if joblib is None: | ||
| pytest.skip() | ||
| Parallel = joblib.Parallel | ||
| delayed = joblib.delayed | ||
| with cluster() as (s, [a, b]): | ||
| with joblib.parallel_backend('dask.distributed', loop=loop, | ||
| scheduler_host=s['address']) as (ba, _): | ||
| with Client(s['address'], loop=loop) as client: | ||
| with joblib.parallel_backend('dask') as (ba, _): | ||
| seq = Parallel()(delayed(inc)(i) for i in range(10)) | ||
| assert seq == [inc(i) for i in range(10)] | ||
|
|
||
| seq = Parallel()(delayed(inc)(i) for i in range(10)) | ||
| assert seq == [inc(i) for i in range(10)] | ||
| with pytest.raises(ValueError): | ||
| Parallel()(delayed(slow_raise_value_error)(i == 3) | ||
| for i in range(10)) | ||
|
|
||
| with pytest.raises(ValueError): | ||
| Parallel()(delayed(slow_raise_value_error)(i == 3) | ||
| for i in range(10)) | ||
|
|
||
| seq = Parallel()(delayed(inc)(i) for i in range(10)) | ||
| assert seq == [inc(i) for i in range(10)] | ||
|
|
||
| ba.client.close() | ||
| seq = Parallel()(delayed(inc)(i) for i in range(10)) | ||
| assert seq == [inc(i) for i in range(10)] | ||
|
|
||
|
|
||
| def random2(): | ||
|
|
@@ -53,21 +52,21 @@ def test_dont_assume_function_purity(loop, joblib): | |
| Parallel = joblib.Parallel | ||
| delayed = joblib.delayed | ||
| with cluster() as (s, [a, b]): | ||
| with joblib.parallel_backend('dask.distributed', loop=loop, | ||
| scheduler_host=s['address']) as (ba, _): | ||
|
|
||
| x, y = Parallel()(delayed(random2)() for i in range(2)) | ||
| assert x != y | ||
|
|
||
| ba.client.close() | ||
| with Client(s['address'], loop=loop) as client: | ||
| with joblib.parallel_backend('dask') as (ba, _): | ||
| x, y = Parallel()(delayed(random2)() for i in range(2)) | ||
| assert x != y | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('joblib', joblibs) | ||
| def test_joblib_funcname(joblib): | ||
| if joblib is None: | ||
| pytest.skip() | ||
| BatchedCalls = joblib.parallel.BatchedCalls | ||
| func = BatchedCalls([(random2,), (random2,)]) | ||
| if LooseVersion(joblib.__version__) <= "0.11.0": | ||
| func = BatchedCalls([(random2,), (random2,)]) | ||
| else: | ||
| func = BatchedCalls([(random2,), (random2,)], None) | ||
| assert joblib_funcname(func) == 'random2' | ||
| assert joblib_funcname(random2) == 'random2' | ||
|
|
||
|
|
@@ -113,25 +112,21 @@ def test_joblib_scatter(loop, joblib): | |
| z = CountSerialized(3) | ||
|
|
||
| with cluster() as (s, [a, b]): | ||
| with joblib.parallel_backend('dask.distributed', loop=loop, | ||
| scheduler_host=s['address'], | ||
| scatter=[x, y]) as (ba, _): | ||
| f = delayed(add5) | ||
| tasks = [f(x, y, z, d=4, e=5), | ||
| f(x, z, y, d=5, e=4), | ||
| f(y, x, z, d=x, e=5), | ||
| f(z, z, x, d=z, e=y)] | ||
| sols = [func(*args, **kwargs) for func, args, kwargs in tasks] | ||
| results = Parallel()(tasks) | ||
|
|
||
| ba.client.close() | ||
|
|
||
| # Scatter must take a list/tuple | ||
| with pytest.raises(TypeError): | ||
| with joblib.parallel_backend('dask.distributed', loop=loop, | ||
| scheduler_host=s['address'], | ||
| scatter=1): | ||
| pass | ||
| with Client(s['address'], loop=loop) as client: | ||
| with joblib.parallel_backend('dask', scatter=[x, y]) as (ba, _): | ||
| f = delayed(add5) | ||
| tasks = [f(x, y, z, d=4, e=5), | ||
| f(x, z, y, d=5, e=4), | ||
| f(y, x, z, d=x, e=5), | ||
| f(z, z, x, d=z, e=y)] | ||
| sols = [func(*args, **kwargs) for func, args, kwargs in tasks] | ||
| results = Parallel()(tasks) | ||
|
|
||
| # Scatter must take a list/tuple | ||
| with pytest.raises(TypeError): | ||
| with joblib.parallel_backend('dask', loop=loop, | ||
| scatter=1): | ||
| pass | ||
|
|
||
| for l, r in zip(sols, results): | ||
| assert l == r | ||
|
|
@@ -140,3 +135,48 @@ def test_joblib_scatter(loop, joblib): | |
| assert x.count == 1 | ||
| assert y.count == 1 | ||
| assert z.count == 4 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('joblib', joblibs) | ||
| def test_nested_backend_context_manager(loop, joblib): | ||
| if joblib is None or LooseVersion(joblib.__version__) <= "0.11.0": | ||
| pytest.skip("Joblib >= 0.11.1 required.") | ||
| Parallel = joblib.Parallel | ||
| delayed = joblib.delayed | ||
|
Member
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. Slight preference to leave these as fully namespaced, especially given the double use of |
||
|
|
||
| def get_nested_pids(): | ||
| pids = set(Parallel(n_jobs=2)(delayed(os.getpid)() for _ in range(2))) | ||
| pids |= set(Parallel(n_jobs=2)(delayed(os.getpid)() for _ in range(2))) | ||
| return pids | ||
|
|
||
| with cluster() as (s, [a, b]): | ||
| with Client(s['address'], loop=loop) as client: | ||
| with joblib.parallel_backend('dask') as (ba, _): | ||
| pid_groups = Parallel(n_jobs=2)( | ||
| delayed(get_nested_pids, check_pickle=False)() | ||
| for _ in range(10) | ||
| ) | ||
| for pid_group in pid_groups: | ||
| assert len(set(pid_group)) <= 2 | ||
|
|
||
| # No deadlocks | ||
| with Client(s['address'], loop=loop) as client: | ||
| with joblib.parallel_backend('dask') as (ba, _): | ||
| pid_groups = Parallel(n_jobs=2)( | ||
| delayed(get_nested_pids, check_pickle=False)() | ||
| for _ in range(10) | ||
| ) | ||
| for pid_group in pid_groups: | ||
| assert len(set(pid_group)) <= 2 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('joblib', joblibs) | ||
| def test_errors(loop, joblib): | ||
| if joblib is None: | ||
| pytest.skip() | ||
|
|
||
| with pytest.raises(ValueError) as info: | ||
| with joblib.parallel_backend('dask'): | ||
| pass | ||
|
|
||
| assert "create a dask client" in str(info.value).lower() | ||
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.
Should we rejoin here? Typically this is necessary if there is additional computational work to do in the task. That may not be the case here. It's harmless either way, but might add a tiny bit of unwanted delay.
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 wasn't sure about that either. To make sure I understand, when the worker thread
secedes, the scheduler will tell the worker to make a new thread to take its place? In that case, yes we should be OK just not rejoining.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.
Right, the thread leaves the thread pool, leaving it with
n - 1threads. We make a new thread to take it's place. Relevant code here: https://github.com/dask/distributed/blob/master/distributed/threadpoolexecutor.py#L94-L105