-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
325 lines (268 loc) · 9.03 KB
/
Copy pathmain.cpp
File metadata and controls
325 lines (268 loc) · 9.03 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
#include <bits/stdc++.h>
#include <pthread.h>
#include <ctype.h>
#include <string.h>
using namespace std;
// Structure to hold shared reducer data
struct ReducerData
{
pthread_mutex_t lock; // Mutex for synchronized access
vector<map<string, set<int>>> lists; // Vector for reducer lists
};
// Structure to hold thread information
struct threadData
{
int threadID; // Thread ID
vector<pair<string, int>> *file_data; // Vector with file names and IDs
int nr_mappers; // Number of mapper threads
int nr_reducers; // Number of reducer threads
pthread_mutex_t *mutex; // Mutex for accessing shared file data
pthread_barrier_t *barrier; // Barrier for synchronizing mappers and reducers
ReducerData *reducerData; // Pointer to shared reducer data (files and mutex)
};
// Assign files to each reducer
vector<int> reducerFileIndex(int nrReducers)
{
vector<int> indexes(26, 0);
int range = 26 / nrReducers;
for (int i = 0; i < 26; ++i)
{
indexes[i] = min(i / range, nrReducers - 1);
}
return indexes;
}
// Add the word in the list of the responsible reducer
void addWord(const string &word, int fileID, vector<map<string, set<int>>> &reducerWords, int numReducers)
{
static vector<int> indexes = reducerFileIndex(numReducers);
if (word.empty() || !isalpha(word[0]))
return;
char firstLetter = tolower(word[0]);
int reducerIndex = indexes[firstLetter - 'a'];
reducerWords[reducerIndex][word].insert(fileID);
}
// Compare function for sorting words
bool compare(const pair<string, set<int>> &wordA, const pair<string, set<int>> &wordB)
{
if (wordA.second.size() != wordB.second.size())
{
return wordA.second.size() > wordB.second.size();
}
return wordA.first < wordB.first;
}
// Assign a file to a mapper thread
bool assignFileToMapper(threadData *t, pair<string, int> &file_data)
{
pthread_mutex_lock(t->mutex);
if (!t->file_data->empty())
{
auto it = prev(t->file_data->end());
file_data = *it;
t->file_data->erase(it);
pthread_mutex_unlock(t->mutex);
return true;
}
pthread_mutex_unlock(t->mutex);
return false;
}
// Process files and distribute words to the responsible reducers
vector<map<string, set<int>>> processFilesForReducers(threadData *t)
{
vector<map<string, set<int>>> reducerWords(t->nr_reducers);
pair<string, int> file_data;
while (assignFileToMapper(t, file_data))
{ // Process files assigned to the mapper
ifstream file(file_data.first);
string line;
while (getline(file, line))
{
istringstream stream(line);
string word;
while (stream >> word)
{ // Extract words from each line
word.erase(remove_if(word.begin(), word.end(), [](char c)
{ return !isalpha(c); }),
word.end()); // Remove non-alphabetic characters
transform(word.begin(), word.end(), word.begin(), ::tolower); // Convert to lowercase
if (!word.empty())
{
addWord(word, file_data.second, reducerWords, t->nr_reducers);
}
}
}
file.close();
}
return reducerWords;
}
// Mapper function to process files and update the shared reducer data
void mapper(threadData *t)
{
vector<map<string, set<int>>> reducerWords = processFilesForReducers(t);
// Add reducerWords to shared data
pthread_mutex_lock(&t->reducerData->lock);
for (int i = 0; i < t->nr_reducers; ++i)
{
auto &localMap = reducerWords[i];
auto &sharedMap = t->reducerData->lists[i];
for (const auto &entry : localMap)
{
const string &word = entry.first;
const set<int> &fileIds = entry.second;
sharedMap[word].insert(fileIds.begin(), fileIds.end());
}
}
pthread_mutex_unlock(&t->reducerData->lock);
}
// Calculate the letter range handled by a reducer
pair<char, char> calculateLetterRange(int reducer_id, int nr_reducers, int nr_mappers)
{
int range = 26 / nr_reducers;
int range_rest = 26 % nr_reducers;
char startLetter = 'a' + (reducer_id - nr_mappers) * range;
char endLetter = 'a' + (reducer_id - nr_mappers + 1) * range - 1;
if (reducer_id == nr_mappers + nr_reducers - 1)
{
endLetter += range_rest;
}
return {startLetter, endLetter};
}
// Group words by their first letter
map<char, vector<pair<string, set<int>>>> groupWords(
const map<string, set<int>> &ReducerData,
char startLetter,
char endLetter)
{
map<char, vector<pair<string, set<int>>>> groupedWords;
for (const auto &entry : ReducerData)
{
char firstChar = entry.first[0];
if (firstChar >= startLetter && firstChar <= endLetter)
{
groupedWords[firstChar].push_back({entry.first, entry.second});
}
}
return groupedWords;
}
// Write grouped words to output files
void writeGroupedWords(
const map<char, vector<pair<string, set<int>>>> &groupedWords,
function<bool(const pair<string, set<int>> &, const pair<string, set<int>> &)> compare)
{
// Iterate over each group of words
for (const auto &entry : groupedWords)
{
char letter = entry.first;
string filename = string(1, letter) + ".txt";
ofstream outputFile(filename); // Open the output file for writing
auto string = entry.second; // The list of word and file ID pairs for the letter group
stable_sort(string.begin(), string.end(), compare); // Sort the words using compare
// Write each word and its associated file IDs to the output file
for (const auto &wordEntry : string)
{
ostringstream lineStream; // Stream to construct the output line
lineStream << wordEntry.first << ":[";
// Write the file IDs associated with the word
for (auto it = wordEntry.second.begin(); it != wordEntry.second.end(); ++it)
{
if (it != wordEntry.second.begin())
{
lineStream << " ";
}
lineStream << *it;
}
lineStream << "]\n";
outputFile << lineStream.str(); // Write the constructed line to the file
}
outputFile.close();
}
}
// Reducer function to group words and write to files
void reducer(threadData *t)
{
auto [startLetter, endLetter] = calculateLetterRange(t->threadID, t->nr_reducers, t->nr_mappers);
const auto &ReducerData = t->reducerData->lists[t->threadID - t->nr_mappers];
auto groupedWords = groupWords(ReducerData, startLetter, endLetter);
writeGroupedWords(groupedWords, compare);
}
// Thread function
void *func(void *arg)
{
threadData *t = (threadData *)arg;
if (t->threadID < t->nr_mappers)
mapper(t);
pthread_barrier_wait(t->barrier);
if (t->threadID >= t->nr_mappers)
reducer(t);
return NULL;
}
// Initialize thread data
void initializeThreadStruct(
threadData &thread_data,
vector<pair<string, int>> *file_data,
pthread_mutex_t *mutex,
pthread_barrier_t *barrier,
ReducerData *reducerData,
int threadID,
int nr_mappers,
int nr_reducers)
{
thread_data.file_data = file_data;
thread_data.mutex = mutex;
thread_data.barrier = barrier;
thread_data.reducerData = reducerData;
thread_data.threadID = threadID;
thread_data.nr_mappers = nr_mappers;
thread_data.nr_reducers = nr_reducers;
}
int main(int argc, char *argv[])
{
// Input data
int nr_mappers = stoi(argv[1]);
int nr_reducers = stoi(argv[2]);
ifstream files(argv[3]);
vector<pair<string, int>> file_data;
vector<threadData> thread_data(nr_mappers + nr_reducers);
vector<pthread_t> threads(nr_mappers + nr_reducers);
// Save the files in file_data
if (files)
{
int file_count;
files >> file_count;
files.ignore();
for (int i = 0; i < file_count; ++i)
{
string fileName;
getline(files, fileName);
file_data.emplace_back(fileName, i + 1);
}
}
// Initialize threads
ReducerData reducerData;
reducerData.lists.resize(nr_reducers);
pthread_mutex_init(&reducerData.lock, NULL);
pthread_barrier_t barrier;
pthread_barrier_init(&barrier, NULL, nr_mappers + nr_reducers);
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, NULL);
for (int i = 0; i < nr_mappers + nr_reducers; ++i)
{
initializeThreadStruct(
thread_data[i],
&file_data,
&mutex,
&barrier,
&reducerData,
i,
nr_mappers,
nr_reducers);
pthread_create(&threads[i], NULL, func, (void *)&thread_data[i]);
}
for (int i = 0; i < nr_mappers + nr_reducers; i++)
{
pthread_join(threads[i], NULL);
}
pthread_barrier_destroy(&barrier);
pthread_mutex_destroy(&mutex);
pthread_mutex_destroy(&reducerData.lock);
return 0;
}