Skip to content

Commit

Permalink
Change Constant defining mechanism (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
a5kin committed Apr 14, 2019
1 parent 4ec9b84 commit d15a890
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 34 deletions.
2 changes: 0 additions & 2 deletions xentica/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,6 @@ def build_source(self):
source += self.build_absorb()
source += self.build_render()
source = self.build_defines() + source
for const in self.constants.values():
source = const.replace_value(source)
self.cuda_source = source

def build_defines(self):
Expand Down
17 changes: 9 additions & 8 deletions xentica/core/color_effects.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,16 @@ class MovingAverage(ColorEffect):

def __call__(self):
"""Implement the effect."""
self.bsca.define_constant(Constant("FADE_IN",
lambda x: x.fade_in))
self.bsca.define_constant(Constant("FADE_OUT",
lambda x: x.fade_out))
if not hasattr(self.bsca, "fade_in"):
self.bsca.fade_in = 255
if not hasattr(self.bsca, "fade_out"):
self.bsca.fade_out = 255
if not hasattr(self.bsca, "smooth_factor"):
self.bsca.smooth_factor = 1
self.bsca.define_constant(Constant("FADE_IN", self.bsca.fade_in))
self.bsca.define_constant(Constant("FADE_OUT", self.bsca.fade_out))
self.bsca.define_constant(Constant("SMOOTH_FACTOR",
lambda x: x.smooth_factor))
self.bsca.fade_in = 255
self.bsca.fade_out = 255
self.bsca.smooth_factor = 1
self.bsca.smooth_factor))
self.effect = """
new_r *= SMOOTH_FACTOR;
new_g *= SMOOTH_FACTOR;
Expand Down
9 changes: 4 additions & 5 deletions xentica/core/topology/lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,11 @@ class Lattice(DimensionsMixin, BscaDetectorMixin, metaclass=abc.ABCMeta):

def _define_constants_once(self):
"""Define field size conctants in C code."""
def get_size(i):
"""Get the field size of along i-th dimension."""
return lambda x: x.size[i]
for i in range(self.bsca.topology.dimensions):
constant = Constant("%s%d" % (self.width_prefix, i),
get_size(i))
size = 1
if hasattr(self.bsca, "size") and i < len(self.bsca.size):
size = self.bsca.size[i]
constant = Constant("%s%d" % (self.width_prefix, i), size)
self.bsca.define_constant(constant)

@abc.abstractmethod
Expand Down
25 changes: 6 additions & 19 deletions xentica/core/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,35 +46,23 @@ class Constant(BscaDetectorMixin):
:param name:
Name to use in ``#define``.
:param value_func:
Function that take ``BSCA`` instance as argument and return
the value to be used as second part of ``#define``.
:param value:
A value for the define, it will be converted to string with
``str()``.
"""

def __init__(self, name, value_func):
def __init__(self, name, value):
"""Initialize the class."""
self._name = name
self._value_func = value_func
self._pattern_name = name
self._value = value
self.base_class = Constant

def get_define_code(self):
"""Get the C code for ``#define``."""
code = "#define %s {%s}\n" % (self._name, self._pattern_name)
code = "#define %s %s\n" % (self._name, str(self._value))
return code

def replace_value(self, source):
"""
Replace the constant's value in generated C code.
:param source:
Generated C code.
"""
val = str(self._value_func(self._holder))
return source.replace("{%s}" % self._pattern_name, val)

@property
def name(self):
"""Get the name of constant."""
Expand All @@ -101,7 +89,6 @@ def __init__(self, val=None, name="var"):
super(Variable, self).__init__()
self.fallback_name = name
self.base_class = Variable
self._declared = False
if val is None:
raise XenticaException("Variable should have initial value.")
self._init_val = DeferredExpression(str(val))
Expand Down

0 comments on commit d15a890

Please sign in to comment.