gem5 v24.0.0.0
Loading...
Searching...
No Matches
multi.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019-2020 Inria
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
35
36#include <cmath>
37#include <queue>
38
39#include "base/bitfield.hh"
40#include "base/logging.hh"
41#include "base/trace.hh"
42#include "debug/CacheComp.hh"
43#include "params/MultiCompressor.hh"
44
45namespace gem5
46{
47
48namespace compression
49{
50
52 std::unique_ptr<Base::CompressionData> comp_data)
53 : CompressionData(), index(index), compData(std::move(comp_data))
54{
55 setSizeBits(compData->getSizeBits());
56}
57
58uint8_t
60{
61 return index;
62}
63
66 numEncodingBits(p.encoding_in_tags ? 0 :
67 std::log2(alignToPowerOfTwo(compressors.size()))),
68 multiStats(stats, *this)
69{
70 fatal_if(compressors.size() == 0, "There must be at least one compressor");
71}
72
74{
75 for (auto& compressor : compressors) {
76 delete compressor;
77 }
78}
79
80void
82{
83 Base::setCache(_cache);
84 for (auto& compressor : compressors) {
85 compressor->setCache(_cache);
86 }
87}
88
89std::unique_ptr<Base::CompressionData>
90Multi::compress(const std::vector<Chunk>& chunks, Cycles& comp_lat,
91 Cycles& decomp_lat)
92{
93 struct Results
94 {
95 unsigned index;
96 std::unique_ptr<Base::CompressionData> compData;
97 Cycles decompLat;
98 uint8_t compressionFactor;
99
100 Results(unsigned index,
101 std::unique_ptr<Base::CompressionData> comp_data,
102 Cycles decomp_lat, std::size_t blk_size)
103 : index(index), compData(std::move(comp_data)),
104 decompLat(decomp_lat)
105 {
106 const std::size_t size = compData->getSize();
107 // If the compressed size is worse than the uncompressed size,
108 // we assume the size is the uncompressed size, and thus the
109 // compression factor is 1.
110 //
111 // Some compressors (notably the zero compressor) may rely on
112 // extra information being stored in the tags, or added in
113 // another compression layer. Their size can be 0, so it is
114 // assigned the highest possible compression factor (the original
115 // block's size).
116 compressionFactor = (size > blk_size) ? 1 :
117 ((size == 0) ? blk_size :
118 alignToPowerOfTwo(std::floor(blk_size / (double) size)));
119 }
120 };
121 struct ResultsComparator
122 {
123 bool
124 operator()(const std::shared_ptr<Results>& lhs,
125 const std::shared_ptr<Results>& rhs) const
126 {
127 const std::size_t lhs_cf = lhs->compressionFactor;
128 const std::size_t rhs_cf = rhs->compressionFactor;
129
130 if (lhs_cf == rhs_cf) {
131 // When they have similar compressed sizes, give the one
132 // with fastest decompression privilege
133 return lhs->decompLat > rhs->decompLat;
134 }
135 return lhs_cf < rhs_cf;
136 }
137 };
138
139 // Each sub-compressor can have its own chunk size; therefore, revert
140 // the chunks to raw data, so that they handle the conversion internally
141 uint64_t data[blkSize / sizeof(uint64_t)];
142 std::memset(data, 0, blkSize);
143 fromChunks(chunks, data);
144
145 // Find the ranking of the compressor outputs
146 std::priority_queue<std::shared_ptr<Results>,
147 std::vector<std::shared_ptr<Results>>, ResultsComparator> results;
148 Cycles max_comp_lat;
149 for (unsigned i = 0; i < compressors.size(); i++) {
150 Cycles temp_decomp_lat;
151 auto temp_comp_data =
152 compressors[i]->compress(data, comp_lat, temp_decomp_lat);
153 temp_comp_data->setSizeBits(temp_comp_data->getSizeBits() +
155 results.push(std::make_shared<Results>(i, std::move(temp_comp_data),
156 temp_decomp_lat, blkSize));
157 max_comp_lat = std::max(max_comp_lat, comp_lat);
158 }
159
160 // Assign best compressor to compression data
161 const unsigned best_index = results.top()->index;
162 std::unique_ptr<CompressionData> multi_comp_data =
163 std::unique_ptr<MultiCompData>(
164 new MultiCompData(best_index, std::move(results.top()->compData)));
165 DPRINTF(CacheComp, "Best compressor: %d\n", best_index);
166
167 // Set decompression latency of the best compressor
168 decomp_lat = results.top()->decompLat + decompExtraLatency;
169
170 // Update compressor ranking stats
171 for (int rank = 0; rank < compressors.size(); rank++) {
172 multiStats.ranks[results.top()->index][rank]++;
173 results.pop();
174 }
175
176 // Set compression latency (compression latency of the slowest compressor
177 // and 1 cycle to pack)
178 comp_lat = Cycles(max_comp_lat + compExtraLatency);
179
180 return multi_comp_data;
181}
182
183void
185 uint64_t* cache_line)
186{
187 const MultiCompData* casted_comp_data =
188 static_cast<const MultiCompData*>(comp_data);
189 compressors[casted_comp_data->getIndex()]->decompress(
190 casted_comp_data->compData.get(), cache_line);
191}
192
194 : statistics::Group(&base_group), compressor(_compressor),
195 ADD_STAT(ranks, statistics::units::Count::get(),
196 "Number of times each compressor had the nth best compression")
197{
198}
199
200void
202{
204
205 const std::size_t num_compressors = compressor.compressors.size();
206 ranks.init(num_compressors, num_compressors);
207 for (unsigned compressor = 0; compressor < num_compressors; compressor++) {
208 ranks.subname(compressor, std::to_string(compressor));
209 ranks.subdesc(compressor, "Number of times compressor " +
210 std::to_string(compressor) + " had the nth best compression.");
211 for (unsigned rank = 0; rank < num_compressors; rank++) {
212 ranks.ysubname(rank, std::to_string(rank));
213 }
214 }
215}
216
217} // namespace compression
218} // namespace gem5
#define DPRINTF(x,...)
Definition trace.hh:210
const char data[]
A basic cache interface.
Definition base.hh:100
Cycles is a wrapper class for representing cycle counts, i.e.
Definition types.hh:79
void setSizeBits(std::size_t size)
Set compression size (in bits).
Definition base.cc:67
Base cache compressor interface.
Definition base.hh:65
const Cycles compExtraLatency
Extra latency added to compression due to packaging, shifting or other operations.
Definition base.hh:114
void fromChunks(const std::vector< Chunk > &chunks, uint64_t *data) const
This function re-joins the chunks to recreate the original data.
Definition base.cc:133
gem5::compression::Base::BaseStats stats
const Cycles decompExtraLatency
Extra latency added to decompression due to packaging, shifting or other operations.
Definition base.hh:126
friend class Multi
This compressor must be able to access the protected functions of its sub-compressors.
Definition base.hh:88
const std::size_t blkSize
Uncompressed cache line size (in bytes).
Definition base.hh:93
virtual void setCache(BaseCache *_cache)
The cache can only be set once.
Definition base.cc:107
uint8_t getIndex() const
Get the index of the best compressor.
Definition multi.cc:59
MultiCompData(unsigned index, std::unique_ptr< Base::CompressionData > comp_data)
Default constructor.
Definition multi.cc:51
std::unique_ptr< Base::CompressionData > compData
Compression data of the best compressor.
Definition multi.hh:125
void setCache(BaseCache *_cache) override
The cache can only be set once.
Definition multi.cc:81
const std::size_t numEncodingBits
An encoding is associated to each sub-compressor to inform which sub-compressor to use when decompres...
Definition multi.hh:80
MultiCompressorParams Params
Definition multi.hh:104
std::vector< Base * > compressors
List of sub-compressors.
Definition multi.hh:63
std::unique_ptr< Base::CompressionData > compress(const std::vector< Base::Chunk > &chunks, Cycles &comp_lat, Cycles &decomp_lat) override
Apply the compression process to the cache line.
Definition multi.cc:90
gem5::compression::Multi::MultiStats multiStats
void decompress(const CompressionData *comp_data, uint64_t *data) override
Apply the decompression process to the compressed data.
Definition multi.cc:184
Statistics container.
Definition group.hh:93
STL vector class.
Definition stl.hh:37
Definition of the a multi compressor that choses the best compression among multiple compressors.
#define ADD_STAT(n,...)
Convenience macro to add a stat to a statistics group.
Definition group.hh:75
constexpr uint64_t alignToPowerOfTwo(uint64_t val)
Align to the next highest power of two.
Definition bitfield.hh:450
#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
virtual void regStats()
Callback to set stat parameters.
Definition group.cc:68
Bitfield< 7 > i
Definition misc_types.hh:67
Bitfield< 30, 0 > index
Bitfield< 0 > p
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
Overload hash function for BasicBlockRange type.
Definition binary32.hh:81
void regStats() override
Callback to set stat parameters.
Definition multi.cc:201
MultiStats(BaseStats &base_group, Multi &_compressor)
Definition multi.cc:193
statistics::Vector2d ranks
Number of times each compressor provided the nth best compression.
Definition multi.hh:100

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