Skip to content
Snippets Groups Projects
Commit 28175d90 authored by Izzard, Robert Dr (Maths & Physics)'s avatar Izzard, Robert Dr (Maths & Physics)
Browse files

add function to determine the cache directory (if available) and improved the disk cache handling

parent c8fb242d
No related branches found
No related tags found
No related merge requests found
......@@ -10,6 +10,7 @@ Please see the LRU_* options in there.
import cachetools
import contextlib
import functools
import getpass
import importlib
import os
import sys
......@@ -17,6 +18,27 @@ import time
import tempfile
class cache():
def __init__(self, **kwargs):
# don't do anything: we just inherit from this class
return
def default_cache_dir(self):
"""
Return a default cache directory path for binary_c-python, or None if we cannot find one. This is used in grid_options_defaults.py
"""
error_string = "__*ERR*__" # string that cannot be a path
for path in [
os.path.join(os.environ.get('HOME',error_string), '.cache'),
os.path.join(os.environ.get('TMP',error_string), 'cache'),
os.path.join('var','tmp',getpass.getuser(),'cache')
]:
if not error_string in path and os.path.isdir(path):
return os.path.join(path,'binary_c')
return None
class NullCache(cachetools.Cache):
"""
A cachetools cache object that does as little as possible and never matches.
......@@ -32,10 +54,6 @@ class cache():
def __delitem__(self, key):
return
def __init__(self, **kwargs):
# don't do anything: we just inherit from this class
return
def setup_function_cache(self,vb=False,type=None):
"""
Function to wrap binary_c-python's functions in function cache.
......
......@@ -42,7 +42,7 @@ class grid_options_defaults():
"parse_function": None, # Function to parse the output with.
"multiplicity_fraction_function": 0, # Which multiplicity fraction function to use. 0: None, 1: Arenou 2010, 2: Rhagavan 2010, 3: Moe and di Stefano 2017
"tmp_dir": temp_dir(), # Setting the temp dir of the program
"cache_dir" : None, # Cache location
"cache_dir" : self.default_cache_dir(), # Cache location, usually $HOME/.cache
"status_dir" : None, #
"_main_pid": -1, # Placeholder for the main process id of the run.
"save_ensemble_chunks": True, # Force the ensemble chunk to be saved even if we are joining a thread (just in case the joining fails)
......@@ -571,3 +571,16 @@ class grid_options_defaults():
file=filehandle,
)
print("", file=filehandle)
def default_cache_dir(self):
"""
Return a default cache directory path, or None if we cannot find one.
"""
error_string = "__*ERR*__" # string that cannot be a path
for path in [
os.path.join(os.environ.get('HOME',error_string), '.cache', 'binary_c'),
os.path.join(os.environ.get('TMP',error_string), 'cache'),
]:
if not error_string in path and os.path.isdir(path):
return path
return None
......@@ -13,7 +13,10 @@ import json
import math
import numpy as np
import py_rinterpolate
import traceback
import sys
import traceback
from typing import Union
class spacing_functions():
......@@ -205,8 +208,9 @@ class spacing_functions():
showlist: if True, show the mass list once generated
logmasses: if True, the masses are logged with math.log()
log10masses: if True, the masses are logged with math.log10()
usecache: if True (the default) uses cached results if they are saved (in cachedir)
cachedir: if None, defaults to grid_options['tmp_dir']/const_dt_cache
usecache: if True (the default) uses cached results if they are saved (in cachedir) and cachedir is not None
cachedir: where the cache is stored. if None, defaults to grid_options['cache_dir']+'/const_dt_cache'
vb : verbose logging flag (default False)
Returns:
Array of masses.
......@@ -222,12 +226,21 @@ class spacing_functions():
samplerfunc="self.const_dt(self,dlogt=0.1,nres=100,mmin=0.07,mmax=80.0,maxdm=((0.07,1.0,0.1),(1.0,10.0,1.0),(10.0,80.0,2.0)),showtable=True,logspacing=True,fsample=1.0/4.0)"
"""
print("Cache dir {}".format(self.grid_options['cache_dir']))
if cachedir is not None:
cachedir = self.grid_options['cache_dir'] + '/const_dt_cache'
cache = diskcache.Cache(cachedir)
else:
cache = None
if usecache:
if cachedir is None:
cachedir = self.grid_options['cache_dir']
if cachedir is not None:
cachedir += '/const_dt_cache'
cache = diskcache.Cache(cachedir)
print("Use const_dt cache in {} [cache object {}]".format(
cachedir,
cache))
else:
print("const_dt uses no cache")
cache = None
def _const_dt_wrapper(cachedir=None,
num_cores=None,
bse_options=None,
......@@ -249,10 +262,30 @@ class spacing_functions():
showlist=False,
showtable=False,
usecache=True,
vb=False
):
print("call _const_dt num_cores={} dt={} dlogt={} mmin={} mmax={} nres={} logspacing={} tmin={} mindm={} maxdm={} fsample={} factor={} logmasses={} log10masses={}".format(
num_cores,dt,dlogt,mmin,mmax,nres,logspacing,tmin,mindm,maxdm,fsample,factor,logmasses,log10masses))
print("call _const_dt num_cores={} dt={} dlogt={} mmin={} mmax={} nres={} logspacing={} tmin={} mindm={} maxdm={} fsample={} factor={} logmasses={} log10masses={} showlist={} usecache={} [cache={} vb={}]".format(
num_cores,
dt,
dlogt,
mmin,
mmax,
nres,
logspacing,
tmin,
mindm,
maxdm,
fsample,
factor,
logmasses,
log10masses,
showlist,
usecache,
cache,
vb))
if vb:
traceback.print_stack()
# strip bse_options of options that will not affect
# _const_dt
......@@ -268,6 +301,8 @@ class spacing_functions():
bse_options_json = json.dumps(bse_stripped,
sort_keys=True,
ensure_ascii=False)
if vb:
print("BSE options JSON:",bse_options_json)
return _const_dt(cachedir=cachedir,
num_cores=num_cores,
......@@ -304,7 +339,7 @@ class spacing_functions():
else:
__decorator = __dummy_decorator
@__decorator
@__decorator()
def _const_dt(cachedir=None,
num_cores=None,
bse_options_json=None, # JSON string
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment