gem5 v24.0.0.0
Loading...
Searching...
No Matches
dueling_rp.cc
Go to the documentation of this file.
1
30
31#include "base/logging.hh"
32#include "params/DuelingRP.hh"
33
34namespace gem5
35{
36
37namespace replacement_policy
38{
39
41 : Base(p), replPolicyA(p.replacement_policy_a),
42 replPolicyB(p.replacement_policy_b),
43 duelingMonitor(p.constituency_size, p.team_size),
44 duelingStats(this)
45{
46 fatal_if((replPolicyA == nullptr) || (replPolicyB == nullptr),
47 "All replacement policies must be instantiated");
48}
49
50void
51Dueling::invalidate(const std::shared_ptr<ReplacementData>& replacement_data)
52{
53 std::shared_ptr<DuelerReplData> casted_replacement_data =
54 std::static_pointer_cast<DuelerReplData>(replacement_data);
55 replPolicyA->invalidate(casted_replacement_data->replDataA);
56 replPolicyB->invalidate(casted_replacement_data->replDataB);
57}
58
59void
60Dueling::touch(const std::shared_ptr<ReplacementData>& replacement_data,
61 const PacketPtr pkt)
62{
63 std::shared_ptr<DuelerReplData> casted_replacement_data =
64 std::static_pointer_cast<DuelerReplData>(replacement_data);
65 replPolicyA->touch(casted_replacement_data->replDataA, pkt);
66 replPolicyB->touch(casted_replacement_data->replDataB, pkt);
67}
68
69void
70Dueling::touch(const std::shared_ptr<ReplacementData>& replacement_data) const
71{
72 std::shared_ptr<DuelerReplData> casted_replacement_data =
73 std::static_pointer_cast<DuelerReplData>(replacement_data);
74 replPolicyA->touch(casted_replacement_data->replDataA);
75 replPolicyB->touch(casted_replacement_data->replDataB);
76}
77
78void
79Dueling::reset(const std::shared_ptr<ReplacementData>& replacement_data,
80 const PacketPtr pkt)
81{
82 std::shared_ptr<DuelerReplData> casted_replacement_data =
83 std::static_pointer_cast<DuelerReplData>(replacement_data);
84 replPolicyA->reset(casted_replacement_data->replDataA, pkt);
85 replPolicyB->reset(casted_replacement_data->replDataB, pkt);
86
87 // A miss in a set is a sample to the duel. A call to this function
88 // implies in the replacement of an entry, which was either caused by
89 // a miss, an external invalidation, or the initialization of the table
90 // entry (when warming up)
91 duelingMonitor.sample(static_cast<Dueler*>(casted_replacement_data.get()));
92}
93
94void
95Dueling::reset(const std::shared_ptr<ReplacementData>& replacement_data) const
96{
97 std::shared_ptr<DuelerReplData> casted_replacement_data =
98 std::static_pointer_cast<DuelerReplData>(replacement_data);
99 replPolicyA->reset(casted_replacement_data->replDataA);
100 replPolicyB->reset(casted_replacement_data->replDataB);
101
102 // A miss in a set is a sample to the duel. A call to this function
103 // implies in the replacement of an entry, which was either caused by
104 // a miss, an external invalidation, or the initialization of the table
105 // entry (when warming up)
106 duelingMonitor.sample(static_cast<Dueler*>(casted_replacement_data.get()));
107}
108
111{
112 // This function assumes that all candidates are either part of the same
113 // sampled set, or are not samples.
114 // @todo This should be improved at some point.
115 panic_if(candidates.size() != params().team_size, "We currently only "
116 "support team sizes that match the number of replacement candidates");
117
118 // The team with the most misses loses
119 bool winner = !duelingMonitor.getWinner();
120
121 // If the entry is a sample, it can only be used with a certain policy.
122 bool team;
123 bool is_sample = duelingMonitor.isSample(static_cast<Dueler*>(
124 std::static_pointer_cast<DuelerReplData>(
125 candidates[0]->replacementData).get()), team);
126
127 // All replacement candidates must be set appropriately, so that the
128 // proper replacement data is used. A replacement policy X must be used
129 // if the candidates are its samples - in which case they must always
130 // use X - or if it is not a sample, and X is currently the best RP.
131 // This assumes that A's team is "false", and B's team is "true".
132 bool team_a;
133 if ((is_sample && !team) || (!is_sample && !winner)) {
135 team_a = true;
136 } else {
138 team_a = false;
139 }
140
141 // Create a temporary list of replacement candidates which re-routes the
142 // replacement data of the selected team
143 std::vector<std::shared_ptr<ReplacementData>> dueling_replacement_data;
144 for (auto& candidate : candidates) {
145 std::shared_ptr<DuelerReplData> dueler_repl_data =
146 std::static_pointer_cast<DuelerReplData>(
147 candidate->replacementData);
148
149 // As of now we assume that all candidates are either part of
150 // the same sampled team, or are not samples.
151 bool candidate_team;
152 panic_if(
153 duelingMonitor.isSample(dueler_repl_data.get(), candidate_team) &&
154 (team != candidate_team),
155 "Not all sampled candidates belong to the same team");
156
157 // Copy the original entry's data, re-routing its replacement data
158 // to the selected one
159 dueling_replacement_data.push_back(dueler_repl_data);
160 candidate->replacementData = team_a ? dueler_repl_data->replDataA :
161 dueler_repl_data->replDataB;
162 }
163
164 // Use the selected replacement policy to find the victim
165 ReplaceableEntry* victim = team_a ? replPolicyA->getVictim(candidates) :
166 replPolicyB->getVictim(candidates);
167
168 // Search for entry within the original candidates and clean-up duplicates
169 for (int i = 0; i < candidates.size(); i++) {
170 candidates[i]->replacementData = dueling_replacement_data[i];
171 }
172
173 return victim;
174}
175
176std::shared_ptr<ReplacementData>
178{
179 DuelerReplData* replacement_data = new DuelerReplData(
181 duelingMonitor.initEntry(static_cast<Dueler*>(replacement_data));
182 return std::shared_ptr<DuelerReplData>(replacement_data);
183}
184
186 : statistics::Group(parent),
187 ADD_STAT(selectedA, "Number of times A was selected to victimize"),
188 ADD_STAT(selectedB, "Number of times B was selected to victimize")
189{
190}
191
192} // namespace replacement_policy
193} // namespace gem5
A dueler is an entry that may or may not be accounted for sampling.
Definition dueling.hh:53
bool getWinner() const
Get the team that is currently winning the duel.
Definition dueling.cc:118
bool isSample(const Dueler *dueler, bool &team) const
Check if the given dueler is a sample for this instance.
Definition dueling.cc:112
void sample(const Dueler *dueler)
If given dueler is a sampling entry, sample it and check if the winning team must be updated.
Definition dueling.cc:91
void initEntry(Dueler *dueler)
Initialize a dueler entry, deciding wether it is a sample or not.
Definition dueling.cc:124
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition packet.hh:295
A replaceable entry is a basic entry in a 2d table-like structure that needs to have replacement func...
A common base class of cache replacement policy objects.
Definition base.hh:55
virtual void invalidate(const std::shared_ptr< ReplacementData > &replacement_data)=0
Invalidate replacement data to set it as the next probable victim.
virtual void reset(const std::shared_ptr< ReplacementData > &replacement_data, const PacketPtr pkt)
Reset replacement data.
Definition base.hh:89
BaseReplacementPolicyParams Params
Definition base.hh:57
virtual void touch(const std::shared_ptr< ReplacementData > &replacement_data, const PacketPtr pkt)
Update replacement data.
Definition base.hh:75
virtual ReplaceableEntry * getVictim(const ReplacementCandidates &candidates) const =0
Find replacement victim among candidates.
virtual std::shared_ptr< ReplacementData > instantiateEntry()=0
Instantiate a replacement data entry.
Base *const replPolicyB
Sub-replacement policy used in this multiple container.
Definition dueling_rp.hh:76
ReplaceableEntry * getVictim(const ReplacementCandidates &candidates) const override
Find replacement victim among candidates.
void reset(const std::shared_ptr< ReplacementData > &replacement_data, const PacketPtr pkt) override
Reset replacement data.
Definition dueling_rp.cc:79
std::shared_ptr< ReplacementData > instantiateEntry() override
Instantiate a replacement data entry.
void touch(const std::shared_ptr< ReplacementData > &replacement_data, const PacketPtr pkt) override
Update replacement data.
Definition dueling_rp.cc:60
DuelingMonitor duelingMonitor
A dueling monitor that decides which is the best sub-policy based on their number of misses.
Definition dueling_rp.hh:82
Base *const replPolicyA
Sub-replacement policy used in this multiple container.
Definition dueling_rp.hh:74
gem5::replacement_policy::Dueling::DuelingStats duelingStats
void invalidate(const std::shared_ptr< ReplacementData > &replacement_data) override
Invalidate replacement data to set it as the next probable victim.
Definition dueling_rp.cc:51
Statistics container.
Definition group.hh:93
STL vector class.
Definition stl.hh:37
#define ADD_STAT(n,...)
Convenience macro to add a stat to a statistics group.
Definition group.hh:75
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition logging.hh:236
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition logging.hh:214
const Params & params() const
Bitfield< 7 > i
Definition misc_types.hh:67
Bitfield< 0 > p
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
Dueler-specific implementation of replacement data.
Definition dueling_rp.hh:60
statistics::Scalar selectedB
Number of times B was selected on victimization.
Definition dueling_rp.hh:92
statistics::Scalar selectedA
Number of times A was selected on victimization.
Definition dueling_rp.hh:89

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