gem5  v22.0.0.2
memoizer.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 Arm Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are
16  * met: redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer;
18  * redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution;
21  * neither the name of the copyright holders nor the names of its
22  * contributors may be used to endorse or promote products derived from
23  * this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #ifndef __BASE_MEMOIZER_HH__
39 #define __BASE_MEMOIZER_HH__
40 
41 #include <functional>
42 #include <map>
43 #include <type_traits>
44 #include <utility>
45 
46 namespace gem5
47 {
48 
81 template <typename Ret, typename... Args>
82 class Memoizer
83 {
84  public:
85  using ret_type = Ret;
86  using args_type = std::tuple<Args...>;
87 
88  constexpr Memoizer(Ret _func(Args...))
89  : func(_func)
90  {
92  }
93 
94  Memoizer() = delete;
95  Memoizer(const Memoizer &rhs) = delete;
96  Memoizer& operator=(const Memoizer &rhs) = delete;
97 
98  auto
99  operator()(Args... args) const
100  {
101  auto key = std::make_tuple(args...);
102  if (auto it = cache.find(key); it != cache.end()) {
103  // Return the cached result
104  return it->second;
105  }
106  auto emplaced = cache.emplace(key, func(args...));
107  return emplaced.first->second;
108  };
109 
111  void flush() { cache.clear(); }
112 
113  protected:
115  bool
116  cached(args_type args) const
117  {
118  return cache.find(args) != cache.end();
119  }
120 
122  size_t
123  cacheSize() const
124  {
125  return cache.size();
126  }
127 
128  private:
133  constexpr void
135  {
136  constexpr size_t num_args = std::tuple_size_v<std::decay_t<args_type>>;
137  iterateTupleArgs<size_t(0), num_args, size_t(1)>([&](auto i) {
138  static_assert(!std::is_reference_v<
139  typename std::tuple_element<
140  i.value, args_type>::type>);
141  });
142  }
143 
144  template <size_t Start, size_t End, size_t Inc, class F>
145  constexpr void
147  {
148  if constexpr (Start < End) {
149  func(std::integral_constant<decltype(Start), Start>());
150  iterateTupleArgs<Start + Inc, End, Inc>(func);
151  }
152  }
153 
154  private:
156  const std::function<Ret(Args...)> func;
158  mutable std::map<args_type, ret_type> cache;
159 
160 };
161 
162 } // namespace gem5
163 
164 #endif // __BASE_MEMOIZER_HH__
gem5::Memoizer::cached
bool cached(args_type args) const
True if the passed value is cached, false otherwise.
Definition: memoizer.hh:116
gem5::Memoizer< int, gem5::ThreadContext *, bool, bool, TCR, ExceptionLevel >::args_type
std::tuple< Args... > args_type
Definition: memoizer.hh:86
gem5::Memoizer::validateMemoizer
constexpr void validateMemoizer()
Validate the memoizer and produce an error if the function signature contains a reference.
Definition: memoizer.hh:134
gem5::Memoizer::Memoizer
Memoizer()=delete
gem5::ArmISA::i
Bitfield< 7 > i
Definition: misc_types.hh:67
gem5::Memoizer::cacheSize
size_t cacheSize() const
Return the size of the memoizer cache.
Definition: memoizer.hh:123
gem5::X86ISA::type
type
Definition: misc.hh:727
gem5::Memoizer
This class takes a function as a constructor argument and memoizes it: every time the function gets i...
Definition: memoizer.hh:82
gem5::Memoizer::flush
void flush()
Clear the memoization cache.
Definition: memoizer.hh:111
gem5::Memoizer::func
const std::function< Ret(Args...)> func
Memoized function.
Definition: memoizer.hh:156
gem5::Memoizer::operator()
auto operator()(Args... args) const
Definition: memoizer.hh:99
gem5::Memoizer< int, gem5::ThreadContext *, bool, bool, TCR, ExceptionLevel >::ret_type
int ret_type
Definition: memoizer.hh:85
gem5::Memoizer::Memoizer
constexpr Memoizer(Ret _func(Args...))
Definition: memoizer.hh:88
gem5::Memoizer::cache
std::map< args_type, ret_type > cache
Result cache.
Definition: memoizer.hh:158
gem5::Memoizer::operator=
Memoizer & operator=(const Memoizer &rhs)=delete
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37
gem5::Memoizer::iterateTupleArgs
constexpr void iterateTupleArgs(F &&func)
Definition: memoizer.hh:146

Generated on Thu Jul 28 2022 13:32:27 for gem5 by doxygen 1.8.17