www.mooseframework.org
Public Member Functions | Protected Member Functions | Private Attributes | List of all members
MemoizedFunctionInterface Class Referenceabstract

Implementation of Function that memoizes (caches) former evaluations in an unordered map using a hash of the evaluation locations as the key. More...

#include <MemoizedFunctionInterface.h>

Inheritance diagram for MemoizedFunctionInterface:
[legend]

Public Member Functions

 MemoizedFunctionInterface (const InputParameters &parameters)
 
virtual void meshChanged () override
 
void useCache (bool use)
 Enable/disable the cache. More...
 
virtual Real value (Real time, const Point &point) const final
 

Protected Member Functions

virtual Real evaluateValue (Real time, const Point &point)=0
 Used in derived classes, equivalent to Function::value() More...
 
void invalidateCache ()
 Called by derived classes to invalidate the cache, perhaps due to a state change. More...
 

Private Attributes

std::unordered_map< hashing::HashValue, Real > _cache
 Cached evaluations for each point. More...
 
Real _current_time
 Stores the time evaluation of the cache. More...
 
bool _enable_cache
 Flag for whether to cache values. More...
 
bool _respect_time
 Flag for whether changes in time invalidate the cache. More...
 

Detailed Description

Implementation of Function that memoizes (caches) former evaluations in an unordered map using a hash of the evaluation locations as the key.

The purpose is to allow for quick evaluation of a complex function that may be reevaluated multiple times without changing the actual outputs.

Definition at line 28 of file MemoizedFunctionInterface.h.

Constructor & Destructor Documentation

◆ MemoizedFunctionInterface()

MemoizedFunctionInterface::MemoizedFunctionInterface ( const InputParameters &  parameters)

Definition at line 32 of file MemoizedFunctionInterface.C.

33  : Function(parameters),
34  _enable_cache(getParam<bool>("enable_cache")),
35  _respect_time(getParam<bool>("respect_time"))
36 {
37 }

Member Function Documentation

◆ evaluateValue()

virtual Real MemoizedFunctionInterface::evaluateValue ( Real  time,
const Point &  point 
)
protectedpure virtual

Used in derived classes, equivalent to Function::value()

Implemented in FunctionSeries.

Referenced by value().

◆ invalidateCache()

void MemoizedFunctionInterface::invalidateCache ( )
protected

Called by derived classes to invalidate the cache, perhaps due to a state change.

Definition at line 84 of file MemoizedFunctionInterface.C.

85 {
86  _cache.clear();
87 }

Referenced by MutableCoefficientsFunctionInterface::coefficientsChanged(), meshChanged(), useCache(), and value().

◆ meshChanged()

void MemoizedFunctionInterface::meshChanged ( )
overridevirtual

Definition at line 40 of file MemoizedFunctionInterface.C.

41 {
42  // The mesh has changed, which invalidates the cache
44 }

◆ useCache()

void MemoizedFunctionInterface::useCache ( bool  use)

Enable/disable the cache.

Definition at line 75 of file MemoizedFunctionInterface.C.

76 {
77  _enable_cache = use;
78 
79  if (!_enable_cache)
81 }

Referenced by FXFluxBC::FXFluxBC(), FXValueBC::FXValueBC(), and FXValuePenaltyBC::FXValuePenaltyBC().

◆ value()

Real MemoizedFunctionInterface::value ( Real  time,
const Point &  point 
) const
finalvirtual

Definition at line 47 of file MemoizedFunctionInterface.C.

48 {
49  MemoizedFunctionInterface * ptr = const_cast<MemoizedFunctionInterface *>(this);
50 
51  if (_enable_cache)
52  {
53  // Start the cache over if we are at a new time step
54  if (_respect_time && time != _current_time)
55  {
56  _current_time = time;
57  ptr->invalidateCache();
58  }
59 
60  // Try to insert a new value into the cache
61  auto result = _cache.insert({hashing::hashCombine(time, point), 0.0});
62 
63  // Evaluate and apply if the insertion worked, i.e. the element didn't exist
64  if (result.second)
65  result.first->second = ptr->evaluateValue(time, point);
66 
67  // Return the cached value
68  return result.first->second;
69  }
70 
71  return ptr->evaluateValue(time, point);
72 }

Member Data Documentation

◆ _cache

std::unordered_map<hashing::HashValue, Real> MemoizedFunctionInterface::_cache
mutableprivate

Cached evaluations for each point.

Definition at line 59 of file MemoizedFunctionInterface.h.

Referenced by invalidateCache(), and value().

◆ _current_time

Real MemoizedFunctionInterface::_current_time
mutableprivate

Stores the time evaluation of the cache.

Definition at line 62 of file MemoizedFunctionInterface.h.

Referenced by value().

◆ _enable_cache

bool MemoizedFunctionInterface::_enable_cache
private

Flag for whether to cache values.

Definition at line 65 of file MemoizedFunctionInterface.h.

Referenced by useCache(), and value().

◆ _respect_time

bool MemoizedFunctionInterface::_respect_time
private

Flag for whether changes in time invalidate the cache.

Definition at line 68 of file MemoizedFunctionInterface.h.

Referenced by value().


The documentation for this class was generated from the following files:
MemoizedFunctionInterface::_respect_time
bool _respect_time
Flag for whether changes in time invalidate the cache.
Definition: MemoizedFunctionInterface.h:68
MemoizedFunctionInterface
Implementation of Function that memoizes (caches) former evaluations in an unordered map using a hash...
Definition: MemoizedFunctionInterface.h:28
hashing::hashCombine
void hashCombine(HashValue &)
Final iteration of the variadic template with no additional arguments.
Definition: Hashing.h:28
MemoizedFunctionInterface::_enable_cache
bool _enable_cache
Flag for whether to cache values.
Definition: MemoizedFunctionInterface.h:65
MemoizedFunctionInterface::invalidateCache
void invalidateCache()
Called by derived classes to invalidate the cache, perhaps due to a state change.
Definition: MemoizedFunctionInterface.C:84
MemoizedFunctionInterface::_current_time
Real _current_time
Stores the time evaluation of the cache.
Definition: MemoizedFunctionInterface.h:62
MemoizedFunctionInterface::evaluateValue
virtual Real evaluateValue(Real time, const Point &point)=0
Used in derived classes, equivalent to Function::value()
MemoizedFunctionInterface::_cache
std::unordered_map< hashing::HashValue, Real > _cache
Cached evaluations for each point.
Definition: MemoizedFunctionInterface.h:59