提交 25b4ef98 authored 作者: Olivier Delalleau's avatar Olivier Delalleau

Avoid double refresh of cache when running 'theano-cache clear'

上级 c7ca7e51
......@@ -6,9 +6,10 @@ from theano.gof.cc import get_module_cache
if len(sys.argv) == 1:
print config.compiledir
elif sys.argv[1] in ('clear'):
# TODO Note that there is a double refresh when running the line below, it
# should be optimized to refresh the cache only once.
get_module_cache().clear(unversioned_min_age=-1, clear_base_files=True,
# We skip the refresh on module cache creation because the refresh will
# be done when calling clear afterwards.
cache = get_module_cache(init_args=dict(do_refresh=False))
cache.clear(unversioned_min_age=-1, clear_base_files=True,
delete_if_problem=True)
else:
print 'command "%s" not recognized' % sys.argv[1]
......
......@@ -63,8 +63,12 @@ def error(*args):
from theano.gof.callcache import CallCache
def get_module_cache():
return cmodule.get_module_cache(config.compiledir)
def get_module_cache(init_args=None):
"""
:param init_args: If not None, the (k, v) pairs in this dictionary will
be forwarded to the ModuleCache constructor as keyword arguments.
"""
return cmodule.get_module_cache(config.compiledir, init_args=init_args)
_persistent_module_cache = None
def get_persistent_module_cache():
......
......@@ -427,11 +427,15 @@ class ModuleCache(object):
"""set of all key.pkl files that have been loaded.
"""
def __init__(self, dirname, force_fresh=None, check_for_broken_eq=True):
def __init__(self, dirname, force_fresh=None, check_for_broken_eq=True,
do_refresh=True):
"""
:param check_for_broken_eq: A bad __eq__ implemenation can break this cache mechanism.
This option turns on a not-too-expensive sanity check during the load of an old cache
file.
:param do_refresh: If True, then the ``refresh`` method will be called
in the constructor.
"""
self.dirname = dirname
self.module_from_name = dict(self.module_from_name)
......@@ -443,6 +447,7 @@ class ModuleCache(object):
self.force_fresh = force_fresh
self.loaded_key_pkl = set()
if do_refresh:
self.refresh()
start = time.time()
......@@ -566,6 +571,7 @@ class ModuleCache(object):
# This exception is often triggered by keys that contain
# references to classes that have not yet been imported. They are
# not necessarily broken.
# TODO But is there a reason to keep them?
pass
continue
......@@ -1111,11 +1117,21 @@ def _rmtree(parent, ignore_nocleanup=False, msg='', level='debug',
warning('Failed to remove or mark cache directory %s for removal' % parent, ee)
_module_cache = None
def get_module_cache(dirname, force_fresh=None):
def get_module_cache(dirname, force_fresh=None, init_args=None):
"""
:param init_args: If not None, the (k, v) pairs in this dictionary will
be forwarded to the ModuleCache constructor as keyword arguments.
"""
global _module_cache
if init_args is None:
init_args = {}
if _module_cache is None:
_module_cache = ModuleCache(dirname, force_fresh=force_fresh)
_module_cache = ModuleCache(dirname, force_fresh=force_fresh,
**init_args)
atexit.register(_module_cache._on_atexit)
elif init_args:
warning('Ignoring init arguments for module cache because it was '
'created prior to this call')
if _module_cache.dirname != dirname:
warning("Returning module cache instance with different dirname than you requested")
return _module_cache
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论