Repository navigation
Expand file tree
/
Copy pathMapReduceFramework.cpp
More file actions
589 lines (521 loc) · 19.2 KB
/
Copy pathMapReduceFramework.cpp
File metadata and controls
589 lines (521 loc) · 19.2 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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
//
// Created by ophir on 19/05/2020.
//
#include "MapReduceFramework.h"
#include <pthread.h>
#include "Barrier.h"
#include <cstdio>
#include <atomic>
typedef std::vector<std::pair<K2*,V2*>> threadVector;
/**
* startMapReduceFramework – This function starts running the MapReduce algorithm (with
several threads) and returns a JobHandle.
client – The implementation of MapReduceClient class, in other words the task that the
framework should run.
inputVec – a vector of type std::vector<std::pair<K1*, V1*>>, the input elements
outputVec – a vector of type std::vector<std::pair<K3*, V3*>>, to which the output
elements will be added before returning. You can assume that outputVec is empty.
multiThreadLevel – the number of worker threads to be used for running the algorithm.
You can assume this argument is valid (greater or equal to 2).
* @param client
* @param inputVec
* @param outputVec
* @param multiThreadLevel
* @return
*/
typedef struct {
// The mapReduceClient
const MapReduceClient* client;
// how many threads have finished map phase
Barrier* mapBarrier;
// Array of threads
pthread_t* threads;
// counter of processed keys in curent phase.
std::atomic<int>* processedKeys;
// pointer to the output vector mutex
pthread_mutex_t* outVecMutex;
// Mutex of jobstate
pthread_mutex_t* jobStateMutex;
// is shuffle phase over?
pthread_mutex_t* doneMutex;
// how many pairs are available for shuffle to process
std::atomic<int>* pairCount;
// how many pairs are available to map or reduce.
std::atomic<unsigned long>* workCounter;
// map of key = k2*, value = V2*
IntermediateMap* inter;
// vector of unique keys to processes
std::vector<K2*>* interKeys;
//output vec
OutputVec* outputVec;
//jobstate
JobState* jobState;
// how many threads do we have
int multiThreadLevel;
// Has someone called wait
std::atomic_bool* someoneWaiting;
} JobContext;
typedef struct{
// the input vector
const InputVec* inputVec;
//common job map threads need
JobContext* job;
// personal output vec for mapthread
threadVector* tv;
// mutex of threadVector
pthread_mutex_t* tvMutex;
} mapThread;
typedef struct{
// all personal output thread vectors
std::vector<threadVector*>* threadVectors;
// all personal output thread vectors mutexes
std::vector<pthread_mutex_t*>* threadMutexes;
//common job struct
JobContext* data;
} shuffleThread;
void finishShuffle(const shuffleThread *sData, int mapThreadCount);
float processVector(const shuffleThread *sData, float shuffleCount, float totalJob,
IntermediateMap *intermediateMap, int treadNum);
float setShuffle(const shuffleThread *sData, float shuffleCount, float totalJob);
JobContext *
initJobContext(const MapReduceClient &client, OutputVec &outputVec, int multiThreadLevel,
pthread_t *threads);
mapThread *initMapThreadData(const InputVec &inputVec, JobContext *data);
shuffleThread *
initShuffleStruct(std::vector<threadVector *> *threadVectors, std::vector<pthread_mutex_t *> *threadMutexes,
JobContext *data);
/**
* This function is responsible to find next available key to reduce and call reduce upon it.
* @param context void* (JobContext*) pointer to struct containing needed resources.
*/
void reduceWrapper(void* context){
auto* tJob = (JobContext*) context;
unsigned long oldValue = (*tJob->workCounter)++;
unsigned long interKeysSize = tJob->interKeys->size();
while(oldValue < interKeysSize)
{
K2* key = tJob->interKeys->at(oldValue);
tJob->client->reduce(key, tJob->inter->at(key), context);
if(pthread_mutex_lock(tJob->jobStateMutex) != 0){
fprintf(stderr, "system error: error on pthread_mutex_lock\n");
exit(1);
}
tJob->jobState->percentage = (PERCENTAGE * ((float) ++(*tJob->processedKeys)/interKeysSize));
if(pthread_mutex_unlock(tJob->jobStateMutex) != 0){
fprintf(stderr, "system error: error on pthread_mutex_unlock\n");
exit(1);
}
oldValue = (*tJob->workCounter)++;
}
}
/**
* This function is responsible to find next available key to map and call map upon it.
* after no more keys are available to map, waits for barrier and sends the thread to reduce phase.
* @param context context void* (JobContext*) pointer to struct containing needed resources.
* @return JobContext
*/
void* mapNreduce(void *context){
auto *tData = (mapThread*) context;
unsigned long old_value = (*tData->job->workCounter)++;
unsigned long vecsize = tData->inputVec->size();
while(old_value < vecsize) {
InputPair a = tData->inputVec->at(old_value);
JobContext* c = tData->job;
const MapReduceClient* b = c->client;
b->map(a.first, a.second, context);
if(pthread_mutex_lock(tData->job->jobStateMutex) != 0){
fprintf(stderr, "system error: error on pthread_mutex_lock\n");
exit(1);
}
tData->job->jobState->percentage = (PERCENTAGE * ((float) ++(*tData->job->processedKeys)/
tData->inputVec->size()));
if(pthread_mutex_unlock(tData->job->jobStateMutex) != 0){
fprintf(stderr, "system error: error on pthread_mutex_unlock\n");
exit(1);
}
old_value = (*tData->job->workCounter)++;
}
tData->job->mapBarrier->barrier();
JobContext* job = tData->job;
delete tData;
reduceWrapper(job);
return context;
}
/**
* Iterates through all private thread vectors and sorts their content into an intermediate map.
* after that
* @param context
* @return
*/
JobHandle shuffle(void* context){
auto* sData = (shuffleThread*) context;
int mapThreadCount = sData->data->multiThreadLevel - 1;
bool done = false;
float shuffleCount = 0;
float totalJob = 0;
IntermediateMap* intermediateMap = sData->data->inter;
while(!done)
{
for(int i = 0; i < mapThreadCount; ++i)
{
shuffleCount = processVector(sData, shuffleCount, totalJob, intermediateMap, i);
}
int count;
sData->data->mapBarrier->getCount(&count);
if(count == mapThreadCount)
{
totalJob = setShuffle(sData, shuffleCount, totalJob);
if (*sData->data->pairCount == 0)
{
done = true;
}
}
}
finishShuffle(sData, mapThreadCount);
sData->data->mapBarrier->barrier();
JobContext* job = sData->data;
delete sData;
reduceWrapper(job);
return nullptr;
}
/**
* Sets the stage to shuffle and updates the percentage accordingly.
* @param sData struct with all resources.
* @param shuffleCount count of keys that shuffle sorted
* @param totalJob total count of k2 keys shuffle has to process
* @return totalJob
*/
float setShuffle(const shuffleThread *sData, float shuffleCount, float totalJob)
{
if(pthread_mutex_lock(sData->data->jobStateMutex) != 0){
fprintf(stderr, "system error: error on pthread_mutex_lock\n");
exit(1);
}
sData->data->jobState->stage = SHUFFLE_STAGE;
totalJob = shuffleCount + *sData->data->pairCount;
sData->data->jobState->percentage = PERCENTAGE * (shuffleCount / totalJob);
if(pthread_mutex_unlock(sData->data->jobStateMutex) != 0){
fprintf(stderr, "system error: error on pthread_mutex_unlock\n");
exit(1);
}
return totalJob;
}
/**
* sort the private vector into intermediate vector until no more keys are available.
* @param sData struct with common resources.
* @param shuffleCount total intermediate pairs keys that were sorted
* @param totalJob total pairs to process
* @param intermediateMap the output vec of shuffle
* @param treadNum current thread and vector num
* @return total pairs proccesed by shuffle.
*/
float processVector(const shuffleThread *sData, float shuffleCount, float totalJob,
IntermediateMap *intermediateMap, int treadNum)
{
threadVector* curVec = sData->threadVectors->at(treadNum);
pthread_mutex_t* curMutex = sData->threadMutexes->at(treadNum);
if(pthread_mutex_lock(curMutex) != 0){
fprintf(stderr, "system error: error on pthread_mutex_lock\n");
exit(1);
}
while(!curVec->empty())
{
IntermediatePair pair = curVec->at(0);
curVec->erase(curVec->begin());
if(intermediateMap->find(pair.first) == intermediateMap->end())
{
intermediateMap->insert(std::make_pair(pair.first,std::vector<V2*>()));
sData->data->interKeys->push_back(pair.first);
}
intermediateMap->at(pair.first).push_back(pair.second);
--(*sData->data->pairCount);
shuffleCount++;
}
if(pthread_mutex_unlock(curMutex) != 0){
fprintf(stderr, "system error: error on pthread_mutex_unlock\n");
exit(1);
}
if(pthread_mutex_lock(sData->data->jobStateMutex) != 0){
fprintf(stderr, "system error: error on pthread_mutex_lock\n");
exit(1);
}
if(sData->data->jobState->stage == SHUFFLE_STAGE)
{
sData->data->jobState->percentage = PERCENTAGE * (shuffleCount / totalJob);
}
if(pthread_mutex_unlock(sData->data->jobStateMutex) != 0){
fprintf(stderr, "system error: error on pthread_mutex_unlock\n");
exit(1);
}
return shuffleCount;
}
/**
* finish shuffle set stage to reduce and init relevant structures.
* @param sData struct containg common resources
* @param mapThreadCount number of thread in map phase.
*/
void finishShuffle(const shuffleThread *sData, int mapThreadCount)
{
for(int i=0; i < mapThreadCount; ++i){
delete sData->threadVectors->at(i);
if(pthread_mutex_destroy(sData->threadMutexes->at(i)) != 0){
fprintf(stderr, "system error: error on pthread_mutex_destroy\n");
exit(1);
}
free(sData->threadMutexes->at(i));
}
delete sData->threadVectors;
delete sData->threadMutexes;
(*sData->data->workCounter) = 0;
(*sData->data->processedKeys) = 0;
if(pthread_mutex_lock(sData->data->jobStateMutex) != 0){
fprintf(stderr, "system error: error on pthread_mutex_lock\n");
exit(1);
}
sData->data->jobState->stage = REDUCE_STAGE;
sData->data->jobState->percentage = 0.0;
if(pthread_mutex_unlock(sData->data->jobStateMutex) != 0){
fprintf(stderr, "system error: error on pthread_mutex_unlock\n");
exit(1);
}
}
/**
* This function starts running the MapReduce algorithm (with several threads)
* and returns a JobHandle
* @param client containing map and reduce implemntations
* @param inputVec Given inputVec
* @param outputVec pointer to the vector to output results to
* @param multiThreadLevel desired number of threads to create for the job.
* @return JobHandle containing jobstate.
*/
JobHandle startMapReduceJob(const MapReduceClient& client,
const InputVec& inputVec, OutputVec& outputVec,
int multiThreadLevel){
auto* threads = new pthread_t[multiThreadLevel];
auto* threadVectors = new std::vector<threadVector*>;
auto threadMutexes = new std::vector<pthread_mutex_t*>;
JobContext *data = initJobContext(client, outputVec, multiThreadLevel, threads);
for(int i = 0; i < multiThreadLevel - 1; ++i){
mapThread *myData = initMapThreadData(inputVec, data);
threadVectors->push_back(myData->tv);
threadMutexes->push_back(myData->tvMutex);
if(pthread_create(threads + i, nullptr, mapNreduce, myData) != 0){
fprintf(stderr, "system error: error on pthread_create\n");
exit(1);
}
}
shuffleThread *shuffleContext = initShuffleStruct(threadVectors, threadMutexes, data);
if(pthread_create(threads + (multiThreadLevel - 1),
nullptr, shuffle, shuffleContext) != 0){
fprintf(stderr, "system error: error on pthread_create\n");
exit(1);
}
return data;
}
/**
* This function initializes the shuffle thread
* @param threadVectors - the private work vector of each map thread
* @param threadMutexes - the mutexes of the work vectors
* @param data - the job context of the current job
* @return all the data needed for the shuffle to commence
*/
shuffleThread *
initShuffleStruct(std::vector<threadVector *> *threadVectors, std::vector<pthread_mutex_t *> *threadMutexes,
JobContext *data) {
auto shuffleContext = new shuffleThread;
shuffleContext->threadVectors = threadVectors;
shuffleContext->threadMutexes = threadMutexes;
shuffleContext->data = data;
return shuffleContext;
}
/**
* This function initialize a mapping thread
* @param inputVec - the input vector of data from the user
* @param data - the job context of the current job
* @return data for a single mapping thread
*/
mapThread *initMapThreadData(const InputVec &inputVec, JobContext *data)
{
auto* myData = new mapThread;
myData->inputVec = &inputVec;
auto vecMutex = (pthread_mutex_t*) malloc(sizeof(pthread_mutex_t));
if(vecMutex == nullptr){
fprintf(stderr, "system error: error on malloc\n");
exit(1);
}
if(pthread_mutex_init(vecMutex, nullptr) != 0){
fprintf(stderr, "system error: error on pthread_mutex_init\n");
exit(1);
}
myData->tv = new threadVector();
myData->tvMutex = vecMutex;
myData->job = data;
return myData;
}
/**
*
* This function initialize all the information needed for the requested job.
* @param client - the current user of the program
* @param outputVec - the output requested in a vector
* @param multiThreadLevel - the maximum number of working threads
* @param threads - an array of threads to create
* @return The information needed for the jon, held in a single struct.
*/
JobContext *
initJobContext(const MapReduceClient &client, OutputVec &outputVec, int multiThreadLevel,
pthread_t *threads)
{
auto* data = new JobContext;
data->jobState = new JobState;
data->outputVec = &outputVec;
data->threads = threads;
data->jobState->stage = UNDEFINED_STAGE;
data->jobState->percentage = 0.0; // only thread running
data->inter = new IntermediateMap;
data->interKeys = new std::vector<K2*>();
data->client = &client;
data->mapBarrier = new Barrier(multiThreadLevel);
data->pairCount = new std::atomic<int>(0);
data->processedKeys = new std::atomic<int>(0);
data->workCounter = new std::atomic<unsigned long>(0);
data->someoneWaiting = new std::atomic_bool(false);
data->multiThreadLevel = multiThreadLevel;
auto percMutex = (pthread_mutex_t*) malloc(sizeof(pthread_mutex_t));
if(percMutex == nullptr){
fprintf(stderr, "system error: error on malloc\n");
exit(1);
}
if(pthread_mutex_init(percMutex, nullptr) != 0){
fprintf(stderr, "system error: error on pthread_mutex_init\n");
exit(1);
}
data->jobStateMutex = percMutex;
auto outVecMutex = (pthread_mutex_t*) malloc(sizeof(pthread_mutex_t));
if(outVecMutex == nullptr){
fprintf(stderr, "system error: error on malloc\n");
exit(1);
}
if(pthread_mutex_init(outVecMutex, nullptr) != 0){
fprintf(stderr, "system error: error on pthread_mutex_init\n");
exit(1);
}
data->outVecMutex = outVecMutex;
auto doneMutex = (pthread_mutex_t*) malloc(sizeof(pthread_mutex_t));
if(doneMutex == nullptr){
fprintf(stderr, "system error: error on malloc\n");
exit(1);
}
if(pthread_mutex_init(doneMutex, nullptr) != 0){
fprintf(stderr, "system error: error on pthread_mutex_init\n");
exit(1);
}
data->doneMutex = doneMutex;
data->jobState->stage = MAP_STAGE;
return data;
}
/**
* This function waits for a job to finish.
* @param job - which the function waits for
*/
void waitForJob(JobHandle job){
auto context = (JobContext*) job;
if(*context->someoneWaiting){
return;
}
*context->someoneWaiting= true;
for(int i=0;i < context->multiThreadLevel; ++i){
if(pthread_join(context->threads[i], nullptr) != 0){
fprintf(stderr, "system error: error on pthread_join\n");
exit(1);
}
}
}
/**
* THis function gets the current job state of the program
* @param job - the context of the function
* @param state - the struct to fill
*/
void getJobState(JobHandle job, JobState* state){
auto context = (JobContext*) job;
if(pthread_mutex_lock(context->jobStateMutex) != 0){
fprintf(stderr, "system error: error on pthread_mutex_lock\n");
exit(1);
}
state->stage = context->jobState->stage;
state->percentage = context->jobState->percentage;
if(pthread_mutex_unlock(context->jobStateMutex) != 0){
fprintf(stderr, "system error: error on pthread_mutex_unlock\n");
exit(1);
}
}
/**
* This function closes all the resources that the program used
* @param job - the context of the function
*/
void closeJobHandle(JobHandle job){
waitForJob(job);
auto* context = (JobContext*) job;
if(pthread_mutex_destroy(context->doneMutex) != 0){
fprintf(stderr, "system error: error on pthread_mutex_destroy\n");
exit(1);
}
free(context->doneMutex);
if(pthread_mutex_destroy(context->jobStateMutex) != 0){
fprintf(stderr, "system error: error on pthread_mutex_destroy\n");
exit(1);
}
free(context->jobStateMutex);
if(pthread_mutex_destroy(context->outVecMutex) != 0){
fprintf(stderr, "system error: error on pthread_mutex_destroy\n");
exit(1);
}
free(context->outVecMutex);
delete context->inter;
delete context->interKeys;
delete context->pairCount;
delete context->processedKeys;
delete context->workCounter;
delete context->jobState;
delete context->mapBarrier;
delete context->someoneWaiting;
delete[] context->threads;
delete context;
}
/**
* This function produces a (K2*,V2*) pair.
* @param key recieved from the user
* @param value received from the user
* @param context - the context of the function
*/
void emit2 (K2* key, V2* value, void* context){
auto tData = (mapThread*) context;
if(pthread_mutex_lock(tData->tvMutex) != 0){
fprintf(stderr, "system error: error on pthread_mutex_lock\n");
exit(1);
}
tData->tv->emplace_back(key,value);
if(pthread_mutex_unlock(tData->tvMutex) != 0){
fprintf(stderr, "system error: error on pthread_mutex_unlock\n");
exit(1);
}
++(*tData->job->pairCount);
}
/**
* This function produces a (K3*,V3*) pair.
* @param key received from the user
* @param value received from the user
* @param context - the context of the function
*/
void emit3 (K3* key, V3* value, void* context){
JobContext* tData;
tData = (JobContext*) context;
if(pthread_mutex_lock(tData->outVecMutex) != 0){
fprintf(stderr, "system error: error on pthread_mutex_lock\n");
exit(1);
}
tData->outputVec->emplace_back(key, value);
if(pthread_mutex_unlock(tData->outVecMutex) != 0){
fprintf(stderr, "system error: error on pthread_mutex_unlock\n");
exit(1);
}
}