| 1 |
.. _graphs: |
| 2 |
|
| 3 |
Task Graphs |
| 4 |
=========== |
| 5 |
|
| 6 |
Internally, Dask encodes algorithms in a simple format involving Python dicts, |
| 7 |
tuples, and functions. This graph format can be used in isolation from the |
| 8 |
dask collections. Working directly with dask graphs is rare, though, unless you intend |
| 9 |
to develop new modules with Dask. Even then, :doc:`dask.delayed <delayed>` is |
| 10 |
often a better choice. If you are a *core developer*, then you should start here. |
| 11 |
|
| 12 |
.. toctree:: |
| 13 |
:maxdepth: 1 |
| 14 |
|
| 15 |
spec.rst |
| 16 |
custom-graphs.rst |
| 17 |
optimize.rst |
| 18 |
graph_manipulation.rst |
| 19 |
custom-collections.rst |
| 20 |
high-level-graphs.rst |
| 21 |
|
| 22 |
|
| 23 |
Motivation |
| 24 |
---------- |
| 25 |
|
| 26 |
Normally, humans write programs and then compilers/interpreters interpret them |
| 27 |
(for example, ``python``, ``javac``, ``clang``). Sometimes humans disagree with how |
| 28 |
these compilers/interpreters choose to interpret and execute their programs. |
| 29 |
In these cases, humans often bring the analysis, optimization, and execution of |
| 30 |
code into the code itself. |
| 31 |
|
| 32 |
Commonly a desire for parallel execution causes this shift of responsibility |
| 33 |
from compiler to human developer. In these cases, we often represent the |
| 34 |
structure of our program explicitly as data within the program itself. |
| 35 |
|
| 36 |
A common approach to parallel execution in user-space is *task scheduling*. In |
| 37 |
task scheduling we break our program into many medium-sized tasks or units of |
| 38 |
computation, often a function call on a non-trivial amount of data. We |
| 39 |
represent these tasks as nodes in a graph with edges between nodes if one task |
| 40 |
depends on data produced by another. We call upon a *task scheduler* to |
| 41 |
execute this graph in a way that respects these data dependencies and leverages |
| 42 |
parallelism where possible, so multiple independent tasks can be run |
| 43 |
simultaneously. |
| 44 |
|
| 45 |
| |
| 46 |
|
| 47 |
.. figure:: images/map-reduce-task-scheduling.svg |
| 48 |
:scale: 40% |
| 49 |
|
| 50 |
There are a number of methods for task scheduling, including embarrassingly parallel, MapReduce, and full task scheduling. |
| 51 |
|
| 52 |
| |
| 53 |
|
| 54 |
Many solutions exist. This is a common approach in parallel execution |
| 55 |
frameworks. Often task scheduling logic hides within other larger frameworks |
| 56 |
(e.g. Luigi, Storm, Spark, IPython Parallel, etc.) and so is often reinvented. |
| 57 |
Dask is a specification that encodes full task scheduling with minimal incidental |
| 58 |
complexity using terms common to all Python projects, namely, dicts, tuples, |
| 59 |
and callables. Ideally this minimum solution is easy to adopt and understand |
| 60 |
by a broad community. |
| 61 |
|
| 62 |
Example |
| 63 |
------- |
| 64 |
|
| 65 |
Consider the following simple program: |
| 66 |
|
| 67 |
.. code-block:: python |
| 68 |
|
| 69 |
def inc(i): |
| 70 |
return i + 1 |
| 71 |
|
| 72 |
def add(a, b): |
| 73 |
return a + b |
| 74 |
|
| 75 |
x = 1 |
| 76 |
y = inc(x) |
| 77 |
z = add(y, 10) |
| 78 |
|
| 79 |
We encode this as a dictionary in the following way: |
| 80 |
|
| 81 |
.. code-block:: python |
| 82 |
|
| 83 |
d = {'x': 1, |
| 84 |
'y': (inc, 'x'), |
| 85 |
'z': (add, 'y', 10)} |
| 86 |
|
| 87 |
Which is represented by the following Dask graph: |
| 88 |
|
| 89 |
.. image:: _static/dask-simple.png |
| 90 |
:height: 400px |
| 91 |
:alt: A simple dask dictionary |
| 92 |
|
| 93 |
| |
| 94 |
|
| 95 |
While less pleasant than our original code, this representation can be analyzed |
| 96 |
and executed by other Python code, not just the CPython interpreter. We don't |
| 97 |
recommend that users write code in this way, but rather that it is an |
| 98 |
appropriate target for automated systems. Also, in non-toy examples, the |
| 99 |
execution times are likely much larger than for ``inc`` and ``add``, warranting |
| 100 |
the extra complexity. |
| 101 |
|
| 102 |
|
| 103 |
Schedulers |
| 104 |
---------- |
| 105 |
|
| 106 |
The Dask library currently contains a few schedulers to execute these |
| 107 |
graphs. Each scheduler works differently, providing different performance |
| 108 |
guarantees and operating in different contexts. These implementations are not |
| 109 |
special and others can write different schedulers better suited to other |
| 110 |
applications or architectures easily. Systems that emit dask graphs (like |
| 111 |
Dask Array, Dask Bag, and so on) may leverage the appropriate scheduler for |
| 112 |
the application and hardware. |
| 113 |
|
| 114 |
|
| 115 |
Task Expectations |
| 116 |
----------------- |
| 117 |
|
| 118 |
When a task is submitted to Dask for execution, there are a number of assumptions |
| 119 |
that are made about that task. |
| 120 |
|
| 121 |
Don't Modify Data In-Place |
| 122 |
~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 123 |
|
| 124 |
In general, tasks with side-effects that alter the state of a future in-place |
| 125 |
are not recommended. Modifying data that is stored in Dask in-place can have |
| 126 |
unintended consequences. For example, consider a workflow involving a Numpy |
| 127 |
array: |
| 128 |
|
| 129 |
.. code-block:: python |
| 130 |
|
| 131 |
from dask.distributed import Client |
| 132 |
import numpy as np |
| 133 |
|
| 134 |
client = Client() |
| 135 |
x = client.submit(np.arange, 10) # [0, 1, 2, 3, ...] |
| 136 |
|
| 137 |
def f(arr): |
| 138 |
arr[arr > 5] = 0 # modifies input directly without making a copy |
| 139 |
arr += 1 # modifies input directly without making a copy |
| 140 |
return arr |
| 141 |
|
| 142 |
y = client.submit(f, x) |
| 143 |
|
| 144 |
In the example above Dask will update the values of the Numpy array |
| 145 |
``x`` in-place. While efficient, this behavior can have unintended consequences, |
| 146 |
particularly if other tasks need to use ``x``, or if Dask needs to rerun this |
| 147 |
computation multiple times because of worker failure. |
| 148 |
|
| 149 |
|
| 150 |
Avoid Holding the GIL |
| 151 |
~~~~~~~~~~~~~~~~~~~~~ |
| 152 |
|
| 153 |
Some Python functions that wrap external C/C++ code can hold onto the GIL, |
| 154 |
which stops other Python code from running in the background. This is |
| 155 |
troublesome because while Dask workers run your function, they also need to |
| 156 |
communicate to each other in the background. |
| 157 |
|
| 158 |
If you wrap external code then please try to release the GIL. This is usually |
| 159 |
easy to do if you are using any of the common solutions to code-wrapping like |
| 160 |
Cython, Numba, ctypes or others. |