Debian Codegraph @

docs/source/shared.rst

raw ◀ ▶
1 Debian patch touch this file
1 Shared Memory
2 =============
3
4 The asynchronous scheduler accepts any ``concurrent.futures.Executor``
5 instance. This includes instances of the ``ThreadPoolExecutor`` and
6 ``ProcessPoolExecutor`` defined in the Python standard library as well as any
7 other subclass from a 3rd party library. Dask also defines its own
8 ``SynchronousExecutor`` for that simply runs functions on the main thread
9 (useful for debugging).
10
11 Full dask ``get`` functions exist in each of ``dask.threaded.get``,
12 ``dask.multiprocessing.get`` and ``dask.get`` respectively.
13
14
15 Policy
16 ------
17
18 The asynchronous scheduler maintains indexed data structures that show which
19 tasks depend on which data, what data is available, and what data is waiting on
20 what tasks to complete before it can be released, and what tasks are currently
21 running. It can update these in constant time relative to the number of total
22 and available tasks. These indexed structures make the dask async scheduler
23 scalable to very many tasks on a single machine.
24
25 .. image:: images/async-embarrassing.gif
26 :width: 50 %
27 :align: right
28 :alt: Embarrassingly parallel dask flow
29
30 To keep the memory footprint small, we choose to keep ready-to-run tasks in a
31 last-in-first-out stack such that the most recently made available tasks get
32 priority. This encourages the completion of chains of related tasks before new
33 chains are started. This can also be queried in constant time.
34
35
36 Performance
37 -----------
38
39 **tl;dr** The threaded scheduler overhead behaves roughly as follows:
40
41 * 200us overhead per task
42 * 10us startup time (if you wish to make a new ThreadPoolExecutor each time)
43 * Constant scaling with number of tasks
44 * Linear scaling with number of dependencies per task
45
46 Schedulers introduce overhead. This overhead effectively limits the
47 granularity of our parallelism. Below we measure overhead of the async
48 scheduler with different apply functions (threaded, sync, multiprocessing), and
49 under different kinds of load (embarrassingly parallel, dense communication).
50
51 The quickest/simplest test we can do it to use IPython's ``timeit`` magic:
52
53 .. ipython::
•54 :verbatim:
55
56 In [1]: import dask.array as da
57
58 In [2]: x = da.ones(1000, chunks=(2,)).sum()
59
60 In [3]: len(x.dask)
61 Out[3]: 1168
62
63 In [4]: %timeit x.compute()
64 92.1 ms ± 2.61 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
65
66 So this takes ~90 microseconds per task. About 100ms of this is from overhead:
67
68 .. ipython::
•69 :verbatim:
70
71 In [5]: x = da.ones(1000, chunks=(1000,)).sum()
72
73 In [6]: %timeit x.compute()
74 1.18 ms ± 8.64 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
75
76 There is some overhead from spinning up a ThreadPoolExecutor each time.
77 This may be mediated by using a global or contextual pool:
78
79 .. code-block:: python
80
81 >>> from concurrent.futures import ThreadPoolExecutor
82 >>> pool = ThreadPoolExecutor()
83 >>> dask.config.set(pool=pool) # set global ThreadPoolExecutor
84
85 or
86
87 >>> with dask.config.set(pool=pool) # use ThreadPoolExecutor throughout with block
88 ... ...
89
90 We now measure scaling the number of tasks and scaling the density of the
91 graph:
92
93 .. image:: images/trivial.svg
94 :width: 30 %
95 :align: right
96 :alt: Adding nodes
97
98 Linear scaling with number of tasks
99 ```````````````````````````````````
100
101 As we increase the number of tasks in a graph, we see that the scheduling
102 overhead grows linearly. The asymptotic cost per task depends on the scheduler.
103 The schedulers that depend on some sort of asynchronous pool have costs of a few
104 milliseconds and the single threaded schedulers have costs of a few microseconds.
105
106 .. figure:: images/scaling-nodes.png
107 :alt: Graph depicting how well Dask scales with the number of nodes in the task graph. Graph shows the duration in seconds on the y-axis versus number of edges per task on the x-axis. The time to schedule the entire graph is constant initially, followed by a linear increase after roughly 500 tasks for multiprocessing and threaded schedulers and 10 tasks for async and core schedulers. The inverse is true for the cost per task, with a linear cost decrease, followed by more or less constant cost.
108
109 Scheduling overhead for the entire graph (left) vs. per task (right)
110
111 .. image:: images/crosstalk.svg
112 :width: 40 %
113 :align: right
114 :alt: Adding edges
115
116 Linear scaling with number of edges
117 ```````````````````````````````````
118
119 As we increase the number of edges per task, the scheduling overhead
120 again increases linearly.
121
122 Note: Neither the naive core scheduler nor the multiprocessing scheduler
123 are good at workflows with non-trivial cross-task
124 communication; they have been removed from the plot.
125
126 .. figure:: images/scaling-edges.png
127 :alt: Graph depicting how well Dask scales with the number of edges in the task graph. Graph shows the duration in seconds on the y-axis versus number of edges per task on the x-axis. As the number of edges increases from 0 to 100, the time to schedule the entire graph using the threaded scheduler goes from 2 to 8 seconds whereas using the async scheduler goes from 0 to 3 seconds. The cost per edge decreases up until about 10 edges, after which the cost plateaus for both the threaded and async schedulers, with the async scheduler being consistently faster.
128
129 Scheduling overhead of the entire graph (left) vs. per edge (right)
130
131 `Download scheduling script`_
132
133
134 Known Limitations
135 -----------------
136
137 The shared memory scheduler has some notable limitations:
138
139 1. It works on a single machine
140 2. The threaded scheduler is limited by the GIL on Python code, so if your
141 operations are pure python functions, you should not expect a multi-core
142 speedup
143 3. The multiprocessing scheduler must serialize functions between workers,
144 which can fail
145 4. The multiprocessing scheduler must serialize data between workers and the
146 central process, which can be expensive
147 5. The multiprocessing scheduler cannot transfer data directly between worker
148 processes; all data routes through the main process.
149
150
151
152 .. _`Download scheduling script`: https://github.com/dask/dask/tree/main/docs/source/scripts/scheduling.py