-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtema1.cpp
More file actions
137 lines (115 loc) · 4.22 KB
/
tema1.cpp
File metadata and controls
137 lines (115 loc) · 4.22 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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <fstream>
#include <map>
#include <set>
#include "helpers.h"
#include "reducers.h"
#include "mappers.h"
int main(const int argc, const char *argv[])
{
/*
Only accept clean input.
*/
helpers::exitIfInvalidArgs(argc, argv);
/*
Input is clean, accept it.
*/
const unsigned int NR_MAPPERS = std::stoi(argv[1]);
const unsigned int NR_REDUCERS = std::stoi(argv[2]);
const unsigned int NR_THREADS = NR_MAPPERS + NR_REDUCERS;
/*
Get the mapper file paths.
*/
const std::string TEST_PATH = argv[3];
std::vector<std::string> inputFilePaths = helpers::getFilePaths(TEST_PATH);
/*
Initialize threads and the barrier.
*/
pthread_t mappers[NR_MAPPERS];
mappers::mapperArg_t<std::map<int, std::map<int, std::vector<long long>>>> mapperArgs[NR_MAPPERS];
pthread_t reducers[NR_REDUCERS];
reducers::reducerArg_t<std::map<int, std::map<int, std::vector<long long>>>> reducerArgs[NR_REDUCERS];
pthread_barrier_t reducersBarrier;
pthread_barrier_init(&reducersBarrier, NULL, NR_MAPPERS + NR_REDUCERS);
/*
Used for testing the output of functions.
*/
int r;
/*
This is where each mapper will
hold its result from the processing of the files.
The structure used is a map of multimaps
with keys of type int (the power) and values of type
int (the perfect numbers of the given power).
All threads will share this multimap.
*/
std::map<int, std::map<int, std::vector<long long>>> mappersRes;
/*
Initialize the
permission files vector.
*/
std::vector<bool> permissionFiles(inputFilePaths.size(), true);
/*
Initialize the
permission lock.
*/
pthread_mutex_t permissionLock;
r = pthread_mutex_init(&permissionLock, NULL);
helpers::exitIf(r, "[ERROR]: Couldn't initialize the permission lock.");
/*
Create the mappers and reducers in one go.
*/
for (unsigned int id = 0; id < NR_MAPPERS + NR_REDUCERS; id++) {
if (id < NR_MAPPERS) {
mappers::mapperArg_t<std::map<int, std::map<int, std::vector<long long>>>> mapperArg(
id,
inputFilePaths,
&reducersBarrier,
NR_REDUCERS,
&mappersRes,
&permissionFiles,
&permissionLock);
mapperArgs[id] = mapperArg;
r = pthread_create(&mappers[id], NULL, mappers::mapperPerfectNumber, &mapperArgs[id]);
helpers::exitIf(r, "[ERROR]: Couldn't create a mapper thread with id = " + std::to_string(id) + ".");
} else {
unsigned int actualId = id - NR_MAPPERS;
reducers::reducerArg_t<std::map<int, std::map<int, std::vector<long long>>>> reducerArg(
actualId,
&reducersBarrier,
&mappersRes);
reducerArgs[actualId] = reducerArg;
r = pthread_create(&reducers[actualId], NULL, reducers::reducerPerfectNumber, &reducerArgs[actualId]);
helpers::exitIf(r, "[ERROR]: Couldn't create a reducer thread with id = " + std::to_string(actualId) + ".");
}
}
/*
One final join on all threads so there's no zombie threads.
Also used in order to not destroy the barrier before all threads are
finished with it.
*/
for (unsigned int id = 0; id < NR_THREADS; id++) {
if (id < NR_MAPPERS) {
r = pthread_join(mappers[id], NULL);
helpers::exitIf(r, "[ERROR]: Couldn't join a mapper thread with id = " + std::to_string(id) + ".");
} else {
unsigned int actualId = id - NR_MAPPERS;
r = pthread_join(reducers[actualId], NULL);
helpers::exitIf(r, "[ERROR]: Couldn't join a reducer thread with id = " + std::to_string(actualId) + ".");
}
}
/*
At the end, destroy the used structures.
*/
pthread_barrier_destroy(&reducersBarrier);
pthread_mutex_destroy(&permissionLock);
return 0;
}