Debian Codegraph @

docs/source/shared.rst

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
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
55 In [1]: import dask.array as da
56
57 In [2]: x = da.ones(1000, chunks=(2,)).sum()
58
59 In [3]: len(x.dask)
60 Out[3]: 1168
61
62 In [4]: %timeit x.compute()
63 92.1 ms ± 2.61 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
64
65 So this takes ~90 microseconds per task. About 100ms of this is from overhead:
66
67 .. ipython::
68
69 In [5]: x = da.ones(1000, chunks=(1000,)).sum()
70
71 In [6]: %timeit x.compute()
72 1.18 ms ± 8.64 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
73
74 There is some overhead from spinning up a ThreadPoolExecutor each time.
75 This may be mediated by using a global or contextual pool:
76
77 .. code-block:: python
78
79 >>> from concurrent.futures import ThreadPoolExecutor
80 >>> pool = ThreadPoolExecutor()
81 >>> dask.config.set(pool=pool) # set global ThreadPoolExecutor
82
83 or
84
85 >>> with dask.config.set(pool=pool) # use ThreadPoolExecutor throughout with block
86 ... ...
87
88 We now measure scaling the number of tasks and scaling the density of the
89 graph:
90
91 .. image:: images/trivial.svg
92 :width: 30 %
93 :align: right
94 :alt: Adding nodes
95
96 Linear scaling with number of tasks
97 ```````````````````````````````````
98
99 As we increase the number of tasks in a graph, we see that the scheduling
100 overhead grows linearly. The asymptotic cost per task depends on the scheduler.
101 The schedulers that depend on some sort of asynchronous pool have costs of a few
102 milliseconds and the single threaded schedulers have costs of a few microseconds.
103
104 .. figure:: images/scaling-nodes.png
105 :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.
106
107 Scheduling overhead for the entire graph (left) vs. per task (right)
108
109 .. image:: images/crosstalk.svg
110 :width: 40 %
111 :align: right
112 :alt: Adding edges
113
114 Linear scaling with number of edges
115 ```````````````````````````````````
116
117 As we increase the number of edges per task, the scheduling overhead
118 again increases linearly.
119
120 Note: Neither the naive core scheduler nor the multiprocessing scheduler
121 are good at workflows with non-trivial cross-task
122 communication; they have been removed from the plot.
123
124 .. figure:: images/scaling-edges.png
125 :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.
126
127 Scheduling overhead of the entire graph (left) vs. per edge (right)
128
129 `Download scheduling script`_
130
131
132 Known Limitations
133 -----------------
134
135 The shared memory scheduler has some notable limitations:
136
137 1. It works on a single machine
138 2. The threaded scheduler is limited by the GIL on Python code, so if your
139 operations are pure python functions, you should not expect a multi-core
140 speedup
141 3. The multiprocessing scheduler must serialize functions between workers,
142 which can fail
143 4. The multiprocessing scheduler must serialize data between workers and the
144 central process, which can be expensive
145 5. The multiprocessing scheduler cannot transfer data directly between worker
146 processes; all data routes through the main process.
147
148
149
150 .. _`Download scheduling script`: https://github.com/dask/dask/tree/main/docs/source/scripts/scheduling.py