Debian Codegraph @

docs/source/order.rst

raw
1 .. _order:
2
3 Ordering
4 ========
5
6 .. note::
7
8 This is an advanced topic that most users won't need to worry about.
9
10 When Dask is given a task graph to compute, it needs to choose an order to
11 execute the tasks in. We have some constraints: dependencies must be executed
12 before their dependants. But beyond that there's a large space of options. We
13 want Dask to choose an ordering that maximizes parallelism while minimizing
14 the footprint necessary to run a computation.
15
16 At a high level, Dask has a policy that works towards *small goals* with *big steps*.
17
18 1. **Small goals**: prefer tasks that have few total dependents and whose final
19 dependents have few total dependencies.
20
21 We prefer to prioritize those tasks that help branches of computation that
22 can terminate quickly.
23
24 With more detail, we compute the total number of dependencies that each
25 task depends on (both its own dependencies, the dependencies of its
26 dependencies, and so on), and then we choose those tasks that drive towards
27 results with a low number of total dependencies. We choose to prioritize
28 tasks that work towards finishing shorter computations first.
29
30 2. **Big steps**: prefer tasks with many dependents
31
32 However, many tasks work towards the same final dependents. Among those,
33 we choose those tasks with the most work left to do. We want to finish
34 the larger portions of a sub-computation before we start on the smaller
35 ones.
36
37 This is done with :func:`dask.order.order`. A more technical discussion is available
38 in :ref:`scheduling-policy`. https://distributed.dask.org/en/latest/scheduling-policies.html
39 also discusses scheduling with a focus on the distributed scheduler, which includes
40 additional choices beyond the static ordering documented here.
41
42 Debugging
43 ---------
44
45 Most of the time Dask's ordering does well. But this is a genuinely hard problem
46 and there might be cases where you observe unexpectedly high memory usage or
47 communication, which may be a result of poor ordering. This section describes
48 how you would identify an ordering problem, and some steps you can take to
49 mitigate the problem.
50
51 Consider a computation that loads several chains of data from disk independently,
52 stacks pieces of them together, and does some reduction:
53
54 .. code-block:: python
55
56 >>> # create data on disk
57 >>> import dask.array as da
58 >>> x = da.zeros((12500, 10000), chunks=('10MB', -1))
59 >>> da.to_zarr(x, 'saved_x1.zarr', overwrite=True)
60 >>> da.to_zarr(x, 'saved_y1.zarr', overwrite=True)
61 >>> da.to_zarr(x, 'saved_x2.zarr', overwrite=True)
62 >>> da.to_zarr(x, 'saved_y2.zarr', overwrite=True)
63
64 We can load the data
65
66 .. code-block:: python
67
68 >>> # load the data.
69 >>> x1 = da.from_zarr('saved_x1.zarr')
70 >>> y1 = da.from_zarr('saved_x2.zarr')
71 >>> x2 = da.from_zarr('saved_y1.zarr')
72 >>> y2 = da.from_zarr('saved_y2.zarr')
73
74 And do some computation on it
75
76 .. code-block:: python
77
78 >>> def evaluate(x1, y1, x2, y2):
79 ... u = da.stack([x1, y1])
80 ... v = da.stack([x2, y2])
81 ... components = [u, v, u ** 2 + v ** 2]
82 ... return [
83 ... abs(c[0] - c[1]).mean(axis=-1)
84 ... for c in components
85 ... ]
86 >>> results = evaluate(x1, y1, x2, y2)
87
88 You can use :func:`dask.visualize` with ``color="order"`` to visualize a
89 task graph with the static ordering included as node labels. As usual with
90 ``dask.visualize``, you may need to trim down the problem to a smaller size,
91 so we'll slice off a subset of the data. Make sure to include ``optimize_graph=True``
92 to get a true representation of what order the tasks will be executed in.
93
94
95 .. code-block:: python
96
97 >>> import dask
98 >>> n = 125 * 4
99 >>> dask.visualize(evaluate(x1[:n], y1[:n], x2[:n], y2[:n]),
100 ... optimize_graph=True, color="order",
101 ... cmap="autumn", node_attr={"penwidth": "4"})
102
103
104 .. image:: images/order-failure.png
105 :alt: Complex task graph of several vertical node chains at the output, and a few input sub-trees. In between these sections, there is a many-to-many area of crossing dependency arrows. The color coding of the output trees is interleaved without a clear progression.
106
107 In this visualization the nodes are colored by order of execution (from dark red
108 to light yellow) and the node labels are the order Dask's assigned to each task.
109
110 It's a bit hard to see, but there are actually four mostly independent "towers"
111 of execution here. We start at the middle-right array (label 1, bottom), move
112 up to the right (label 8, top-right) and then jump to a completely different
113 array (label 11, bottom-left). However, computing the first tower (downstream
114 of label 8, top-right) required loading some data from our second input array
115 (label 5, bottom-right). We'd much prefer to finish tasks downstream of it.
116
117 When Dask is executing that task graph, you might observe high memory usage.
118 The poor static ordering means we fail to complete tasks that would let us
119 release pieces of data. We load more pieces into memory at once, leading to
120 higher memory usage.
121
122 This specific ordering failure (which may be fixed) comes from the shared
123 dependencies (the boxes at the bottom of each task, which represent the input
124 Zarr arrays) at the bottom of each computation chain. We can inline those and
125 see the effect of ordering:
126
127 .. code-block:: python
128
129 >>> # load and profile data
130 >>> x1 = da.from_zarr('saved_x1.zarr', inline_array=True)
131 >>> y1 = da.from_zarr('saved_x2.zarr', inline_array=True)
132 >>> x2 = da.from_zarr('saved_y1.zarr', inline_array=True)
133 >>> y2 = da.from_zarr('saved_y2.zarr', inline_array=True)
134
135 >>> import dask
136 >>> n = 125 * 4
137 >>> dask.visualize(evaluate(x1[:n], y1[:n], x2[:n], y2[:n]),
138 ... optimize_graph=True, color="order",
139 ... cmap="autumn", node_attr={"penwidth": "4"})
140
141
142 .. image:: images/order-success.png
143 :alt: Complex task graph of several vertical node chains at the output, and a similar number of input blocks. The outputs and inputs are linked by simple nodes of a few inputs each, laid out without significant crossover between sections of the tree. The color coding of the output chains shows clear progression in the order of execution with each output color having a corresponding input of the same color.
144
145 At a glance, we can see that this ordering is looks much more regular and
146 uniform. There's fewer lines crossing, and the color of the ordering moves
147 smoothly from bottom to top, left to right. This shows that Dask is completing
148 one chain of computation before moving onto the next.
149
150 The lesson here is *not* "always use ``inline_array=True``". While the static
151 ordering looks better, there are other :ref:`phases-of-computation` to consider.
152 Whether the actual performance is better will depend on more factors than we
153 can consider here. See :func:`dask.array.from_array` for more.
154
155 Instead, the lessons to take away here are:
156
157 1. What symptoms might lead you to diagnose Dask's ordering as a problem (e.g.
158 high memory usage)
159 2. How to generate and read task graphs with Dask's ordering information
160 included.