gem5  v20.0.0.2
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 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/trace.hh"
41 #include "debug/CacheComp.hh"
42 #include "params/MultiCompressor.hh"
43 
45  std::unique_ptr<BaseCacheCompressor::CompressionData> comp_data)
46  : CompressionData(), index(index), compData(std::move(comp_data))
47 {
48  setSizeBits(compData->getSizeBits());
49 }
50 
51 uint8_t
53 {
54  return index;
55 }
56 
59 {
60  fatal_if(compressors.size() == 0, "There must be at least one compressor");
61 }
62 
64 {
65  for (auto& compressor : compressors) {
66  delete compressor;
67  }
68 }
69 
70 std::unique_ptr<BaseCacheCompressor::CompressionData>
71 MultiCompressor::compress(const uint64_t* cache_line, Cycles& comp_lat,
72  Cycles& decomp_lat)
73 {
74  struct Results
75  {
76  unsigned index;
77  std::unique_ptr<BaseCacheCompressor::CompressionData> compData;
78  Cycles decompLat;
79  uint8_t compressionFactor;
80 
81  Results(unsigned index,
82  std::unique_ptr<BaseCacheCompressor::CompressionData> comp_data,
83  Cycles decomp_lat, std::size_t blk_size)
84  : index(index), compData(std::move(comp_data)),
85  decompLat(decomp_lat)
86  {
87  const std::size_t size = compData->getSize();
88  // If the compressed size is worse than the uncompressed size,
89  // we assume the size is the uncompressed size, and thus the
90  // compression factor is 1
91  compressionFactor = (size > blk_size) ? 1 :
92  alignToPowerOfTwo(std::floor(blk_size / (double) size));
93  }
94  };
95  struct ResultsComparator
96  {
97  bool
98  operator()(const std::shared_ptr<Results>& lhs,
99  const std::shared_ptr<Results>& rhs) const
100  {
101  const std::size_t lhs_cf = lhs->compressionFactor;
102  const std::size_t rhs_cf = rhs->compressionFactor;
103 
104  if (lhs_cf == rhs_cf) {
105  // When they have similar compressed sizes, give the one
106  // with fastest decompression privilege
107  return lhs->decompLat > rhs->decompLat;
108  }
109  return lhs_cf < rhs_cf;
110  }
111  };
112 
113  // Find the ranking of the compressor outputs
114  std::priority_queue<std::shared_ptr<Results>,
115  std::vector<std::shared_ptr<Results>>, ResultsComparator> results;
116  Cycles max_comp_lat;
117  for (unsigned i = 0; i < compressors.size(); i++) {
118  Cycles temp_decomp_lat;
119  auto temp_comp_data =
120  compressors[i]->compress(cache_line, comp_lat, temp_decomp_lat);
121  results.push(std::make_shared<Results>(i, std::move(temp_comp_data),
122  temp_decomp_lat, blkSize));
123  max_comp_lat = std::max(max_comp_lat, comp_lat);
124  }
125 
126  // Assign best compressor to compression data
127  const unsigned best_index = results.top()->index;
128  std::unique_ptr<CompressionData> multi_comp_data =
129  std::unique_ptr<MultiCompData>(
130  new MultiCompData(best_index, std::move(results.top()->compData)));
131  DPRINTF(CacheComp, "Best compressor: %d\n", best_index);
132 
133  // Set decompression latency of the best compressor
134  decomp_lat = results.top()->decompLat;
135 
136  // Update compressor ranking stats
137  for (int rank = 0; rank < compressors.size(); rank++) {
138  rankStats[results.top()->index][rank]++;
139  results.pop();
140  }
141 
142  // Set compression latency (compression latency of the slowest compressor
143  // and 1 cycle to pack)
144  comp_lat = Cycles(max_comp_lat + 1);
145 
146  return multi_comp_data;
147 }
148 
149 void
151  uint64_t* cache_line)
152 {
153  const MultiCompData* casted_comp_data =
154  static_cast<const MultiCompData*>(comp_data);
155  compressors[casted_comp_data->getIndex()]->decompress(
156  casted_comp_data->compData.get(), cache_line);
157 }
158 
159 void
161 {
163 
164  rankStats
165  .init(compressors.size(), compressors.size())
166  .name(name() + ".rank")
167  .desc("Number of times each compressor had the nth best compression.")
168  ;
169 
170  for (int compressor = 0; compressor < compressors.size(); compressor++) {
171  rankStats.subname(compressor, std::to_string(compressor));
172  rankStats.subdesc(compressor, "Number of times compressor " +
173  std::to_string(compressor) + " had the nth best compression.");
174  for (unsigned rank = 0; rank < compressors.size(); rank++) {
175  rankStats.ysubname(rank, std::to_string(rank));
176  }
177  }
178 }
179 
181 MultiCompressorParams::create()
182 {
183  return new MultiCompressor(this);
184 }
#define DPRINTF(x,...)
Definition: trace.hh:222
uint64_t alignToPowerOfTwo(uint64_t val)
Align to the next highest power of two.
Definition: bitfield.hh:277
Definition of the a multi compressor that choses the best compression among multiple compressors...
Bitfield< 30, 0 > index
Derived & init(size_type _x, size_type _y)
Definition: statistics.hh:1285
Derived & subname(off_type index, const std::string &name)
Set the subfield name for the given index, and marks this stat to print at the end of simulation...
Definition: statistics.hh:376
Cycles is a wrapper class for representing cycle counts, i.e.
Definition: types.hh:81
Bitfield< 7 > i
void decompress(const CompressionData *comp_data, uint64_t *data) override
Apply the decompression process to the compressed data.
Definition: multi.cc:150
const uint8_t index
Index of the compressor that provided these compression results.
Definition: multi.hh:92
Derived & subdesc(off_type index, const std::string &desc)
Set the subfield description for the given index and marks this stat to print at the end of simulatio...
Definition: statistics.hh:400
Overload hash function for BasicBlockRange type.
Definition: vec_reg.hh:587
std::vector< BaseCacheCompressor * > compressors
List of sub-compressors.
Definition: multi.hh:53
std::unique_ptr< BaseCacheCompressor::CompressionData > compData
Compression data of the best compressor.
Definition: multi.hh:96
STL vector class.
Definition: stl.hh:37
MultiCompData(unsigned index, std::unique_ptr< BaseCacheCompressor::CompressionData > comp_data)
Default constructor.
Definition: multi.cc:44
std::unique_ptr< BaseCacheCompressor::CompressionData > compress(const uint64_t *data, Cycles &comp_lat, Cycles &decomp_lat) override
Apply the compression process to the cache line.
Definition: multi.cc:71
Stats::Vector2d rankStats
Number of times each compressor provided the nth best compression.
Definition: multi.hh:64
const std::size_t blkSize
Uncompressed cache line size (in bytes).
Definition: base.hh:65
MultiCompressor(const Params *p)
Default constructor.
Definition: multi.cc:57
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:199
uint8_t getIndex() const
Get the index of the best compressor.
Definition: multi.cc:52
Base cache compressor interface.
Definition: base.hh:52
virtual const std::string name() const
Definition: sim_object.hh:128
BaseCacheCompressorParams Params
Convenience typedef.
Definition: base.hh:128
Derived & ysubname(off_type index, const std::string &subname)
Definition: statistics.hh:463
void regStats() override
Callback to set stat parameters.
Definition: multi.cc:160
~MultiCompressor()
Default destructor.
Definition: multi.cc:63
virtual void regStats()
Callback to set stat parameters.
Definition: group.cc:64
void setSizeBits(std::size_t size)
Set compression size (in bits).
Definition: base.cc:58
Bitfield< 0 > p
const std::string to_string(sc_enc enc)
Definition: sc_fxdefs.cc:60

Generated on Mon Jun 8 2020 15:45:11 for gem5 by doxygen 1.8.13