-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreducers.cpp
More file actions
36 lines (30 loc) · 1.23 KB
/
Copy pathreducers.cpp
File metadata and controls
36 lines (30 loc) · 1.23 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
#include "reducers.h"
#include <pthread.h>
#include <set>
#include <map>
#include <vector>
#include <fstream>
#include <iostream>
void *reducers::reducerPerfectNumber(void *arg) {
reducers::reducerArg_t<std::map<int, std::map<int, std::vector<long long>>>> reducerArg =
*(reducers::reducerArg_t<std::map<int, std::map<int, std::vector<long long>>>> *)arg;
/*
Wait on barrier before doing any reducing
in order to let the mappers end their processing.
*/
pthread_barrier_wait(reducerArg.reducerBarrier);
std::map<int, std::map<int, std::vector<long long>>> mapRes =
*(reducerArg.fromMappers);
// reduce
std::set<long long> individualPerfects;
for (const auto &x : mapRes) {
if (x.second.find(reducerArg.rId + 2) == x.second.end()) continue;
for (long el : x.second.at(reducerArg.rId + 2)) {
individualPerfects.insert(el);
}
}
// write size of perfect numbers to output file
std::ofstream reducerOutputFile("out" + std::to_string(reducerArg.rId + 2) + ".txt");
reducerOutputFile << individualPerfects.size();
pthread_exit(NULL);
}