Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic functional lazy saving. #5031

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 136 additions & 13 deletions lib/iris/fileformats/netcdf/saver.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
import warnings

import cf_units
import dask
import dask.array as da
from dask.utils import SerializableLock
import filelock
import netCDF4
import numpy as np
import numpy.ma as ma
Expand Down Expand Up @@ -490,10 +493,45 @@ def __setitem__(self, keys, arr):
MESH_ELEMENTS = ("node", "edge", "face")


class DeferredSaveWrapper:
"""
An object which mimics the data access of a netCDF4.Variable, and can be written to.
It encapsulates the netcdf file and variable which are actually to be written to.
This opens the file each time, to enable writing the data chunk, then closes it.
TODO: could be improved with a caching scheme, but this just about works.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pp-mo Apart from caching, locking should be considered, right?

Copy link
Member Author

@pp-mo pp-mo Oct 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dead right. I had not understood much, which is why it did not work with 'distributed'.
Hopefully that is now fixed @ed654d ...

"""

def __init__(self, filepath, cf_var, lockfile_path):
self.path = filepath
self.varname = cf_var.name
self.lockfile_path = lockfile_path

def __setitem__(self, keys, array_data):
# Write to the variable.
# First acquire a file-specific lock
# Importantly, in working via the file-system, this is common to all workers,
# even when using processes or distributed.
lock = filelock.FileLock(self.lockfile_path)
lock.acquire()
# Now re-open the file for writing + write to the specific file variable.
dataset = None
try:
dataset = netCDF4.Dataset(self.path, "r+")
var = dataset.variables[self.varname]
var[keys] = array_data
finally:
if dataset:
dataset.close()
lock.release()

def __repr__(self):
return f"<{self.__class__.__name__} path={self.path!r} var={self.varname!r}>"


class Saver:
"""A manager for saving netcdf files."""

def __init__(self, filename, netcdf_format):
def __init__(self, filename, netcdf_format, compute=True):
"""
A manager for saving netcdf files.

Expand All @@ -506,6 +544,15 @@ def __init__(self, filename, netcdf_format):
Underlying netCDF file format, one of 'NETCDF4', 'NETCDF4_CLASSIC',
'NETCDF3_CLASSIC' or 'NETCDF3_64BIT'. Default is 'NETCDF4' format.

* compute (bool):
If True, the Saver performs normal 'synchronous' data writes, where data
is streamed directly into file variables during the save operation.
If False, the file is created as normal, but computation and streaming of
any lazy array content is instead deferred to :class:`dask.delayed` objects,
which are held in a list in the saver 'delayed_writes' property.
Copy link
Member Author

@pp-mo pp-mo Oct 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bit can probably be treated as internal/private detail now.
Probably better to make .deferred_writes private, and ._deferred_save() public.

The relavant file variables are created empty, and the write can
subsequently be completed by computing the 'save.deferred_writes'.

Returns:
None.

Expand Down Expand Up @@ -542,18 +589,27 @@ def __init__(self, filename, netcdf_format):
self._mesh_dims = {}
#: A dictionary, mapping formula terms to owner cf variable name
self._formula_terms_cache = {}
#: Whether lazy saving.
self.lazy_saves = not compute
#: A list of deferred writes for lazy saving : each is a (source, target) pair
self.deferred_writes = []
#: Target filepath
self.filepath = os.path.abspath(filename)
#: Target lockfile path
self._lockfile_path = self.filepath + ".lock"
#: NetCDF dataset
self._dataset = None
try:
self._dataset = netCDF4.Dataset(
filename, mode="w", format=netcdf_format
self.filepath, mode="w", format=netcdf_format
)
except RuntimeError:
dir_name = os.path.dirname(filename)
dir_name = os.path.dirname(self.filepath)
if not os.path.isdir(dir_name):
msg = "No such file or directory: {}".format(dir_name)
raise IOError(msg)
if not os.access(dir_name, os.R_OK | os.W_OK):
msg = "Permission denied: {}".format(filename)
msg = "Permission denied: {}".format(self.filepath)
raise IOError(msg)
else:
raise
Expand Down Expand Up @@ -2442,8 +2498,7 @@ def _increment_name(self, varname):

return "{}_{}".format(varname, num)

@staticmethod
def _lazy_stream_data(data, fill_value, fill_warn, cf_var):
def _lazy_stream_data(self, data, fill_value, fill_warn, cf_var):
if hasattr(data, "shape") and data.shape == (1,) + cf_var.shape:
# (Don't do this check for string data).
# Reduce dimensionality where the data array has an extra dimension
Expand All @@ -2453,13 +2508,32 @@ def _lazy_stream_data(data, fill_value, fill_warn, cf_var):
data = data.squeeze(axis=0)

if is_lazy_data(data):
if self.lazy_saves:
# deferred lazy streaming
def store(data, cf_var, fill_value):
# Create a data-writeable object that we can stream into, which
# encapsulates the file to be opened + variable to be written.
writeable_var_wrapper = DeferredSaveWrapper(
self.filepath, cf_var, self._lockfile_path
)
# Add to the list of deferred writes, used in _deferred_save().
self.deferred_writes.append((data, writeable_var_wrapper))
# NOTE: in this case, no checking of fill-value violations so just
# return dummy values for this.
# TODO: just for now -- can probably make this work later
is_masked, contains_value = False, False
return is_masked, contains_value

def store(data, cf_var, fill_value):
# Store lazy data and check whether it is masked and contains
# the fill value
target = _FillValueMaskCheckAndStoreTarget(cf_var, fill_value)
da.store([data], [target])
return target.is_masked, target.contains_value
else:
# Immediate streaming store : check mask+fill as we go.
def store(data, cf_var, fill_value):
# Store lazy data and check whether it is masked and contains
# the fill value
target = _FillValueMaskCheckAndStoreTarget(
cf_var, fill_value
)
da.store([data], [target])
return target.is_masked, target.contains_value

else:

Expand Down Expand Up @@ -2509,6 +2583,39 @@ def store(data, cf_var, fill_value):
)
warnings.warn(msg.format(cf_var.name, fill_value))

def _deferred_save(self):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth making public?
Almost certainly better named 'delayed' than 'deferred' (!)

"""
Create a 'delayed' to trigger file completion for lazy saves.

