gem5  v20.1.0.0
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  extraDecompressionLatency(p->extra_decomp_lat),
65  multiStats(stats, *this)
66 {
67  fatal_if(compressors.size() == 0, "There must be at least one compressor");
68 }
69 
71 {
72  for (auto& compressor : compressors) {
73  delete compressor;
74  }
75 }
76 
77 std::unique_ptr<Base::CompressionData>
78 Multi::compress(const std::vector<Chunk>& chunks, Cycles& comp_lat,
79  Cycles& decomp_lat)
80 {
81  struct Results
82  {
83  unsigned index;
84  std::unique_ptr<Base::CompressionData> compData;
85  Cycles decompLat;
86  uint8_t compressionFactor;
87 
88  Results(unsigned index,
89  std::unique_ptr<Base::CompressionData> comp_data,
90  Cycles decomp_lat, std::size_t blk_size)
91  : index(index), compData(std::move(comp_data)),
92  decompLat(decomp_lat)
93  {
94  const std::size_t size = compData->getSize();
95  // If the compressed size is worse than the uncompressed size,
96  // we assume the size is the uncompressed size, and thus the
97  // compression factor is 1.
98  //
99  // Some compressors (notably the zero compressor) may rely on
100  // extra information being stored in the tags, or added in
101  // another compression layer. Their size can be 0, so it is
102  // assigned the highest possible compression factor (the original
103  // block's size).
104  compressionFactor = (size > blk_size) ? 1 :
105  ((size == 0) ? blk_size :
106  alignToPowerOfTwo(std::floor(blk_size / (double) size)));
107  }
108  };
109  struct ResultsComparator
110  {
111  bool
112  operator()(const std::shared_ptr<Results>& lhs,
113  const std::shared_ptr<Results>& rhs) const
114  {
115  const std::size_t lhs_cf = lhs->compressionFactor;
116  const std::size_t rhs_cf = rhs->compressionFactor;
117 
118  if (lhs_cf == rhs_cf) {
119  // When they have similar compressed sizes, give the one
120  // with fastest decompression privilege
121  return lhs->decompLat > rhs->decompLat;
122  }
123  return lhs_cf < rhs_cf;
124  }
125  };
126 
127  // Each sub-compressor can have its own chunk size; therefore, revert
128  // the chunks to raw data, so that they handle the conversion internally
129  uint64_t data[blkSize / sizeof(uint64_t)];
130  std::memset(data, 0, blkSize);
131  fromChunks(chunks, data);
132 
133  // Find the ranking of the compressor outputs
134  std::priority_queue<std::shared_ptr<Results>,
135  std::vector<std::shared_ptr<Results>>, ResultsComparator> results;
136  Cycles max_comp_lat;
137  for (unsigned i = 0; i < compressors.size(); i++) {
138  Cycles temp_decomp_lat;
139  auto temp_comp_data =
140  compressors[i]->compress(data, comp_lat, temp_decomp_lat);
141  temp_comp_data->setSizeBits(temp_comp_data->getSizeBits() +
143  results.push(std::make_shared<Results>(i, std::move(temp_comp_data),
144  temp_decomp_lat, blkSize));
145  max_comp_lat = std::max(max_comp_lat, comp_lat);
146  }
147 
148  // Assign best compressor to compression data
149  const unsigned best_index = results.top()->index;
150  std::unique_ptr<CompressionData> multi_comp_data =
151  std::unique_ptr<MultiCompData>(
152  new MultiCompData(best_index, std::move(results.top()->compData)));
153  DPRINTF(CacheComp, "Best compressor: %d\n", best_index);
154 
155  // Set decompression latency of the best compressor
156  decomp_lat = results.top()->decompLat + extraDecompressionLatency;
157 
158  // Update compressor ranking stats
159  for (int rank = 0; rank < compressors.size(); rank++) {
160  multiStats.ranks[results.top()->index][rank]++;
161  results.pop();
162  }
163 
164  // Set compression latency (compression latency of the slowest compressor
165  // and 1 cycle to pack)
166  comp_lat = Cycles(max_comp_lat + 1);
167 
168  return multi_comp_data;
169 }
170 
171 void
173  uint64_t* cache_line)
174 {
175  const MultiCompData* casted_comp_data =
176  static_cast<const MultiCompData*>(comp_data);
177  compressors[casted_comp_data->getIndex()]->decompress(
178  casted_comp_data->compData.get(), cache_line);
179 }
180 
182  : Stats::Group(&base_group), compressor(_compressor),
183  ranks(this, "ranks",
184  "Number of times each compressor had the nth best compression")
185 {
186 }
187 
188 void
190 {
192 
193  const std::size_t num_compressors = compressor.compressors.size();
194  ranks.init(num_compressors, num_compressors);
195  for (unsigned compressor = 0; compressor < num_compressors; compressor++) {
196  ranks.subname(compressor, std::to_string(compressor));
197  ranks.subdesc(compressor, "Number of times compressor " +
198  std::to_string(compressor) + " had the nth best compression.");
199  for (unsigned rank = 0; rank < num_compressors; rank++) {
200  ranks.ysubname(rank, std::to_string(rank));
201  }
202  }
203 }
204 
205 } // namespace Compressor
206 
208 MultiCompressorParams::create()
209 {
210  return new Compressor::Multi(this);
211 }
Compressor::Multi::extraDecompressionLatency
const Cycles extraDecompressionLatency
Extra decompression latency to be added to the sub-compressor's decompression latency.
Definition: multi.hh:83
Stats::Group::regStats
virtual void regStats()
Callback to set stat parameters.
Definition: group.cc:64
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:78
data
const char data[]
Definition: circlebuf.test.cc:42
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
alignToPowerOfTwo
uint64_t alignToPowerOfTwo(uint64_t val)
Align to the next highest power of two.
Definition: bitfield.hh:316
Compressor::Multi::multiStats
Compressor::Multi::MultiStats multiStats
Compressor::Base::blkSize
const std::size_t blkSize
Uncompressed cache line size (in bytes).
Definition: base.hh:87
Compressor
Definition: base.cc:46
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:109
Compressor::Base
Base cache compressor interface.
Definition: base.hh:58
Compressor::Base::BaseStats
Definition: base.hh:98
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:172
Compressor::Multi
Definition: multi.hh:48
Compressor::Base::CompressionData
Definition: base.hh:209
bitfield.hh
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:234
Compressor::Multi::MultiStats::MultiStats
MultiStats(BaseStats &base_group, Multi &_compressor)
Definition: multi.cc:181
Compressor::Base::Params
BaseCacheCompressorParams Params
Definition: base.hh:168
Compressor::Multi::MultiStats::regStats
void regStats() override
Callback to set stat parameters.
Definition: multi.cc:189
Compressor::Multi::MultiCompData::compData
std::unique_ptr< Base::CompressionData > compData
Compression data of the best compressor.
Definition: multi.hh:119
Compressor::Multi::MultiCompData
Definition: multi.hh:111
Compressor::Multi::~Multi
~Multi()
Definition: multi.cc:70
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:83
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:61
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:82
trace.hh
Compressor::Base::CompressionData::setSizeBits
void setSizeBits(std::size_t size)
Set compression size (in bits).
Definition: base.cc:61
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::Multi::MultiCompData::getIndex
uint8_t getIndex() const
Get the index of the best compressor.
Definition: multi.cc:55

Generated on Wed Sep 30 2020 14:02:12 for gem5 by doxygen 1.8.17