gem5  v20.0.0.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 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 "debug/CacheComp.hh"
41 #include "params/MultiCompressor.hh"
42 
44  std::unique_ptr<BaseCacheCompressor::CompressionData> comp_data)
45  : CompressionData(), index(index), compData(std::move(comp_data))
46 {
47  setSizeBits(compData->getSizeBits());
48 }
49 
50 uint8_t
52 {
53  return index;
54 }
55 
58 {
59  fatal_if(compressors.size() == 0, "There must be at least one compressor");
60 }
61 
63 {
64  for (auto& compressor : compressors) {
65  delete compressor;
66  }
67 }
68 
69 std::unique_ptr<BaseCacheCompressor::CompressionData>
70 MultiCompressor::compress(const uint64_t* cache_line, Cycles& comp_lat,
71  Cycles& decomp_lat)
72 {
73  struct Results
74  {
75  unsigned index;
76  std::unique_ptr<BaseCacheCompressor::CompressionData> compData;
77  Cycles decompLat;
78  uint8_t compressionFactor;
79 
80  Results(unsigned index,
81  std::unique_ptr<BaseCacheCompressor::CompressionData> comp_data,
82  Cycles decomp_lat, std::size_t blk_size)
83  : index(index), compData(std::move(comp_data)),
84  decompLat(decomp_lat)
85  {
86  const std::size_t size = compData->getSize();
87  // If the compressed size is worse than the uncompressed size,
88  // we assume the size is the uncompressed size, and thus the
89  // compression factor is 1
90  compressionFactor = (size > blk_size) ? 1 :
91  alignToPowerOfTwo(std::floor(blk_size / (double) size));
92  }
93  };
94  struct ResultsComparator
95  {
96  bool
97  operator()(const std::shared_ptr<Results>& lhs,
98  const std::shared_ptr<Results>& rhs) const
99  {
100  const std::size_t lhs_cf = lhs->compressionFactor;
101  const std::size_t rhs_cf = rhs->compressionFactor;
102 
103  if (lhs_cf == rhs_cf) {
104  // When they have similar compressed sizes, give the one
105  // with fastest decompression privilege
106  return lhs->decompLat > rhs->decompLat;
107  }
108  return lhs_cf < rhs_cf;
109  }
110  };
111 
112  // Find the ranking of the compressor outputs
113  std::priority_queue<std::shared_ptr<Results>,
114  std::vector<std::shared_ptr<Results>>, ResultsComparator> results;
115  Cycles max_comp_lat;
116  for (unsigned i = 0; i < compressors.size(); i++) {
117  Cycles temp_decomp_lat;
118  auto temp_comp_data =
119  compressors[i]->compress(cache_line, comp_lat, temp_decomp_lat);
120  results.push(std::make_shared<Results>(i, std::move(temp_comp_data),
121  temp_decomp_lat, blkSize));
122  max_comp_lat = std::max(max_comp_lat, comp_lat);
123  }
124 
125  // Assign best compressor to compression data
126  const unsigned best_index = results.top()->index;
127  std::unique_ptr<CompressionData> multi_comp_data =
128  std::unique_ptr<MultiCompData>(
129  new MultiCompData(best_index, std::move(results.top()->compData)));
130  DPRINTF(CacheComp, "Best compressor: %d\n", best_index);
131 
132  // Set decompression latency of the best compressor
133  decomp_lat = results.top()->decompLat;
134 
135  // Update compressor ranking stats
136  for (int rank = 0; rank < compressors.size(); rank++) {
137  rankStats[results.top()->index][rank]++;
138  results.pop();
139  }
140 
141  // Set compression latency (compression latency of the slowest compressor
142  // and 1 cycle to pack)
143  comp_lat = Cycles(max_comp_lat + 1);
144 
145  return multi_comp_data;
146 }
147 
148 void
150  uint64_t* cache_line)
151 {
152  const MultiCompData* casted_comp_data =
153  static_cast<const MultiCompData*>(comp_data);
154  compressors[casted_comp_data->getIndex()]->decompress(
155  casted_comp_data->compData.get(), cache_line);
156 }
157 
158 void
160 {
162 
163  rankStats
164  .init(compressors.size(), compressors.size())
165  .name(name() + ".rank")
166  .desc("Number of times each compressor had the nth best compression.")
167  ;
168 
169  for (int compressor = 0; compressor < compressors.size(); compressor++) {
170  rankStats.subname(compressor, std::to_string(compressor));
171  rankStats.subdesc(compressor, "Number of times compressor " +
172  std::to_string(compressor) + " had the nth best compression.");
173  for (unsigned rank = 0; rank < compressors.size(); rank++) {
174  rankStats.ysubname(rank, std::to_string(rank));
175  }
176  }
177 }
178 
180 MultiCompressorParams::create()
181 {
182  return new MultiCompressor(this);
183 }
#define DPRINTF(x,...)
Definition: trace.hh:225
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:149
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:43
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:70
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:56
#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:51
Base cache compressor interface.
Definition: base.hh:52
virtual const std::string name() const
Definition: sim_object.hh:129
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:159
~MultiCompressor()
Default destructor.
Definition: multi.cc:62
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:57
Bitfield< 0 > p
const std::string to_string(sc_enc enc)
Definition: sc_fxdefs.cc:60

Generated on Thu May 28 2020 16:21:33 for gem5 by doxygen 1.8.13