This contains all the deferred writes, which complete the file by filling out
the data of variables initially created empty.

"""
# Create a lock to satisfy the da.store call.
# We need a serialisable lock for scheduling with processes or distributed.
# See : https://github.com/dask/distributed/issues/780
# However, this does *not* imply safe access for file writing in parallel.
# For that, DeferredSaveWrapper uses a filelock as well.
lock = SerializableLock()
Comment on lines +2594 to +2599
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you mention, the synchronisation of the parallel writing should be taken care of by the DeferredSaveWrapper, so one could set lock=False here.

Copy link
Member Author

@pp-mo pp-mo Dec 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the hint. The truth is, we are (I am) still learning here.
And also, it looks like the #5016 -> #5095 issues will affect the eventual implementation of this -- this code is definitely only a draft "Proof of Concept" solution.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! Just thought I would write this down, since it also took me a while to figure out from Xarray..


# Create a single delayed da.store operation to complete the file.
sources, targets = zip(*self.deferred_writes)
result = da.store(sources, targets, compute=False, lock=lock)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of the sources can be numpy array here, so one could do something like:

Suggested change
result = da.store(sources, targets, compute=False, lock=lock)
result = da.store([da.asarray(s) for s in sources], targets, compute=False, lock=lock)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above
I think this is the same issue ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's also related to having real data. One needs to make sure these are converted to Dask arrays.


# Wrap that in an extra operation that follows it by deleting the lockfile.
@dask.delayed
def postsave_remove_lockfile(store_op, lock_path):
if os.path.exists(lock_path):
try:
os.unlink(lock_path)
except Exception as e:
msg = f'Could not remove lockfile "{lock_path}". Error:\n{e}'
raise Exception(msg)

result = postsave_remove_lockfile(result, self._lockfile_path)

return result


def save(
cube,
Expand All @@ -2526,6 +2633,7 @@ def save(
least_significant_digit=None,
packing=None,
fill_value=None,
compute=True,
):
"""
Save cube(s) to a netCDF file, given the cube and the filename.
Expand Down Expand Up @@ -2648,6 +2756,14 @@ def save(
`:class:`iris.cube.CubeList`, or a single element, and each element of
this argument will be applied to each cube separately.

* compute (bool):
When False, create the output file but defer writing any lazy array content to
its variables, such as (lazy) data and aux-coords points and bounds.
Instead return a class:`dask.delayed` which, when computed, will compute all
the lazy content and stream it to complete the file.
Several such data saves can be performed in parallel, by passing a list of them
into a :func:`dask.compute` call.

Returns:
None.

Expand Down Expand Up @@ -2748,7 +2864,7 @@ def is_valid_packspec(p):
raise ValueError(msg)

# Initialise Manager for saving
with Saver(filename, netcdf_format) as sman:
with Saver(filename, netcdf_format, compute=compute) as sman:
# Iterate through the cubelist.
for cube, packspec, fill_value in zip(cubes, packspecs, fill_values):
sman.write(
Expand Down Expand Up @@ -2793,3 +2909,10 @@ def is_valid_packspec(p):

# Add conventions attribute.
sman.update_global_attributes(Conventions=conventions)

if compute:
result = None
else:
result = sman._deferred_save()

return result
9 changes: 7 additions & 2 deletions lib/iris/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ def save(source, target, saver=None, **kwargs):

# Single cube?
if isinstance(source, Cube):
saver(source, target, **kwargs)
result = saver(source, target, **kwargs)

# CubeList or sequence of cubes?
elif isinstance(source, CubeList) or (
Expand All @@ -463,13 +463,18 @@ def save(source, target, saver=None, **kwargs):
# Force append=True for the tail cubes. Don't modify the incoming
# kwargs.
kwargs = kwargs.copy()
result = []
for i, cube in enumerate(source):
if i != 0:
kwargs["append"] = True
saver(cube, target, **kwargs)

result = None
# Netcdf saver.
else:
saver(source, target, **kwargs)
result = saver(source, target, **kwargs)

else:
raise ValueError("Cannot save; non Cube found in source")

return result