Skip to content

Commit 218e77a

Browse files
fmaussionmax-sixty
andauthored
Add some warnings about rechunking to the docs (#6569)
* Dask doc changes * small change * More edits * Update doc/user-guide/dask.rst * Update doc/user-guide/dask.rst * Back to one liners Co-authored-by: Maximilian Roos <[email protected]>
1 parent 4b76831 commit 218e77a

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

doc/user-guide/dask.rst

+29-14
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ argument to :py:func:`~xarray.open_dataset` or using the
8484
8585
In this example ``latitude`` and ``longitude`` do not appear in the ``chunks``
8686
dict, so only one chunk will be used along those dimensions. It is also
87-
entirely equivalent to opening a dataset using :py:meth:`~xarray.open_dataset`
87+
entirely equivalent to opening a dataset using :py:func:`~xarray.open_dataset`
8888
and then chunking the data using the ``chunk`` method, e.g.,
8989
``xr.open_dataset('example-data.nc').chunk({'time': 10})``.
9090

@@ -95,13 +95,21 @@ use :py:func:`~xarray.open_mfdataset`::
9595

9696
This function will automatically concatenate and merge datasets into one in
9797
the simple cases that it understands (see :py:func:`~xarray.combine_by_coords`
98-
for the full disclaimer). By default, :py:meth:`~xarray.open_mfdataset` will chunk each
98+
for the full disclaimer). By default, :py:func:`~xarray.open_mfdataset` will chunk each
9999
netCDF file into a single Dask array; again, supply the ``chunks`` argument to
100100
control the size of the resulting Dask arrays. In more complex cases, you can
101-
open each file individually using :py:meth:`~xarray.open_dataset` and merge the result, as
102-
described in :ref:`combining data`. Passing the keyword argument ``parallel=True`` to :py:meth:`~xarray.open_mfdataset` will speed up the reading of large multi-file datasets by
101+
open each file individually using :py:func:`~xarray.open_dataset` and merge the result, as
102+
described in :ref:`combining data`. Passing the keyword argument ``parallel=True`` to
103+
:py:func:`~xarray.open_mfdataset` will speed up the reading of large multi-file datasets by
103104
executing those read tasks in parallel using ``dask.delayed``.
104105

106+
.. warning::
107+
108+
:py:func:`~xarray.open_mfdataset` called without ``chunks`` argument will return
109+
dask arrays with chunk sizes equal to the individual files. Re-chunking
110+
the dataset after creation with ``ds.chunk()`` will lead to an ineffective use of
111+
memory and is not recommended.
112+
105113
You'll notice that printing a dataset still shows a preview of array values,
106114
even if they are actually Dask arrays. We can do this quickly with Dask because
107115
we only need to compute the first few values (typically from the first block).
@@ -224,6 +232,7 @@ disk.
224232
available memory.
225233

226234
.. note::
235+
227236
For more on the differences between :py:meth:`~xarray.Dataset.persist` and
228237
:py:meth:`~xarray.Dataset.compute` see this `Stack Overflow answer <https://stackoverflow.com/questions/41806850/dask-difference-between-client-persist-and-client-compute>`_ and the `Dask documentation <https://distributed.dask.org/en/latest/manage-computation.html#dask-collections-to-futures>`_.
229238

@@ -236,6 +245,11 @@ sizes of Dask arrays is done with the :py:meth:`~xarray.Dataset.chunk` method:
236245
237246
rechunked = ds.chunk({"latitude": 100, "longitude": 100})
238247
248+
.. warning::
249+
250+
Rechunking an existing dask array created with :py:func:`~xarray.open_mfdataset`
251+
is not recommended (see above).
252+
239253
You can view the size of existing chunks on an array by viewing the
240254
:py:attr:`~xarray.Dataset.chunks` attribute:
241255

@@ -295,8 +309,7 @@ each block of your xarray object, you have three options:
295309
``apply_ufunc``
296310
~~~~~~~~~~~~~~~
297311

