Skip to content
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

Consistent use of ordering by task name #3271

Merged
merged 6 commits into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 22 additions & 3 deletions dask/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,17 @@ def order(dsk, dependencies=None):
waiting = {k: set(v) for k, v in dependencies.items()}

def dependencies_key(x):
return total_dependencies.get(x, 0), StrComparable(x)
return total_dependencies.get(x, 0), ReverseStrComparable(x)

def dependents_key(x):
return total_dependents.get(x, 0), StrComparable(x)
return (total_dependents.get(x, 0),
StrComparable(x))

result = dict()
i = 0

stack = [k for k, v in dependents.items() if not v]
stack = sorted(stack, key=total_dependencies.get)
stack = sorted(stack, key=dependencies_key)

while stack:
item = stack.pop()
Expand Down Expand Up @@ -214,3 +215,21 @@ def __lt__(self, other):
return self.obj < other.obj
except Exception:
return str(self.obj) < str(other.obj)


class ReverseStrComparable(object):
""" Wrap object so that it defaults to string comparison

Used when sorting in reverse direction. See StrComparable for normal
documentation.
"""
__slots__ = ('obj',)

def __init__(self, obj):
self.obj = obj

def __lt__(self, other):
try:
return self.obj > other.obj
except Exception:
return str(self.obj) > str(other.obj)
2 changes: 1 addition & 1 deletion dask/tests/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,4 @@ def append(i):

get_sync(dsk, 'y')

assert L == sorted(L, reverse=True)
assert L == sorted(L)
60 changes: 59 additions & 1 deletion dask/tests/test_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ def test_avoid_upwards_branching_complex(abcde):
(a, 2): (f, (a, 3)),
(a, 3): (f, (b, 1), (c, 1)),
(b, 1): (f, (b, 2)),
(b, 2): (f,),
(c, 1): (f, (c, 2)),
(c, 2): (f, (c, 3)),
(c, 3): (f,),
(d, 1): (f, (c, 1)),
(d, 2): (f, (d, 1)),
(d, 3): (f, (d, 1)),
Expand Down Expand Up @@ -239,7 +241,8 @@ def test_gh_3055():
dsk = dict(w.__dask_graph__())
o = order(dsk)
L = [o[k] for k in w.__dask_keys__()]
assert sorted(L) == L
assert sum(x < len(o) / 2 for x in L) > len(L) / 3 # some complete quickly
assert sorted(L) == L # operate in order


def test_type_comparisions_ok(abcde):
Expand Down Expand Up @@ -351,3 +354,58 @@ def _(*args):
dask.get(dsk, [a1, b1, c1]) # trigger computation

assert log == expected


def test_nearest_neighbor(abcde):
"""

a1 a2 a3 a4 a5 a6 a7 a8 a9
\ | / \ | / \ | / \ | /
b1 b2 b3 b4

Want to finish off a local group before moving on.
This is difficult because all groups are connected.
"""
a, b, c, _, _ = abcde
a1, a2, a3, a4, a5, a6, a7, a8, a9 = [a + i for i in '123456789']
b1, b2, b3, b4 = [b + i for i in '1234']

dsk = {b1: (f,),
b2: (f,),
b3: (f,),
b4: (f,),
a1: (f, b1),
a2: (f, b1),
a3: (f, b1, b2),
a4: (f, b2),
a5: (f, b2, b3),
a6: (f, b3),
a7: (f, b3, b4),
a8: (f, b4),
a9: (f, b4)}

o = order(dsk)

assert 3 < sum(o[a + i] < len(o) / 2 for i in '123456789') < 7
assert 1 < sum(o[b + i] < len(o) / 2 for i in '1234') < 4
assert o[min([b1, b2, b3, b4])] == 0


def test_string_ordering():
""" Prefer ordering tasks by name first """
dsk = {('a', 1): (f,), ('a', 2): (f,), ('a', 3): (f,)}
o = order(dsk)
assert o == {('a', 1): 0,
('a', 2): 1,
('a', 3): 2}


def test_string_ordering_dependents():
""" Prefer ordering tasks by name first even when in dependencies """
dsk = {('a', 1): (f, 'b'), ('a', 2): (f, 'b'), ('a', 3): (f, 'b'),
'b': (f,)}
o = order(dsk)
assert o == {'b': 0,
('a', 1): 1,
('a', 2): 2,
('a', 3): 3}
1 change: 1 addition & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Core
++++

- Fixed bug when using unexpected but hashable types for keys (:pr:`3238`) `Daniel Collins`_
- Fix bug in task ordering so that we break ties consistently with the key name (:pr:`3271`) `Matthew Rocklin`_


0.17.1 / 2018-02-22
Expand Down