/
loadDL_Sada.cpp
285 lines (237 loc) · 7.86 KB
/
loadDL_Sada.cpp
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* loadDL_Sada.cpp
*
* Created on: 27-05-2015
* Author: hector
*/
#include "SadaDocList64.h"
#include <ConfigFile.h>
bool PRINT = false; // true: print all details for console
bool TEST_IND = true; // true: apply exhaustive test
bool RUN_EXP = false;
uint REPET = 5; // number of repetitions for each experiment
uint M_MAX = 10; // maximum length pattern value to run test
// Structure with all globals parameters program
typedef struct {
string configFile; // properties file
uchar *seq; // original sequence (1 byte for symbol)
ulong n; // Length of generalize Text = T1$T2$...TD$
char cutDoc; // symbol to separate documents
SadaDocList64 *index;
char dirStore[300]; // directory to save/load the data structure (files *.dls)
char dirResult[300]; // directory to save the results of the experiments as summary files
ulong* patterns;
uchar **patt;
suint* lenPat;
} ParamProgram;
void timeCount(ParamProgram *par, uint m);
void runExperiments(ParamProgram *par);
void testDLSadLoad(ParamProgram *par);
uint searchPattInFMILoad(ParamProgram *par, uchar* pat, uint m, bool *V);
int main(int argc, char *argv[]) {
ParamProgram *par = new ParamProgram();
char fileName[300];
cout << "Running loadDL_Sada.cpp ..." << endl;
if(argc != 2){
cout << "ERRORR !! " << endl;
cout << "loadDL_Sada.cpp's usage requires the Properties File as parameter !! " << endl;
cout << "Example for the file 'config.txt' in the same directory: ./loadDL_Sada.cpp config.txt" << endl;
exit(1);
}
par->configFile = string(argv[1]);
cout << "Congif file: " << par->configFile << endl;
ConfigFile cf(par->configFile);
PRINT = cf.Value("GLOBALS","TRACE");
TEST_IND = cf.Value("GLOBALS","TEST");
REPET = cf.Value("GLOBALS","N_REP");
RUN_EXP = cf.Value("GLOBALS","RUN_EXP");
strcpy(par->dirStore, ((string)(cf.Value("DL","dirStore"))).c_str());
strcpy(par->dirResult, ((string)(cf.Value("DL","dirResult"))).c_str());
cout << "loadAppTopkLZ parameters..." << endl;
cout << "dirStore: " << par->dirStore << endl;
cout << "dirResult: " << par->dirResult << endl;
par->index = new SadaDocList64(par->dirStore);
par->n = par->index->n;
par->cutDoc = par->index->cutDoc;
cout << "____________________________________________________" << endl;
cout << "*** Index size " << par->index->sizeDS << " bytes = " << (float)par->index->sizeDS*8.0/(float)par->n << " bpc" << endl;
cout << "====================================================" << endl;
strcpy(fileName, "");
strcpy(fileName, par->dirStore);
strcat(fileName, "sequence.test");
ifstream is(fileName, ios::binary);
par->seq = new uchar[par->n];
is.read((char*)par->seq, par->n*sizeof(uchar));
is.close();
if (TEST_IND){
cout << "Test Index..." << endl;
testDLSadLoad(par);
cout << "Test Index OK !!" << endl;
}
if (RUN_EXP){
cout << "Experiments..." << endl;
runExperiments(par);
cout << "Experiments OK !!" << endl;
}
cout << "$$$$$$$$$$$$$$$$$$$$$" << endl;
return 0;
}
void runExperimentsOne(ParamProgram *par, uint m){
uint nDocsSad, k, i;
uint *occSad = new uint[par->index->nDocs];
double t, avgTime;
float avgnOcc;
char aFile[400];
char str[20];
cout << "____________________________________________________" << endl;
cout << "Start DL for patterns of length m = " << m << endl;
avgTime = 0.0;
avgnOcc = 0.0;
for (i=0; i<par->index->nDocs; i++)
par->index->V[i] = 0;
for (k=0; k<REPET; k++){
nDocsSad =0;
t = getTime_ms();
par->index->documentListing(par->seq+par->patterns[k], m, occSad, &nDocsSad);
t = getTime_ms() - t;
avgTime += t/(double)REPET;
avgnOcc += nDocsSad;
// It cleans the V[] vector or unmarks the documents reported !!
for (i=0; i<nDocsSad; i++)
par->index->V[occSad[i]] = 0;
}
avgnOcc /= (float)REPET;
cout << "Average CPU time for execution: " << avgTime*1000.0 << " Microseconds" << endl;
cout << "Average nDocs found : " << avgnOcc << endl;
cout << "Size : " << par->index->sizeDS*8.0/(float)par->n << endl;
cout << "____________________________________________________" << endl;
strcpy(aFile, par->dirResult);
strcpy(str, "");
sprintf(str, "SadaDL_m%d", m);
strcat(aFile, str);
FILE *fp = fopen(aFile, "a+" );
// [m] [SAMPLE SA] [SAMPLE ISA] [size] [nDocs] [time]
fprintf(fp, "%d %d %d %f %f %G\n", m, SaS, SaI, par->index->sizeDS*8.0/(float)par->n, avgnOcc, avgTime*1000.0);
fclose(fp);
}
void createPatternsLoad(ParamProgram *par, uint m){
ulong i, j, k;
par->patterns = new ulong[REPET];
bool eq;
//cout << "Creating patterns of length m=" << m << endl;
for(k=0; k<REPET; k++){
eq = true;
while(eq){
i = (rand() % (par->n-(m+1)))+1;
for(j=i; j<i+(m+1); j++){
if (par->seq[j] == par->cutDoc){
i = (rand() % (par->n-(m+1)))+1;
j = i-1;
}
if(j==0) break;
else{
if (j>i && par->seq[j-1] != par->seq[j])
eq = false;
}
}
}
par->patterns[k] = i;
//cout << "["<<k<<"] " << i << endl;
}
//cout << "Patterns created !!" << endl;
}
void timeCount(ParamProgram *par, uint m){
double t, avgTime;
uchar* pat;
uint k;
string query;
uint64_t lb, rb, l, r;
avgTime = 0.0;
for (k=0; k<REPET; k++){
pat = par->seq+par->patterns[k];
query = string((char *)pat);
l=lb=0;
r=rb=par->index->fmi.size()-1;
t = getTime_ms();
backward_search(par->index->fmi, lb, rb, query.begin(), query.begin()+m, l, r);
t = getTime_ms() - t;
avgTime += t/(double)REPET;
}
cout << "____________________________________________________" << endl;
cout << "Time for count..." << endl;
cout << "Average CPU time for count m=" << m << " = " << avgTime*1000.0 << " Microseconds" << endl;
cout << "____________________________________________________" << endl;
}
void runExperiments(ParamProgram *par){
uint m;
cout << "====================================================" << endl;
par->patterns = new ulong[REPET];
m = 6;
createPatternsLoad(par, m);
cout << "Patterns created for m = " << m << endl;
timeCount(par, m);
cout << "Document Listing..." << endl;
runExperimentsOne(par, m);
m = 10;
createPatternsLoad(par, m);
cout << "Patterns created for m = " << m << endl;
timeCount(par, m);
cout << "Document Listing..." << endl;
runExperimentsOne(par, m);
delete [] (par->patterns);
}
void testDLSadLoad(ParamProgram *par){
uint m, t, i;
uint nDocsSad, nDocsFMI;
bool *V = new bool[par->index->nDocs];
uint *occSad = new uint[par->index->nDocs];
uchar* pat;
for (m=1; m<=M_MAX; m++){
createPatternsLoad(par, m+1);
for (t=0; t<REPET; t++){
nDocsSad = nDocsFMI = 0;
for (i=0; i<par->index->nDocs; i++)
V[i]=par->index->V[i]=0;
nDocsFMI = searchPattInFMILoad(par, par->seq+par->patterns[t], m, V);
par->index->documentListing(par->seq+par->patterns[t], m, occSad, &nDocsSad);
if(nDocsFMI != nDocsSad){
cout << "ERROR: m=" << m << ", test=" << t << ", nDocsFMI = " << nDocsFMI << " != nDocsSad = " << nDocsSad << endl;
pat = par->seq+par->patterns[t];
cout << "patt = [";
for(i=0; i<m; i++)
cout << pat[i];
cout << "]" << endl;
for(i=0; i<par->index->nDocs; i++){
if(V[i] != par->index->V[i])
cout << "Document " << i << ". par->index->V[i] == " << par->index->V[i] << " != V[i] = " << V[i] << endl;
}
exit(1);
}
}
delete [] (par->patterns);
}
}
uint searchPattInFMILoad(ParamProgram *par, uchar* pat, uint m, bool *V){
ulong doc, i;
uint nDocsFMI=0;
string query = string((char *)pat);
size_t occs = sdsl::count(par->index->fmi, query.begin(), query.begin()+m);
auto locations = locate(par->index->fmi, query.begin(), query.begin()+m);
if (PRINT){
cout << "Total occurrences found with FMI : " << occs << endl;
cout << "locations..." << endl;
}
for(i=0; i<occs; i++){
doc = par->index->searchDocument(locations[i]);
if (PRINT) cout << locations[i] << "(" << doc << ") " << endl;
if (V[doc]==0){
V[doc]=1;
nDocsFMI++;
}
}
if (PRINT){
cout << endl;
cout << "nDocs found with FMI : " << nDocsFMI << endl;
}
return nDocsFMI;
}