def __setstate__(self, state): attribs, p_hash_digest = state new_fields = set() for k, v in six.iteritems(attribs): setattr(self, k, v) new_fields.add(k) self.register_fields(new_fields) if 0: # {{{ check that 'reconstituted' object has same hash from loopy.tools import LoopyKeyBuilder LoopyKeyBuilder()(self) assert p_hash_digest == self._pytools_persistent_hash_digest # }}} else: self._pytools_persistent_hash_digest = p_hash_digest from loopy.kernel.tools import SetOperationCacheManager self.cache_manager = SetOperationCacheManager() self._kernel_executor_cache = {}
def __getstate__(self): result = dict( (key, getattr(self, key)) for key in self.__class__.fields if hasattr(self, key)) result.pop("cache_manager", None) # Make the instructions lazily unpickling, to support faster # cache retrieval for execution. from loopy.kernel.instruction import _get_insn_eq_key, _get_insn_hash_key from loopy.tools import ( LazilyUnpicklingListWithEqAndPersistentHashing as LazyList) result["instructions"] = LazyList( self.instructions, eq_key_getter=_get_insn_eq_key, persistent_hash_key_getter=_get_insn_hash_key) # Cache written variables to avoid having to unpickle instructions in # order to compute the written variables. This is needed on the # cache-to-execution path. result["_cached_written_variables"] = self.get_written_variables() # make sure that kernels are pickled with a cached hash key in place from loopy.tools import LoopyKeyBuilder LoopyKeyBuilder()(self) return (result, self._pytools_persistent_hash_digest)
def test_regression_persistent_hash(): knl1 = lp.make_kernel("{[i] : 0<=i<n}", "cse_exprvar = d[2]*d[2]") knl2 = lp.make_kernel("{[i] : 0<=i<n}", "cse_exprvar = d[0]*d[0]") from loopy.tools import LoopyKeyBuilder lkb = LoopyKeyBuilder() assert lkb(knl1.instructions[0]) != lkb(knl2.instructions[0]) assert lkb(knl1) != lkb(knl2)
def __hash__(self): if self._hash_value is not None: return self._hash_value from loopy.tools import LoopyKeyBuilder from pytools.persistent_dict import new_hash key_hash = new_hash() self.update_persistent_hash(key_hash, LoopyKeyBuilder()) self._hash_value = hash(key_hash.digest()) return self._hash_value
def test_LazilyUnpicklingListWithEqAndPersistentHashing(): from loopy.tools import LazilyUnpicklingListWithEqAndPersistentHashing cls = PickleDetectorForLazilyUnpicklingListWithEqAndPersistentHashing from pickle import loads, dumps # {{{ test comparison of a pair of lazy lists lst0 = LazilyUnpicklingListWithEqAndPersistentHashing( [cls(0), cls(1)], eq_key_getter=repr, persistent_hash_key_getter=repr) lst1 = LazilyUnpicklingListWithEqAndPersistentHashing( [cls(0), cls(1)], eq_key_getter=repr, persistent_hash_key_getter=repr) assert not cls.instance_unpickled assert lst0 == lst1 assert not cls.instance_unpickled lst0 = loads(dumps(lst0)) lst1 = loads(dumps(lst1)) assert lst0 == lst1 assert not cls.instance_unpickled lst0.append(cls(3)) lst1.append(cls(2)) assert lst0 != lst1 # }}} # {{{ comparison with plain lists lst = [cls(0), cls(1), cls(3)] assert lst == lst0 assert lst0 == lst assert not cls.instance_unpickled # }}} # {{{ persistent hashing from loopy.tools import LoopyKeyBuilder kb = LoopyKeyBuilder() assert kb(lst0) == kb(lst) assert not cls.instance_unpickled
def __getstate__(self): result = dict( (key, getattr(self, key)) for key in self.__class__.fields if hasattr(self, key)) result.pop("cache_manager", None) # make sure that kernels are pickled with a cached hash key in place from loopy.tools import LoopyKeyBuilder LoopyKeyBuilder()(self) return (result, self._pytools_persistent_hash_digest)
def test_Optional(): # noqa from loopy import Optional # {{{ test API opt = Optional() assert not opt.has_value with pytest.raises(AttributeError): opt.value opt = Optional(1) assert opt.has_value assert 1 == opt.value assert Optional(1) == Optional(1) assert Optional(1) != Optional(2) assert Optional() == Optional() assert Optional() != Optional(1) # }}} # {{{ test pickling import pickle assert not pickle.loads(pickle.dumps(Optional())).has_value assert pickle.loads(pickle.dumps(Optional(1))).value == 1 # }}} # {{{ test key builder from loopy.tools import LoopyKeyBuilder kb = LoopyKeyBuilder() kb(Optional()) kb(Optional(None))
from loopy.codegen.result import merge_codegen_results return merge_codegen_results(self, result) @property def ast_builder(self): if self.is_generating_device_code: return self.kernel.target.get_device_ast_builder() else: return self.kernel.target.get_host_ast_builder() # }}} code_gen_cache = PersistentDict("loopy-code-gen-cache-v3-"+DATA_MODEL_VERSION, key_builder=LoopyKeyBuilder()) class PreambleInfo(ImmutableRecord): """ .. attribute:: kernel .. attribute:: seen_dtypes .. attribute:: seen_functions .. attribute:: seen_atomic_dtypes """ # {{{ main code generation entrypoint def generate_code_v2(kernel): """
def __hash__(self): from loopy.tools import LoopyKeyBuilder from pytools.persistent_dict import new_hash key_hash = new_hash() self.update_persistent_hash(key_hash, LoopyKeyBuilder()) return hash(key_hash.digest())