-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec.cpp
102 lines (89 loc) · 2.31 KB
/
vec.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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <hpx/hpx_init.hpp>
#include <hpx/include/actions.hpp>
#include <hpx/include/util.hpp>
#include <hpx/include/lcos.hpp>
#include <hpx/util/unwrapped.hpp>
#include <hpx/include/iostreams.hpp>
std::vector<double> vector(int n)
{
std::vector<double> out;
for(int i = 0; i < n; i++)
{
double r = (double)(rand())/((double)(rand()+1));
out.push_back(r);
}
return out;
}
double dotproduct(std::vector<double> a, std::vector<double> b)
{
double out = 0;
if(a.size() != b.size()) ;// error
for(int i = 0; i < (int)a.size(); i++)
out += a[i] * b[i];
return out;
}
double parallel_dotproduct(std::vector<double> vector1, std::vector<double> vector2, int start, int stop, int thresh)
{
double out = 0;
if(stop-start <= thresh)
if(stop-start > 0)
for(int i = start; i <= stop; i++)
out += vector1[i] * vector2[i];
//out += 1;
else out = vector1[start] * vector2[start];
//out = 1;
else
{
hpx::lcos::future<double> left = hpx::async(¶llel_dotproduct,vector1,vector2,start,(int)((start+stop)/2),thresh);
hpx::lcos::future<double> right = hpx::async(¶llel_dotproduct,vector1,vector2,(int)((start+stop)/2)+1,stop,thresh);
out = left.get() + right.get();
}
return out;
}
int hpx_main()
{
hpx::util::high_resolution_timer t;
//Length of vectors
double len;
std::cin >> len;
//Minimum threshold
double minthresh = len/16;
//Initialization of vectors
std::cout << "Initializing Vector 1...\n";
std::vector<double> a = vector(len);
std::cout << "Initializing Vector 2...\n";
std::vector<double> b = vector(len);
double n;
//Serial Test
{
double x = t.elapsed();
double c = dotproduct(a,b);
double y = t.elapsed();
n = y-x;
std::cout << "Result: " << c << "\n";
std::cout << "Serial: " << n << "\n\n";
}
//Parallel Tests
for(double thresh = len*2; thresh >= ceil(minthresh); thresh /= 2)
{
double x = t.elapsed();
double c = parallel_dotproduct(a,b,0,a.size()-1,thresh);
double y = t.elapsed();
double m = y-x;
std::cout << "Result: " << c << "\n";
std::cout << "Parallel: " << m << " (t=" << thresh << ")\n";
double q = n/(m+0.000001);
std::cout << "Ratio: " << q << "\n\n";
}
return hpx::finalize();
}
int main()
{
return hpx::init();
return 0;
}