-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaprioriomp.cpp
More file actions
405 lines (358 loc) · 7.23 KB
/
Copy pathaprioriomp.cpp
File metadata and controls
405 lines (358 loc) · 7.23 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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* Parallel Code for apriori algorithm by optimizing set_count */
#include <bits/stdc++.h>
#include <omp.h>
#include <stdio.h>
using namespace std;
//macros
#define structure map<vector<int>, int>
#define FOR_MAP(ii,T) for(structure::iterator (ii)=(T).begin();(ii)!=(T).end();(ii)++)
#define FOR_next_MAP(jj,ii,T) for(structure::iterator (jj)=(ii);(jj)!=(T).end();(jj)++)
#define VI vector<int>
int MIN_SUP;
structure C;
structure L;
void C1(string);
void L1();
void generate_C();
void generate_L();
void output(structure );
void scan_D(int,int,string);
void prune();
bool check_compatibility(VI ,VI );
void set_count(VI , structure*);
int main(int argc, char const *argv[])
{
int l ;
string file_name;
cout<<"Enter the support count: "<<endl;
cin>>MIN_SUP;
cout<<"Enter transactions: "<<endl;
cin>>l;
cout<<"Enter the input_file name"<<endl;
cin>>file_name;
int threads;
cout<<"Enter the number of threads: "<<endl;
cin>>threads;
double start,end,time;
start = omp_get_wtime();
C.clear();
L.clear();
bool mv=true;
int index=2;
while(true)
{
if (mv)
{
C1(file_name);
cout<<"Initial Candidates\n";
output(C);
L1();
cout<<"Candidates with sufficient Frequency\n";
output(L);
mv=!mv;
}
else
{
generate_C();
if(C.size()==0)
break;
prune();
if (C.size()==0)
break;
scan_D(l,threads,file_name);
generate_L();
if (L.size()==0)
break;
cout<<"\nFrequency List "<<index<<"\n";
output(L);
index++;
}
}
end = omp_get_wtime();
time = end - start;
cout<<"the time required is : "<<time<<"\n";
cout<<"no of iterations : "<<index<<"\n";
return 0;
}
/* Generating the first candidate list */
void C1(string file_name)
{
ifstream fin;
fin.open(file_name.c_str());
if(!fin)
{
cout<<"Input file opening error\n";
exit(0);
}
int n;
VI v;
while(fin>>n)
{
v.clear();
if (n==-1)
{
continue;
}
v.push_back(n);
if(C.count(v)>0)
C[v]++;
else
C[v]=1;
}
fin.close();
}
/* Prints all the candidates or frequency list along with their count */
void output(structure T)
{
cout<<"\n";
VI v;
FOR_MAP(ii,T)
{
v.clear();
v=ii->first;
for (int i = 0; i < v.size(); ++i)
{
cout<<v[i]<<" ";
}
cout<<" ---(frequency)----->> "<<ii->second;
cout<<"\n";
}
}
/* Generating the first frequency list */
void L1()
{
FOR_MAP(ii,C)
{
if (ii->second >= MIN_SUP)
{
L[ii->first]=ii->second;
}
}
}
/* Generating all the cadidates of size k from frequency list of size k-1 */
void generate_C()
{
C.clear();
FOR_MAP(ii,L)
{
FOR_next_MAP(jj,ii,L)
{
if(jj==ii)
continue;
VI a,b;
a.clear();
b.clear();
a=ii->first;
b=jj->first;
if(check_compatibility(a,b))
{
a.push_back(b.back());
sort(a.begin(), a.end());
C[a]=0;
}
}
}
}
/* Checking if any two frequency item sets are same or not */
bool check_compatibility(VI a,VI b)
{
bool compatible=true;
for (int i = 0; i < a.size()-1; ++i)
{
if (a[i]!=b[i])
{
compatible=false;
break;
}
}
return compatible;
}
/* Removing all the candidates of size k whose subsets of size k-1 are not in the frequency list of size k-1 */
void prune()
{
VI a,b;
FOR_MAP(ii,C)
{
a.clear();
b.clear();
a=ii->first;
for(int i = 0;i<a.size();i++)
{
b.clear();
for (int j = 0; j < a.size(); ++j)
{
if(j==i)
continue;
b.push_back(a[j]); // all the subsets are generated one by one and stored in b
}
if(L.find(b)==L.end()) // check if subset exists in frequency item-set
{
ii->second=-1;
break;
}
}
}
structure temp;
temp.clear();
FOR_MAP(ii,C)
{
if (ii->second != -1)
{
temp[ii->first]=ii->second;
}
}
C.clear();
C=temp;
temp.clear();
}
/* For going to a particular line in the file */
FILE * GotoLine(FILE* file, unsigned int num){
int count = 0;
char buf[100];
do
{
if(count == num)
break;
fgets(buf,100,file);
count++;
}while(1);
return file;
}
/* Scanning the database and calling set_count for each transaction */
void scan_D(int l,int t,string file_name)
{
int p = l/t; //no of transactions per database
FILE *f[t];
int i;
for(i=0;i<t;i++){
f[i] = fopen(file_name.c_str(), "r"); //each thread opens its own file
if(f[i] == NULL)
{
cout<<"Input file opening error\n";
exit(0);
}
f[i] = GotoLine(f[i],i*p); // each file has a pointer after p lines for each thread
}
int n;
VI a;
int count = 0;
int nthreads, tid, procs, maxt, inpar, dynamic, nested;
structure temp;
temp.clear();
FOR_MAP(ii,C)
{
temp[ii->first]=ii->second;
}
int max = omp_get_thread_limit();
omp_set_dynamic(max);
omp_set_num_threads(t);
/* Dividing the transactions among the threads */
#pragma omp parallel firstprivate(temp) private(count,n,a)
{
if(!omp_in_parallel())
{
cout<<"not parallel"<<endl;
exit(0);
}
#pragma omp master
{
/* Get environment information */
procs = omp_get_num_procs();
nthreads = omp_get_num_threads();
maxt = omp_get_max_threads();
inpar = omp_in_parallel();
dynamic = omp_get_dynamic();
nested = omp_get_nested();
/* Print environment information */
printf("Number of processors = %d\n", procs);
printf("Number of threads = %d\n", nthreads);
printf("Max threads = %d\n", maxt);
printf("In parallel? = %d\n", inpar);
printf("Dynamic threads enabled? = %d\n", dynamic);
printf("Nested parallelism supported? = %d\n", nested);
}
count=0;
char buf[1000];
int x;
int tid = omp_get_thread_num();
int lev = omp_get_level();
do
{
fgets(buf,100,f[tid]);
string s = buf;
istringstream iss(s);
a.clear();
while (iss)
{
string sub;
iss >> sub;
stringstream convert(sub);
convert >> x;
if(x==-1 && a.size()>0)
{
set_count(a,&temp);
count++;
break;
}
a.push_back(x);
};
}while(count<p);
#pragma omp flush(a)
#pragma omp barrier
/* Each thread calculates its local count for all the candidates and at the end they are added to get final count */
#pragma omp critical
{
FOR_MAP(ii,temp)
{
C[ii->first]+=ii->second;
}
temp.clear();
}
}
for(i=0;i<t;i++)
fclose(f[i]);
}
/* Incrementing the count of the candidate if it exists in the transaction, it is done for all candidates */
void set_count(VI a,structure* temp)
{
FOR_MAP(ii,(*temp))
{
VI b;
b.clear();
b=ii->first;
int x;
int true_count=0;
int prev=-1;
if (b.size()<=a.size())
{
for (int i = 0; i < b.size(); ++i)
{
for (int j = prev+1; j < a.size(); ++j) // Number of iterations of inner loop can be decreased based on the previous iteration result
{
if(b[i]==a[j])
{
true_count++;
prev = j;
break;
}
}
}
}
if (true_count==b.size())
{
ii->second++;
}
}
}
/* Finding the frequent item-sets of size k by removing all candidate item-sets having their count less than minimum support count */
void generate_L()
{
L.clear();
FOR_MAP(ii,C)
{
if(ii->second >= MIN_SUP)
{
L[ii->first]=ii->second;
}
}
}