-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_iterative_solvers_test.cpp
128 lines (103 loc) · 4.51 KB
/
block_iterative_solvers_test.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
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <cmath>
#include <string>
#include <random>
#include <chrono>
#include <fstream>
#include <tuple>
#include <Eigen/Dense>
#include <Eigen/Sparse>
#include <unsupported/Eigen/SparseExtra>
#include "block_iterative_solvers.hpp"
#include "linear_algebra_addon.hpp"
using namespace std;
using namespace Eigen;
using namespace std::chrono;
int main(int argc, char *argv[]){
double tol= 1e-8;
int itermax= 1000;
std::srand((unsigned int) time(0));
int L=atoi(argv[1]); // dimension of matrix A
MatrixXcd A;
switch (argc)
{
case 2:
{
// load A from a file in matrix market format .mtx
cout << "please enter filename for A (.mtx): " ;
string matfname;
cin >> matfname;
SparseMatrix<complex<double>> mat;
//Matrix collections is available in http://www.cise.ufl.edu/research/sparse/matrices/
if(!loadMarket(mat,matfname)){
cerr << "can't open file" << matfname << endl;
exit(EXIT_FAILURE);
}
cout << "A was loaded from " << matfname << endl;
A= MatrixXcd(mat);
break;
}
case 3:
{
// Set A as a random matrix
int n=atoi(argv[2]); // number of RHS vector
A= MatrixXcd::Random(n,n);
A= A.transpose()*A; // complex-symmetric
cout << "A is a " << n << " x " << n << " square random matrix" << endl;
break;
}
default:
{
break;
}
}
MatrixXcd B= MatrixXcd::Random(A.rows(),L);
{
time_point<steady_clock> start= steady_clock::now();
cout << "solving AX=B using bl_cocg_rq ..." << endl;
MatrixXcd X= bl_cocg_rq(A, B, tol, itermax); // only applicable to complex-symmetric matrix A
cout << " bl_cocg_rq relative error: " << (A*X-B).norm()/B.norm() << endl << endl;
auto end= steady_clock::now();
cout << "bl_cocg_rq computation time= " << duration_cast<seconds>((end - start)).count() << " sec" << endl << endl<< endl;
}
{
time_point<steady_clock> start= steady_clock::now();
cout << "solving AX=B using bl_bicg_rq ..." << endl;
MatrixXcd X= bl_bicg_rq(A, B, tol, itermax); // applicable to general matrix A but needs A.adjoint()
cout << " bl_bicg_rq relative error: " << (A*X-B).norm()/B.norm() << endl << endl;
auto end= steady_clock::now();
cout << "bl_bicg_rq computation time= " << duration_cast<seconds>((end - start)).count() << " sec" << endl << endl<< endl;
}
{
time_point<steady_clock> start= steady_clock::now();
cout << "solving AX=B using bl_bicr_rq ..." << endl;
MatrixXcd X= bl_bicr_rq(A, B, tol, itermax); // applicable to general matrix A but needs A.adjoint()
cout << " bl_bicr_rq relative error: " << (A*X-B).norm()/B.norm() << endl << endl;
auto end= steady_clock::now();
cout << "bl_bicr_rq computation time= " << duration_cast<seconds>((end - start)).count() << " sec" << endl << endl<< endl;
}
{
time_point<steady_clock> start= steady_clock::now();
cout << "solving AX=B using bl_bicgstab_rq ..." << endl;
MatrixXcd X= bl_bicgstab_rq(A, B, tol, itermax); // only applicable to complex-symmetric matrix A
cout << " bl_bicgstab_rq relative error: " << (A*X-B).norm()/B.norm() << endl << endl;
auto end= steady_clock::now();
cout << "bl_bicgstab_rq computation time= " << duration_cast<seconds>((end - start)).count() << " sec" << endl << endl<< endl;
}
{
time_point<steady_clock> start= steady_clock::now();
cout << "solving AX=B using bl_bicgstab ..." << endl;
MatrixXcd X= bl_bicgstab(A, B, tol, itermax); // only applicable to complex-symmetric matrix A
cout << " bl_bicgstab relative error: " << (A*X-B).norm()/B.norm() << endl << endl;
auto end= steady_clock::now();
cout << "bl_bicgstab computation time= " << duration_cast<seconds>((end - start)).count() << " sec" << endl << endl<< endl;
}
{
time_point<steady_clock> start= steady_clock::now();
cout << "solving AX=B using bl_bicggr ..." << endl;
MatrixXcd X= bl_bicggr(A, B, tol, itermax); // only applicable to complex-symmetric matrix A
cout << " bl_bicggr relative error: " << (A*X-B).norm()/B.norm() << endl << endl;
auto end= steady_clock::now();
cout << "bl_bicggr computation time= " << duration_cast<seconds>((end - start)).count() << " sec" << endl << endl<< endl;
}
}