| 1 |
.. _phases-of-computation: |
| 2 |
|
| 3 |
|
| 4 |
Stages of Computation |
| 5 |
===================== |
| 6 |
|
| 7 |
This page describes all of the parts of computation, some common causes of |
| 8 |
slowness, and how to effectively profile. This is intended for more advanced |
| 9 |
users who are encountering slowdowns on larger computations. |
| 10 |
|
| 11 |
Graph Construction |
| 12 |
------------------ |
| 13 |
|
| 14 |
Operations on Dask collections (array, dataframe, bag, delayed) build task |
| 15 |
graphs. These are dictionaries of Python functions that include an entry |
| 16 |
every time some function needs to run on some chunk of data. When these |
| 17 |
dictionaries become large (millions of tasks) the overhead of constructing them |
| 18 |
can become considerable. Additionally the code that builds the graphs may |
| 19 |
itself be inefficient. |
| 20 |
|
| 21 |
Fortunately, this computation is all happening in normal Python right on your |
| 22 |
own computer, so you can profile it just as you would any other Python code on |
| 23 |
your computer using tools like the cProfile module, or the ``%prun`` or |
| 24 |
``%snakeviz`` IPython magics. |
| 25 |
|
| 26 |
Assuming that no obvious cause comes up when profiling, a common solution to |
| 27 |
this problem is to reduce your graph size by increasing your chunk size if |
| 28 |
possible, or manually batching many operations into fewer functions. |
| 29 |
|
| 30 |
Graph Optimization |
| 31 |
------------------ |
| 32 |
|
| 33 |
Just before you submit the graph to be executed, Dask sees if it can clean up |
| 34 |
the graph a bit. This helps to remove unnecessary work, and sometimes swaps |
| 35 |
out more efficient operations. As before though, if your graph is very large |
| 36 |
(millions of tasks) then this can take some time. |
| 37 |
|
| 38 |
Also as before, this is all happening in Python on your local machine. You can |
| 39 |
profile optimization separately from computation with the ``dask.optimize`` |
| 40 |
function. |
| 41 |
|
| 42 |
.. code-block:: python |
| 43 |
|
| 44 |
# x, y = dask.compute(x, y) |
| 45 |
x, y = dask.optimize(x, y) |
| 46 |
|
| 47 |
It's rare for people to change optimization. It is rarely the main cause of |
| 48 |
slowdown. |
| 49 |
|
| 50 |
|
| 51 |
Graph serialization |
| 52 |
------------------- |
| 53 |
|
| 54 |
When you are using the distributed scheduler the graph must be sent to the |
| 55 |
scheduler process, and from there to the workers. To send this data it must |
| 56 |
first be converted into bytes. This serialization process can sometimes be |
| 57 |
expensive either if the objects that you're passing around are very complex, or |
| 58 |
if they are very large. |
| 59 |
|
| 60 |
The easiest way to profile this is to profile the ``persist`` call with the |
| 61 |
distributed scheduler. This will include both the optimization phase above, as |
| 62 |
well as the serialization and some of the communication phase below |
| 63 |
(serialization is often the largest component). Fortunately ``persist`` |
| 64 |
returns immediately after, not waiting for the computation to actually finish. |
| 65 |
|
| 66 |
Most often the cause of long serialization times is placing large objects |
| 67 |
like NumPy arrays or Pandas dataframes into your graph repeatedly. Dask will |
| 68 |
usually raise a warning when it notices this. Often the best solution is to |
| 69 |
read your data in as a task instead of include it directly, pre-scatter large |
| 70 |
data, or wrap them in ``dask.delayed``. Sometimes serialization is caused by |
| 71 |
other issues with complex objects. These tend to be very library specific, and |
| 72 |
so it is hard to provide general guidelines for them. |
| 73 |
|
| 74 |
Graph Communication |
| 75 |
------------------- |
| 76 |
|
| 77 |
The graph must then be communicated to the scheduler. You can watch the |
| 78 |
``/system`` tab of the dashboard to watch network communication to and from the |
| 79 |
scheduler. There is no good way to profile this. |
| 80 |
|
| 81 |
|
| 82 |
Scheduling |
| 83 |
---------- |
| 84 |
|
| 85 |
The scheduler now receives the graph, and must populate its internal data |
| 86 |
structures to be able to efficiently schedule these tasks to the various |
| 87 |
workers. |
| 88 |
|
| 89 |
It is only after these data structures are populated that the dashboard will |
| 90 |
show any activity. All time between pressing ``compute/persist`` and seeing |
| 91 |
activity is taken up in the stages above. |
| 92 |
|
| 93 |
You can profile scheduling costs with the ``/profile-server`` page of the |
| 94 |
dashboard. However, this is rarely useful for users because, unless you're |
| 95 |
willing to dive into the scheduling code, it is hard to act here. Still, the |
| 96 |
interested user may find the profile of the inter workings of the scheduler of |
| 97 |
interest. |
| 98 |
|
| 99 |
If scheduling is expensive then the best you can do is to reduce your graph |
| 100 |
size, often by increasing chunk size. |
| 101 |
|
| 102 |
|
| 103 |
Execution |
| 104 |
--------- |
| 105 |
|
| 106 |
Finally your workers get sent some tasks and get to run them. Your code runs |
| 107 |
on a thread of a worker, and does whatever it was told to do. |
| 108 |
|
| 109 |
Dask's Dashboard is a good tool to profile and investigate performance here, |
| 110 |
particularly the ``/status`` and ``/profile`` pages. |
| 111 |
|
| 112 |
Accelerating this phase is often up to the author of the tasks that you are |
| 113 |
submitting. This might be you if you are using custom code, or the NumPy or |
| 114 |
Pandas developers. We encourage you to consider efficient libraries like |
| 115 |
Cython, Numba, or any other solution that is commonly used to accelerate Python |
| 116 |
code. |