Skip to content

Commit

Permalink
Do not use constant names in generated code (#298)
Browse files Browse the repository at this point in the history
* Do not use constant names in generated code
  • Loading branch information
connorjward authored Sep 26, 2023
1 parent aa7e30b commit 47c1324
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
7 changes: 1 addition & 6 deletions tsfc/fem.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,12 +629,7 @@ def callback(entity_id):

@translate.register(TSFCConstantMixin)
def translate_constant_value(terminal, mt, ctx):
value_size = numpy.prod(terminal.ufl_shape, dtype=int)
expression = gem.reshape(
gem.Variable(terminal.name, (value_size,)),
terminal.ufl_shape
)
return expression
return ctx.constant(terminal)


@translate.register(Coefficient)
Expand Down
4 changes: 4 additions & 0 deletions tsfc/kernel_interface/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def coefficient(self, ufl_coefficient, restriction):
"""A function that maps :class:`ufl.Coefficient`s to GEM
expressions."""

@abstractmethod
def constant(self, const):
"""Return the GEM expression corresponding to the constant."""

@abstractmethod
def cell_orientation(self, restriction):
"""Cell orientation as a GEM expression."""
Expand Down
8 changes: 6 additions & 2 deletions tsfc/kernel_interface/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ def coefficient(self, ufl_coefficient, restriction):
else:
return kernel_arg[{'+': 0, '-': 1}[restriction]]

def constant(self, const):
return self.constant_map[const]

def cell_orientation(self, restriction):
"""Cell orientation as a GEM expression."""
f = {None: 0, '+': 0, '-': 1}[restriction]
Expand Down Expand Up @@ -435,16 +438,17 @@ def check_requirements(ir):
return cell_orientations, cell_sizes, tuple(sorted(rt_tabs.items()))


def prepare_constant(constant):
def prepare_constant(constant, number):
"""Bridges the kernel interface and the GEM abstraction for
Constants.
:arg constant: Firedrake Constant
:arg number: Value to uniquely identify the constant
:returns: (funarg, expression)
expression - GEM expression referring to the Constant value(s)
"""
value_size = numpy.prod(constant.ufl_shape, dtype=int)
return gem.reshape(gem.Variable(constant.name, (value_size,)),
return gem.reshape(gem.Variable(f"c_{number}", (value_size,)),
constant.ufl_shape)


Expand Down
8 changes: 4 additions & 4 deletions tsfc/kernel_interface/firedrake_loopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ def set_coefficients(self, coefficients):
self._coefficient(coefficient, f"w_{i}")

def set_constants(self, constants):
for const in constants:
gemexpr = prepare_constant(const)
for i, const in enumerate(constants):
gemexpr = prepare_constant(const, i)
self.constant_map[const] = gemexpr

def set_coefficient_numbers(self, coefficient_numbers):
Expand Down Expand Up @@ -334,8 +334,8 @@ def set_coefficients(self, integral_data, form_data):
n += 1

def set_constants(self, constants):
for const in constants:
gemexpr = prepare_constant(const)
for i, const in enumerate(constants):
gemexpr = prepare_constant(const, i)
self.constant_map[const] = gemexpr

def register_requirements(self, ir):
Expand Down

0 comments on commit 47c1324

Please sign in to comment.