From 457d7161c6cdca4d1a1dd7d4c5e9b8bb9518d94f Mon Sep 17 00:00:00 2001 From: Jack Betteridge Date: Fri, 10 Jan 2025 14:09:26 +0000 Subject: [PATCH 1/2] compiler: Check sympy_type returns a floating point type --- devito/symbolics/printer.py | 3 +++ tests/test_symbolics.py | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/devito/symbolics/printer.py b/devito/symbolics/printer.py index 2b4dbd6113..82d7956be8 100644 --- a/devito/symbolics/printer.py +++ b/devito/symbolics/printer.py @@ -44,7 +44,10 @@ def compiler(self): return self._settings['compiler'] def single_prec(self, expr=None): + # Extract the dtype of the expression dtype = sympy_dtype(expr) if expr is not None else self.dtype + # Check that the dtype is a floating point type + dtype = dtype if np.issubdtype(dtype, np.floating) else self.dtype return dtype in [np.float32, np.float16] def parenthesize(self, item, level, strict=False): diff --git a/tests/test_symbolics.py b/tests/test_symbolics.py index 7beb0c0b97..b293133189 100644 --- a/tests/test_symbolics.py +++ b/tests/test_symbolics.py @@ -313,7 +313,7 @@ def test_cos_vs_cosf(): # Doesn't make much sense, but it's legal c = dSymbol('c', dtype=np.int32) - assert ccode(cos(c)) == "cos(c)" + assert ccode(cos(c)) == "cosf(c)" def test_intdiv(): From c0a7a731ad8561184a48cbb736433146a6e5d6fb Mon Sep 17 00:00:00 2001 From: Jack Betteridge Date: Mon, 13 Jan 2025 19:43:43 +0000 Subject: [PATCH 2/2] sympy: Add integer assumption to dimension --- devito/symbolics/printer.py | 8 ++++---- devito/types/dimension.py | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/devito/symbolics/printer.py b/devito/symbolics/printer.py index 82d7956be8..c86f7e5d43 100644 --- a/devito/symbolics/printer.py +++ b/devito/symbolics/printer.py @@ -170,12 +170,12 @@ def _print_Abs(self, expr): # AOMPCC errors with abs, always use fabs if isinstance(self.compiler, AOMPCompiler): return "fabs(%s)" % self._print(expr.args[0]) - # Check if argument is an integer - if has_integer_args(*expr.args[0].args): + # Check if argument (always exactly one!) is an integer + if has_integer_args(expr.args[0]): func = "abs" - elif self.single_prec(expr): + elif self.single_prec(expr.args[0]): func = "fabsf" - elif any([isinstance(a, Real) for a in expr.args[0].args]): + elif isinstance(expr.args[0], Real): # The previous condition isn't sufficient to detect case with # Python `float`s in that case, fall back to the "default" func = "fabsf" if self.single_prec() else "fabs" diff --git a/devito/types/dimension.py b/devito/types/dimension.py index 152cf4f627..501695567b 100644 --- a/devito/types/dimension.py +++ b/devito/types/dimension.py @@ -128,10 +128,11 @@ def __new__(cls, *args, **kwargs): This is only necessary for backwards compatibility, as originally there was no BasicDimension (i.e., Dimension was just the top class). """ + # Dimensions can only be integers, so pass this assumption to the constructor if cls is Dimension: - return BasicDimension(*args, **kwargs) + return BasicDimension(*args, integer=True, **kwargs) else: - return BasicDimension.__new__(cls, *args, **kwargs) + return BasicDimension.__new__(cls, *args, integer=True, **kwargs) @classmethod def class_key(cls):