Commit 46a35ef1 authored by Ingo Meyer's avatar Ingo Meyer

Allow multiple pyMolDyn instances

- The discretization cache is one file for all discretizations that must
  be shared between instances. Therefore, now the discretization cache
  locks the corresponding hdf5 file only as long as necessary. If it
  cannot get the lock, the cache file is simply ignored.
- The discretization cache is not considered as a potential calculation
  cache file any more.
parent e128bfc3
......@@ -301,7 +301,8 @@ class Calculation(object):
else:
cachepath = os.path.join(self.cachedir, 'discretization_cache.hdf5')
discretization_cache = DiscretizationCache(cachepath)
discretization = discretization_cache.get_discretization(volume, resolution)
with DiscretizationCache(cachepath) as discretization_cache:
discretization = discretization_cache.get_discretization(volume, resolution)
atom_discretization = AtomDiscretization(atoms, discretization)
message.progress(10)
if (domains and results.domains is None) \
......@@ -490,9 +491,9 @@ class CalculationCache(object):
def buildindex(self):
self.index = dict()
for filename in os.listdir(self.directory):
if not filename.split(".")[-1] == "hdf5":
continue
filenames = set(f for f in os.listdir(self.directory) if os.path.splitext(f)[1] == "hdf5")
filenames.discard('discretization_cache.hdf5')
for filename in filenames:
cachepath = self.abspath(filename)
try:
cachefile = core.file.HDF5File(cachepath)
......
......@@ -29,9 +29,28 @@ class DiscretizationCache(object):
"""
def __init__(self, filename):
self.file = h5py.File(filename, 'a')
if '/discretizations' not in self.file:
self.file.create_group('/discretizations')
self.filename = filename
self.file = None
def lock(self):
try:
self.file = h5py.File(self.filename, 'a')
if '/discretizations' not in self.file:
self.file.create_group('/discretizations')
except IOError:
self.file = None
return self
def unlock(self):
if self.file is not None:
self.file.close()
self.file = None
def __enter__(self):
return self.lock()
def __exit__(self, exc_type, exc_val, exc_tb):
self.unlock()
def get_discretization(self, volume, d_max):
"""
......@@ -40,15 +59,16 @@ class DiscretizationCache(object):
"""
discretization_repr = repr(volume) + " d_max=%d" % d_max
print_message("{volume}, discretization resolution: {resolution:d}".format(volume=repr(volume), resolution=d_max))
if discretization_repr in self.file['/discretizations']:
if self.file is not None and discretization_repr in self.file['/discretizations']:
stored_discretization = self.file['/discretizations/' + discretization_repr]
grid = np.array(stored_discretization)
discretization = Discretization(volume, d_max, grid)
else:
discretization = Discretization(volume, d_max)
grid = discretization.grid
self.file['/discretizations/' + discretization_repr] = grid
self.file.flush()
if self.file is not None:
self.file['/discretizations/' + discretization_repr] = grid
self.file.flush()
return discretization
def get_discretization_from_string(self, string):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment