Skip to content
Snippets Groups Projects
Commit 54837529 authored by Richard Angersbach's avatar Richard Angersbach
Browse files

Introduce reduction symbol property and add to lhs of reduced symbol

parent b263d752
No related branches found
No related tags found
1 merge request!438Reduction Support
...@@ -75,6 +75,8 @@ class KernelCreationContext: ...@@ -75,6 +75,8 @@ class KernelCreationContext:
self._symbol_ctr_pattern = re.compile(r"__[0-9]+$") self._symbol_ctr_pattern = re.compile(r"__[0-9]+$")
self._symbol_dup_table: defaultdict[str, int] = defaultdict(lambda: 0) self._symbol_dup_table: defaultdict[str, int] = defaultdict(lambda: 0)
# TODO: add list of reduction symbols
self._fields_and_arrays: dict[str, FieldArrayPair] = dict() self._fields_and_arrays: dict[str, FieldArrayPair] = dict()
self._fields_collection = FieldsInKernel() self._fields_collection = FieldsInKernel()
......
...@@ -65,6 +65,9 @@ from ..exceptions import PsInputError ...@@ -65,6 +65,9 @@ from ..exceptions import PsInputError
from ..functions import PsMathFunction, MathFunctions from ..functions import PsMathFunction, MathFunctions
from ..exceptions import FreezeError from ..exceptions import FreezeError
import backend.functions
from codegen.properties import ReductionSymbolProperty
ExprLike = ( ExprLike = (
sp.Expr sp.Expr
...@@ -188,27 +191,32 @@ class FreezeExpressions: ...@@ -188,27 +191,32 @@ class FreezeExpressions:
lhs = self.visit(expr.lhs) lhs = self.visit(expr.lhs)
rhs = self.visit(expr.rhs) rhs = self.visit(expr.rhs)
assert isinstance(lhs, PsExpression)
assert isinstance(rhs, PsExpression) assert isinstance(rhs, PsExpression)
assert isinstance(lhs, PsSymbolExpr)
match expr.op: match expr.op:
case "+=": case "+":
op = add op = add
case "-=": init_val = PsConstant(0)
case "-":
op = sub op = sub
case "*=": init_val = PsConstant(0)
case "*":
op = mul op = mul
case "/=": init_val = PsConstant(1)
op = truediv # TODO: unsure if sp.Min & sp.Max are mapped by map_Min/map_Max afterwards
# TODO: unsure if sp.Min & sp.Max work here case "min":
case "min=":
op = sp.Min op = sp.Min
case "max=": init_val = backend.functions.NumericLimitsFunctions("min")
case "max":
op = sp.Max op = sp.Max
init_val = backend.functions.NumericLimitsFunctions("max")
case _: case _:
raise FreezeError(f"Unsupported reduced assignment: {expr.op}.") raise FreezeError(f"Unsupported reduced assignment: {expr.op}.")
return PsAssignment(lhs, op(lhs.clone(), rhs)) # TODO: PsReducedAssignment? lhs.symbol.add_property(ReductionSymbolProperty(expr.op, init_val))
return PsAssignment(lhs, op(lhs.clone(), rhs))
def map_Symbol(self, spsym: sp.Symbol) -> PsSymbolExpr: def map_Symbol(self, spsym: sp.Symbol) -> PsSymbolExpr:
symb = self._ctx.get_symbol(spsym.name) symb = self._ctx.get_symbol(spsym.name)
......
...@@ -3,6 +3,8 @@ from dataclasses import dataclass ...@@ -3,6 +3,8 @@ from dataclasses import dataclass
from ..field import Field from ..field import Field
from backend.ast.expressions import PsExpression
@dataclass(frozen=True) @dataclass(frozen=True)
class PsSymbolProperty: class PsSymbolProperty:
...@@ -14,6 +16,14 @@ class UniqueSymbolProperty(PsSymbolProperty): ...@@ -14,6 +16,14 @@ class UniqueSymbolProperty(PsSymbolProperty):
"""Base class for unique properties, of which only one instance may be registered at a time.""" """Base class for unique properties, of which only one instance may be registered at a time."""
@dataclass(frozen=True)
class ReductionSymbolProperty(UniqueSymbolProperty):
"""Symbol acts as a base pointer to a field."""
op: str
init_val: PsExpression
@dataclass(frozen=True) @dataclass(frozen=True)
class FieldShape(PsSymbolProperty): class FieldShape(PsSymbolProperty):
"""Symbol acts as a shape parameter to a field.""" """Symbol acts as a shape parameter to a field."""
......
...@@ -14,11 +14,9 @@ class ReducedAssignment(AssignmentBase): ...@@ -14,11 +14,9 @@ class ReducedAssignment(AssignmentBase):
""" """
binop = None # type: str binop = None # type: str
# TODO: initial value
@property @property
def op(self): def op(self):
return self.binop + '=' return self.binop
class AddReducedAssignment(ReducedAssignment): class AddReducedAssignment(ReducedAssignment):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment