-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaxpy.cpp
executable file
·106 lines (81 loc) · 2.42 KB
/
axpy.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
#include<DD-AVX.hpp>
#include<vector>
#include<iostream>
#include<chrono>
#define TOL 1.0e-6
std::vector<double> make_ans(const double alpha, const std::vector<double>& x){
std::vector<double> y(x.size(), 0.0);
#pragma omp parallel for
for(int i=0; i<y.size(); i++){
y[i] = alpha * x[i] + y[i];
}
return y;
}
bool err_check(const std::vector<double>& ans, const std::vector<double>& val, const double tol){
for(int i=0; i<ans.size(); i++){
double err = fabs((val[i] - ans[i])) / fabs(ans[i]);
if(err > tol){
printf("ans[%d] = %e, data[%d] = %e, err = %e\n", i, ans[i], i, val[i], err);
return false;
}
}
return true;
}
template<typename ALPHA, typename X, typename Y>
int test(long N)
{
ALPHA alpha = rand();
X x;
Y y;
for(int i=0; i<N; i++)
x.push_back(rand());
for(int i=0; i<N; i++)
y.push_back(0);
dd_avx::axpy(alpha, x, y);
auto ref = make_ans(alpha, x.HI());
if(!err_check(ref, y.HI(), TOL)){
std::cout << "...fail" << std::endl;
return false;
}
auto start = std::chrono::system_clock::now();
for(int i=0; i<100; i++)
dd_avx::axpy(alpha, x, y);
auto end = std::chrono::system_clock::now();
double sec = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count()/1.0e+9/100;
std::cout << "...pass\t" << sec << std::endl;
return true;
}
int main(int argc, char** argv){
bool ret=0;
if(argc!=2){
std::cout << "error, $1 = size" << std::endl;
return 1;
}
long N = atoi(argv[1]);
std::cout << "axpy, size = " << N << std::endl;
std::cout << "DD, DD, DD" << std::flush;
ret = test<dd_real, dd_real_vector, dd_real_vector>(N);
if(!ret) return 1;
std::cout << "DD, DD, D" << std::flush;
ret = test<dd_real, dd_real_vector, d_real_vector>(N);
if(!ret) return 1;
std::cout << "DD, D, DD" << std::flush;
ret = test<dd_real, d_real_vector, dd_real_vector>(N);
if(!ret) return 1;
std::cout << "DD, D, D" << std::flush;
ret = test<dd_real, d_real_vector, d_real_vector>(N);
if(!ret) return 1;
std::cout << "D, DD, DD" << std::flush;
ret = test<d_real, dd_real_vector, dd_real_vector>(N);
if(!ret) return 1;
std::cout << "D, DD, D" << std::flush;
ret = test<d_real, dd_real_vector, d_real_vector>(N);
if(!ret) return 1;
std::cout << "D, D, DD" << std::flush;
ret = test<d_real, d_real_vector, dd_real_vector>(N);
if(!ret) return 1;
std::cout << "D, D, D" << std::flush;
ret = test<d_real, d_real_vector, d_real_vector>(N);
if(!ret) return 1;
return 0;
}