-
Notifications
You must be signed in to change notification settings - Fork 0
/
hybrid.cc
executable file
·150 lines (130 loc) · 3.22 KB
/
hybrid.cc
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
#include "interface.hh"
#include "stdlib.h"
#include "stdio.h"
/*
* DISTANCE PREFETCHER: Hybrid version
* */
#define GHB_SIZE 1024
#define AIT_SIZE 1024
#define DEGREE 3
#define WIDTH 3
#define DEPTH 2
void ghb_add_entry(Addr addr);
int16_t ait_get_prev_ghb_entry(Addr addr, bool sign);
int16_t ghb_get_prev_entry(Addr addr);
struct ghb_entry {
Addr addr;
bool valid;
int16_t prev;
};
struct ait_entry {
Addr delta;
bool sign;
bool valid;
int16_t entry;
};
ghb_entry ghb[GHB_SIZE];
int16_t ghb_head = -1;
ait_entry ait[AIT_SIZE];
void prefetch_init(void) {
for (uint16_t i = 0; i < GHB_SIZE; i++) {
ghb[i].valid = false;
ghb[i].prev = -1;
}
for (uint16_t i = 0; i < AIT_SIZE; i++) ait[i].valid = false;
}
int16_t ait_get_prev_ghb_entry(Addr delta, bool sign) {
uint16_t hash = delta % AIT_SIZE;
ait_entry* bucket = &ait[hash];
uint16_t b_delta = bucket->delta;
int16_t b_entry = bucket->entry;
bool b_valid = bucket->valid;
bool b_sign = bucket->sign;
bucket->delta = delta;
bucket->sign = sign;
bucket->entry = ghb_head;
bucket->valid = true;
if (b_valid && ghb[b_entry].valid && b_delta == delta && b_sign == sign)
return b_entry;
return -1;
}
void ghb_add_entry(Addr addr) {
int16_t tmp_head = ghb_head;
ghb_head = (ghb_head + 1) % GHB_SIZE;
ghb_entry* entry = &ghb[ghb_head];
entry->valid = false;
// Find prev entry
int16_t prev;
if (tmp_head != -1) {
Addr delta;
Addr tmp_addr = ghb[tmp_head].addr;
bool sign;
if (tmp_addr > addr) {
delta = tmp_addr - addr;
sign = 0;
} else {
delta = addr - tmp_addr;
sign = 1;
}
prev = ait_get_prev_ghb_entry(delta, sign);
} else {
prev = -1;
}
entry->prev = prev;
entry->addr = addr;
entry->valid = true;
}
void prefetch_access(AccessStat stat) {
/*
* Traverse the linked list of previous occurences for the current candidate and fetch
* prefetch candidates up to the prefetch depth
* */
Addr addr = stat.mem_addr;
if (stat.miss) {
ghb_add_entry(addr);
int16_t prev = ghb[ghb_head].prev;
if (prev == -1) {
Addr tmp_addr = addr;
uint8_t strided = 0;
while (strided++ < DEGREE) {
tmp_addr += BLOCK_SIZE;
if (!in_cache(tmp_addr))
issue_prefetch(tmp_addr);
}
return;
}
uint8_t width = 0;
uint8_t depth = 0;
Addr prev_addr;
Addr cand_addr;
Addr delta;
bool sign;
int16_t cand;
while (prev != -1 && width++ < WIDTH) {
prev_addr = ghb[prev].addr;
cand = (prev + 1) % GHB_SIZE;
while (cand != ghb_head && depth++ < DEPTH) {
cand_addr = ghb[cand].addr;
cand = (cand + 1) % GHB_SIZE;
prev = ghb[prev].prev;
if (prev_addr > cand_addr) {
delta = prev_addr - cand_addr;
sign = 0;
} else {
delta = cand_addr - prev_addr;
sign = 1;
}
if (sign && addr + delta < MAX_PHYS_MEM_ADDR)
addr += delta;
else if (!sign && addr > delta)
addr -= delta;
// If delta causes underflow or overflow, do not prefetch
else continue;
if (!in_cache(addr))
issue_prefetch(addr);
}
}
}
}
void prefetch_complete(Addr addr) {
}