Skip to content

Commit

Permalink
Merge pull request #2148 from devitocodes/fix-pickle-alias-sf
Browse files Browse the repository at this point in the history
compiler: Fix pickling of aliasing SparseFunction
  • Loading branch information
FabioLuporini committed Jun 17, 2023
2 parents 4c54253 + 9fe2128 commit f80847e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion devito/types/sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,10 @@ def coordinates(self):

@property
def coordinates_data(self):
return self.coordinates.data.view(np.ndarray)
try:
return self.coordinates.data.view(np.ndarray)
except AttributeError:
return None

@cached_property
def _point_symbols(self):
Expand Down
19 changes: 19 additions & 0 deletions tests/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,25 @@ def test_receiver(self, pickle):
assert np.all(new_rec.data == 1)
assert np.all(new_rec.coordinates.data == [[0.], [1.], [2.]])

def test_alias_sparse_function(self, pickle):
grid = Grid(shape=(3,))
sf = SparseFunction(name='sf', grid=grid, npoint=3, space_order=2,
coordinates=[(0.,), (1.,), (2.,)])
sf.data[0] = 1.

# Create alias
f0 = sf._rebuild(name='f0', alias=True)

pkl_f0 = pickle.dumps(f0)
new_f0 = pickle.loads(pkl_f0)

assert f0.data is None and new_f0.data is None
assert f0.coordinates.data is None and new_f0.coordinates.data is None

assert sf.space_order == f0.space_order == new_f0.space_order
assert sf.dtype == f0.dtype == new_f0.dtype
assert sf.npoint == f0.npoint == new_f0.npoint


@pytest.mark.parametrize('pickle', [pickle0, pickle1])
class TestOperator(object):
Expand Down

0 comments on commit f80847e

Please sign in to comment.