tlm_adjoint.caches

Module Contents

class tlm_adjoint.caches.CacheRef(value=None)

A cache entry. Stores a reference to a cached value, which can later be cleared. Calling a CacheRef returns the cached object, or None if no object is referenced.

Parameters:

value – The object to reference. None may be supplied to indicate an empty cache entry.

clear()

Clear the cache entry. After calling this method, calling the CacheRef will return None.

tlm_adjoint.caches.clear_caches(*deps)

Clear caches entries.

Parameters:

deps – A Sequence of variables. If non-empty then clear only cache entries which depend on the supplied variables. Otherwise clear all cache entries.

tlm_adjoint.caches.local_caches(fn)

Decorator clearing caches before and after calling the decorated callable.

Parameters:

fn – A callable for which caches should be cleared.

Returns:

A callable where caches are cleared before and after calling.

class tlm_adjoint.caches.Cache

Stores cache entries.

Cleared cache entries are removed from the Cache.

property id

A unique int ID associated with this Cache.

clear(*deps)

Clear cache entries.

Parameters:

deps – A Sequence of variables. If non-empty then only clear cache entries which depend on the supplied variables. Otherwise clear all cache entries.

add(key, value, deps=None)

Add a cache entry.

Parameters:
  • key – The key associated with the cache entry.

  • value – A callable, taking no arguments, returning the value associated with the cache entry. Only called to if no entry associated with key exists.

  • deps – A Sequence of variables, defining dependencies of the cache entry.

Returns:

A tuple (value_ref, value), where value is the cache entry value and value_ref is a CacheRef storing a reference to the value.

get(key, *args)

Return the cache entry associated with a given key.

Parameters:

key – The key.

Returns:

The cache entry or, if supplied, a default value.

args should contain zero or one elements and defines the default value. If there is no entry associated with the key then:

  • If args has no elements an exception is raised.

  • If args has one element then this is returned.

class tlm_adjoint.caches.Caches(x)

Multiple Cache objects, associated with a variable.

Cache entries may depend on the variable. The variable also defines an initial value, and the value is indicated by the variable ID and variable state value. The value may be changed either by supplying a new variable (changing the ID), or by changing the value of the current variable defining the value (which should be indicated by a change to the variable state value). Either change invalidates cache entries, in the Cache objects, which depend on the original variable.

The Caches.update() method can be used to check for cache entry invalidation, and to clear invalid cache entries.

Parameters:

x – The variable defining a possible cache entry dependency, and an initial value for that dependency.

clear()

Clear cache entries which depend on the associated variable.

add(cache)

Add a new Cache to the Caches.

Parameters:

cache – The Cache to add to the Caches.

remove(cache)

Remove a Cache from the Caches.

Parameters:

cache – The Cache to remove from the Caches.

update(x)

Check for cache invalidation associated with a possible change in value, and clear invalid cache entries.

Parameters:

x – A variable which defines a potentially new value.