-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapReduceFrameWork.cpp
More file actions
304 lines (254 loc) · 8.47 KB
/
MapReduceFrameWork.cpp
File metadata and controls
304 lines (254 loc) · 8.47 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
#include "MapReduceFramework.h"
#include "MapReduceClient.h"
#include "WarpContext.h"
#include "Context.h"
#include <stdio.h>
#include <cstring> // for std::strerror
#include <mutex>
#include <semaphore.h>
#include <stdlib.h>
#include <algorithm>
#define ERROR_MSG "system error"
std::vector<pthread_t> threads;
std::mutex waitMutex; // Mutex to synchronize access to alreadyWaited
void emit2 (K2* key, V2* value, void* context)
{
auto warpContext = static_cast<WarpContext*>(context);
auto context1 = warpContext->context;
if( key&& value)
{
context1->addToIntermediate (std::make_pair (key, value), warpContext->getThreadIndex ());
}
else{
std::printf ("or key or value is null");
}
}
void emit3 (K3* key, V3* value, void* context){
auto warpContext = static_cast<WarpContext*>(context);
auto context1 = warpContext->context;
context1->addToOutPut(std::make_pair(key, value));
}
/**
* Active the map phase for all the threads
*/
void activeMapPhase(WarpContext* warpContext)
{
auto context = warpContext->context;
context->jobHandler->setJobHandlerTotalElements(context->inputVec.size());
context->jobHandler->setJobHandlerState (MAP_STAGE);
int vectorSize = context->inputVec.size();
while (true) {
// Atomically fetch and increment the index
int index = context->mapIndex++;
// Break if the index is out of range
if (index >= vectorSize) {
break;
}
context->useMap(context->inputVec[index].first,context->inputVec[index].second, warpContext);
context->jobHandler->addJobHandlerElements();
}
}
/**
* Active the sort phase for all the threads
*/void activeSortPhase(WarpContext* warpContext)
{
auto context = warpContext->context;
// Sort the vector of IntermediatePair
std::sort(context->threadsIntermediateVec[warpContext->getThreadIndex()].begin(),
context->threadsIntermediateVec[warpContext->getThreadIndex()].end(),
[](IntermediatePair& a, IntermediatePair& b)
{
if (a.first == nullptr && b.first == nullptr) {
return false; // Both are null, they are equal
}
if (a.first == nullptr) {
return true; // a is null, b is not
}
if (b.first == nullptr) {
return false; // b is null, a is not
}
// Both are non-null, compare the values
return *(a.first) < *(b.first);
});
}
bool allThreadsIntermediateVecEmpty(WarpContext* warpContext) {
auto context = warpContext->context;
for (const auto& vec : context->threadsIntermediateVec) {
if (!vec.empty()) {
return false;
}
}
return true;
}
K2* findMaxKey(Context* context) {
K2* maxKey = nullptr;
int numThreads = context->threadsIntermediateVec.size();
for (int i = 0; i < numThreads; ++i) {
if (!context->threadsIntermediateVec[i].empty()) {
IntermediatePair backPair = context->threadsIntermediateVec[i].back();
if (maxKey == nullptr || *maxKey < *(backPair.first)) {
maxKey = backPair.first;
}
}
}
return maxKey;
}
std::vector<IntermediatePair> getAllSameKeys(WarpContext* warpContext, K2 *current_key) {
int numThreads = warpContext->context->num_of_threads;
Context* context = warpContext->context;
std::vector<IntermediatePair> currentKeyGroup;
for (int i = 0; i < numThreads; ++i) {
auto& threadVec = context->threadsIntermediateVec[i];
// Check if the thread's vector is not empty and the back pair's key is equal to current_key
while (!threadVec.empty() && !(*threadVec.back().first < *current_key)) {
currentKeyGroup.push_back(threadVec.back());
threadVec.pop_back();
}
}
return currentKeyGroup;
}
void activeShufflePhase(WarpContext* warpContext){
int numThreads = warpContext->context->num_of_threads;
Context* context = warpContext->context;
// update job handler
int64_t shuffle_num = (static_cast<int64_t>(context->threadsIntermediateVec.size()) << 31) |
(static_cast<int64_t>(SHUFFLE_STAGE) << 62);
context->jobHandler->setJobHandlerAll (shuffle_num);
// context->jobHandler->setJobHandlerElements(0);
// context->jobHandler->setJobHandlerTotalElements
// (context->threadsIntermediateVec.size());
// context->jobHandler->setJobHandlerState(SHUFFLE_STAGE);
while (!allThreadsIntermediateVecEmpty(warpContext))
{
K2 *current_key = findMaxKey (context);
std::vector<IntermediatePair> currentKeyGroup = getAllSameKeys
(warpContext,current_key) ;
context->shuffleQueue.push_back (currentKeyGroup);
context->queueSize++;
// elhanan did quesize++ for each pair
context->jobHandler->setJobHandlerElements(context->queueSize);
}
// empty JobHandler for next stage
int64_t reduce_num = (static_cast<int64_t>(context->queueSize) << 31) |
(static_cast<int64_t>(REDUCE_STAGE) << 62);
context->jobHandler->setJobHandlerAll (reduce_num);
}
void activeReducePhase(WarpContext* warp_context)
{
Context *context = warp_context->context;
int vectorSize = context->shuffleQueue.size();
while (true) {
// Atomically fetch and increment the index
int index = context->reduceIndex++;
// Break if the index is out of range
if (index >= vectorSize) {
break;
}
context->useReduce (&context->shuffleQueue[index], warp_context);
context->jobHandler->addJobHandlerElements();
}
}
void *f(void * arg)
{
auto warpContext = static_cast<WarpContext*> (arg);
auto context = warpContext->context;
activeMapPhase(warpContext);
activeSortPhase(warpContext);
warpContext->context->barrier->barrier(); // active barrier,all threads
// sorted
if(warpContext->getThreadIndex() == 0)
{
activeShufflePhase (warpContext);
for (int i = 1; i < warpContext->context->num_of_threads; ++i)
{
if(sem_post(&context->semaphore_shuffle) != 0){
perror (ERROR_MSG);
exit(1);
}
}
}
else{
if(sem_wait(&context->semaphore_shuffle) != 0) {
perror (ERROR_MSG);
exit(1);
}
}
activeReducePhase(warpContext);
return (void*)NULL;
}
JobHandle startMapReduceJob(const MapReduceClient& client,
const InputVec& inputVec, OutputVec& outputVec,
int multiThreadLevel)
{
JobHandler *jobHandler = new JobHandler();
jobHandler->setJobHandlerAll (0);
Context *context = new Context(client, multiThreadLevel, inputVec,
outputVec,jobHandler);
for (int i = multiThreadLevel - 1; i >= 0; --i) {
jobHandler->warpVector.push_back(new WarpContext(i, context));
}
// create threads
jobHandler->threads = std::vector<pthread_t>(multiThreadLevel);
for (int i = 0; i < multiThreadLevel; ++i)
{
if(pthread_create(&(jobHandler->threads[i]), nullptr, f, static_cast<void *>
(jobHandler->warpVector[i]))
!= 0)
{
perror(ERROR_MSG);
exit(1);
}
}
return context;
}void waitForJob(JobHandle job)
{
Context* context = static_cast<Context*>(job);
auto jobHandler = context->jobHandler;
std::unique_lock<std::mutex> lock (waitMutex); // Lock the mutex for synchronized access
if (jobHandler->alreadyWaited)
{
return;
}
jobHandler->alreadyWaited = true;
lock.unlock ();
for (pthread_t thread: jobHandler->threads)
{
int rc = pthread_join (thread, nullptr);
if (rc != 0)
{
fprintf (stderr, "Error: pthread_join() failed with code %d\n", rc);
if (rc == ESRCH)
{
fprintf (stderr, "Thread does not exist or already joined (ESRCH)\n");
}
else if (rc == EINVAL)
{
fprintf (stderr, "Thread is not joinable (EINVAL)\n");
}
else if (rc == EDEADLK)
{
fprintf (stderr, "Deadlock detected (EDEADLK)\n");
}
else
{
perror (ERROR_MSG); // This may not give useful output, as errno is not set
}
exit (1);
}
}
}
void getJobState(JobHandle job, JobState* state)
{
Context* context = static_cast<Context*>(job);
int64_t jobHandlerValues = context->jobHandler->getJobStateValues();
state->stage = (stage_t)((jobHandlerValues >> 62) & 0x3);
state->percentage = std::min(static_cast<float>((jobHandlerValues & 0x3FFFFFFF)) /
static_cast<float>(((jobHandlerValues >> 31) & 0x3FFFFFFF)) * 100.0f,
100.0f);
}
void closeJobHandle(JobHandle job)
{
Context* context = static_cast<Context*>(job);
waitForJob(job);
delete context;
}