gem5  v22.0.0.2
Public Types | Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
gem5::Memoizer< Ret, Args > Class Template Reference

This class takes a function as a constructor argument and memoizes it: every time the function gets invoked through the Memoizer object (see operator()), the result gets saved in the internal cache, ready to be retrieved next time an invokation is made with the same arguments. More...

#include <memoizer.hh>

Public Types

using ret_type = Ret
 
using args_type = std::tuple< Args... >
 

Public Member Functions

constexpr Memoizer (Ret _func(Args...))
 
 Memoizer ()=delete
 
 Memoizer (const Memoizer &rhs)=delete
 
Memoizeroperator= (const Memoizer &rhs)=delete
 
auto operator() (Args... args) const
 
void flush ()
 Clear the memoization cache. More...
 

Protected Member Functions

bool cached (args_type args) const
 True if the passed value is cached, false otherwise. More...
 
size_t cacheSize () const
 Return the size of the memoizer cache. More...
 

Private Member Functions

constexpr void validateMemoizer ()
 Validate the memoizer and produce an error if the function signature contains a reference. More...
 
template<size_t Start, size_t End, size_t Inc, class F >
constexpr void iterateTupleArgs (F &&func)
 

Private Attributes

const std::function< Ret(Args...)> func
 Memoized function. More...
 
std::map< args_type, ret_typecache
 Result cache. More...
 

Detailed Description

template<typename Ret, typename... Args>
class gem5::Memoizer< Ret, Args >

This class takes a function as a constructor argument and memoizes it: every time the function gets invoked through the Memoizer object (see operator()), the result gets saved in the internal cache, ready to be retrieved next time an invokation is made with the same arguments.

Example usage:

int fibonacci(int n);

Memoizer fibonacci_memo(fibonacci); fibonacci_memo(5);

Not every function is eligible for memoization. We explicitly validate the memoizer at compile time and produce an error if the function signature contains a reference.

There are two ways to discard a memoization

1) Delete the Memoizer object 2) Use the Memoizer::flush method

In some cases there is little or no reason to discard a memoization (like in the fibonacci example, where fibonacci(k) always returns the same value for the same input k) The memoizer could be used in more complex cases, where a change in the global state affects the output of the function, which effectively invalidates the cached results. It is up to the client to understand when memoization is no longer valid and to flush the result cache as a consequence.

Definition at line 82 of file memoizer.hh.

Member Typedef Documentation

◆ args_type

template<typename Ret , typename... Args>
using gem5::Memoizer< Ret, Args >::args_type = std::tuple<Args...>

Definition at line 86 of file memoizer.hh.

◆ ret_type

template<typename Ret , typename... Args>
using gem5::Memoizer< Ret, Args >::ret_type = Ret

Definition at line 85 of file memoizer.hh.

Constructor & Destructor Documentation

◆ Memoizer() [1/3]

template<typename Ret , typename... Args>
constexpr gem5::Memoizer< Ret, Args >::Memoizer ( Ret   _funcArgs...)
inlineconstexpr

Definition at line 88 of file memoizer.hh.

◆ Memoizer() [2/3]

template<typename Ret , typename... Args>
gem5::Memoizer< Ret, Args >::Memoizer ( )
delete

◆ Memoizer() [3/3]

template<typename Ret , typename... Args>
gem5::Memoizer< Ret, Args >::Memoizer ( const Memoizer< Ret, Args > &  rhs)
delete

Member Function Documentation

◆ cached()

template<typename Ret , typename... Args>
bool gem5::Memoizer< Ret, Args >::cached ( args_type  args) const
inlineprotected

True if the passed value is cached, false otherwise.

Definition at line 116 of file memoizer.hh.

◆ cacheSize()

template<typename Ret , typename... Args>
size_t gem5::Memoizer< Ret, Args >::cacheSize ( ) const
inlineprotected

Return the size of the memoizer cache.

Definition at line 123 of file memoizer.hh.

◆ flush()

template<typename Ret , typename... Args>
void gem5::Memoizer< Ret, Args >::flush ( )
inline

Clear the memoization cache.

Definition at line 111 of file memoizer.hh.

◆ iterateTupleArgs()

template<typename Ret , typename... Args>
template<size_t Start, size_t End, size_t Inc, class F >
constexpr void gem5::Memoizer< Ret, Args >::iterateTupleArgs ( F &&  func)
inlineconstexprprivate

Definition at line 146 of file memoizer.hh.

◆ operator()()

template<typename Ret , typename... Args>
auto gem5::Memoizer< Ret, Args >::operator() ( Args...  args) const
inline

Definition at line 99 of file memoizer.hh.

◆ operator=()

template<typename Ret , typename... Args>
Memoizer& gem5::Memoizer< Ret, Args >::operator= ( const Memoizer< Ret, Args > &  rhs)
delete

◆ validateMemoizer()

template<typename Ret , typename... Args>
constexpr void gem5::Memoizer< Ret, Args >::validateMemoizer ( )
inlineconstexprprivate

Validate the memoizer and produce an error if the function signature contains a reference.

Definition at line 134 of file memoizer.hh.

Referenced by gem5::Memoizer< int, gem5::ThreadContext *, bool, bool, TCR, ExceptionLevel >::Memoizer().

Member Data Documentation

◆ cache

template<typename Ret , typename... Args>
std::map<args_type, ret_type> gem5::Memoizer< Ret, Args >::cache
mutableprivate

◆ func

template<typename Ret , typename... Args>
const std::function<Ret(Args...)> gem5::Memoizer< Ret, Args >::func
private

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

Generated on Thu Jul 28 2022 13:33:07 for gem5 by doxygen 1.8.17