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
|
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/matrix_sparse.hpp>
#include <boost/numeric/ublas/triangular.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/timer/timer.hpp>
namespace ublas = boost::numeric::ublas;
template<class mat, class vec>
double diff(const mat& A, const vec& x, const vec& b) {
vec temp(prod(A, x) - b);
double result = 0;
for (typename vec::size_type i=0; i<temp.size(); ++i) {
result += temp(i)*temp(i);
}
return std::sqrt(result);
}
template<class mat, class vec>
double diff(const vec& x, const mat& A, const vec& b) {
return diff(trans(A), x, b);
}
namespace ublas = boost::numeric::ublas;
int main() {
const int n=7000;
#if 1
ublas::compressed_matrix<double, ublas::row_major> mat_row_upp(n, n);
ublas::compressed_matrix<double, ublas::column_major> mat_col_upp(n, n);
ublas::compressed_matrix<double, ublas::row_major> mat_row_low(n, n);
ublas::compressed_matrix<double, ublas::column_major> mat_col_low(n, n);
#else
ublas::matrix<double, ublas::row_major> mat_row_upp(n, n, 0);
ublas::matrix<double, ublas::column_major> mat_col_upp(n, n, 0);
ublas::matrix<double, ublas::row_major> mat_row_low(n, n, 0);
ublas::matrix<double, ublas::column_major> mat_col_low(n, n, 0);
#endif
ublas::vector<double> b(n, 1);
std::cerr << "Constructing..." << std::endl;
for (int i=0; i<n; ++i) {
b(i) = std::rand() % 10;
double main = -10 + std::rand() % 20 ;
if (main == 0) main+=1;
double side = -10 + std::rand() % 20 ;
if (i-1>=0) {
mat_row_low(i, i-1) = side;
}
mat_row_low(i, i) = main;
mat_col_low(i, i) = main;
if (i+1<n) {
mat_col_low(i+1, i) = side;
}
mat_row_upp(i, i) = main;
if (i+1<n) {
mat_row_upp(i, i+1) = side;
}
if (i-1>=0) {
mat_col_upp(i-1, i) = side;
}
mat_col_upp(i, i) = main;
}
std::cerr << "Starting..." << std::endl;
{
boost::timer::auto_cpu_timer t(std::cerr, "col_low x: %t sec CPU, %w sec real\n");
ublas::vector<double> x(b);
ublas::inplace_solve(mat_col_low, x, ublas::lower_tag());
std::cerr << "delta: " << diff(mat_col_low, x, b) << "\n";
}
{
boost::timer::auto_cpu_timer t(std::cerr, "row_low x: %t sec CPU, %w sec real\n");
ublas::vector<double> x(b);
ublas::inplace_solve(mat_row_low, x, ublas::lower_tag());
std::cerr << "delta: " << diff(mat_row_low, x, b) << "\n";
}
{
boost::timer::auto_cpu_timer t(std::cerr, "col_upp x: %t sec CPU, %w sec real\n");
ublas::vector<double> x(b);
ublas::inplace_solve(mat_col_upp, x, ublas::upper_tag());
std::cerr << "delta: " << diff(mat_col_upp, x, b) << "\n";
}
{
boost::timer::auto_cpu_timer t(std::cerr, "row_upp x: %t sec CPU, %w sec real\n");
ublas::vector<double> x(b);
ublas::inplace_solve(mat_row_upp, x, ublas::upper_tag());
std::cerr << "delta: " << diff(mat_row_upp, x, b) << "\n";
}
{
boost::timer::auto_cpu_timer t(std::cerr, "x col_low: %t sec CPU, %w sec real\n");
ublas::vector<double> x(b);
ublas::inplace_solve(x, mat_col_low, ublas::lower_tag());
std::cerr << "delta: " << diff(x, mat_col_low, b) << "\n";
}
{
boost::timer::auto_cpu_timer t(std::cerr, "x row_low: %t sec CPU, %w sec real\n");
ublas::vector<double> x(b);
ublas::inplace_solve(x, mat_row_low, ublas::lower_tag());
std::cerr << "delta: " << diff(x, mat_row_low, b) << "\n";
}
{
boost::timer::auto_cpu_timer t(std::cerr, "x col_upp: %t sec CPU, %w sec real\n");
ublas::vector<double> x(b);
ublas::inplace_solve(x, mat_col_upp, ublas::upper_tag());
std::cerr << "delta: " << diff(x, mat_col_upp, b) << "\n";
}
{
boost::timer::auto_cpu_timer t(std::cerr, "x row_upp: %t sec CPU, %w sec real\n");
ublas::vector<double> x(b);
ublas::inplace_solve(x, mat_row_upp, ublas::upper_tag());
std::cerr << "delta: " << diff(x, mat_row_upp, b) << "\n";
}
}
|