gem5  v19.0.0.0
dictionary_compressor_impl.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-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 
35 #ifndef __MEM_CACHE_COMPRESSORS_DICTIONARY_COMPRESSOR_IMPL_HH__
36 #define __MEM_CACHE_COMPRESSORS_DICTIONARY_COMPRESSOR_IMPL_HH__
37 
38 #include <algorithm>
39 
40 #include "debug/CacheComp.hh"
42 #include "params/BaseDictionaryCompressor.hh"
43 
44 template <class T>
46  : CompressionData()
47 {
48 }
49 
50 template <class T>
51 void
52 DictionaryCompressor<T>::CompData::addEntry(std::unique_ptr<Pattern> pattern)
53 {
54  // Increase size
55  setSizeBits(getSizeBits() + pattern->getSizeBits());
56 
57  // Push new entry to list
58  entries.push_back(std::move(pattern));
59 }
60 
61 template <class T>
64 {
65  dictionary.resize(dictionarySize);
66 
68 }
69 
70 template <class T>
71 void
73 {
74  // Reset number of valid entries
75  numEntries = 0;
76 
77  // Set all entries as 0
79 }
80 
81 template <typename T>
82 std::unique_ptr<typename DictionaryCompressor<T>::Pattern>
84 {
85  // Split data in bytes
86  const DictionaryEntry bytes = toDictionaryEntry(data);
87 
88  // Start as a no-match pattern. A negative match location is used so that
89  // patterns that depend on the dictionary entry don't match
90  std::unique_ptr<Pattern> pattern =
91  getPattern(bytes, toDictionaryEntry(0), -1);
92 
93  // Search for word on dictionary
94  for (std::size_t i = 0; i < numEntries; i++) {
95  // Try matching input with possible patterns
96  std::unique_ptr<Pattern> temp_pattern =
97  getPattern(bytes, dictionary[i], i);
98 
99  // Check if found pattern is better than previous
100  if (temp_pattern->getSizeBits() < pattern->getSizeBits()) {
101  pattern = std::move(temp_pattern);
102  }
103  }
104 
105  // Update stats
106  patternStats[pattern->getPatternNumber()]++;
107 
108  // Push into dictionary
109  if (pattern->shouldAllocate()) {
110  addToDictionary(bytes);
111  }
112 
113  return pattern;
114 }
115 
116 template <class T>
117 std::unique_ptr<BaseCacheCompressor::CompressionData>
119 {
120  std::unique_ptr<BaseCacheCompressor::CompressionData> comp_data =
121  std::unique_ptr<CompData>(new CompData());
122 
123  // Reset dictionary
124  resetDictionary();
125 
126  // Compress every value sequentially
127  CompData* const comp_data_ptr = static_cast<CompData*>(comp_data.get());
128  const std::vector<T> values((T*)data, (T*)data + blkSize / sizeof(T));
129  for (const auto& value : values) {
130  std::unique_ptr<Pattern> pattern = compressValue(value);
131  DPRINTF(CacheComp, "Compressed %016x to %s\n", value,
132  pattern->print());
133  comp_data_ptr->addEntry(std::move(pattern));
134  }
135 
136  // Return compressed line
137  return comp_data;
138 }
139 
140 template <class T>
141 T
143 {
144  // Search for matching entry
145  auto entry_it = dictionary.begin();
146  std::advance(entry_it, pattern->getMatchLocation());
147 
148  // Decompress the match. If the decompressed value must be added to
149  // the dictionary, do it
150  const DictionaryEntry data = pattern->decompress(*entry_it);
151  if (pattern->shouldAllocate()) {
152  addToDictionary(data);
153  }
154 
155  // Return value
156  return fromDictionaryEntry(data);
157 }
158 
159 template <class T>
160 void
162  uint64_t* data)
163 {
164  const CompData* casted_comp_data = static_cast<const CompData*>(comp_data);
165 
166  // Reset dictionary
167  resetDictionary();
168 
169  // Decompress every entry sequentially
170  std::vector<T> decomp_values;
171  for (const auto& entry : casted_comp_data->entries) {
172  const T value = decompressValue(&*entry);
173  decomp_values.push_back(value);
174  DPRINTF(CacheComp, "Decompressed %s to %x\n", entry->print(), value);
175  }
176 
177  // Concatenate the decompressed values to generate the original data
178  for (std::size_t i = 0; i < blkSize/8; i++) {
179  data[i] = 0;
180  const std::size_t values_per_entry = sizeof(uint64_t)/sizeof(T);
181  for (int j = values_per_entry - 1; j >= 0; j--) {
182  data[i] |=
183  static_cast<uint64_t>(decomp_values[values_per_entry*i+j]) <<
184  (j*8*sizeof(T));
185  }
186  }
187 }
188 
189 template <class T>
192 {
193  DictionaryEntry entry;
194  for (int i = 0; i < sizeof(T); i++) {
195  entry[i] = value & 0xFF;
196  value >>= 8;
197  }
198  return entry;
199 }
200 
201 template <class T>
202 T
204 {
205  T value = 0;
206  for (int i = sizeof(T) - 1; i >= 0; i--) {
207  value <<= 8;
208  value |= entry[i];
209  }
210  return value;
211 }
212 
213 #endif //__MEM_CACHE_COMPRESSORS_DICTIONARY_COMPRESSOR_IMPL_HH__
#define DPRINTF(x,...)
Definition: trace.hh:229
Bitfield< 7 > i
static DictionaryEntry toDictionaryEntry(T value)
Turn a value into a dictionary entry.
std::unique_ptr< Pattern > compressValue(const T data)
Compress data.
std::size_t numEntries
Number of valid entries in the dictionary.
void decompress(const CompressionData *comp_data, uint64_t *data) override
Decompress data.
STL vector class.
Definition: stl.hh:40
const std::size_t dictionarySize
Dictionary size.
virtual void resetDictionary()
Clear all dictionary entries.
static void setSizeBits(CacheBlk *blk, const std::size_t size_bits)
Set the size of the compressed block, in bits.
Definition: base.cc:151
T decompressValue(const Pattern *pattern)
Decompress a pattern into a value that fits in a dictionary entry.
const std::size_t blkSize
Uncompressed cache line size (in bytes).
Definition: base.hh:67
std::vector< std::unique_ptr< Pattern > > entries
The patterns matched in the original line.
static T fromDictionaryEntry(const DictionaryEntry &entry)
Turn a dictionary entry into a value.
virtual std::unique_ptr< Pattern > getPattern(const DictionaryEntry &bytes, const DictionaryEntry &dict_bytes, const int match_location) const =0
Since the factory cannot be instantiated here, classes that inherit from this base class have to impl...
std::array< uint8_t, sizeof(uint64_t)> DictionaryEntry
Convenience typedef for a dictionary entry.
Bitfield< 24 > j
std::vector< DictionaryEntry > dictionary
The dictionary.
Stats::Vector patternStats
Number of data entries that were compressed to each pattern.
Definition of a dictionary based cache compressor.
BaseCacheCompressorParams Params
Convenience typedef.
Definition: base.hh:130
std::unique_ptr< BaseCacheCompressor::CompressionData > compress(const uint64_t *data)
Apply compression.
Bitfield< 0 > p
const char data[]
virtual void addToDictionary(const DictionaryEntry data)=0
Add an entry to the dictionary.
virtual void addEntry(std::unique_ptr< Pattern >)
Add a pattern entry to the list of patterns.

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