-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmappers.cpp
More file actions
77 lines (65 loc) · 2.78 KB
/
mappers.cpp
File metadata and controls
77 lines (65 loc) · 2.78 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
#include "mappers.h"
#include <map>
#include <vector>
#include <fstream>
#include <pthread.h>
void *mappers::mapperPerfectNumber(void *arg) {
mappers::mapperArg_t<std::map<int, std::map<int, std::vector<long long>>>> mapperArg =
*(mappers::mapperArg_t<std::map<int, std::map<int, std::vector<long long>>>> *)arg;
std::map<int, std::vector<long long>> mapperEntry;
for (long unsigned int i = 0; i < mapperArg.filePaths.size(); i++) {
/*
Only one thread can see the
permission of a file at a given time.
*/
pthread_mutex_lock(mapperArg.permissionLock);
/*
If the file is not operated on /
has not been operated on, this thread
will operate on it.
*/
if (mapperArg.permissionToFiles->at(i)) {
/*
Set permission to false, so that
no other thread tries accessing this file.
*/
mapperArg.permissionToFiles->at(i) = false;
pthread_mutex_unlock(mapperArg.permissionLock);
/*
Now, the thread starts collecting
all of the perfect numbers from the file.
*/
std::ifstream inputFile(mapperArg.filePaths[i]);
std::string lineStr;
getline(inputFile, lineStr);
helpers::exitIf(!helpers::isPosInt(lineStr), "[ERROR]: first line must be a positive int.");
const unsigned int nr = std::stoi(lineStr);
unsigned long value;
for (unsigned int lineNr = 0; lineNr < nr; lineNr++) {
getline(inputFile, lineStr);
value = std::stol(lineStr);
for (unsigned int pwr = 2; pwr < mapperArg.nrReducers + 2; pwr++) {
if (helpers::isPerfectNumber(value, pwr)) {
mapperEntry[pwr].push_back(value);
}
}
}
}
/*
If the file is operated on / has been
operated on, continue onwards to the next file.
*/
else {
pthread_mutex_unlock(mapperArg.permissionLock);
continue;
}
}
mapperArg.result->insert(std::make_pair(mapperArg.mId, mapperEntry));
/*
Wait on barrier after doing all the mapper work
in order to make the reducers start processing after
all the mappers have finished.
*/
pthread_barrier_wait(mapperArg.reducerBarrier);
pthread_exit(NULL);
}