Commit 7563d84e authored by Martin Bauer's avatar Martin Bauer
Browse files

Merge branch 'reproducible-hash-for-textures' into 'master'

Use reproducible hashlib for representing TextureCachedField

See merge request pycodegen/pystencils!80
parents e9a17305 ba96ba03
...@@ -8,21 +8,25 @@ ...@@ -8,21 +8,25 @@
""" """
import hashlib
import itertools import itertools
from enum import Enum from enum import Enum
from typing import Set from typing import Set
import pystencils
import sympy as sp import sympy as sp
from sympy.core.cache import cacheit
import pystencils
from pystencils.astnodes import Node from pystencils.astnodes import Node
from pystencils.data_types import TypedSymbol, cast_func, create_type from pystencils.data_types import TypedSymbol, cast_func, create_type
from sympy.core.cache import cacheit
try: try:
import pycuda.driver import pycuda.driver
except Exception: except Exception:
pass pass
_hash = hashlib.md5
class InterpolationMode(str, Enum): class InterpolationMode(str, Enum):
NEAREST_NEIGHBOR = "nearest_neighbour" NEAREST_NEIGHBOR = "nearest_neighbour"
...@@ -85,6 +89,14 @@ class Interpolator(object): ...@@ -85,6 +89,14 @@ class Interpolator(object):
self.allow_textures = allow_textures self.allow_textures = allow_textures
self.interpolation_mode = interpolation_mode self.interpolation_mode = interpolation_mode
@property
def _hashable_contents(self):
return (str(self.address_mode),
str(type(self)),
self.symbol,
self.address_mode,
self.use_normalized_coordinates)
def at(self, offset): def at(self, offset):
return InterpolatorAccess(self.symbol, *[sp.S(o) for o in offset]) return InterpolatorAccess(self.symbol, *[sp.S(o) for o in offset])
...@@ -92,17 +104,17 @@ class Interpolator(object): ...@@ -92,17 +104,17 @@ class Interpolator(object):
return InterpolatorAccess(self.symbol, *[sp.S(o) for o in offset]) return InterpolatorAccess(self.symbol, *[sp.S(o) for o in offset])
def __str__(self): def __str__(self):
return '%s_interpolator' % self.field.name return '%s_interpolator_%s' % (self.field.name, self.reproducible_hash)
def __repr__(self): def __repr__(self):
return self.__str__() return self.__str__()
def __hash__(self): def __hash__(self):
return hash((str(self.address_mode), return hash(self._hashable_contents)
str(type(self)),
self.symbol, @property
self.address_mode, def reproducible_hash(self):
self.use_normalized_coordinates)) return _hash(str(self._hashable_contents).encode()).hexdigest()
class LinearInterpolator(Interpolator): class LinearInterpolator(Interpolator):
...@@ -327,18 +339,26 @@ class TextureCachedField: ...@@ -327,18 +339,26 @@ class TextureCachedField:
return TextureAccess(self.symbol, *offset) return TextureAccess(self.symbol, *offset)
def __str__(self): def __str__(self):
return '%s_texture_%x' % (self.field.name, abs(hash(self))) return '%s_texture_%s' % (self.field.name, self.reproducible_hash)
def __repr__(self): def __repr__(self):
return self.__str__() return self.__str__()
@property
def _hashable_contents(self):
return (type(self),
self.address_mode,
self.filter_mode,
self.read_as_integer,
self.interpolation_mode,
self.use_normalized_coordinates)
def __hash__(self): def __hash__(self):
return hash((str(type(self)), return hash(self._hashable_contents)
self.address_mode,
self.filter_mode, @property
self.read_as_integer, def reproducible_hash(self):
self.interpolation_mode, _hash(str(self._hashable_contents).encode()).hexdigest()
self.use_normalized_coordinates))
class TextureAccess(InterpolatorAccess): class TextureAccess(InterpolatorAccess):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment