gem5  v21.0.1.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 Compressor {
46 
48  std::unique_ptr<Base::CompressionData> comp_data)
49  : CompressionData(), index(index), compData(std::move(comp_data))
50 {
51  setSizeBits(compData->getSizeBits());
52 }
53 
54 uint8_t
56 {
57  return index;
58 }
59 
62  numEncodingBits(p.encoding_in_tags ? 0 :
63  std::log2(alignToPowerOfTwo(compressors.size()))),
64  multiStats(stats, *this)
65 {
66  fatal_if(compressors.size() == 0, "There must be at least one compressor");
67 }
68 
70 {
71  for (auto& compressor : compressors) {
72  delete compressor;
73  }
74 }
75 
76 void
78 {
79  Base::setCache(_cache);
80  for (auto& compressor : compressors) {
81  compressor->setCache(_cache);
82  }
83 }
84 
85 std::unique_ptr<Base::CompressionData>
86 Multi::compress(const std::vector<Chunk>& chunks, Cycles& comp_lat,
87  Cycles& decomp_lat)
88 {
89  struct Results
90  {
91  unsigned index;
92  std::unique_ptr<Base::CompressionData> compData;
93  Cycles decompLat;
94  uint8_t compressionFactor;
95 
96  Results(unsigned index,
97  std::unique_ptr<Base::CompressionData> comp_data,
98  Cycles decomp_lat, std::size_t blk_size)
99  : index(index), compData(std::move(comp_data)),
100  decompLat(decomp_lat)
101  {
102  const std::size_t size = compData->getSize();
103  // If the compressed size is worse than the uncompressed size,
104  // we assume the size is the uncompressed size, and thus the
105  // compression factor is 1.
106  //
107  // Some compressors (notably the zero compressor) may rely on
108  // extra information being stored in the tags, or added in
109  // another compression layer. Their size can be 0, so it is
110  // assigned the highest possible compression factor (the original
111  // block's size).
112  compressionFactor = (size > blk_size) ? 1 :
113  ((size == 0) ? blk_size :
114  alignToPowerOfTwo(std::floor(blk_size / (double) size)));
115  }
116  };
117  struct ResultsComparator
118  {
119  bool
120  operator()(const std::shared_ptr<Results>& lhs,
121  const std::shared_ptr<Results>& rhs) const
122  {
123  const std::size_t lhs_cf = lhs->compressionFactor;
124  const std::size_t rhs_cf = rhs->compressionFactor;
125 
126  if (lhs_cf == rhs_cf) {
127  // When they have similar compressed sizes, give the one
128  // with fastest decompression privilege
129  return lhs->decompLat > rhs->decompLat;
130  }
131  return lhs_cf < rhs_cf;
132  }
133  };
134 
135  // Each sub-compressor can have its own chunk size; therefore, revert
136  // the chunks to raw data, so that they handle the conversion internally
137  uint64_t data[blkSize / sizeof(uint64_t)];
138  std::memset(data, 0, blkSize);
139  fromChunks(chunks, data);
140 
141  // Find the ranking of the compressor outputs
142  std::priority_queue<std::shared_ptr<Results>,
143  std::vector<std::shared_ptr<Results>>, ResultsComparator> results;
144  Cycles max_comp_lat;
145  for (unsigned i = 0; i < compressors.size(); i++) {
146  Cycles temp_decomp_lat;
147  auto temp_comp_data =
148  compressors[i]->compress(data, comp_lat, temp_decomp_lat);
149  temp_comp_data->setSizeBits(temp_comp_data->getSizeBits() +
151  results.push(std::make_shared<Results>(i, std::move(temp_comp_data),
152  temp_decomp_lat, blkSize));
153  max_comp_lat = std::max(max_comp_lat, comp_lat);
154  }
155 
156  // Assign best compressor to compression data
157  const unsigned best_index = results.top()->index;
158  std::unique_ptr<CompressionData> multi_comp_data =
159  std::unique_ptr<MultiCompData>(
160  new MultiCompData(best_index, std::move(results.top()->compData)));
161  DPRINTF(CacheComp, "Best compressor: %d\n", best_index);
162 
163  // Set decompression latency of the best compressor
164  decomp_lat = results.top()->decompLat + decompExtraLatency;
165 
166  // Update compressor ranking stats
167  for (int rank = 0; rank < compressors.size(); rank++) {
168  multiStats.ranks[results.top()->index][rank]++;
169  results.pop();
170  }
171 
172  // Set compression latency (compression latency of the slowest compressor
173  // and 1 cycle to pack)
174  comp_lat = Cycles(max_comp_lat + compExtraLatency);
175 
176  return multi_comp_data;
177 }
178 
179 void
181  uint64_t* cache_line)
182 {
183  const MultiCompData* casted_comp_data =
184  static_cast<const MultiCompData*>(comp_data);
185  compressors[casted_comp_data->getIndex()]->decompress(
186  casted_comp_data->compData.get(), cache_line);
187 }
188 
190  : Stats::Group(&base_group), compressor(_compressor),
191  ADD_STAT(ranks, UNIT_COUNT,
192  "Number of times each compressor had the nth best compression")
193 {
194 }
195 
196 void
198 {
200 
201  const std::size_t num_compressors = compressor.compressors.size();
202  ranks.init(num_compressors, num_compressors);
203  for (unsigned compressor = 0; compressor < num_compressors; compressor++) {
204  ranks.subname(compressor, std::to_string(compressor));
205  ranks.subdesc(compressor, "Number of times compressor " +
206  std::to_string(compressor) + " had the nth best compression.");
207  for (unsigned rank = 0; rank < num_compressors; rank++) {
208  ranks.ysubname(rank, std::to_string(rank));
209  }
210  }
211 }
212 
213 } // namespace Compressor
Stats::Group::regStats
virtual void regStats()
Callback to set stat parameters.
Definition: group.cc:63
Compressor::Base::decompExtraLatency
const Cycles decompExtraLatency
Extra latency added to decompression due to packaging, shifting or other operations.
Definition: base.hh:121
Compressor::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:86
data
const char data[]
Definition: circlebuf.test.cc:47
Compressor::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:76
MipsISA::index
Bitfield< 30, 0 > index
Definition: pra_constants.hh:44
ArmISA::i
Bitfield< 7 > i
Definition: miscregs_types.hh:63
Compressor::Multi::multiStats
Compressor::Multi::MultiStats multiStats
Compressor::Base::blkSize
const std::size_t blkSize
Uncompressed cache line size (in bytes).
Definition: base.hh:88
Compressor
Definition: base.cc:47
sc_dt::to_string
const std::string to_string(sc_enc enc)
Definition: sc_fxdefs.cc:91
Compressor::Multi::MultiCompData::MultiCompData
MultiCompData(unsigned index, std::unique_ptr< Base::CompressionData > comp_data)
Default constructor.
Definition: multi.cc:47
std::vector< Chunk >
Compressor::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:128
Compressor::Base
Base cache compressor interface.
Definition: base.hh:59
Compressor::Base::BaseStats
Definition: base.hh:126
Compressor::Multi::compressors
std::vector< Base * > compressors
List of sub-compressors.
Definition: multi.hh:56
multi.hh
Compressor::Multi::decompress
void decompress(const CompressionData *comp_data, uint64_t *data) override
Apply the decompression process to the compressed data.
Definition: multi.cc:180
Compressor::Multi
Definition: multi.hh:48
Compressor::Base::CompressionData
Definition: base.hh:240
bitfield.hh
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:237
ADD_STAT
#define ADD_STAT(n,...)
Convenience macro to add a stat to a statistics group.
Definition: group.hh:71
Compressor::Multi::MultiStats::MultiStats
MultiStats(BaseStats &base_group, Multi &_compressor)
Definition: multi.cc:189
Compressor::Base::Params
BaseCacheCompressorParams Params
Definition: base.hh:196
Compressor::Multi::MultiStats::regStats
void regStats() override
Callback to set stat parameters.
Definition: multi.cc:197
Compressor::Multi::MultiCompData::compData
std::unique_ptr< Base::CompressionData > compData
Compression data of the best compressor.
Definition: multi.hh:121
Compressor::Multi::MultiCompData
Definition: multi.hh:113
BaseCache
A basic cache interface.
Definition: base.hh:89
UNIT_COUNT
#define UNIT_COUNT
Definition: units.hh:49
Compressor::Multi::setCache
void setCache(BaseCache *_cache) override
The cache can only be set once.
Definition: multi.cc:77
alignToPowerOfTwo
constexpr uint64_t alignToPowerOfTwo(uint64_t val)
Align to the next highest power of two.
Definition: bitfield.hh:349
Compressor::Base::compExtraLatency
const Cycles compExtraLatency
Extra latency added to compression due to packaging, shifting or other operations.
Definition: base.hh:109
Compressor::Multi::~Multi
~Multi()
Definition: multi.cc:69
std
Overload hash function for BasicBlockRange type.
Definition: vec_reg.hh:587
logging.hh
Cycles
Cycles is a wrapper class for representing cycle counts, i.e.
Definition: types.hh:79
Compressor::Multi::MultiStats::ranks
Stats::Vector2d ranks
Number of times each compressor provided the nth best compression.
Definition: multi.hh:96
Stats
Definition: statistics.cc:53
Compressor::Base::stats
Compressor::Base::BaseStats stats
Compressor::Base::Multi
friend class Multi
This compressor must be able to access the protected functions of its sub-compressors.
Definition: base.hh:83
trace.hh
MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:323
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:219
Compressor::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:219
Compressor::Base::setCache
virtual void setCache(BaseCache *_cache)
The cache can only be set once.
Definition: base.cc:102
Compressor::Multi::MultiCompData::getIndex
uint8_t getIndex() const
Get the index of the best compressor.
Definition: multi.cc:55

Generated on Tue Jun 22 2021 15:28:29 for gem5 by doxygen 1.8.17