-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCloud_lab2.cpp
142 lines (119 loc) · 3.68 KB
/
Cloud_lab2.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
#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 num_threads(16) default (none)\
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;
// }
}
}
}
}else {
cout << "Error al abrir archivo"<<endl;
}
infile.close();
}
// 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;
for(int i = 0; i < NTHREADS; i++){
stringstream out;
out << i + 1;
infile = "file" + out.str()+".txt";
cout<<infile<<endl;
// pthread_create(&thread_id[i], NULL, thread_funtion,NULL);
thread_id[i] = thread(InvertedIndex,infile);
}
for(int i = 0; i < NTHREADS; i++){
thread_id[i].join();
}
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");
}