-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
115 lines (110 loc) · 4.02 KB
/
main.cpp
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
#include <vector>
#include <algorithm>
#include <random>
#include <chrono>
#include <ctime>
#include <iostream>
std::vector<float> dumbMult(std::vector<float> &poly1, std::vector<float> &poly2);
std::vector<float> fourMult(std::vector<float> &poly1, std::vector<float> &poly2);
std::vector<float> threeMult(std::vector<float> &poly1, std::vector<float> &poly2);
int main(int argc, char **argv){
if(argc < 3){
std::cout<<"Not enough arguments"<<std::endl;
return -1;
}
int POLYTERMS = atoi(argv[2]);
if(POLYTERMS < 2 || (POLYTERMS & (POLYTERMS - 1))){
std::cout<<"Problem size must be a power of 2\n";
return -1;
}
std::mt19937 rng(time(NULL));
std::uniform_real_distribution<> dist(-1, 1);
std::vector<float> times;
for(int i = 0; i < 10; ++i){
std::vector<float> poly1(POLYTERMS), poly2(POLYTERMS);
std::generate(poly1.begin(), poly1.end(), [&](){return dist(rng);});
std::generate(poly2.begin(), poly2.end(), [&](){return dist(rng);});
std::vector<float> temp;
auto start = std::chrono::steady_clock::now();
switch(argv[1][1]){
case 'd': temp = dumbMult(poly1, poly2); break;
case '4': temp = fourMult(poly1, poly2); break;
case '3': temp = threeMult(poly1, poly2); break;
default: "Invalid algorithm argument\n"; return -1;
}
times.push_back(std::chrono::duration_cast<std::chrono::duration<float>>(std::chrono::steady_clock::now() - start).count());
// std::cout<<"Ran in "<<(std::chrono::duration_cast<std::chrono::duration<float>>(std::chrono::steady_clock::now() - start).count())<<" seconds"<<std::endl;
}
std::cout<<"Mean time: "<<(std::accumulate(times.begin(), times.end(), 0.0) / times.size())<<" seconds"<<std::endl;
// std::cout<<"Resulting coefficients: ";
// for(auto &term : temp){
// std::cout<<term<<" ";
// }
// std::cout<<std::endl;
}
std::vector<float> dumbMult(std::vector<float> &poly1, std::vector<float> &poly2){
std::vector<float> result(2 * poly1.size() - 1, 0);
for(int i = 0; i < poly1.size(); ++i){
for(int j = 0; j < poly2.size(); ++j){
result[i + j] += poly1[i] * poly2[j];
}
}
return result;
}
std::vector<float> fourMult(std::vector<float> &poly1, std::vector<float> &poly2){
if(poly1.size() == 1){
return std::vector<float> {poly1[0] * poly2[0]};
}
std::vector<float> p1Low(&poly1[0], &poly1[poly1.size() / 2]);
std::vector<float> p1High(&poly1[poly1.size() / 2], &poly1[poly1.size()]);
std::vector<float> p2Low(&poly2[0], &poly2[poly2.size() / 2]);
std::vector<float> p2High(&poly2[poly2.size() / 2], &poly2[poly2.size()]);
std::vector<float> result(2 * poly1.size() - 1, 0);
auto temp = fourMult(p1Low, p2Low);
for(int i = 0; i < temp.size(); ++i){
result[i] += temp[i];
}
temp = fourMult(p1High, p2High);
for(int i = 0; i < temp.size(); ++i){
result[poly1.size() + i] += temp[i];
}
temp = fourMult(p1Low, p2High);
for(int i = 0; i < temp.size(); ++i){
result[poly1.size() / 2 + i] += temp[i];
}
temp = fourMult(p1High, p2Low);
for(int i = 0; i < temp.size(); ++i){
result[poly1.size() / 2 + i] += temp[i];
}
return result;
}
std::vector<float> threeMult(std::vector<float> &poly1, std::vector<float> &poly2){
if(poly1.size() == 1){
return std::vector<float> {poly1[0] * poly2[0]};
}
std::vector<float> p1Low(&poly1[0], &poly1[poly1.size() / 2]);
std::vector<float> p1High(&poly1[poly1.size() / 2], &poly1[poly1.size()]);
std::vector<float> p2Low(&poly2[0], &poly2[poly2.size() / 2]);
std::vector<float> p2High(&poly2[poly2.size() / 2], &poly2[poly2.size()]);
std::vector<float> result(2 * poly1.size() - 1, 0);
auto lowLow = fourMult(p1Low, p2Low);
for(int i = 0; i < lowLow.size(); ++i){
result[i] += lowLow[i];
}
auto highHigh = fourMult(p1High, p2High);
for(int i = 0; i < highHigh.size(); ++i){
result[poly1.size() + i] += highHigh[i];
}
for(int i = 0; i < p1Low.size(); ++i){
p1Low[i] += p1High[i];
p2Low[i] += p2High[i];
}
auto temp = fourMult(p1Low, p2Low);
for(int i = 0; i < temp.size(); ++i){
temp[i] -= (lowLow[i] + highHigh[i]);
}
for(int i = 0; i < temp.size(); ++i){
result[poly1.size() / 2 + i] += temp[i];
}
return result;
}