-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathConcurrentHashMap.cpp
376 lines (310 loc) · 11 KB
/
ConcurrentHashMap.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
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
#include "ConcurrentHashMap.hpp"
using namespace std;
void ConcurrentHashMap::print_tabla(){
cout << "{";
for (size_t i = 0; i < 26; i++) {
auto a = tabla[i]->CrearIt();
bool hay = false;
for (auto it = tabla[i]->CrearIt(); it.HaySiguiente(); it.Avanzar()) {
cout << " < " <<it.Siguiente().first << " , "<< it.Siguiente().second <<" > " ;
}
if (hay) cout << "]";
}
cout << "}"<< endl;
}
/************************************** Constructor *******************************************/
ConcurrentHashMap::ConcurrentHashMap(){
/* Inizializamos la tabla y los mutex */
for (size_t i = 0; i < 26; i++) {
tabla[i] = new Lista<Elem>();
pthread_mutex_init(&(_locks[i]), NULL);
}
}
/************************************Constructor por copia ***********************************/
ConcurrentHashMap::ConcurrentHashMap (const ConcurrentHashMap& other){
for (size_t i = 0; i < 26; i++) {
tabla[i] = new Lista<Elem>();
for (auto it = other.tabla[i]->CrearIt(); it.HaySiguiente(); it.Avanzar()) {
tabla[i]->push_front(make_pair(it.Siguiente().first,it.Siguiente().second));
}
}
}
/**********************************************************************************************/
ConcurrentHashMap& ConcurrentHashMap::operator= (const ConcurrentHashMap& other){
for (size_t i = 0; i < 26; i++) {
delete tabla[i];
tabla[i] = new Lista<Elem>();
for (auto it = other.tabla[i]->CrearIt(); it.HaySiguiente(); it.Avanzar()) {
tabla[i]->push_front(make_pair(it.Siguiente().first,it.Siguiente().second));
}
}
return *this;
}
/**************************************** Destructor ******************************************/
ConcurrentHashMap::~ConcurrentHashMap(){
for (size_t i = 0; i < 26; i++) {
delete tabla[i];
pthread_mutex_destroy(&_locks[i]);
}
}
/**************************************** addAndInc *******************************************/
void ConcurrentHashMap::addAndInc(string key){
int h = hash(key);
/* Agregarmos la clave a la lista si no esta */
while (!member(key)){
pthread_mutex_lock(& _locks[h]);
if(!member(key)) {
tabla[h]->push_front(make_pair(key,0));
}
pthread_mutex_unlock(& _locks[h]);
}
/* Incrementamos su valor */
for (auto it = tabla[h]->CrearIt(); it.HaySiguiente(); it.Avanzar()) {
if (it.Siguiente().first == key) {
pthread_mutex_lock(& _locks[h]);
it.Siguiente().second++;
pthread_mutex_unlock(& _locks[h]);
break;
}
}
}
/**************************************** memeber *********************************************/
bool ConcurrentHashMap::member(string key){
for (auto it = tabla[hash(key)]->CrearIt(); it.HaySiguiente(); it.Avanzar()) {
if (it.Siguiente().first == key) return true;
}
return false;
}
/**********************************************************************************************/
/* maxThrWrapper y maxThr funciones que usa cada threada para calcular el máximo de un hashmap*/
void* ConcurrentHashMap::maxThrWrapper(void * args){
infoThread * info = (infoThread *) args ;
return info->context->maxThr(info);
}
void * ConcurrentHashMap::maxThr(void * args){
infoThread* inf = (infoThread*) args;
int next;
while(next = atomic_fetch_add(inf->siguiente,1), next < 26 ){
/* Vamos a la proxima entrada de la tabla para recorrer. */
for (auto it = tabla[next]->CrearIt(); it.HaySiguiente(); it.Avanzar()) {
Elem* m;
do {
m = (* (inf->max)).load();
if( m == NULL || it.Siguiente().second > m->second ){
// Actualizo el máximo
atomic_compare_exchange_weak(inf->max, &m , &it.Siguiente());
}
} while( it.Siguiente().second > ((*inf->max).load())->second );
}
}
/* Si no quedan mas entradas de la tabla para reccorer terminamos. */
return NULL;
}
/************************************ maximum *************************************************/
pair<string,unsigned int> ConcurrentHashMap::maximum(unsigned int nt){
atomic<int> siguiente(0);
atomic<Elem *> maximo(nullptr);
pthread_t threads[nt]; int tid;
infoThread vars[nt];
/* Pedimos los mutex de cada entrada de la tabla */
for (size_t i = 0; i < 26; i++) pthread_mutex_lock(& _locks[i]);
/* Inizializamos los threads */
for (size_t tid = 0; tid < nt; tid++) {
vars[tid].siguiente = &siguiente;
vars[tid].max = &maximo;
vars[tid].context = this;
pthread_create(&threads[tid],NULL,&ConcurrentHashMap::maxThrWrapper,& (vars[tid]) );
}
/* Joineamos los threads */
for (size_t tid = 0; tid < nt; tid++) pthread_join(threads[tid],NULL);
pair<string,unsigned int> res = make_pair(maximo.load()->first,maximo.load()->second);
/* Desbloqueamos los mutex de cada entrada de la tabla */
for (size_t i = 0; i < 26; i++) pthread_mutex_unlock(& _locks[i]);
return res;
}
/******************************** END class functions******************************************/
/**********************************************************************************************/
/**********************************************************************************************/
/************************EJ 2.2 versión NO concurrente del count_words ************************/
ConcurrentHashMap count_words(string archivo){
string word;
// Abrimos el archivo
ifstream file(archivo);
ConcurrentHashMap h;
if (file) {
while (getline(file,word)) {
// Agregamos cada palabra al hashsmap
h.addAndInc(word);
}
file.close();
}
return h;
}
/*********** función que utilizarán los threads para la versión concurrent count_words 2.3*****/
void * count_words_threads(void * args){
pair<string*,ConcurrentHashMap*> input = *((pair<string*,ConcurrentHashMap*> *) args);
ConcurrentHashMap *h = input.second;
string inputFile = *(input.first);
ifstream file(inputFile);
string word;
if (file) {
while(getline(file,word)){
(*h).addAndInc(word);
}
}
file.close();
return NULL;
}
/************************EJ 2.3 versión concurrente del count_words ***************************/
ConcurrentHashMap count_words(list<string>archs){
ConcurrentHashMap h;
// Se crea un thread por cada archivo.
pthread_t threads[archs.size()];
pair<string*,ConcurrentHashMap*> vars[archs.size()];
int tid = 0;
// List no es accesible con [], por eso el iterador.
for (auto it = archs.begin(); it != archs.end(); it++){
vars[tid].first = &(*it);
vars[tid].second = &h;
pthread_create(&threads[tid],NULL, count_words_threads, & vars[tid]); //
tid++;
}
for (tid = 0; tid < archs.size(); tid++) {
pthread_join(threads[tid],NULL);
}
return h;
}
/********* función que utilizarán los n threads para la versión concurrente count_words 2.4****/
void * count_words_nthreads(void * args){
infoFile inf = *(infoFile*) args;
int next;
int last = inf.words->size();
ConcurrentHashMap *h = inf.context;
while(next = atomic_fetch_add(inf.siguiente,1), next < last){
string archivo = (*inf.words)[next];
ifstream file(archivo);
string word;
if (file) {
while(getline(file,word)){
(*h).addAndInc(word);
}
}
file.close();
}
return NULL;
}
/**********************EJ 2.4 versión concurrente del count_words con n threads****************/
ConcurrentHashMap count_words(unsigned int n, list<string>archs){
ConcurrentHashMap h;
pthread_t threads[n];
int tid;
infoFile vars[n];
vector<string> words;
for (auto it = archs.begin(); it != archs.end(); it++){
string s = *it;
words.push_back(s);
}
atomic<int> siguiente(0);
for(tid = 0; tid < n; tid++){
vars[tid].siguiente = &siguiente;
vars[tid].words = &words;
vars[tid].context = &h;
pthread_create(&threads[tid], NULL, count_words_nthreads, & (vars[tid]));
}
for(tid = 0; tid < n; tid++){
pthread_join(threads[tid], NULL);
}
return h;
}
/*********************************************************************************************/
/**** función que utilizarán los p_archivos threads para leer los archivos (ej 2.5) **********/
void * count_words_nthreads_2(void * args){
infoFileVector inf = *(infoFileVector*) args;
int next;
int last = inf.words->size();
vector<ConcurrentHashMap> *h = inf.hashMaps;
while(next = atomic_fetch_add(inf.siguiente,1), next < last){
string archivo = (*inf.words)[next];
ifstream file(archivo);
string word;
if (file) {
while(getline(file,word)){
(*h)[next].addAndInc(word);
}
}
file.close();
}
return NULL;
}
/**** función que utilizarán los p_máximos threads para calcular el máximo(ej 2.5) **********/
void * count_row(void * args){
infoFileFind inf = *(infoFileFind*) args;
int next;
vector<ConcurrentHashMap> *h = inf.hashMaps;
ConcurrentHashMap *hGral = inf.hashMapGral;
while(next = atomic_fetch_add(inf.row, 1), next < 26){
int i;
for (i = 0; i < h->size(); i++){
auto it = (*h)[i].tabla[next]->CrearIt();
for(auto it = (*h)[i].tabla[next]->CrearIt(); it.HaySiguiente(); it.Avanzar()){
string key = it.Siguiente().first;
int j;
for (j = 0; j < it.Siguiente().second; j++) (*hGral).addAndInc(key);
}
}
}
return NULL;
}
/*************************EJ 2.5 versión concurrente del count_words ************************/
pair<string, unsigned int>maximum(unsigned int p_archivos,unsigned int p_maximos, list<string>archs){
// Primero con los p_archivos threads se crean un hashsmap por cada archivo
int n = archs.size();
vector<ConcurrentHashMap> hashMaps(n);
pthread_t threads[p_archivos];
int tid;
infoFileVector vars[p_archivos];
vector<string> words;
for (auto it = archs.begin(); it != archs.end(); it++){
string s = *it;
words.push_back(s);
}
atomic<int> siguiente(0);
for(tid = 0; tid < p_archivos ; tid++){
vars[tid].siguiente = &siguiente;
vars[tid].words = &words;
vars[tid].hashMaps = &hashMaps;
pthread_create(&threads[tid], NULL, count_words_nthreads_2, & (vars[tid]));
}
for(tid = 0; tid < p_archivos; tid++){
pthread_join(threads[tid], NULL);
}
// Luego con p_maximos threads cada uno irá construyendo de a una fila el hashmap general
p_maximos = (p_maximos > 26)? 26 : p_maximos;
pthread_t threads_find[p_maximos];
infoFileFind vars2[p_maximos];
atomic<int> row(0);
ConcurrentHashMap hashMapGral;
for(tid = 0; tid < p_maximos; tid++){
vars2[tid].row =&row;
vars2[tid].hashMaps = &hashMaps;
vars2[tid].hashMapGral = &hashMapGral;
pthread_create(&threads_find[tid], NULL, count_row, & (vars2[tid]));
}
for(tid = 0; tid < p_maximos; tid++){
pthread_join(threads_find[tid], NULL);
}
pair<string, unsigned int> max = hashMapGral.maximum(p_maximos);
return max;
}
/*****************EJ 2.6 versiones alternativas de maximum utilizando count_words **************/
pair<string, unsigned int>maximum2(unsigned int p_archivos,unsigned int p_maximos, list<string>archs){
ConcurrentHashMap h = count_words(archs);
pair<string, unsigned int> max = h.maximum(p_maximos);
return max;
}
pair<string, unsigned int>maximum3(unsigned int p_archivos,unsigned int p_maximos, list<string>archs){
ConcurrentHashMap h = count_words(p_archivos, archs);
pair<string, unsigned int> max = h.maximum(p_maximos);
return max;
}
/************************************************************************************************/