-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
171 lines (138 loc) · 3.58 KB
/
main.cpp
File metadata and controls
171 lines (138 loc) · 3.58 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
#include <iostream>
#include <fstream>
#include <cmath>
#include <set>
#include "map_reduce.h"
#define ulli unsigned long long int
struct mapperSpecifications
{
std::string fileName;
int reducerCount;
};
struct Accumulator
{
int id;
std::set<int> values;
};
double quickPower(double base, int exponent)
{
if(exponent == 0)
return 1.0;
if( exponent % 2 == 0 )
return quickPower(base*base, exponent/2);
else
return base*quickPower(base*base, exponent/2);
}
double root(double number, double order, double error = 0.1)
{
if( order == 2 )
return sqrt(number);
double x = 1;
while (true)
{
double h = ( quickPower(x, order) - number )/( order*quickPower(x, order - 1) );
if( std::abs(h) < error )
return x;
x = x - h;
}
}
ulli quickPow(ulli base, ulli exponent)
{
if( exponent == 0 )
return 1LL;
if( exponent % 2 == 0 )
return quickPow(base*base, exponent/2);
else
return base*quickPow(base*base, exponent/2);
}
bool isPerfectPower(ulli number, ulli exponent)
{
if( number == 1 )
return true;
ulli val = root(number, exponent);
bool ok = false;
for( ulli i = val - 2; i <= val + 2; i++ )
ok = ok || (number == quickPow(i, exponent));
return ok;
}
void* mapperJob(void* specs)
{
auto mArgs = (mapperSpecifications*)specs;
auto result = new std::map<int, std::set<int>>;
std::ifstream fin(mArgs->fileName);
int n; fin>>n;
for( int i = 0; i < n; i++ )
{
int value; fin>>value;
if( value > 0 )
for( int j = 2; j <= mArgs->reducerCount + 1; j++ )
if(((*result)[j].find(value) == (*result)[j].end()) && isPerfectPower(value, j) )
(*result)[j].insert(value);
}
fin.close();
delete mArgs;
return result;
}
std::vector<task> convert(void *x)
{
auto res = (std::map<int, std::set<int>>*)x;
std::vector<task> tasks;
for(auto & re : *res)
{
auto s = new std::set<int>(re.second.begin(), re.second.end());
task t = {re.first, s, nullptr};
tasks.push_back(t);
}
delete res;
return tasks;
}
void accumulate(void *result, void *args)
{
auto res = (std::set<int>*)result;
auto acc = (Accumulator*)args;
for( int x : (*res) )
acc->values.insert(x);
delete res;
}
void* writeToFile(void *args)
{
auto acc = (Accumulator*)args;
std::ofstream fout("out" + std::to_string(acc->id) + ".txt");
fout<<acc->values.size();
fout.close();
delete acc;
return nullptr;
}
int main(int argc, char **argv)
{
if( argc != 4 )
{
std::cout<<"exec M R fileName\n";
return 1;
}
int M = std::stoi(argv[1]);
int R = std::stoi(argv[2]);
std::ifstream fin(argv[3]);
workpool mapperWorkpool, reducerWorkpool;
int fileCount; fin>>fileCount;
for( int i = 0; i < fileCount; i++ )
{
auto *mSpecs = new mapperSpecifications;
mSpecs->reducerCount = R;
fin>>mSpecs->fileName;
mapperWorkpool.putTask({0, mSpecs, mapperJob});
}
mapperArgs mArgs {0, &mapperWorkpool, &reducerWorkpool, convert, nullptr};
std::vector<reducerArgs> rArgs;
for( int i = 0; i < R; i++ )
{
auto acc = new Accumulator;
acc->id = i + 2;
acc->values = std::set<int>();
reducerArgs rArg {i + 2, &reducerWorkpool, acc, accumulate, writeToFile, nullptr};
rArgs.push_back(rArg);
}
mapreduceArgs mrArgs {M, mArgs, R, rArgs};
map_reduce::start(&mrArgs);
return 0;
}