simple_cache module¶
Caching module to avoid multiple evaluations of a discipline¶
-
class
gemseo.caches.simple_cache.
SimpleCache
(tolerance=0.0, name=None)[source]¶ Bases:
gemseo.core.cache.AbstractCache
Simple discipline cache based on a dictionary. Only caches the last execution.
Initialize cache tolerance. By default, don’t use approximate cache. It is up to the user to choose to optimize CPU time with this or not could be something like 2 * finfo(float).eps
- Parameters
tolerance (float) – Tolerance that defines if two input vectors are equal and cached data shall be returned. If 0, no approximation is made. Default: 0.
name (str) – Name of the cache.
Examples
>>> from gemseo.caches.simple_cache import SimpleCache >>> cache = SimpleCache()
-
cache_jacobian
(input_data, input_names, jacobian)[source]¶ Cache jacobian data to avoid re evaluation
- Parameters
input_data (dict) – Input data to cache.
input_names (list(str)) – List of input data names.
jacobian (dict) – Jacobian to cache.
Examples
>>> from gemseo.caches.simple_cache import SimpleCache >>> from numpy import array >>> cache = SimpleCache() >>> data = {'x': array([1.]), 'y': array([2.])} >>> jacobian = {'y': {'x': array([3.])}} >>> cache.cache_jacobian(data, ['x'], jacobian) (None, {'y': {'x': array([3.])}})
-
cache_outputs
(input_data, input_names, output_data, output_names=None)[source]¶ Cache data to avoid re evaluation.
- Parameters
input_data (dict) – Input data to cache.
input_names (list(str)) – List of input data names.
output_data (dict) – Output data to cache.
output_names (list(str)) – List of output data names. If None, use all output names. Default: None.
Examples
>>> from gemseo.caches.simple_cache import SimpleCache >>> from numpy import array >>> cache = SimpleCache() >>> data = {'x': array([1.]), 'y': array([2.])} >>> cache.cache_outputs(data, ['x'], data, ['y']) >>> cache[1] {'y': array([2.]), 'x': array([1.])}
-
clear
()[source]¶ Clear the cache.
Examples
>>> from gemseo.caches.simple_cache import SimpleCache >>> from numpy import array >>> cache = SimpleCache() >>> data = {'x': array([1.]), 'y': array([.2])} >>> cache.cache_outputs(data, ['x'], data, ['y']) >>> cache.get_length() 1 >>> cache.clear() >>> cache.get_length() 0
-
get_all_data
(as_iterator=False)[source]¶ Read all the data in the cache
- Parameters
as_iterator (bool) – If True, return an iterator. Otherwise a dictionary. Default: False.
- Returns
all_data – A dictionary of dictionaries for inputs, outputs and jacobian where keys are data indices.
- Return type
dict
Examples
>>> from gemseo.caches.simple_cache import SimpleCache >>> from numpy import array >>> cache = SimpleCache() >>> data = {'x': array([1.]), 'y': array([2.])} >>> cache.cache_outputs(data, ['x'], data, ['y']) >>> cache.get_all_data() {1: {'inputs': {'x': array([1.])}, 'jacobian': None, 'outputs': {'y': array([0.2])}}}
-
get_data
(index, **options)[source]¶ Returns an elementary sample.
- Parameters
index (int) – sample index.
options – getter options
-
get_last_cached_inputs
()[source]¶ Retrieve the last execution inputs.
- Returns
inputs – Last cached inputs.
- Return type
dict
Examples
>>> from gemseo.caches.simple_cache import SimpleCache >>> from numpy import array >>> cache = SimpleCache() >>> data = {'x': array([1.]), 'y': array([2.])} >>> cache.cache_outputs(data, ['x'], data, ['y']) >>> cache.get_last_cached_inputs() {'X': array([1.])}
-
get_last_cached_outputs
()[source]¶ Retrieve the last execution outputs
- Returns
outputs – Last cached outputs.
- Return type
dict
Examples
>>> from gemseo.caches.simple_cache import SimpleCache >>> from numpy import array >>> cache = SimpleCache() >>> data = {'x': array([1.]), 'y': array([2.])} >>> cache.cache_outputs(data, ['x'], data, ['y']) >>> cache.get_last_cached_outputs() {'y': array([2.])}
-
get_length
()[source]¶ Get the length of the cache, ie the number of stored elements.
- Returns
length – Length of the cache.
- Return type
int
Examples
>>> from gemseo.caches.simple_cache import SimpleCache >>> from numpy import array >>> cache = SimpleCache() >>> data = {'x': array([1.]), 'y': array([2.])} >>> cache.cache_outputs(data, ['x'], data, ['y']) >>> cache.get_length() 1
-
get_outputs
(input_data, input_names=None)[source]¶ Check if the discipline has already been evaluated for the given input data dictionary. If True, return the associated cache, otherwise return None.
- Parameters
input_data (dict) – Input data dictionary to test for caching.
input_names (list(str)) – List of input data names. If None, uses them all
- Returns
output_data (dict) – Output data if there is no need to evaluate the discipline. None otherwise.
jacobian (dict) – Jacobian if there is no need to evaluate the discipline. None otherwise.
Examples
>>> from gemseo.caches.simple_cache import SimpleCache >>> from numpy import array >>> cache = SimpleCache() >>> data = {'x': array([1.]), 'y': array([2.])} >>> cache.cache_outputs(data, ['x'], data, ['y']) >>> cache.get_outputs({'x': array([1.])}, ['x']) ({'y': array([2.])}, None) >>> cache.get_outputs({'x': array([2.])}, ['x']) (None, None)