298-
Another option is to use xarray's :py:func:`~xarray.apply_ufunc`, which can
299-
automate `embarrassingly parallel
312+
:py:func:`~xarray.apply_ufunc` automates `embarrassingly parallel
300313
<https://en.wikipedia.org/wiki/Embarrassingly_parallel>`__ "map" type operations
301314
where a function written for processing NumPy arrays should be repeatedly
302315
applied to xarray objects containing Dask arrays. It works similarly to
@@ -542,18 +555,20 @@ larger chunksizes.
542555
Optimization Tips
543556
-----------------
544557

545-
With analysis pipelines involving both spatial subsetting and temporal resampling, Dask performance can become very slow in certain cases. Here are some optimization tips we have found through experience:
558+
With analysis pipelines involving both spatial subsetting and temporal resampling, Dask performance
559+
can become very slow or memory hungry in certain cases. Here are some optimization tips we have found
560+
through experience:
546561

547-
1. Do your spatial and temporal indexing (e.g. ``.sel()`` or ``.isel()``) early in the pipeline, especially before calling ``resample()`` or ``groupby()``. Grouping and resampling triggers some computation on all the blocks, which in theory should commute with indexing, but this optimization hasn't been implemented in Dask yet. (See `Dask issue #746 <https://github.com/dask/dask/issues/746>`_).
562+
1. Do your spatial and temporal indexing (e.g. ``.sel()`` or ``.isel()``) early in the pipeline, especially before calling ``resample()`` or ``groupby()``. Grouping and resampling triggers some computation on all the blocks, which in theory should commute with indexing, but this optimization hasn't been implemented in Dask yet. (See `Dask issue #746 <https://github.com/dask/dask/issues/746>`_). More generally, ``groupby()`` is a costly operation and does not (yet) perform well on datasets split across multiple files (see :pull:`5734` and linked discussions there).
548563

549564
2. Save intermediate results to disk as a netCDF files (using ``to_netcdf()``) and then load them again with ``open_dataset()`` for further computations. For example, if subtracting temporal mean from a dataset, save the temporal mean to disk before subtracting. Again, in theory, Dask should be able to do the computation in a streaming fashion, but in practice this is a fail case for the Dask scheduler, because it tries to keep every chunk of an array that it computes in memory. (See `Dask issue #874 <https://github.com/dask/dask/issues/874>`_)
550565

551-
3. Specify smaller chunks across space when using :py:meth:`~xarray.open_mfdataset` (e.g., ``chunks={'latitude': 10, 'longitude': 10}``). This makes spatial subsetting easier, because there's no risk you will load chunks of data referring to different chunks (probably not necessary if you follow suggestion 1).
566+
3. Specify smaller chunks across space when using :py:meth:`~xarray.open_mfdataset` (e.g., ``chunks={'latitude': 10, 'longitude': 10}``). This makes spatial subsetting easier, because there's no risk you will load subsets of data which span multiple chunks. On individual files, prefer to subset before chunking (suggestion 1).
567+
568+
4. Chunk as early as possible, and avoid rechunking as much as possible. Always pass the ``chunks={}`` argument to :py:func:`~xarray.open_mfdataset` to avoid redundant file reads.
552569

553-
4. Using the h5netcdf package by passing ``engine='h5netcdf'`` to :py:meth:`~xarray.open_mfdataset`
554-
can be quicker than the default ``engine='netcdf4'`` that uses the netCDF4 package.
570+
5. Using the h5netcdf package by passing ``engine='h5netcdf'`` to :py:meth:`~xarray.open_mfdataset` can be quicker than the default ``engine='netcdf4'`` that uses the netCDF4 package.
555571

556-
5. Some dask-specific tips may be found `here <https://docs.dask.org/en/latest/array-best-practices.html>`_.
572+
6. Some dask-specific tips may be found `here <https://docs.dask.org/en/latest/array-best-practices.html>`_.
557573

558-
6. The dask `diagnostics <https://docs.dask.org/en/latest/understanding-performance.html>`_ can be
559-
useful in identifying performance bottlenecks.
574+
7. The dask `diagnostics <https://docs.dask.org/en/latest/understanding-performance.html>`_ can be useful in identifying performance bottlenecks.

0 commit comments

Comments
 (0)