-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapReduceFramework.cpp
More file actions
285 lines (255 loc) · 9.08 KB
/
Copy pathMapReduceFramework.cpp
File metadata and controls
285 lines (255 loc) · 9.08 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
#include "MapReduceFramework.h"
#include <math.h>
#include "Barrier.h"
#include <atomic>
#include <algorithm>
#include <semaphore.h>
#include <unordered_map>
#include <iostream>
#include <pthread.h>
sem_t emit3Sem;
std::vector<sem_t*> jobs_sem();
void haltError(std::string text){
std::cout << "system error: "<< text << std::endl;
exit(1);
}
struct JobCard
{
JobState jobState;
const InputVec& inputVec;
OutputVec& outputVec;
pthread_t* threadsId;
const MapReduceClient& client;
unsigned int currentUnmapedPlace;
std::vector<IntermediateVec> intermediateVec;
sem_t precentageSem,shuffleSem,finishMapSem,reduceSem,mapSem;
pthread_mutex_t waitForMe;
std::atomic<size_t> numOfVectorsAfterShuffle;
int activeThreads,mapCounter,reduceCounter,handledPairs,totalPairsToHandle;
Barrier barrier;
public:
JobCard() = delete;
JobCard(const InputVec& inputVec,OutputVec& outputVec,const MapReduceClient& client,int activeThreads) :
jobState({MAP_STAGE,0}),inputVec(inputVec),outputVec(outputVec),client(client),activeThreads(activeThreads),
mapCounter(0),reduceCounter(0),handledPairs(0),totalPairsToHandle(0),barrier(activeThreads){
threadsId = new pthread_t[activeThreads];
currentUnmapedPlace=0;
intermediateVec = std::vector<IntermediateVec>(activeThreads);
if(sem_init(&precentageSem,0,1)!= 0){
haltError("Faild in sem_init");
}
if(sem_init(&shuffleSem,0,1)!=0){
haltError("Faild in sem_init");
}
if(sem_init(&finishMapSem,0,0)!=0){
haltError("Faild in sem_init");
}
if(sem_init(&reduceSem,0,1)!=0){
haltError("Faild in sem_init");
}
if(sem_init(&mapSem,0,1)!=0){
haltError("Faild in sem_init");
}
if(pthread_mutex_init(&waitForMe,0)!=0){
haltError("Faild in sem_init");
}
numOfVectorsAfterShuffle = 0;
}
~JobCard(){
free(threadsId);
sem_destroy(&precentageSem);
sem_destroy(&shuffleSem);
sem_destroy(&finishMapSem);
sem_destroy(&mapSem);
sem_destroy(&reduceSem);
pthread_mutex_unlock(&waitForMe);
pthread_mutex_destroy(&waitForMe);
}
/**
* @brief The function return one input pair
* @return pointer to the input pair
*/
const InputPair* getInputPairToMap(){
sem_wait(&mapSem);
if(inputVec.size() <= currentUnmapedPlace){
sem_post(&mapSem);
return NULL;
}
const InputPair& tmp = inputVec[currentUnmapedPlace];
currentUnmapedPlace++;
sem_post(&mapSem);
return &tmp;
}
/**
* @brief The function return one vector to reduce
*/
IntermediateVec getVectorToReduce(){
sem_wait(&reduceSem);
if(intermediateVec.empty()){
sem_post(&reduceSem);
return IntermediateVec();
}
IntermediateVec tmp = intermediateVec.back();
intermediateVec.pop_back();
sem_post(&reduceSem);
return tmp;
}
void updatePrecentage(stage_t stage){
sem_wait(&precentageSem);
switch(stage){
case MAP_STAGE:
mapCounter++;
jobState.percentage = (mapCounter*100.0)/inputVec.size();
if(jobState.percentage >= 100 ){
jobState.stage = SHUFFLE_STAGE;
jobState.percentage = 0;
sem_post(&finishMapSem);
}
break;
case SHUFFLE_STAGE:
jobState.percentage = (handledPairs*100.0)/totalPairsToHandle;
if(jobState.percentage >= 100){
jobState.percentage = 0;
jobState.stage = REDUCE_STAGE;
}
break;
case REDUCE_STAGE:
reduceCounter++;
jobState.percentage = ((reduceCounter) * 100)/numOfVectorsAfterShuffle;
break;
default:
break;
}
sem_post(&precentageSem);
}
K2* getMaxKey(std::vector<IntermediateVec>& unShuffledVectors){
K2* highestKey = NULL;
for(IntermediateVec& vec: unShuffledVectors){
if(!vec.empty()){
if(highestKey==NULL){
highestKey = vec.back().first;
}
if(*highestKey < *(vec.back().first)){
highestKey = vec.back().first;
}
}
}
return highestKey;
}
int countPairs(std::vector<IntermediateVec>& vecOfVecs){
int counter = 0;
for(IntermediateVec& vec : vecOfVecs){
counter+=vec.size();
}
return counter;
}
void readyToShuffle(int uid){
if(uid == 0){
sem_wait(&finishMapSem);
std::vector<IntermediateVec> unShuffledVectors = intermediateVec;
totalPairsToHandle = countPairs(unShuffledVectors);
intermediateVec.clear();
numOfVectorsAfterShuffle=0;
K2* highestKey;
while(!unShuffledVectors.empty()){
highestKey = getMaxKey(unShuffledVectors);
if(highestKey==NULL){
break;
}
numOfVectorsAfterShuffle++;
intermediateVec.push_back(IntermediateVec());
for(std::vector<IntermediateVec>::iterator it = unShuffledVectors.begin();it != unShuffledVectors.end();++it){
while (!it->empty() && !(*(it->back().first) < *highestKey || *highestKey < *(it->back().first)))
{
intermediateVec.back().push_back(it->back());
it->pop_back();
}
if(it->empty()){
unShuffledVectors.erase(it);
--it;
}
}
handledPairs = countPairs(intermediateVec);
updatePrecentage(SHUFFLE_STAGE);
}
}
barrier.barrier();
}
};
struct threadInfo { /* Used as argument to thread_start() */
int uid;
JobCard* jobHandle;
threadInfo(int uid,JobCard* jobHandle): uid(uid),jobHandle(jobHandle){};
};
void* worker_function(void* arg){
int uid = ((threadInfo*)arg)->uid;
JobCard* jobHandle = ((threadInfo*)arg)->jobHandle;
free(arg);
const InputPair* input = jobHandle->getInputPairToMap();
while (input != nullptr)
{
jobHandle->client.map(input->first,input->second,&(jobHandle->intermediateVec[uid]));
jobHandle->updatePrecentage(MAP_STAGE);
input = jobHandle->getInputPairToMap();
}
std::sort(jobHandle->intermediateVec[uid].begin(),jobHandle->intermediateVec[uid].end(),[](const IntermediatePair& left,const IntermediatePair& right){
return *left.first < *right.first;
});
jobHandle->readyToShuffle(uid);
IntermediateVec vectorToReduce = jobHandle->getVectorToReduce();
while (!vectorToReduce.empty())
{
jobHandle->client.reduce(&vectorToReduce,&(jobHandle->outputVec));
jobHandle->updatePrecentage(REDUCE_STAGE);
vectorToReduce = jobHandle->getVectorToReduce();
}
return nullptr;
}
void emit2 (K2* key, V2* value, void* context){
((IntermediateVec*) context)->push_back(IntermediatePair(key,value));
}
void emit3 (K3* key, V3* value, void* context){
//The function saves the output element in the context data structures (output vector)
sem_wait(&emit3Sem);
((OutputVec*)context)->push_back(OutputPair(key,value));
sem_post(&emit3Sem);
//the function updates the number of output elements using atomic counter
}
JobHandle startMapReduceJob(const MapReduceClient& client,
const InputVec& inputVec, OutputVec& outputVec,
int multiThreadLevel){
sem_init(&emit3Sem,0,1);
JobCard* jobHandle = new JobCard(inputVec,outputVec,client,multiThreadLevel);
for(int i=0;i<multiThreadLevel;i++){
threadInfo* arg = new threadInfo(i,jobHandle);
if(pthread_create(&(jobHandle->threadsId[i]),NULL,&worker_function,arg)==-1){
haltError("Fail to create a new Thread");
}
}
return jobHandle;
}
void waitForJob(JobHandle job){
if(job != NULL){
JobCard* jobPtr = (JobCard*)job;
pthread_mutex_lock(&(jobPtr->waitForMe));
for(int i=0;i < jobPtr->activeThreads;i++){
pthread_join(jobPtr->threadsId[i],nullptr);
}
if(job != NULL){
pthread_mutex_unlock(&(jobPtr->waitForMe));
}
}
}
void getJobState(JobHandle job, JobState* state){
*state = ((JobCard*)job)->jobState;
}
void closeJobHandle(JobHandle job){
JobCard* jobPtr = (JobCard*)job;
if(!(jobPtr->jobState.stage == REDUCE_STAGE && jobPtr->jobState.percentage >=100)){
waitForJob(job);
}
if(jobPtr->jobState.stage == REDUCE_STAGE && jobPtr->jobState.percentage >=100){
delete jobPtr;
jobPtr =NULL;
}
}