| 1 |
.. _scheduling-policy: |
| 2 |
|
| 3 |
Scheduling in Depth |
| 4 |
=================== |
| 5 |
|
| 6 |
*Note: this technical document is not optimized for user readability.* |
| 7 |
|
| 8 |
The default shared memory scheduler used by most dask collections lives in |
| 9 |
``dask/local.py``. This scheduler dynamically schedules tasks to new |
| 10 |
workers as they become available. It operates in a shared memory environment |
| 11 |
without consideration to data locality, all workers have access to all data |
| 12 |
equally. |
| 13 |
|
| 14 |
We find that our workloads are best served by trying to minimize the memory |
| 15 |
footprint. This document talks about our policies to accomplish this in our |
| 16 |
scheduling budget of one millisecond per task, irrespective of the number of |
| 17 |
tasks. |
| 18 |
|
| 19 |
Generally we are faced with the following situation: A worker arrives with a |
| 20 |
newly completed task. We update our data structures of execution state and |
| 21 |
have to provide a new task for that worker. In general there are very many |
| 22 |
available tasks, which should we give to the worker? |
| 23 |
|
| 24 |
*Q: Which of our available tasks should we give to the newly ready worker?* |
| 25 |
|
| 26 |
This question is simple and local and yet strongly impacts the performance of |
| 27 |
our algorithm. We want to choose a task that lets us free memory now and in |
| 28 |
the future. We need a clever and cheap way to break a tie between the set of |
| 29 |
available tasks. |
| 30 |
|
| 31 |
At this stage we choose the policy of "last in, first out." That is we choose |
| 32 |
the task that was most recently made available, quite possibly by the worker |
| 33 |
that just returned to us. This encourages the general theme of finishing |
| 34 |
things before starting new things. |
| 35 |
|
| 36 |
We implement this with a stack. When a worker arrives with its finished task |
| 37 |
we figure out what new tasks we can now compute with the new data and put those |
| 38 |
on top of the stack if any exist. We pop an item off of the top of the stack |
| 39 |
and deliver that to the waiting worker. |
| 40 |
|
| 41 |
And yet if the newly completed task makes ready multiple newly ready tasks in |
| 42 |
which order should we place them on the stack? This is yet another opportunity |
| 43 |
for a tie breaker. This is particularly important at *the beginning* of |
| 44 |
execution where we typically add a large number of leaf tasks onto the stack. |
| 45 |
Our choice in this tie breaker also strongly affects performance in many cases. |
| 46 |
|
| 47 |
We want to encourage depth first behavior where, if our computation is composed |
| 48 |
of something like many trees we want to fully explore one subtree before moving |
| 49 |
on to the next. This encourages our workers to complete blocks/subtrees of our |
| 50 |
graph before moving on to new blocks/subtrees. |
| 51 |
|
| 52 |
And so to encourage this "depth first behavior" we do a depth first search and |
| 53 |
number all nodes according to their number in the depth first search (DFS) |
| 54 |
traversal. We use this number to break ties when adding tasks on to the stack. |
| 55 |
Please note that while we spoke of optimizing the many-distinct-subtree case |
| 56 |
above this choice is entirely local and applies quite generally beyond this |
| 57 |
case. Anything that behaves even remotely like the many-distinct-subtree case |
| 58 |
will benefit accordingly, and this case is quite common in normal workloads. |
| 59 |
|
| 60 |
And yet we have glossed over another tie breaker. Performing the depth |
| 61 |
first search, when we arrive at a node with many children we can choose the |
| 62 |
order in which to traverse the children. We resolve this tie breaker by |
| 63 |
selecting those children whose result is depended upon by the most nodes. This |
| 64 |
dependence can be either direct for those nodes that take that data as input or |
| 65 |
indirect for any ancestor node in the graph. This emphasizing traversing first |
| 66 |
those nodes that are parts of critical paths having long vertical chains that |
| 67 |
rest on top of this node's result, and nodes whose data is depended upon by |
| 68 |
many nodes in the future. We choose to dive down into these subtrees first in |
| 69 |
our depth first search so that future computations don't get stuck waiting for |
| 70 |
them to complete. |
| 71 |
|
| 72 |
And so we have three tie breakers |
| 73 |
|
| 74 |
1. Q: Which of these available tasks should I run? |
| 75 |
|
| 76 |
A: Last in, first out |
| 77 |
2. Q: Which of these tasks should I put on the stack first? |
| 78 |
|
| 79 |
A: Do a depth first search before the computation, use that ordering. |
| 80 |
3. Q: When performing the depth first search how should I choose between |
| 81 |
children? |
| 82 |
|
| 83 |
A: Choose those children on whom the most data depends |
| 84 |
|
| 85 |
We have found common workflow types that require each of these decisions. We |
| 86 |
have not yet run into a commonly occurring graph type in data analysis that is |
| 87 |
not well handled by these heuristics for the purposes of minimizing memory use. |