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

ckp: Checkpointing update #1774

Merged
merged 6 commits into from
Oct 13, 2021
Merged
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
1 change: 1 addition & 0 deletions devito/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from devito.builtins import * # noqa
from devito.data.allocators import * # noqa
from devito.mpi import MPI # noqa
from devito.checkpointing import DevitoCheckpoint, CheckpointOperator # noqa

# Imports required to initialize Devito
from devito.arch import compiler_registry, platform_registry
Expand Down
File renamed without changes.
97 changes: 97 additions & 0 deletions devito/checkpointing/checkpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
from pyrevolve import Checkpoint, Operator
from devito import TimeFunction
from devito.tools import flatten


class CheckpointOperator(Operator):
"""Devito's concrete implementation of the ABC pyrevolve.Operator. This class wraps
devito.Operator so it conforms to the pyRevolve API. pyRevolve will call apply
with arguments t_start and t_end. Devito calls these arguments time_m and time_M
so the following dict is used to perform the translations between different names.

Parameters
----------
op : Operator
devito.Operator object that this object will wrap.
args : dict
If devito.Operator.apply() expects any arguments, they can be provided
here to be cached. Any calls to CheckpointOperator.apply() will
automatically include these cached arguments in the call to the
underlying devito.Operator.apply().
"""
t_arg_names = {'t_start': 'time_m', 't_end': 'time_M'}

def __init__(self, op, **kwargs):
self.op = op
self.args = self.op._prepare_arguments(**kwargs)
self.start_offset = self.args[self.t_arg_names['t_start']]

def _prepare_args(self, t_start, t_end):
args = self.args.copy()
args[self.t_arg_names['t_start']] = t_start + self.start_offset
args[self.t_arg_names['t_end']] = t_end - 1 + self.start_offset
return args

def apply(self, t_start, t_end):
""" If the devito operator requires some extra arguments in the call to apply
they can be stored in the args property of this object so pyRevolve calls
pyRevolve.Operator.apply() without caring about these extra arguments while
this method passes them on correctly to devito.Operator
"""
# Build the arguments list to invoke the kernel function
args = self._prepare_args(t_start, t_end)
# Invoke kernel function with args
arg_values = [args[p.name] for p in self.op.parameters]
self.op.cfunction(*arg_values)


class DevitoCheckpoint(Checkpoint):
"""Devito's concrete implementation of the Checkpoint abstract base class provided by
pyRevolve. Holds a list of symbol objects that hold data.
"""
def __init__(self, objects):
"""Intialise a checkpoint object. Upon initialisation, a checkpoint
stores only a reference to the objects that are passed into it."""
assert(all(isinstance(o, TimeFunction) for o in objects))
dtypes = set([o.dtype for o in objects])
assert(len(dtypes) == 1)
self._dtype = dtypes.pop()
self.objects = objects

@property
def dtype(self):
return self._dtype

def get_data(self, timestep):
data = flatten([get_symbol_data(s, timestep) for s in self.objects])
return data

def get_data_location(self, timestep):
return self.get_data(timestep)

@property
def size(self):
"""The memory consumption of the data contained in a checkpoint."""
return sum([int((o.size_allocated/(o.time_order+1))*o.time_order)
for o in self.objects])

def save(*args):
raise RuntimeError("Invalid method called. Did you check your version" +
" of pyrevolve?")

def load(*args):
raise RuntimeError("Invalid method called. Did you check your version" +
" of pyrevolve?")


def get_symbol_data(symbol, timestep):
timestep += symbol.time_order - 1
ptrs = []
for i in range(symbol.time_order):
# Use `._data`, instead of `.data`, as `.data` is a view of the DOMAIN
# data region which is non-contiguous in memory. The performance hit from
# dealing with non-contiguous memory is so big (introduces >1 copy), it's
# better to checkpoint unneccesarry stuff to get a contiguous chunk of memory.
ptr = symbol._data[timestep - i, :, :]
ptrs.append(ptr)
return ptrs
101 changes: 5 additions & 96 deletions examples/checkpointing/checkpoint.py
Original file line number Diff line number Diff line change
@@ -1,98 +1,7 @@
from pyrevolve import Checkpoint, Operator
from devito import TimeFunction
from devito.tools import flatten
from devito import warning

warning("""The location of Devito's checkpointing has changed. This location will be
deprecated soon. Please change your imports to 'from devito import
DevitoCheckpoint, CheckpointOperato'""")

class CheckpointOperator(Operator):
"""Devito's concrete implementation of the ABC pyrevolve.Operator. This class wraps
devito.Operator so it conforms to the pyRevolve API. pyRevolve will call apply
with arguments t_start and t_end. Devito calls these arguments t_s and t_e so
the following dict is used to perform the translations between different names.

Parameters
----------
op : Operator
devito.Operator object that this object will wrap.
args : dict
If devito.Operator.apply() expects any arguments, they can be provided
here to be cached. Any calls to CheckpointOperator.apply() will
automatically include these cached arguments in the call to the
underlying devito.Operator.apply().
"""
t_arg_names = {'t_start': 'time_m', 't_end': 'time_M'}

