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

Generated on Wed Dec 21 2022 10:22:36 for gem5 by doxygen 1.9.1