gem5  [DEVELOP-FOR-23.0]
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
45 namespace gem5
46 {
47 
48 namespace 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 
58 uint8_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 
80 void
82 {
83  Base::setCache(_cache);
84  for (auto& compressor : compressors) {
85  compressor->setCache(_cache);
86  }
87 }
88 
89 std::unique_ptr<Base::CompressionData>
90 Multi::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 
183 void
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 
200 void
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
gem5::compression::Base::CompressionData
Definition: base.hh:245
gem5::MipsISA::misc_reg::Count
@ Count
Definition: misc.hh:94
data
const char data[]
Definition: circlebuf.test.cc:48
gem5::compression::Multi::compressors
std::vector< Base * > compressors
List of sub-compressors.
Definition: multi.hh:60
gem5::MipsISA::index
Bitfield< 30, 0 > index
Definition: pra_constants.hh:47
gem5::compression::Multi::MultiCompData::compData
std::unique_ptr< Base::CompressionData > compData
Compression data of the best compressor.
Definition: multi.hh:125
sc_dt::to_string
const std::string to_string(sc_enc enc)
Definition: sc_fxdefs.cc:91
gem5::compression::Base::Params
BaseCacheCompressorParams Params
Definition: base.hh:201
std::vector< Chunk >
gem5::compression::Base::blkSize
const std::size_t blkSize
Uncompressed cache line size (in bytes).
Definition: base.hh:93
gem5::compression::Base::compExtraLatency
const Cycles compExtraLatency
Extra latency added to compression due to packaging, shifting or other operations.
Definition: base.hh:114
gem5::ArmISA::i
Bitfield< 7 > i
Definition: misc_types.hh:67
gem5::compression::Multi::numEncodingBits
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
multi.hh
gem5::compression::Multi::decompress
void decompress(const CompressionData *comp_data, uint64_t *data) override
Apply the decompression process to the compressed data.
Definition: multi.cc:184
gem5::compression::Base::setCache
virtual void setCache(BaseCache *_cache)
The cache can only be set once.
Definition: base.cc:107
gem5::compression::Base::fromChunks
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::Cycles
Cycles is a wrapper class for representing cycle counts, i.e.
Definition: types.hh:78
gem5::compression::Multi::MultiCompData::MultiCompData
MultiCompData(unsigned index, std::unique_ptr< Base::CompressionData > comp_data)
Default constructor.
Definition: multi.cc:51
gem5::compression::Base::decompExtraLatency
const Cycles decompExtraLatency
Extra latency added to decompression due to packaging, shifting or other operations.
Definition: base.hh:126
bitfield.hh
gem5::VegaISA::p
Bitfield< 54 > p
Definition: pagetable.hh:70
gem5::BaseCache
A basic cache interface.
Definition: base.hh:94
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:210
ADD_STAT
#define ADD_STAT(n,...)
Convenience macro to add a stat to a statistics group.
Definition: group.hh:75
gem5::compression::Base
Base cache compressor interface.
Definition: base.hh:64
gem5::compression::Base::setSizeBits
static void setSizeBits(CacheBlk *blk, const std::size_t size_bits)
Set the size of the compressed block, in bits.
Definition: base.cc:224
gem5::compression::Multi::MultiStats::ranks
statistics::Vector2d ranks
Number of times each compressor provided the nth best compression.
Definition: multi.hh:100
gem5::compression::Base::stats
gem5::compression::Base::BaseStats stats
gem5::statistics::Group::regStats
virtual void regStats()
Callback to set stat parameters.
Definition: group.cc:68
gem5::compression::Multi::MultiStats::regStats
void regStats() override
Callback to set stat parameters.
Definition: multi.cc:201
gem5::compression::Multi::setCache
void setCache(BaseCache *_cache) override
The cache can only be set once.
Definition: multi.cc:81
gem5::compression::Base::Multi
friend class Multi
This compressor must be able to access the protected functions of its sub-compressors.
Definition: base.hh:88
gem5::compression::Multi::MultiStats::MultiStats
MultiStats(BaseStats &base_group, Multi &_compressor)
Definition: multi.cc:193
std
Overload hash function for BasicBlockRange type.
Definition: misc.hh:2909
gem5::compression::Multi::multiStats
gem5::compression::Multi::MultiStats multiStats
gem5::compression::Multi::compress
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
logging.hh
gem5::statistics::Group
Statistics container.
Definition: group.hh:92
gem5::compression::Multi::~Multi
~Multi()
Definition: multi.cc:73
gem5::compression::Multi::MultiCompData
Definition: multi.hh:117
trace.hh
gem5::compression::Multi::MultiCompData::getIndex
uint8_t getIndex() const
Get the index of the best compressor.
Definition: multi.cc:59
fatal_if
#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
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37
gem5::alignToPowerOfTwo
constexpr uint64_t alignToPowerOfTwo(uint64_t val)
Align to the next highest power of two.
Definition: bitfield.hh:385
gem5::compression::Multi
Definition: multi.hh:52
gem5::compression::Base::BaseStats
Definition: base.hh:131

Generated on Sun Jul 30 2023 01:56:57 for gem5 by doxygen 1.8.17