From c377f579ec586e0c324841c9fe41c4002841aa6a Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Tue, 30 Jul 2024 18:35:51 +0100 Subject: [PATCH 1/5] types: Add LocalType --- devito/ir/iet/utils.py | 3 +-- devito/types/array.py | 23 +++++++++-------------- devito/types/basic.py | 24 ++++++++++++++++++++++++ devito/types/object.py | 17 ++--------------- 4 files changed, 36 insertions(+), 31 deletions(-) diff --git a/devito/ir/iet/utils.py b/devito/ir/iet/utils.py index 2bac05733c..a31c82973a 100644 --- a/devito/ir/iet/utils.py +++ b/devito/ir/iet/utils.py @@ -123,8 +123,7 @@ def derive_parameters(iet, drop_locals=False, ordering='default'): # Maybe filter out all other compiler-generated objects if drop_locals: - parameters = [p for p in parameters - if not (p.is_ArrayBasic or p.is_LocalObject)] + parameters = [p for p in parameters if not p.is_LocalType] # NOTE: This is requested by the caller when the parameters are used to # construct Callables whose signature only depends on the object types, diff --git a/devito/types/array.py b/devito/types/array.py index e45c3d78c9..00faeac072 100644 --- a/devito/types/array.py +++ b/devito/types/array.py @@ -6,17 +6,24 @@ from devito.tools import (Reconstructable, as_tuple, c_restrict_void_p, dtype_to_ctype, dtypes_vector_mapper, is_integer) -from devito.types.basic import AbstractFunction +from devito.types.basic import AbstractFunction, LocalType from devito.types.utils import CtypesFactory, DimensionTuple __all__ = ['Array', 'ArrayMapped', 'ArrayObject', 'PointerArray', 'Bundle', 'ComponentAccess', 'Bag'] -class ArrayBasic(AbstractFunction): +class ArrayBasic(AbstractFunction, LocalType): is_ArrayBasic = True + __rkwargs__ = AbstractFunction.__rkwargs__ + ('is_const', 'liveness') + + def __init_finalize__(self, *args, **kwargs): + self._liveness = kwargs.setdefault('liveness', 'lazy') + super().__init_finalize__(*args, **kwargs) + self._is_const = kwargs.get('is_const', False) + @classmethod def __indices_setup__(cls, *args, **kwargs): dimensions = kwargs['dimensions'] @@ -143,10 +150,6 @@ def __padding_setup__(self, **kwargs): raise TypeError("`padding` must be int or %d-tuple of ints" % self.ndim) return DimensionTuple(*padding, getters=self.dimensions) - @property - def liveness(self): - return self._liveness - @property def scope(self): return self._scope @@ -155,14 +158,6 @@ def scope(self): def _C_ctype(self): return POINTER(dtype_to_ctype(self.dtype)) - @property - def _mem_internal_eager(self): - return self._liveness == 'eager' - - @property - def _mem_internal_lazy(self): - return self._liveness == 'lazy' - @property def _mem_stack(self): return self._scope in ('stack', 'shared') diff --git a/devito/types/basic.py b/devito/types/basic.py index d1dd3dcb93..c500307fa6 100644 --- a/devito/types/basic.py +++ b/devito/types/basic.py @@ -259,6 +259,7 @@ class Basic(CodeSymbol): is_Bundle = False is_Object = False is_LocalObject = False + is_LocalType = False # Created by the user is_Input = False @@ -1662,3 +1663,26 @@ class IrregularFunctionInterface: @property def nbytes_max(self): raise NotImplementedError + + +class LocalType(Basic): + + is_LocalType = True + + @property + def liveness(self): + return self._liveness + + @property + def _mem_internal_eager(self): + return self._liveness == 'eager' + + @property + def _mem_internal_lazy(self): + return self._liveness == 'lazy' + + @property + def is_const(self): + return self._is_const + + _C_modifier = None diff --git a/devito/types/object.py b/devito/types/object.py index 3768dc76fd..45f4852a26 100644 --- a/devito/types/object.py +++ b/devito/types/object.py @@ -5,7 +5,7 @@ from devito.tools import Pickable, as_tuple, sympy_mutex from devito.types.args import ArgProvider from devito.types.caching import Uncached -from devito.types.basic import Basic +from devito.types.basic import Basic, LocalType from devito.types.utils import CtypesFactory __all__ = ['Object', 'LocalObject', 'CompositeObject'] @@ -155,7 +155,7 @@ def fields(self): return [i for i, _ in self.pfields] -class LocalObject(AbstractObject): +class LocalObject(AbstractObject, LocalType): """ Object with derived type defined inside an Operator. @@ -193,10 +193,6 @@ def _hashable_content(self): self.cargs + (self.initvalue, self.liveness, self.is_global)) - @property - def liveness(self): - return self._liveness - @property def is_global(self): return self._is_global @@ -235,21 +231,12 @@ def _C_free(self): """ return None - _C_modifier = None """ A modifier added to the LocalObject's C declaration when the object appears in a function signature. For example, a subclass might define `_C_modifier = '&'` to impose pass-by-reference semantics. """ - @property - def _mem_internal_eager(self): - return self._liveness == 'eager' - - @property - def _mem_internal_lazy(self): - return self._liveness == 'lazy' - @property def _mem_global(self): return self._is_global From d99c2d568d41b7a0ae36599bacea2a684b87c6b8 Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Wed, 31 Jul 2024 11:49:00 +0100 Subject: [PATCH 2/5] misc: Add docstring for LocalType --- devito/types/array.py | 4 ++++ devito/types/basic.py | 12 ++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/devito/types/array.py b/devito/types/array.py index 00faeac072..2ca0324023 100644 --- a/devito/types/array.py +++ b/devito/types/array.py @@ -53,6 +53,10 @@ def shape(self): def shape_allocated(self): return self.symbolic_shape + @property + def is_const(self): + return self._is_const + class Array(ArrayBasic): diff --git a/devito/types/basic.py b/devito/types/basic.py index c500307fa6..6230d55eab 100644 --- a/devito/types/basic.py +++ b/devito/types/basic.py @@ -1666,6 +1666,14 @@ def nbytes_max(self): class LocalType(Basic): + """ + This is the abstract base class for local types, which are + generated by the compiler in C rather than in Python. + + Notes + ----- + Subclasses should override __init_finalize__ to setup `liveness`. + """ is_LocalType = True @@ -1681,8 +1689,4 @@ def _mem_internal_eager(self): def _mem_internal_lazy(self): return self._liveness == 'lazy' - @property - def is_const(self): - return self._is_const - _C_modifier = None From a51d9564101e7667c50111de2a488f749c57f54e Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Wed, 31 Jul 2024 12:14:51 +0100 Subject: [PATCH 3/5] misc: Edit docstring --- devito/types/basic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/devito/types/basic.py b/devito/types/basic.py index 6230d55eab..0e237d8b3c 100644 --- a/devito/types/basic.py +++ b/devito/types/basic.py @@ -1672,7 +1672,7 @@ class LocalType(Basic): Notes ----- - Subclasses should override __init_finalize__ to setup `liveness`. + Subclasses should setup `liveness`. """ is_LocalType = True From afa5311861ce72c637d78cc1725d244c1d265eb2 Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Wed, 31 Jul 2024 12:16:54 +0100 Subject: [PATCH 4/5] misc: Flake8 --- devito/types/basic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/devito/types/basic.py b/devito/types/basic.py index 0e237d8b3c..fe7484f37a 100644 --- a/devito/types/basic.py +++ b/devito/types/basic.py @@ -1663,7 +1663,7 @@ class IrregularFunctionInterface: @property def nbytes_max(self): raise NotImplementedError - + class LocalType(Basic): """ @@ -1688,5 +1688,5 @@ def _mem_internal_eager(self): @property def _mem_internal_lazy(self): return self._liveness == 'lazy' - + _C_modifier = None From fec34913cacd418339e63ac5bef4a7cfb0c1a8c6 Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Wed, 31 Jul 2024 14:35:57 +0100 Subject: [PATCH 5/5] misc: Address comments --- devito/types/array.py | 12 ++++++------ devito/types/basic.py | 7 ++++++- devito/types/object.py | 6 ------ 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/devito/types/array.py b/devito/types/array.py index 2ca0324023..34237f01bf 100644 --- a/devito/types/array.py +++ b/devito/types/array.py @@ -20,8 +20,11 @@ class ArrayBasic(AbstractFunction, LocalType): __rkwargs__ = AbstractFunction.__rkwargs__ + ('is_const', 'liveness') def __init_finalize__(self, *args, **kwargs): - self._liveness = kwargs.setdefault('liveness', 'lazy') super().__init_finalize__(*args, **kwargs) + + self._liveness = kwargs.get('liveness', 'lazy') + assert self._liveness in ['eager', 'lazy'] + self._is_const = kwargs.get('is_const', False) @classmethod @@ -112,8 +115,8 @@ class Array(ArrayBasic): is_Array = True - __rkwargs__ = (AbstractFunction.__rkwargs__ + - ('dimensions', 'liveness', 'scope', 'initvalue')) + __rkwargs__ = (ArrayBasic.__rkwargs__ + + ('dimensions', 'scope', 'initvalue')) def __new__(cls, *args, **kwargs): kwargs.update({'options': {'evaluate': False}}) @@ -127,9 +130,6 @@ def __new__(cls, *args, **kwargs): def __init_finalize__(self, *args, **kwargs): super().__init_finalize__(*args, **kwargs) - self._liveness = kwargs.get('liveness', 'lazy') - assert self._liveness in ['eager', 'lazy'] - self._scope = kwargs.get('scope', 'heap') assert self._scope in ['heap', 'stack', 'static', 'constant', 'shared'] diff --git a/devito/types/basic.py b/devito/types/basic.py index fe7484f37a..10e07087b2 100644 --- a/devito/types/basic.py +++ b/devito/types/basic.py @@ -1672,7 +1672,7 @@ class LocalType(Basic): Notes ----- - Subclasses should setup `liveness`. + Subclasses should setup `_liveness`. """ is_LocalType = True @@ -1689,4 +1689,9 @@ def _mem_internal_eager(self): def _mem_internal_lazy(self): return self._liveness == 'lazy' + """ + A modifier added to the subclass C declaration when it appears + in a function signature. For example, a subclass might define `_C_modifier = '&'` + to impose pass-by-reference semantics. + """ _C_modifier = None diff --git a/devito/types/object.py b/devito/types/object.py index 45f4852a26..cba54b0add 100644 --- a/devito/types/object.py +++ b/devito/types/object.py @@ -231,12 +231,6 @@ def _C_free(self): """ return None - """ - A modifier added to the LocalObject's C declaration when the object appears - in a function signature. For example, a subclass might define `_C_modifier = '&'` - to impose pass-by-reference semantics. - """ - @property def _mem_global(self): return self._is_global