diff --git a/python/dune/perftool/generation/__init__.py b/python/dune/perftool/generation/__init__.py index cdbcb77d92a4460ba81b83a26a036cc641c41a62..906183acd8de80ea5e48d78b3bd4631825b17c72 100644 --- a/python/dune/perftool/generation/__init__.py +++ b/python/dune/perftool/generation/__init__.py @@ -1,7 +1,6 @@ from __future__ import absolute_import from dune.perftool.generation.cache import (cached, - cache_context, generator_factory, retrieve_cache_items, delete_cache_items, @@ -24,3 +23,6 @@ from dune.perftool.generation.loopy import (domain, temporary_variable, valuearg, ) + +from dune.perftool.generation.context import (cache_context, + ) \ No newline at end of file diff --git a/python/dune/perftool/generation/cache.py b/python/dune/perftool/generation/cache.py index ac5dc15540d59d3af8fb9bdefe951ff41fb19e1a..3975dda94c5bdbc638b57391a3693897a5ed4d18 100644 --- a/python/dune/perftool/generation/cache.py +++ b/python/dune/perftool/generation/cache.py @@ -11,31 +11,6 @@ from pytools import memoize # have one cache the module level. It is easier than handing around an instance of it. _cache = {} -# Implement a context manager that allows to temporarily define tags globally. -_cache_context_stack = [] - - -class _CacheContext(object): - def __init__(self, tags): - self.tags = tags - - def __enter__(self): - _cache_context_stack.append(self.tags) - - def __exit__(self, exc_type, exc_value, traceback): - _cache_context_stack.pop() - - -def cache_context(*tags): - return _CacheContext(tags) - - -def _get_context_tags(): - result = tuple() - for items in _cache_context_stack: - result = result + items - return result - def _freeze(data): """ A function that deterministically generates an @@ -133,10 +108,11 @@ class _RegisteredFunction(object): self.cache_key_generator = cache_key_generator # Append the current context item tags to the given ones + from dune.perftool.generation.context import get_context_tags if 'item_tags' in kwargs: - kwargs['item_tags'] = tuple(kwargs['item_tags']) + _get_context_tags() + kwargs['item_tags'] = tuple(kwargs['item_tags']) + get_context_tags() else: - kwargs['item_tags'] = _get_context_tags() + kwargs['item_tags'] = get_context_tags() self.itemtype = _construct_cache_item_type("CacheItemType", **kwargs) diff --git a/python/dune/perftool/generation/context.py b/python/dune/perftool/generation/context.py new file mode 100644 index 0000000000000000000000000000000000000000..d96e7e9cd65123e93f1f70e7e91801a37df001cb --- /dev/null +++ b/python/dune/perftool/generation/context.py @@ -0,0 +1,26 @@ +""" Context managers for code generation. """ + +# Implement a context manager that allows to temporarily define tags globally. +_cache_context_stack = [] + + +class _CacheContext(object): + def __init__(self, tags): + self.tags = tags + + def __enter__(self): + _cache_context_stack.append(self.tags) + + def __exit__(self, exc_type, exc_value, traceback): + _cache_context_stack.pop() + + +def cache_context(*tags): + return _CacheContext(tags) + + +def get_context_tags(): + result = tuple() + for items in _cache_context_stack: + result = result + items + return result