def __init__(self, op, **kwargs):
self.op = op
self.args = kwargs
op_default_args = self.op._prepare_arguments(**kwargs)
self.start_offset = op_default_args[self.t_arg_names['t_start']]

def _prepare_args(self, t_start, t_end):
args = self.args.copy()
args[self.t_arg_names['t_start']] = t_start + self.start_offset
args[self.t_arg_names['t_end']] = t_end - 1 + self.start_offset
return args

def apply(self, t_start, t_end):
""" If the devito operator requires some extra arguments in the call to apply
they can be stored in the args property of this object so pyRevolve calls
pyRevolve.Operator.apply() without caring about these extra arguments while
this method passes them on correctly to devito.Operator
"""
# Build the arguments list to invoke the kernel function
args = self.op.arguments(**self._prepare_args(t_start, t_end))
# Invoke kernel function with args
arg_values = [args[p.name] for p in self.op.parameters]
self.op.cfunction(*arg_values)


class DevitoCheckpoint(Checkpoint):
"""Devito's concrete implementation of the Checkpoint abstract base class provided by
pyRevolve. Holds a list of symbol objects that hold data.
"""
def __init__(self, objects):
"""Intialise a checkpoint object. Upon initialisation, a checkpoint
stores only a reference to the objects that are passed into it."""
assert(all(isinstance(o, TimeFunction) for o in objects))
dtypes = set([o.dtype for o in objects])
assert(len(dtypes) == 1)
self._dtype = dtypes.pop()
self.objects = objects

@property
def dtype(self):
return self._dtype

def get_data(self, timestep):
data = flatten([get_symbol_data(s, timestep) for s in self.objects])
return data

def get_data_location(self, timestep):
return self.get_data(timestep)

@property
def size(self):
"""The memory consumption of the data contained in a checkpoint."""
return sum([int((o.size_allocated/(o.time_order+1))*o.time_order)
for o in self.objects])

def save(*args):
raise RuntimeError("Invalid method called. Did you check your version" +
" of pyrevolve?")

def load(*args):
raise RuntimeError("Invalid method called. Did you check your version" +
" of pyrevolve?")


def get_symbol_data(symbol, timestep):
timestep += symbol.time_order - 1
ptrs = []
for i in range(symbol.time_order):
# Use `._data`, instead of `.data`, as `.data` is a view of the DOMAIN
# data region which is non-contiguous in memory. The performance hit from
# dealing with non-contiguous memory is so big (introduces >1 copy), it's
# better to checkpoint unneccesarry stuff to get a contiguous chunk of memory.
ptr = symbol._data[timestep - i, :, :]
ptrs.append(ptr)
return ptrs
from devito.checkpointing import * # noqa
3 changes: 1 addition & 2 deletions examples/seismic/acoustic/wavesolver.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from devito import Function, TimeFunction
from devito import Function, TimeFunction, DevitoCheckpoint, CheckpointOperator
from devito.tools import memoized_meth
from examples.seismic.acoustic.operators import (
ForwardOperator, AdjointOperator, GradientOperator, BornOperator
)
from examples.checkpointing.checkpoint import DevitoCheckpoint, CheckpointOperator
from pyrevolve import Revolver


Expand Down
3 changes: 1 addition & 2 deletions examples/seismic/tti/wavesolver.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# coding: utf-8
from devito import Function, TimeFunction, warning
from devito import Function, TimeFunction, warning, DevitoCheckpoint, CheckpointOperator
from devito.tools import memoized_meth
from examples.seismic.tti.operators import ForwardOperator, AdjointOperator
from examples.seismic.tti.operators import JacobianOperator, JacobianAdjOperator
from examples.seismic.tti.operators import particle_velocity_fields
from examples.checkpointing.checkpoint import DevitoCheckpoint, CheckpointOperator
from pyrevolve import Revolver


Expand Down
4 changes: 2 additions & 2 deletions examples/seismic/viscoacoustic/wavesolver.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from devito import VectorTimeFunction, TimeFunction, Function, NODE
from devito import (VectorTimeFunction, TimeFunction, Function, NODE,
DevitoCheckpoint, CheckpointOperator)
from devito.tools import memoized_meth
from examples.seismic import PointSource
from examples.seismic.viscoacoustic.operators import (
ForwardOperator, AdjointOperator, GradientOperator, BornOperator
)
from examples.checkpointing.checkpoint import DevitoCheckpoint, CheckpointOperator
from pyrevolve import Revolver


Expand Down
4 changes: 2 additions & 2 deletions tests/test_checkpointing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from pyrevolve import Revolver
import numpy as np

from devito import Grid, TimeFunction, Operator, Function, Eq, switchconfig, Constant
from examples.checkpointing.checkpoint import DevitoCheckpoint, CheckpointOperator
from devito import (Grid, TimeFunction, Operator, Function, Eq, switchconfig, Constant,
DevitoCheckpoint, CheckpointOperator)
from examples.seismic.acoustic.acoustic_example import acoustic_setup


Expand Down