-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
149 lines (124 loc) · 4 KB
/
test.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
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <omp.h>
#include <stdio.h>
#include <time.h>
#include <sstream>
// #include "Cloud_lab1.cpp"
#include <thread>
#include<mutex>
using namespace std;
mutex mtx;
#define NTHREADS 10
map<string,map<string,int> > dic;
// void PrintDic();
// void ReadFiles(int num){
// string infile = "";
// // int file;
// map<string,map<string,int> > dic;
// clock_t begin = clock();
// for (int file = 1; file <= num; file++){
// stringstream out;
// out << file;
// infile = "file" + out.str()+".txt";
// cout <<"==========="<<infile<<"==========="<<endl;
// // fun_count_words(infile);
// InvertedIndex(infile,dic);
// }
// clock_t end = clock();
// double elapsed_secs = (double(end - begin) / CLOCKS_PER_SEC)/60;
// cout<<"time: "<<elapsed_secs<<endl;
// PrintDic();
// }
// void *thread_funtion(void *dum){
// }
void InvertedIndex(string filename){
// void InvertedIndex(string filename,map<string,map<string,int> > &dic){
ifstream infile;
infile.open (filename);
// map<string,map<string,int> > dic;
// map<string,int>::iterator it = dic.begin();
string word;
// clock_t begin = clock();
mtx.lock();
if (infile.is_open()){
//bool flag=true;
#pragma omp parrallel for default(none) num_threads(8) private(infile,word)
{
for (;infile>>word;)
{
// int tid = omp_get_thread_num();
// cout << "hilo_id:"<<tid<<" "<<word<<endl;
#pragma omp critical
if(dic.find(word) == dic.end()){
// cout<<"nueva: "<<word<<" añado: "<<filename<<endl;
map<string,int> tmp;
tmp.insert(make_pair(filename,1));
dic.insert(make_pair(word,tmp));
}else{
// cout<<" ya_puesta: "<<word<<endl;
if(dic[word].find(filename) == dic[word].end()){
// cout<<" añadiendo_txt = "<<filename<<endl;
dic[word].insert(make_pair(filename,1));
}
// else{
// cout<<" ya_esta = "<<filename<<endl;
// }
}
}
}
infile.close();
}else {
cout << "Error al abrir archivo"<<endl;
}
mtx.unlock();
// clock_t end = clock();
// double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
// cout<<"time: "<<elapsed_secs<<endl;
// for (it=dic.begin(); it!=dic.end(); ++it)
// cout << it->first << " " << it->second << '\n';
}
// void PrintDic(map<string,map<string,int> > &dic){
void PrintDic(){
map<string,map<string,int> >::iterator it;
for (it=dic.begin(); it!=dic.end(); ++it){
cout << it->first << ": ";
// map<string,int> tmp = it;
map<string,int>::iterator it1;
cout<<"("<<it->second.size()<<") ";
for(it1 = it->second.begin();it1 != it->second.end(); it1++){
cout << it1->first << " ";
}
cout<<endl;
}
}
void ReadFiles2(){
string infile = "";
// pthread_t thread_id[NTHREADS];
map<string,map<string,int> > dic;
thread *thread_id = new thread[NTHREADS];
clock_t start= clock();
int i,j;
#pragma omp parrallel for default(none) num_threads(NTHREADS) private(out)
for(i = 0; i < NTHREADS; i++){
stringstream out;
// pthread_create(&thread_id[i], NULL, thread_funtion,NULL);
// thread_id[i] = thread(InvertedIndex,infile);
#pragma omp critical
out << i + 1;
infile = "file" + out.str()+".txt";
cout<<infile<<endl;
InvertedIndex(infile);
}
clock_t fin =clock();
double time=(double(fin-start)/CLOCKS_PER_SEC);
cout<<"time:"<<time<<endl;
PrintDic();
}
int main(){
// ReadFiles(5);
ReadFiles2();
// fun_count_words("example1.txt");
}