Skip to content

Commit 0e00af3

Browse files
First draft of explanation_performance guide
1 parent 1dc5462 commit 0e00af3

2 files changed

Lines changed: 86 additions & 1 deletion

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# 📖 Squeezing performance in Parcels
2+
3+
In many Parcels simulations, the bottle-neck in terms of performance is the retrieval of the hydrodynamic field data from disk. This is especially true for simulations with a relatively small number of particles, where the time spent on retrieving the fields can be much larger than the time spent on computing the particle trajectories.
4+
5+
In this tutorial, we will show how to squeeze performance in Parcels by using a few different techniques. Which technique works best for your case depends on the amount of field data you have and how you can store it on disk.
6+
7+
## Option 1: explicitly load the full FieldSet into memory
8+
9+
**Works well for: small Datasets (less than a few GB)**
10+
11+
For relatively small Datasets (less than a few GB), it is possible to load the entire FieldSet into memory. This can be done by calling the `load()` method on the `xarray.Dataset` object:
12+
13+
```{code-cell}
14+
ds = ds.load()
15+
```
16+
17+
This will make Parcels use `numpy` functions in the interpolation routines, which are much faster than the `xarray` functions. However, this will also increase the memory usage of your simulation, so it is not always possible to use this option.
18+
19+
### Advantages and disadvantages
20+
21+
| Advantages | Disadvantages |
22+
| ---------------------------------- | ------------------------------------------------------ |
23+
| Very fast and simple to implement. | Will only work if the entire Dataset fits into memory. |
24+
25+
## Option 2: use cached zarr files
26+
27+
**Works well for: large Datasets (more than a few GB) and particles distributed over a small part of the domain**
28+
29+
If your Dataset is too large to fit into memory, but your particles are only distributed over a small part of the domain, it could be efficient to use cached zarr files. This can be done by using the (experimental) `zarr.CacheStore` in combination with the `parcels.open_raw_zarr()` function. This will make Parcels only load the chunks that are needed for the particles, and cache these chunks in memory for future use.
30+
31+
```{code-cell}
32+
source_store = zarr.storage.LocalStore(filenames)
33+
cache_store = zarr.storage.MemoryStore()
34+
35+
store = CacheStore(
36+
store=source_store, cache_store=cache_store, max_size=MAX_CACHE_SIZE
37+
)
38+
ds = parcels.open_raw_zarr(store)
39+
```
40+
41+
### Advantages and disadvantages
42+
43+
| Advantages | Disadvantages |
44+
| -------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
45+
| Parcels will only load the chunks that are needed for the particles, which can be much less than the entire Dataset. | The hydrodynamic data will have to be stored in zarr format, which may require additional storage space. |
46+
| | The fieldset data can't be changed after it is loaded, as dask operations are not supported on the raw zarr data. |
47+
48+
```{note}
49+
In our performance testing, we have found that using zarr files saved without any compression can be considerably faster than using compressed zarr files. However, we are working on an upstream fix in to make caching compressed zarr files faster, so this may change in the future.
50+
```
51+
52+
## Option 3: use `fieldset.to_windowed_arrays()`
53+
54+
**Works well for: large Datasets (more than a few GB) and particles distributed over the entire domain**
55+
56+
If your Dataset is so large that it doesn't fit into memory, you can use the `fieldset.to_windowed_arrays()` method to make Parcels only hold two timeslices in memory. Note that this only works if the two timeslices still fit into memory.
57+
58+
The two timeslices (the current and the next) are fully loaded into memory, so this method is especially useful if your particles are distributed over the entire domain, as all data will then have to be accessed anyway.
59+
60+
```{code-cell}
61+
fieldset.to_windowed_arrays()
62+
```
63+
64+
### Advantages and disadvantages
65+
66+
| Advantages | Disadvantages |
67+
| -------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
68+
| Parcels will only hold two timeslices in memory, which is much less than the entire Dataset. | Parcels will still have to load the entire two timeslices into memory, which can be a lot of data if the Dataset is large. |
69+
| Hydrodynamic files do not have to be reformatted and stored. | Inefficient when the Particles only sample a small part of the domain, as Parcels will still load the entire two timeslices into memory. |
70+
71+
## Option 4: use Dask
72+
73+
**Works well for: large Datasets (more than a few GB) and small ParticleSets (less than a few hundred particles)**
74+
75+
If your Dataset is so large that it doesn't fit into memory, and you have very few particles, you can use Dask to perform the interpolation operations. In this case, you don't have to do any special setup, as Parcels will automatically use Dask if the `xarray.Dataset` is a Dask array.
76+
77+
### Advantages and disadvantages
78+
79+
| Advantages | Disadvantages |
80+
| -------------------- | ---------------------------------------------- |
81+
| Works out-of-the-box | Only performs well for very small ParticleSets |
82+
83+
```{note}
84+
The long-term plan for Parcles development is to make this Option 4 work well for all cases. However, this will require significant work on Dask indexing.
85+
```

docs/user_guide/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ examples/tutorial_interpolation.ipynb
6969
:caption: Run a simulation
7070
:name: tutorial-execute
7171
:titlesonly:
72-
72+
examples/explanation_performance.md
7373
examples/tutorial_dt_integrators.ipynb
7474
```
7575

0 commit comments

Comments
 (0)