-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLRUCache_Prefetch.h
More file actions
231 lines (209 loc) · 5.03 KB
/
LRUCache_Prefetch.h
File metadata and controls
231 lines (209 loc) · 5.03 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <iostream>
#include<stdint.h>
#include <unordered_map>
#include <vector>
using namespace std;
vector<int64_t>List_offset;
struct AIOReadInfo
{
int64_t readlength;
int64_t readoffset;
int64_t listlength;
int64_t offsetForenums;
int64_t memoffset;
int64_t curSendpos;
uint8_t *list_data;
uint32_t termid;
};
vector<int64_t>curReadpos;
vector<int64_t>usedFreq;
const uint64_t DISK_BLOCK = 4096;
const int64_t READ_BLOCK = 64 * 1024;
struct Node{
AIOReadInfo aiodata;
Node*prev, *next;
};
int64_t CACHE_SIZE = 1024 * 1024;
class LRUCache{
public:
LRUCache();
~LRUCache();
Node* Put(unsigned key);
Node* Get(unsigned key, bool& flag);
Node* Put_Prefetch(unsigned key);
Node* Get_Prefetch(unsigned key, bool& flag);
void print();
uint64_t hit_size;
uint64_t miss_size;
uint64_t hit_count;
uint64_t miss_count;
void attach(Node *node);
void detach(Node *node);
AIOReadInfo calAioreadinfo(unsigned term);
unordered_map<unsigned, Node*>hashmap_;
Node*head_, *tail_;
int64_t sumBytes;
};
LRUCache::LRUCache()
{
miss_size = 0; hit_size = 0;
miss_count = 0; hit_count = 0;
head_ = new Node;
tail_ = new Node;
head_->prev = NULL;
head_->next = tail_;
tail_->prev = head_;
tail_->next = NULL;
sumBytes = 0;
}
LRUCache::~LRUCache()
{
delete head_;
delete tail_;
}
AIOReadInfo LRUCache::calAioreadinfo(unsigned term)
{
AIOReadInfo tmpaio;
tmpaio.termid = term;
int64_t listlength = List_offset[term + 1] - List_offset[term];
tmpaio.listlength = listlength;
tmpaio.memoffset = 0;
int64_t offset = List_offset[term];
tmpaio.readoffset = ((int64_t)(offset / DISK_BLOCK))*DISK_BLOCK;
tmpaio.offsetForenums = offset - tmpaio.readoffset;
int64_t readlength = ((int64_t)(ceil((double)(listlength + tmpaio.offsetForenums) / READ_BLOCK)))*READ_BLOCK;
tmpaio.readlength = readlength;
tmpaio.curSendpos = -tmpaio.offsetForenums;
curReadpos[term] = -tmpaio.offsetForenums;
#pragma omp flush(curReadpos)
miss_size += tmpaio.listlength;
return tmpaio;
}
Node* LRUCache::Put(unsigned key)
{
AIOReadInfo tmpaio = calAioreadinfo(key);
Node *node;
if (tmpaio.readlength> CACHE_SIZE)
{
cout << "That block overflow!!" << endl;
return NULL;
}
node = tail_->prev;
while (sumBytes + tmpaio.readlength>CACHE_SIZE)
{
if (node == head_){ node = tail_->prev; }
#pragma omp flush(usedFreq)
if (usedFreq[node->aiodata.termid] > 0){ node = node->prev; continue; }
detach(node);
free(node->aiodata.list_data);
curReadpos[node->aiodata.termid] = node->aiodata.offsetForenums;
sumBytes -= node->aiodata.readlength;
hashmap_.erase(node->aiodata.termid);
Node *tmp = node->prev;
delete node;
node = tmp;
}
node = new Node();
posix_memalign((void**)&tmpaio.list_data, DISK_BLOCK, tmpaio.readlength);
node->aiodata = tmpaio;
sumBytes += tmpaio.readlength;
attach(node);
hashmap_[key] = node;
return node;
}
Node* LRUCache::Put_Prefetch(unsigned key)
{
AIOReadInfo tmpaio = calAioreadinfo(key);
Node *node;
if (tmpaio.readlength> CACHE_SIZE)
{
cout << "That block overflow!!" << endl;
return NULL;
}
node = tail_->prev;
while (sumBytes + tmpaio.readlength>CACHE_SIZE&&node != head_)
{
#pragma omp flush(usedFreq)
if (usedFreq[node->aiodata.termid] > 0){ node = node->prev; continue; }
detach(node);
free(node->aiodata.list_data);
curReadpos[node->aiodata.termid] = node->aiodata.offsetForenums;
sumBytes -= node->aiodata.readlength;
hashmap_.erase(node->aiodata.termid);
Node *tmp = node->prev;
delete node;
node = tmp;
}
if (node == head_)
{
return NULL;
}
node = new Node();
posix_memalign((void**)&tmpaio.list_data, DISK_BLOCK, tmpaio.readlength);
node->aiodata = tmpaio;
sumBytes += tmpaio.readlength;
attach(node);
hashmap_[key] = node;
return node;
}
Node* LRUCache::Get(unsigned key, bool &flag)
{
Node *node;
unordered_map<unsigned, Node* >::iterator it = hashmap_.find(key);
if (it != hashmap_.end())
{
node = it->second;
flag = true;
hit_count++;
detach(node);
attach(node);
}
else
{
flag = false;
miss_count++;
node = Put(key);
}
return node;
}
Node* LRUCache::Get_Prefetch(unsigned key, bool &flag)
{
Node *node;
unordered_map<unsigned, Node* >::iterator it = hashmap_.find(key);
if (it != hashmap_.end())
{
node = it->second;
flag = true;
detach(node);
attach(node);
}
else
{
flag = false;
miss_count++;
node = Put_Prefetch(key);
}
return node;
}
void LRUCache::attach(Node *node)
{
node->next = head_->next;
head_->next = node;
node->next->prev = node;
node->prev = head_;
}
void LRUCache::detach(Node *node)
{
node->prev->next = node->next;
node->next->prev = node->prev;
}
void LRUCache::print()
{
unordered_map<unsigned, Node* >::iterator iter;
int64_t mysumsize = 0;
for (iter = hashmap_.begin(); iter != hashmap_.end(); iter++)
{
mysumsize += iter->second->aiodata.listlength;
}
cout << "sumsize=" << mysumsize << endl;
}