Skip to content

Commit

Permalink
types: Fix caching by changing BoundSymbol inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
FabioLuporini committed Feb 4, 2021
1 parent cb4f6ad commit f367a6f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
15 changes: 12 additions & 3 deletions devito/types/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1203,18 +1203,27 @@ def __getitem__(self, indices, **kwargs):
__reduce_ex__ = Pickable.__reduce_ex__


class BoundSymbol(Symbol):
class BoundSymbol(AbstractSymbol):

"""
Wrapper class for Symbols that are bound to a symbolic data object.
Notes
-----
By deliberately inheriting from AbstractSymbol, a BoundSymbol won't be
in the devito cache. This will avoid cycling references in the cache
(e.g., an entry for a Function `u(x)` and an entry for `u._C_symbol` with
the latter's key including `u(x)`). This is totally fine. The BoundSymbol
is tied to a specific Function; once the Function gets out of scope, the
BoundSymbol will also become a garbage collector candidate.
"""

def __new__(cls, *args, function=None, **kwargs):
obj = Symbol.__new__(cls, *args, **kwargs)
obj = AbstractSymbol.__new__(cls, *args, **kwargs)
obj._function = function
return obj

@cached_property
@property
def function(self):
return self._function

Expand Down
22 changes: 17 additions & 5 deletions tests/test_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,13 +455,25 @@ def test_clear_cache(self, operate_on_empty_cache, nx=1000, ny=1000):
for i in range(10):
assert(len(_SymbolCache) == cache_size)

u = Function(name='u', grid=grid, space_order=2)
u._C_symbol
# All u, u(inds) and u.function through u._c_symbol added to cache
assert(len(_SymbolCache) == cache_size + 3)
del u
Function(name='u', grid=grid, space_order=2)
# Both u and u(inds) added to cache
assert(len(_SymbolCache) == cache_size + 2)

clear_cache()

def test_clear_cache_with_Csymbol(self, operate_on_empty_cache, nx=1000, ny=1000):
grid = Grid(shape=(nx, ny), dtype=np.float64)
cache_size = len(_SymbolCache)

u = Function(name='u', grid=grid, space_order=2)
# Both u and u(inds) added to cache
assert(len(_SymbolCache) == cache_size + 2)

a = u._C_symbol
# Cache size won't change since _C_symbol isn't cached by devito to
# avoid circular references in the cache
assert(len(_SymbolCache) == cache_size + 2)

def test_clear_cache_with_alive_symbols(self, operate_on_empty_cache,
nx=1000, ny=1000):
"""
Expand Down

0 comments on commit f367a6f

Please sign in to comment.