-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGreedy_Reorder.cpp
More file actions
215 lines (210 loc) · 5.44 KB
/
Greedy_Reorder.cpp
File metadata and controls
215 lines (210 loc) · 5.44 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
//Greedy reordering for batch queries.
#include<iostream>
#include<vector>
#include<stdint.h>
#include<stdio.h>
#include<stdlib.h>
#include<fstream>
#include <sstream>
#include <string>
#include <algorithm>
#include<numeric>
#include<limits.h>
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include<string.h>
#include <omp.h>
#include<unordered_map>
#include<set>
#include"LRU_Cache_Batch.h"
using namespace std;
struct QueryInfo
{
unsigned oldqueryid;
int64_t score;
int64_t qlength;
};
vector<vector<unsigned>>queries;
vector<QueryInfo>queryinfo;
vector<unsigned>queryOrder;
LRUCache global_LRUCache;
unsigned QSIZE = 100000;
unsigned batchSize = 10000;
void read_query(string filename, unsigned turn)
{
queries.clear();
ifstream fin(filename);
string str = "";
unsigned startqid = turn*batchSize, endqid = (turn + 1)*batchSize;
unsigned qid = 0;
while (getline(fin, str) && qid<endqid)
{
if (qid >= startqid)
{
istringstream sin(str);
string field = "";
vector<unsigned> tmpq;
while (getline(sin, field, '\t'))
tmpq.push_back(atoi(field.c_str()));
queries.push_back(tmpq);
}
qid++;
}
}
void read_List_Length(string filename)
{
FILE *lengthfile = fopen((filename + ".list-l").c_str(), "rb");
unsigned tmplength = 0;
while (fread(&tmplength, sizeof(unsigned), 1, lengthfile))
{
listLength.push_back(tmplength);
}
fclose(lengthfile);
int64_t sumsize = 0;
for (auto i : listLength)
sumsize += i;
}
void LRUGet_Query(unsigned qid)
{
for (unsigned i = 0; i < queries[qid].size(); i++)
global_LRUCache.Get(queries[qid][i]);
}
int64_t LRUGet_Score(unsigned qid)
{
vector<unsigned>terms = queries[qid];
std::sort(terms.begin(), terms.end());
terms.erase(std::unique(terms.begin(), terms.end()), terms.end());
int64_t misssize = 0;;
for (unsigned i = 0; i < terms.size(); i++)
{
if (global_LRUCache.hashmap_.find(terms[i]) == global_LRUCache.hashmap_.end())
{
misssize += listLength[terms[i]];
}
}
return misssize;
}
bool cmpScore(QueryInfo const&a, QueryInfo const&b)
{
if ((a.score < b.score) || ((a.score == b.score) && (a.qlength < b.qlength)))
return true;
return false;
}
bool cmpqlength(QueryInfo const&a, QueryInfo const&b)
{
return a.qlength>b.qlength;
}
void greedyLongtail()
{
sort(queryinfo.begin(), queryinfo.end(), cmpqlength);
unsigned pos = 0.05*(double)queries.size();
for (unsigned t = 0; t < pos; t++)
{
for (unsigned i = 0; i<pos - t; i++)
{
queryinfo[i].score = LRUGet_Score(queryinfo[i].oldqueryid);
}
sort(queryinfo.begin(), queryinfo.begin() + pos - t, cmpScore);
unsigned qid = queryinfo[0].oldqueryid;
LRUGet_Query(qid);
queryOrder.push_back(qid);
queryinfo.erase(queryinfo.begin());
}
}
void greedyReorder()
{
greedyLongtail();
unsigned pos = queryinfo.size();
for (unsigned t = 0; t < pos; t++)
{
for (unsigned i = 0; i<queryinfo.size(); i++)
{
queryinfo[i].score = LRUGet_Score(queryinfo[i].oldqueryid);
}
sort(queryinfo.begin(), queryinfo.end(), cmpScore);
unsigned qid = queryinfo[0].oldqueryid;
LRUGet_Query(qid);
queryOrder.push_back(qid);
queryinfo.erase(queryinfo.begin());
}
cout << "After reorder queryOrdersize=" << queryOrder.size() << endl;
cout << "We eistimate the miss size=" << global_LRUCache.miss_size << endl;
}
void writeQuery(string filename)
{
ofstream fout(filename, ios::app);
for (unsigned i = 0; i < queryOrder.size(); i++)
{
unsigned qid = queryOrder[i];
for (unsigned j = 0; j < queries[qid].size() - 1; j++)
{
fout << queries[qid][j] << "\t";
}
fout << queries[qid][queries[qid].size() - 1] << endl;
}
fout.close();
}
void initData()
{
read_List_Length("");
}
int64_t cal_qlength(unsigned qid)
{
vector<unsigned>terms = queries[qid];
std::sort(terms.begin(), terms.end());
terms.erase(std::unique(terms.begin(), terms.end()), terms.end());
int64_t length = 0;
for (auto tid : terms)
length += listLength[tid];
return length;
}
bool cmpListLength(QueryInfo const&a, QueryInfo const&b)
{
return a.score > b.score;
}
void cacheWarmup()
{
vector<QueryInfo>listInfo(listLength.size());
for (unsigned i = 0; i < listLength.size(); i++)
{
listInfo[i].oldqueryid = i;
listInfo[i].score = listLength[i];
}
sort(listInfo.begin(), listInfo.end(), cmpListLength);
unsigned warmuplrucount = 0;
int64_t tmpsize = 0;
for (unsigned i = 0; i < listLength.size() && tmpsize<CACHE_SIZE; i++, warmuplrucount++)
tmpsize += listLength[listInfo[i].oldqueryid];
for (unsigned i = 0; i < warmuplrucount; i++)
global_LRUCache.Get(listInfo[i].oldqueryid);
}
void batchProcess()
{
unsigned turn = ceil((double)QSIZE / (double)batchSize);
global_LRUCache.cacheClear(); cacheWarmup();
for (unsigned i = 0; i < turn; i++)
{
cout << "***********************turn=" << i << "*****************************" << endl;
read_query("", i);
queryinfo.clear(); queryOrder.clear();
queryinfo.resize(queries.size());
for (unsigned i = 0; i < queryinfo.size(); i++)
{
queryinfo[i].oldqueryid = i;
queryinfo[i].qlength = cal_qlength(i);
}
greedyReorder();
writeQuery("QueryGreedy.txt");
}
}
int main(int argc, char *argv[])
{
CACHE_SIZE *= atoi(argv[1]);
batchSize = atoi(argv[2]);
cout << "Cache size=" << CACHE_SIZE << endl;
initData();
cout << "Initdata over" << endl;
batchProcess();
cout << "Greedy Reorder Over" << endl;
}