BiCGSolver.hh 4.73 KB
Newer Older
1
2
3
4
5
#include "Preconditioner.h"

namespace AMDiS {

  template<typename VectorType>
6
  BiCGSolver<VectorType>::BiCGSolver(std::string name)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
    : OEMSolver<VectorType>(name),
      rstar(NULL), d(NULL), s(NULL), CAd(NULL), h(NULL)
  {
  }

  template<typename VectorType>
  BiCGSolver<VectorType>::~BiCGSolver()
  {}

  template<typename VectorType>
  void BiCGSolver<VectorType>::init()
  {
    rstar = this->vectorCreator->create();
    d     = this->vectorCreator->create();
    s     = this->vectorCreator->create();
    CAd   = this->vectorCreator->create();
    h     = this->vectorCreator->create();
  }

  template<typename VectorType>
  void BiCGSolver<VectorType>::exit()
  {
    this->vectorCreator->free(rstar);
    this->vectorCreator->free(d);
    this->vectorCreator->free(s);
    this->vectorCreator->free(CAd);
    this->vectorCreator->free(h);
  }

  template<typename VectorType>
  int BiCGSolver<VectorType>::solveSystem(MatVecMultiplier<VectorType> *matVec,
					  VectorType *x, VectorType *b)
  {
    FUNCNAME("BiCGSolver::solveSystem");
    VectorType *t = h;

    double res, old_res = -1.0;
    double rh1, rh2, dad, alpha, ast, att, omega, beta;
    int    iter;

    const double TOL = 1.e-30;

    /*--------------------------------------------------------------------------*/
    /*---  Initalization  ------------------------------------------------------*/
    /*--------------------------------------------------------------------------*/

    if (norm(b) < TOL)
      {
	INFO(this->info,2)("b == 0, x = 0 is the solution of the linear system\n");
	set(*x, 0.0);
	this->residual = 0.0;
	return(0);
      }

    // h = Ax
    matVec->matVec(NoTranspose, *x, *h);

    // h -= b
    *h -= *b;

    // rstar = h;
    *rstar = *h;

    // preconditioning
    if(this->leftPrecon) this->leftPrecon->precon(h);
    if(this->rightPrecon) this->rightPrecon->precon(b);

    /*--------------------------------------------------------------------------*/
    /*---  check initial residual  ---------------------------------------------*/
    /*--------------------------------------------------------------------------*/

    res = norm(h);

    START_INFO();
    if (SOLVE_INFO(0, res, &old_res))
      return(0);

    // d = h
    *d = *h;

    // rh1 = h*rstar
    rh1 = *h * *rstar;

    /*--------------------------------------------------------------------------*/
    /*---  Iteration  ----------------------------------------------------------*/
    /*--------------------------------------------------------------------------*/

    for (iter = 1; iter <= this->max_iter; iter++)
      {
	/*--------------------------------------------------------------------------*/
	/*---  calculate CA.d and CA.d*rstar  --------------------------------------*/
	/*--------------------------------------------------------------------------*/

	// CAd = Ad
	matVec->matVec(NoTranspose, *d, *CAd);

	if(this->leftPrecon) this->leftPrecon->precon(CAd);

	// dad = CAd * rstar;
	dad = *CAd * *rstar;

	if (abs(rh1) < TOL) {
	  BREAK_INFO("(h,r^*)_2 = 0", iter, res, &old_res);
	  return(iter);
	}
	if (abs(dad) < TOL) {
	  BREAK_INFO("(Ad,d^*)_2 = 0", iter, res, &old_res);
	  return(iter);
	}

	/*-------------------------------------------------------------------------*/
	/*---  update s and t  ----------------------------------------------------*/
	/*-------------------------------------------------------------------------*/

	alpha = rh1/dad;
    
	// s = h
	*s = *h;

	// s = -alpha CAd + s
	axpy(-alpha, *CAd, *s);
    
	// t = As
	matVec->matVec(NoTranspose, *s, *t);

	/*--------------------------------------------------------------------------*/
	/*---  calculate omega  ----------------------------------------------------*/
	/*--------------------------------------------------------------------------*/

	// ast = s*t
	ast = *s * *t;

	// att = t*t
	att = *t * *t;
    
	if (abs(ast) < TOL) {
	  INFO(this->info,2)("omega = 0");
	  ast = ast > 0 ? TOL : -TOL;
	}
    
	if (att < TOL)
	  {
	    INFO(this->info,2)("t = 0");
	    att = TOL;
	  }

	omega= ast/att;
    
	/*--------------------------------------------------------------------------*/
	/*---   update x and calculate new h  --------------------------------------*/
	/*--------------------------------------------------------------------------*/

	// x = -alpha d + x
	axpy(-alpha, *d, *x);

	// x = -omega s + x
	axpy(-omega, *s, *x);

	// h = s - omega * h
	*h *= -omega;
	*h += *s;
    
	res = norm(h);
	if (SOLVE_INFO(iter, res, &old_res))
	  return(iter);

	if (this->leftPrecon) this->leftPrecon->precon(t);

	// rh2 = h * rstar
	rh2 = *h * *rstar;
    
	beta= (rh2/rh1)* (alpha/omega);

	// d = -omega CAd + d
	axpy(-omega, *CAd, *d);
    
	// d = h + beta d
	*d *= beta;
	*d += *h;

	rh1 = rh2;
      }

    return 0; // never reached !
  }

}