Skip to content

Commit

Permalink
handle unhashable types for cached mappers
Browse files Browse the repository at this point in the history
  • Loading branch information
zachjweiner committed May 14, 2022
1 parent d344e19 commit ada1de3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
22 changes: 19 additions & 3 deletions pystella/field/__init__.py
Expand Up @@ -23,7 +23,7 @@

import pymbolic.primitives as pp
from pymbolic import parse
from pymbolic.mapper import Collector
from pymbolic.mapper import Mapper, Collector
import loopy as lp
from loopy.symbolic import (
IdentityMapper as IdentityMapperBase,
Expand Down Expand Up @@ -300,7 +300,23 @@ def d(self, *args):
mapper_method = "map_dynamic_field"


class IdentityMapperMixin:
class UnhashableTypeHandlingMixin:
def __call__(self, expr, *args, **kwargs):
try:
cache_key = self.get_cache_key(expr, *args, **kwargs)
return self._cache[cache_key]
except TypeError:
# not hashable, oh well
return Mapper.rec(self, expr, *args, **kwargs)
except KeyError:
result = super().rec(expr, *args, **kwargs)
self._cache[cache_key] = result
return result

rec = __call__


class IdentityMapperMixin(UnhashableTypeHandlingMixin):
def map_field(self, expr, *args, **kwargs):
return expr.copy(
child=self.rec(expr.child, *args, **kwargs),
Expand Down Expand Up @@ -352,7 +368,7 @@ class IdentityMapper(IdentityMapperMixin, IdentityMapperBase):
pass


class CombineMapperMixin:
class CombineMapperMixin(UnhashableTypeHandlingMixin):
def map_field(self, expr, *args, **kwargs):
return set()

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -103,7 +103,7 @@ def run(self):
install_requires=[
"numpy",
"pyopencl>=2020.2",
"loopy>=2020.2",
"loopy>=2022.1",
],
author="Zachary J Weiner",
url="https://github.com/zachjweiner/pystella",
Expand Down

0 comments on commit ada1de3

Please sign in to comment.