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) 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 29 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 }
bool _enable_cache
Flag for whether to cache values.
bool _respect_time
Flag for whether changes in time invalidate the cache.

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 82 of file MemoizedFunctionInterface.C.

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

83 {
84  _cache.clear();
85 }
std::unordered_map< hashing::HashValue, Real > _cache
Cached evaluations for each point.

◆ meshChanged()

void MemoizedFunctionInterface::meshChanged ( )
overridevirtual

Definition at line 40 of file MemoizedFunctionInterface.C.

41 {
42  // The mesh has changed, which invalidates the cache
44 }
void invalidateCache()
Called by derived classes to invalidate the cache, perhaps due to a state change. ...

◆ useCache()

void MemoizedFunctionInterface::useCache ( bool  use)

Enable/disable the cache.

Definition at line 73 of file MemoizedFunctionInterface.C.

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

74 {
75  _enable_cache = use;
76 
77  if (!_enable_cache)
79 }
void invalidateCache()
Called by derived classes to invalidate the cache, perhaps due to a state change. ...
bool _enable_cache
Flag for whether to cache values.

◆ value()

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

Definition at line 47 of file MemoizedFunctionInterface.C.

48 {
49  if (_enable_cache)
50  {
51  // Start the cache over if we are at a new time step
52  if (_respect_time && time != _current_time)
53  {
54  _current_time = time;
56  }
57 
58  // Try to insert a new value into the cache
59  auto result = _cache.insert({hashing::hashCombine(time, point), 0.0});
60 
61  // Evaluate and apply if the insertion worked, i.e. the element didn't exist
62  if (result.second)
63  result.first->second = evaluateValue(time, point);
64 
65  // Return the cached value
66  return result.first->second;
67  }
68 
69  return evaluateValue(time, point);
70 }
void invalidateCache()
Called by derived classes to invalidate the cache, perhaps due to a state change. ...
bool _enable_cache
Flag for whether to cache values.
Real _current_time
Stores the time evaluation of the cache.
void hashCombine(HashValue &)
Final iteration of the variadic template with no additional arguments.
Definition: Hashing.h:27
bool _respect_time
Flag for whether changes in time invalidate the cache.
std::unordered_map< hashing::HashValue, Real > _cache
Cached evaluations for each point.
virtual Real evaluateValue(Real time, const Point &point)=0
Used in derived classes, equivalent to Function::value()

Member Data Documentation

◆ _cache

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

Cached evaluations for each point.

Definition at line 60 of file MemoizedFunctionInterface.h.

Referenced by invalidateCache(), and value().

◆ _current_time

Real MemoizedFunctionInterface::_current_time
private

Stores the time evaluation of the cache.

Definition at line 63 of file MemoizedFunctionInterface.h.

Referenced by value().

◆ _enable_cache

bool MemoizedFunctionInterface::_enable_cache
private

Flag for whether to cache values.

Definition at line 66 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 69 of file MemoizedFunctionInterface.h.

Referenced by value().


The documentation for this class was generated from the following files: