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

Generated on Fri Feb 28 2020 16:27:01 for gem5 by doxygen 1.8.13