gem5 v24.0.0.0
Loading...
Searching...
No Matches
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.
 

Protected Member Functions

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

Private Member Functions

constexpr void validateMemoizer ()
 Validate the memoizer and produce an error if the function signature contains a reference.
 
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.
 
std::map< args_type, ret_typecache
 Result cache.
 

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>
gem5::Memoizer< Ret, Args >::Memoizer ( Ret(* _func )(Args...))
inlineconstexpr

Definition at line 88 of file memoizer.hh.

References gem5::Memoizer< Ret, Args >::validateMemoizer().

◆ 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.

References gem5::Memoizer< Ret, Args >::cache.

◆ 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.

References gem5::Memoizer< Ret, Args >::cache.

◆ 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.

References gem5::Memoizer< Ret, Args >::cache.

◆ iterateTupleArgs()

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

◆ operator()()

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

◆ operator=()

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

◆ validateMemoizer()

template<typename Ret , typename... Args>
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.

References gem5::ArmISA::i, gem5::Memoizer< Ret, Args >::iterateTupleArgs(), and gem5::X86ISA::type.

Referenced by gem5::Memoizer< Ret, Args >::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

Memoized function.

Definition at line 156 of file memoizer.hh.

Referenced by gem5::Memoizer< Ret, Args >::iterateTupleArgs(), and gem5::Memoizer< Ret, Args >::operator()().


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

Generated on Tue Jun 18 2024 16:24:12 for gem5 by doxygen 1.11.0