| 1 |
Best Practices |
| 2 |
============== |
| 3 |
|
| 4 |
It is easy to get started with Dask arrays, but using them *well* does require |
| 5 |
some experience. This page contains suggestions for best practices, and |
| 6 |
includes solutions to common problems. |
| 7 |
|
| 8 |
Use NumPy |
| 9 |
--------- |
| 10 |
|
| 11 |
If your data fits comfortably in RAM and you are not performance bound, then |
| 12 |
using NumPy might be the right choice. Dask adds another layer of complexity |
| 13 |
which may get in the way. |
| 14 |
|
| 15 |
If you are just looking for speedups rather than scalability then you may want |
| 16 |
to consider a project like `Numba <https://numba.pydata.org>`_ |
| 17 |
|
| 18 |
|
| 19 |
Select a good chunk size |
| 20 |
------------------------ |
| 21 |
|
| 22 |
A common performance problem among Dask Array users is that they have chosen a |
| 23 |
chunk size that is either too small (leading to lots of overhead) or poorly |
| 24 |
aligned with their data (leading to inefficient reading). |
| 25 |
|
| 26 |
While optimal sizes and shapes are highly problem specific, it is rare to see |
| 27 |
chunk sizes below 100 MB in size. If you are dealing with float64 data then |
| 28 |
this is around ``(4000, 4000)`` in size for a 2D array or ``(100, 400, 400)`` |
| 29 |
for a 3D array. |
| 30 |
|
| 31 |
You want to choose a chunk size that is large in order to reduce the number of |
| 32 |
chunks that Dask has to think about (which affects overhead) but also small |
| 33 |
enough so that many of them can fit in memory at once. Dask will often have as |
| 34 |
many chunks in memory as twice the number of active threads. |
| 35 |
|
| 36 |
|
| 37 |
Orient your chunks |
| 38 |
------------------ |
| 39 |
|
| 40 |
When reading data you should align your chunks with your storage format. |
| 41 |
Most array storage formats store data in chunks themselves. If your Dask array |
| 42 |
chunks aren't multiples of these chunk shapes then you will have to read the |
| 43 |
same data repeatedly, which can be expensive. Note though that often |
| 44 |
storage formats choose chunk sizes that are much smaller than is ideal for |
| 45 |
Dask, closer to 1MB than 100MB. In these cases you should choose a Dask chunk |
| 46 |
size that aligns with the storage chunk size and that every Dask chunk |
| 47 |
dimension is a multiple of the storage chunk dimension. |
| 48 |
|
| 49 |
So for example if we have an HDF file that has chunks of size ``(128, 64)``, we |
| 50 |
might choose a chunk shape of ``(1280, 6400)``. |
| 51 |
|
| 52 |
.. code-block:: python |
| 53 |
|
| 54 |
>>> import h5py |
| 55 |
>>> storage = h5py.File('myfile.hdf5')['x'] |
| 56 |
>>> storage.chunks |
| 57 |
(128, 64) |
| 58 |
|
| 59 |
>>> import dask.array as da |
| 60 |
>>> x = da.from_array(storage, chunks=(1280, 6400)) |
| 61 |
|
| 62 |
Note that if you provide ``chunks='auto'`` then Dask Array will look for a |
| 63 |
``.chunks`` attribute and use that to provide a good chunking. |
| 64 |
|
| 65 |
|
| 66 |
Avoid Oversubscribing Threads |
| 67 |
----------------------------- |
| 68 |
|
| 69 |
.. tip:: |
| 70 |
|
| 71 |
When using the ``distributed`` scheduler, the ``OMP_NUM_THREADS``, |
| 72 |
``MKL_NUM_THREADS``, and ``OPENBLAS_NUM_THREADS`` environment variables |
| 73 |
are automatically set to ``1`` when using :ref:`nanny` workers. |
| 74 |
This helps avoid oversubscribing threads in common cases. |
| 75 |
|
| 76 |
By default Dask will run as many concurrent tasks as you have logical cores. |
| 77 |
It assumes that each task will consume about one core. However, many |
| 78 |
array-computing libraries are themselves multi-threaded, which can cause |
| 79 |
contention and low performance. In particular the BLAS/LAPACK libraries that |
| 80 |
back most of NumPy's linear algebra routines are often multi-threaded, and need |
| 81 |
to be told to use only one thread explicitly. You can do this with the |
| 82 |
following environment variables (using bash ``export`` command below, but this |
| 83 |
may vary depending on your operating system). |
| 84 |
|
| 85 |
.. code-block:: bash |
| 86 |
|
| 87 |
export OMP_NUM_THREADS=1 |
| 88 |
export MKL_NUM_THREADS=1 |
| 89 |
export OPENBLAS_NUM_THREADS=1 |
| 90 |
|
| 91 |
You need to run this before you start your Python process for it to take effect. |
| 92 |
|
| 93 |
|
| 94 |
Consider Xarray |
| 95 |
--------------- |
| 96 |
|
| 97 |
The `Xarray <http://xarray.pydata.org/en/stable/>`_ package wraps around Dask |
| 98 |
Array, and so offers the same scalability, but also adds convenience when |
| 99 |
dealing with complex datasets. In particular Xarray can help with the |
| 100 |
following: |
| 101 |
|
| 102 |
1. Manage multiple arrays together as a consistent dataset |
| 103 |
2. Read from a stack of HDF or NetCDF files at once |
| 104 |
3. Switch between Dask Array and NumPy with a consistent API |
| 105 |
|
| 106 |
Xarray is used in wide range of fields, including physics, astronomy, geoscience, |
| 107 |
microscopy, bioinformatics, engineering, finance, and deep learning. |
| 108 |
Xarray also has a thriving user community that is good at providing support. |
| 109 |
|
| 110 |
|
| 111 |
Build your own Operations |
| 112 |
------------------------- |
| 113 |
|
| 114 |
Often we want to perform computations for which there is no exact function |
| 115 |
in Dask Array. In these cases we may be able to use some of the more generic |
| 116 |
functions to build our own. These include: |
| 117 |
|
| 118 |
.. currentmodule:: dask.array |
| 119 |
|
| 120 |
.. autosummary:: |
| 121 |
blockwise |
| 122 |
map_blocks |
| 123 |
map_overlap |
| 124 |
reduction |
| 125 |
|
| 126 |
These functions may help you to apply a function that you write for NumPy |
| 127 |
functions onto larger Dask arrays. |