gem5 v24.0.0.0
|
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 | |
Memoizer & | operator= (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_type > | cache |
Result cache. | |
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.
using gem5::Memoizer< Ret, Args >::args_type = std::tuple<Args...> |
Definition at line 86 of file memoizer.hh.
using gem5::Memoizer< Ret, Args >::ret_type = Ret |
Definition at line 85 of file memoizer.hh.
|
inlineconstexpr |
Definition at line 88 of file memoizer.hh.
References gem5::Memoizer< Ret, Args >::validateMemoizer().
|
delete |
|
delete |
|
inlineprotected |
True if the passed value is cached, false otherwise.
Definition at line 116 of file memoizer.hh.
References gem5::Memoizer< Ret, Args >::cache.
|
inlineprotected |
Return the size of the memoizer cache.
Definition at line 123 of file memoizer.hh.
References gem5::Memoizer< Ret, Args >::cache.
|
inline |
Clear the memoization cache.
Definition at line 111 of file memoizer.hh.
References gem5::Memoizer< Ret, Args >::cache.
|
inlineconstexprprivate |
Definition at line 146 of file memoizer.hh.
References gem5::Memoizer< Ret, Args >::func, and gem5::Memoizer< Ret, Args >::iterateTupleArgs().
Referenced by gem5::Memoizer< Ret, Args >::iterateTupleArgs(), and gem5::Memoizer< Ret, Args >::validateMemoizer().
|
inline |
Definition at line 99 of file memoizer.hh.
References gem5::Memoizer< Ret, Args >::cache, and gem5::Memoizer< Ret, Args >::func.
|
delete |
|
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().
|
mutableprivate |
Result cache.
Definition at line 158 of file memoizer.hh.
Referenced by gem5::Memoizer< Ret, Args >::cached(), gem5::Memoizer< Ret, Args >::cacheSize(), gem5::Memoizer< Ret, Args >::flush(), and gem5::Memoizer< Ret, Args >::operator()().
|
private |
Memoized function.
Definition at line 156 of file memoizer.hh.
Referenced by gem5::Memoizer< Ret, Args >::iterateTupleArgs(), and gem5::Memoizer< Ret, Args >::operator()().