-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.h
185 lines (153 loc) · 4.21 KB
/
cache.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#pragma once
#include "bucket.h"
#include "config.h"
#include "mcslock.h"
#include <algorithm>
#include <list>
#include <unordered_map>
#include <vector>
#include <cstdint>
#include <cstdio>
typedef uint Addr;
// make sure that memblocks are powers of two
static constexpr bool is_pow2(int a) { return !(a & (a - 1)); }
static_assert(is_pow2(MEMBLOCKLEN), "is_pow2(MEMBLOCKLEN)");
static_assert(is_pow2(CACHE_LINESIZE), "is_pow2(MEMBLOCKLEN)");
// find first bit set
static constexpr int ffs_constexpr(int x)
{
int n = 0;
while ((x & 1) == 0) {
x >>= 1;
n++;
}
return n;
}
static_assert(ffs_constexpr(256) == 8, "ffs_constexpr(256) == 8"); // sanity check
static constexpr int first_set = ffs_constexpr(MEMBLOCKLEN);
// gets the cache line number of an index
template <typename T, size_t CLSIZE>
uint cline(uint64_t idx)
{
constexpr static auto fs = ffs_constexpr(CLSIZE / sizeof(T));
return idx >> fs;
}
struct alignas(CACHE_LINESIZE) AddrWrapper {
Addr addr = 0;
};
static_assert(sizeof(AddrWrapper) == CACHE_LINESIZE, "sizeof(AddrWrapper) != CACHE_LINESIZE");
class Cache
{
public:
void handle_cline(Addr addr)
{
if (addr == last_) {
incr_access(0);
return;
}
last_ = addr;
auto map_it = refmap_.find(addr);
if (map_it == refmap_.end()) {
refmap_[addr] = on_block_new(MemoryBlock{});
incr_access_inf();
} else {
int bucket = on_block_seen(map_it->second);
incr_access(bucket);
}
}
StackIterator on_block_new(MemoryBlock &&mb);
int on_block_seen(StackIterator &it);
void incr_access(uint bucket) { buckets_[bucket].access_count++; }
// count access with infinite reuse distance
void incr_access_inf() { incr_access(buckets_.size() - 1); }
const std::vector<Bucket> &buckets() const { return buckets_; }
void print_csv(FILE *file, const auto &matrix, int id) const
{
size_t i = 0u;
for (auto &b : buckets_) {
// matrix name, nnz, nrow, cache id, shared, min bucket, count
fprintf(file,
"%s,%zu,%zu,%d,%d,%lu,%lu\n",
matrix.name,
matrix.nnz,
matrix.nrow,
id,
shared_,
Bucket::min_dists[i],
b.access_count);
++i;
}
}
void reset_buckets()
{
for (auto &b : buckets_)
b.access_count = 0;
}
private:
void move_markers(uint);
void on_next_bucket_gets_active();
void check_consistency();
std::list<MemoryBlock> stack_{};
std::unordered_map<Addr, StackIterator> refmap_{};
Addr last_{(Addr)-1};
uint next_bucket_{1u}; //
std::vector<Bucket> buckets_{std::vector<Bucket>{Bucket::min_dists.size()}}; //
protected:
bool shared_{false};
};
class SharedCache : public Cache
{
public:
SharedCache() { shared_ = true; }
void handle_cline_shared(int tid, Addr a)
{
mcslock_.lock(tid);
handle_cline(a);
mcslock_.unlock(tid);
}
void handle_clines_shared(int tid, Addr a0, Addr a1)
{
mcslock_.lock(tid);
handle_cline(a0);
handle_cline(a1);
mcslock_.unlock(tid);
}
void handle_clines_shared(int tid, Addr a0, Addr a1, Addr a2)
{
mcslock_.lock(tid);
handle_cline(a0);
handle_cline(a1);
handle_cline(a2);
mcslock_.unlock(tid);
}
#if 0
template<typename... As>
void handle_clines_shared(int tid, As... addrs)
{
mcslock_.lock(tid);
handle_clines_shared(addrs...);
mcslock_.unlock(tid);
}
template<typename... As>
void handle_clines_shared(Addr a, As... addrs)
{
handle_cline(a);
handle_clines_shared(addrs...);
}
void handle_clines_shared(Addr a)
{
handle_cline(a);
}
#endif
void reset_buckets_shared(int tid)
{
mcslock_.lock(tid);
reset_buckets();
mcslock_.unlock(tid);
}
private:
MCSLock mcslock_{};
};
class PrivateCache : public Cache
{
};