solver_bcgsl.hpp 40.9 KB
Newer Older
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1

Kirill Terekhov's avatar
Kirill Terekhov committed
2
3
4
#ifndef __SOLVER_BCGS__
#define __SOLVER_BCGS__

Kirill Terekhov's avatar
Kirill Terekhov committed
5
6
7
8
//\todo
// 1. comply solvers with Method prototype, after TODO in solver_prototypes.hpp is done
// 2. Implement tricks from Read/solver/bcgsl/download.pdf with convex update and true residual correction
// 3. Detect numerical accuracy breakdown - when preconditioned residual is too far from true residual (probably 2 will fix).
Kirill Terekhov's avatar
Kirill Terekhov committed
9
10
11

#include "inmost_solver.h"

Kirill Terekhov's avatar
Kirill Terekhov committed
12
13
//#define CONVEX_COMBINATION
#define PSEUDOINVERSE  // same trick as in petsc with pseudoinverse
Kirill Terekhov's avatar
Kirill Terekhov committed
14
15
//#define USE_LAPACK_SVD // use lapack's dgesvd routine instead of built-in svdnxn

Kirill Terekhov's avatar
Kirill Terekhov committed
16
//#if !defined(NDEBUG)
Kirill Terekhov's avatar
Kirill Terekhov committed
17
#define REPORT_RESIDUAL
Kirill Terekhov's avatar
Kirill Terekhov committed
18
//#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
19
20
//#define USE_OMP

Kirill Terekhov's avatar
Kirill Terekhov committed
21
22
23

namespace INMOST
{
Kirill Terekhov's avatar
Kirill Terekhov committed
24
25
26
27
28
29
30
	//lapack svd
#if defined(PSEUDOINVERSE)
#if defined(USE_LAPACK_SVD)
	extern "C"
	{
		void dgesvd_(char*,char*,int*,int*,double*,int*,double*,double*,int*,double*,int*,double*,int*,int*);
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
31
32
33
34
35
#else
	//SVD adopted from http://www.public.iastate.edu/~dicook/JSS/paper/code/svd.c
	static INMOST_DATA_REAL_TYPE SIGNFUNC(INMOST_DATA_REAL_TYPE a, INMOST_DATA_REAL_TYPE b) { return b >= 0.0 ? fabs(a) : -fabs(a); }
	static INMOST_DATA_REAL_TYPE MAXFUNC(INMOST_DATA_REAL_TYPE x,INMOST_DATA_REAL_TYPE y) { return x > y? x : y; }
	static INMOST_DATA_REAL_TYPE PYTHAG(INMOST_DATA_REAL_TYPE a, INMOST_DATA_REAL_TYPE b)
Kirill Terekhov's avatar
Kirill Terekhov committed
36
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
37
38
39
40
41
		INMOST_DATA_REAL_TYPE at = fabs(a), bt = fabs(b), ct, result;
		if (at > bt)       { ct = bt / at; result = sqrt(at) * sqrt(at + ct * bt); }
		else if (bt > 0.0) { ct = at / bt; result = sqrt(bt) * sqrt(bt + ct * at); }
		else result = 0.0;
		return(result);
Kirill Terekhov's avatar
Kirill Terekhov committed
42
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
43
	int svdnxn(INMOST_DATA_REAL_TYPE * pa,INMOST_DATA_REAL_TYPE * pu, INMOST_DATA_REAL_TYPE *pw, INMOST_DATA_REAL_TYPE * pv, const int n)
Kirill Terekhov's avatar
Kirill Terekhov committed
44
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
45
46
47
48
		shell<INMOST_DATA_REAL_TYPE> a(pa,n*n);
		shell<INMOST_DATA_REAL_TYPE> u(pu,n*n);
		shell<INMOST_DATA_REAL_TYPE> v(pv,n*n);
		shell<INMOST_DATA_REAL_TYPE> w(pw,n);
Kirill Terekhov's avatar
Kirill Terekhov committed
49
		//std::copy(a.begin(),a.end(),u.begin());
Kirill Terekhov's avatar
Kirill Terekhov committed
50
		//memcpy(u,a,sizeof(INMOST_DATA_REAL_TYPE)*n*n);
Kirill Terekhov's avatar
Kirill Terekhov committed
51
52
53
		int flag, i, its, j, jj, k, l, nm;
		INMOST_DATA_REAL_TYPE c, f, h, s, x, y, z;
		INMOST_DATA_REAL_TYPE anorm = 0.0, g = 0.0, scale = 0.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
54
55
		dynarray<INMOST_DATA_REAL_TYPE,64> rv1;
    rv1.resize(n);
Kirill Terekhov's avatar
Kirill Terekhov committed
56
		// Householder reduction to bidiagonal form
Kirill Terekhov's avatar
Kirill Terekhov committed
57
		for (i = 0; i < n*n; ++i) u[i] = a[i];
Kirill Terekhov's avatar
Kirill Terekhov committed
58
		for (i = 0; i < n; i++) 
Kirill Terekhov's avatar
Kirill Terekhov committed
59
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
60
61
62
63
64
			// left-hand reduction
			l = i + 1;
			rv1[i] = scale * g;
			g = s = scale = 0.0;
			if (i < n) 
Kirill Terekhov's avatar
Kirill Terekhov committed
65
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
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
				for (k = i; k < n; k++) 
					scale += fabs(u[k*n+i]);
				if (scale) 
				{
					for (k = i; k < n; k++) 
					{
						u[k*n+i] = u[k*n+i]/scale;
						s += (u[k*n+i] * u[k*n+i]);
					}
					f = u[i*n+i];
					g = -SIGNFUNC(sqrt(s), f);
					h = f * g - s;
					u[i*n+i] = (f - g);
					if (i != n - 1) 
					{
						for (j = l; j < n; j++) 
						{
							for (s = 0.0, k = i; k < n; k++) 
								s += (u[k*n+i] * u[k*n+j]);
							f = s / h;
							for (k = i; k < n; k++) 
								u[k*n+j] += (f * u[k*n+i]);
						}
					}
					for (k = i; k < n; k++) 
						u[k*n+i] = (u[k*n+i]*scale);
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
93
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
94
			w[i] = (scale * g);
Kirill Terekhov's avatar
Kirill Terekhov committed
95

Kirill Terekhov's avatar
Kirill Terekhov committed
96
97
98
			// right-hand reduction 
			g = s = scale = 0.0;
			if (i < n && i != n - 1) 
Kirill Terekhov's avatar
Kirill Terekhov committed
99
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
100
101
102
				for (k = l; k < n; k++) 
					scale += fabs(u[i*n+k]);
				if (scale) 
Kirill Terekhov's avatar
Kirill Terekhov committed
103
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
104
					for (k = l; k < n; k++) 
Kirill Terekhov's avatar
Kirill Terekhov committed
105
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
106
107
						u[i*n+k] = (u[i*n+k]/scale);
						s += (u[i*n+k] * u[i*n+k]);
Kirill Terekhov's avatar
Kirill Terekhov committed
108
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
109
110
111
112
113
114
115
					f = u[i*n+l];
					g = -SIGNFUNC(sqrt(s), f);
					h = f * g - s;
					u[i*n+l] = (f - g);
					for (k = l; k < n; k++) 
						rv1[k] = u[i*n+k] / h;
					if (i != n - 1) 
Kirill Terekhov's avatar
Kirill Terekhov committed
116
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
117
118
119
120
121
122
123
						for (j = l; j < n; j++) 
						{
							for (s = 0.0, k = l; k < n; k++) 
								s += (u[j*n+k] * u[i*n+k]);
							for (k = l; k < n; k++) 
								u[j*n+k] += (s * rv1[k]);
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
124
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
125
126
					for (k = l; k < n; k++) 
						u[i*n+k] = (u[i*n+k]*scale);
Kirill Terekhov's avatar
Kirill Terekhov committed
127
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
128
129
130
			}
			anorm = MAXFUNC(anorm, (fabs(w[i]) + fabs(rv1[i])));
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
131

Kirill Terekhov's avatar
Kirill Terekhov committed
132
133
134
135
136
137
		// accumulate the right-hand transformation 
		for (i = n - 1; i >= 0; i--) 
		{
			if (i < n - 1) 
			{
				if (g) 
Kirill Terekhov's avatar
Kirill Terekhov committed
138
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
139
140
141
142
					for (j = l; j < n; j++)
						v[j*n+i] = ((u[i*n+j] / u[i*n+l]) / g);
					// double division to avoid underflow 
					for (j = l; j < n; j++) 
Kirill Terekhov's avatar
Kirill Terekhov committed
143
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
144
145
146
147
						for (s = 0.0, k = l; k < n; k++) 
							s += (u[i*n+k] * v[k*n+j]);
						for (k = l; k < n; k++) 
							v[k*n+j] += (s * v[k*n+i]);
Kirill Terekhov's avatar
Kirill Terekhov committed
148
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
149
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
150
151
				for (j = l; j < n; j++) 
					v[i*n+j] = v[j*n+i] = 0.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
152
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
153
154
155
			v[i*n+i] = 1.0;
			g = rv1[i];
			l = i;
Kirill Terekhov's avatar
Kirill Terekhov committed
156
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
157

Kirill Terekhov's avatar
Kirill Terekhov committed
158
159
		// accumulate the left-hand transformation
		for (i = n - 1; i >= 0; i--) 
Kirill Terekhov's avatar
Kirill Terekhov committed
160
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
161
162
163
164
165
166
167
168
169
			l = i + 1;
			g = w[i];
			if (i < n - 1) 
				for (j = l; j < n; j++) 
					u[i*n+j] = 0.0;
			if (g) 
			{
				g = 1.0 / g;
				if (i != n - 1) 
Kirill Terekhov's avatar
Kirill Terekhov committed
170
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
171
172
173
174
175
176
177
178
					for (j = l; j < n; j++) 
					{
						for (s = 0.0, k = l; k < n; k++) 
							s += (u[k*n+i] * u[k*n+j]);
						f = (s / u[i*n+i]) * g;
						for (k = i; k < n; k++) 
							u[k*n+j] += (f * u[k*n+i]);
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
179
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
180
181
				for (j = i; j < n; j++) 
					u[j*n+i] = (u[j*n+i]*g);
Kirill Terekhov's avatar
Kirill Terekhov committed
182
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
183
			else 
Kirill Terekhov's avatar
Kirill Terekhov committed
184
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
185
186
187
188
189
				for (j = i; j < n; j++) 
					u[j*n+i] = 0.0;
			}
			++u[i*n+i];
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
190

Kirill Terekhov's avatar
Kirill Terekhov committed
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
		// diagonalize the bidiagonal form
		for (k = n - 1; k >= 0; k--) 
		{                             
			// loop over singular values 
			for (its = 0; its < 30; its++) 
			{                         
				// loop over allowed iterations
				flag = 1;
				for (l = k; l >= 0; l--) 
				{                     
					// test for splitting 
					nm = l - 1;
					if (fabs(rv1[l]) + anorm == anorm) 
					{
						flag = 0;
						break;
					}
					if (fabs(w[nm]) + anorm == anorm) 
						break;
				}
				if (flag) 
				{
					c = 0.0;
					s = 1.0;
					for (i = l; i <= k; i++) 
					{
						f = s * rv1[i];
						if (fabs(f) + anorm != anorm) 
						{
							g = w[i];
							h = PYTHAG(f, g);
							w[i] = h; 
							h = 1.0 / h;
							c = g * h;
							s = (- f * h);
							for (j = 0; j < n; j++) 
							{
								y = u[j*n+nm];
								z = u[j*n+i];
								u[j*n+nm] = (y * c + z * s);
								u[j*n+i] = (z * c - y * s);
							}
						}
					}
				}
				z = w[k];
				if (l == k) 
				{                  
					// convergence
					if (z < 0.0) 
					{              
						// make singular value nonnegative
						w[k] = (-z);
						for (j = 0; j < n; j++) 
							v[j*n+k] = (-v[j*n+k]);
					}
					break;
				}
				if (its >= 30) 
				{
					fprintf(stderr, "No convergence after 30,000! iterations \n");
					return 1;
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
254

Kirill Terekhov's avatar
Kirill Terekhov committed
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
				// shift from bottom 2 x 2 minor
				x = w[l];
				nm = k - 1;
				y = w[nm];
				g = rv1[nm];
				h = rv1[k];
				f = ((y - z) * (y + z) + (g - h) * (g + h)) / (2.0 * h * y);
				g = PYTHAG(f, 1.0);
				f = ((x - z) * (x + z) + h * ((y / (f + SIGNFUNC(g, f))) - h)) / x;

				// next QR transformation
				c = s = 1.0;
				for (j = l; j <= nm; j++) 
				{
					i = j + 1;
					g = rv1[i];
					y = w[i];
					h = s * g;
					g = c * g;
					z = PYTHAG(f, h);
					rv1[j] = z;
					c = f / z;
					s = h / z;
					f = x * c + g * s;
					g = g * c - x * s;
					h = y * s;
					y = y * c;
					for (jj = 0; jj < n; jj++) 
					{
						x = v[jj*n+j];
						z = v[jj*n+i];
						v[jj*n+j] = (x * c + z * s);
						v[jj*n+i] = (z * c - x * s);
					}
					z = PYTHAG(f, h);
					w[j] = z;
					if (z) 
					{
						z = 1.0 / z;
						c = f * z;
						s = h * z;
					}
					f = (c * g) + (s * y);
					x = (c * y) - (s * g);
					for (jj = 0; jj < n; jj++) 
					{
						y = u[jj*n+j];
						z = u[jj*n+i];
						u[jj*n+j] = (y * c + z * s);
						u[jj*n+i] = (z * c - y * s);
					}
				}
				rv1[l] = 0.0;
				rv1[k] = f;
				w[k] = x;
Kirill Terekhov's avatar
Kirill Terekhov committed
310
311
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
312
313

		for(int i = 0; i < n; i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
314
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
315
			INMOST_DATA_REAL_TYPE temp;
Kirill Terekhov's avatar
Kirill Terekhov committed
316
			for(int j = i + 1; j < n; j++)
Kirill Terekhov's avatar
Kirill Terekhov committed
317
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
318
319
320
				temp = u[i+n*j];
				u[i+n*j] = u[j+n*i];
				u[j+n*i] = temp;
Kirill Terekhov's avatar
Kirill Terekhov committed
321
322
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
323
		for(i = 0; i < n; i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
324
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
325
326
327
328
329
			k = i;
			for(j = i+1; j < n; ++j)
				if( w[k] < w[j] ) k = j;
			INMOST_DATA_REAL_TYPE temp;
			if( w[k] > w[i] )
Kirill Terekhov's avatar
Kirill Terekhov committed
330
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
331
332
333
334
335
336
337
338
339
340
341
342
				temp = w[k];
				w[k] = w[i];
				w[i] = temp;
				for(int j = 0; j < n; ++j)
				{
					temp = u[k*n+j];
					u[k*n+j] = u[i*n+j];
					u[i*n+j] = temp;
					temp = v[j*n+k];
					v[j*n+k] = v[j*n+i];
					v[j*n+i] = temp;
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
343
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
344
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
345
		return 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
346
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
347
#endif //USE_LAPACK_SVD
Kirill Terekhov's avatar
Kirill Terekhov committed
348
#else //PSEUDOINVERSE
Kirill Terekhov's avatar
Kirill Terekhov committed
349
	static int solvenxn(INMOST_DATA_REAL_TYPE * A, INMOST_DATA_REAL_TYPE * x, INMOST_DATA_REAL_TYPE * b, int n, int * order)
Kirill Terekhov's avatar
Kirill Terekhov committed
350
351
352
353
354
	{
		INMOST_DATA_REAL_TYPE temp, max;
		int temp2;
		for(int i = 0; i < n; i++) order[i] = i;
		for(int i = 0; i < n; i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
355
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
			int maxk = i, maxq = i;
			max = fabs(A[maxk*n+maxq]);
			//Find best pivot
			for(int q = i; q < n; q++) // over columns
			{
				for(int k = i; k < n; k++) // over rows
				{
					if( fabs(A[k*n+q]) > max )
					{
						max = fabs(A[k*n+q]);
						maxk = k;
						maxq = q;
					}
				}
			}
			//Exchange rows
			if( maxk != i ) 
			{
				for(int q = 0; q < n; q++)
				{
					temp = A[maxk*n+q];
					A[maxk*n+q] = A[i*n+q];
					A[i*n+q] = temp;
				}
				//exchange rhs
				{
					temp = b[maxk];
					b[maxk] = b[i];
					b[i] = temp;
				}
			}
			//Exchange columns
			if( maxq != i ) 
			{
				for(int k = 0; k < n; k++)
				{
					temp = A[k*n+maxq];
					A[k*n+maxq] = A[k*n+i];
					A[k*n+i] = temp;
				}
				//remember order in sol
				{
					temp2 = order[maxq];
					order[maxq] = order[i];
					order[i] = temp2;
				}
			}
			if( fabs(b[i]/A[i*n+i]) > 1.0e+100 )
				return i+1;
		
			for(int k = i+1; k < n; k++)
			{
				A[i*n+k] /= A[i*n+i];
				A[k*n+i] /= A[i*n+i];
			}
			for(int k = i+1; k < n; k++)
			for(int q = i+1; q < n; q++)
			{
				A[k*n+q] -= A[k*n+i] * A[i*n+i] * A[i*n+q];
			}
			for(int j = i+1; j < n; j++) //iterate over columns of L
			{
				b[j] -= b[i] * A[j*n+i];
			}
			b[i] /= A[i*n+i];
Kirill Terekhov's avatar
Kirill Terekhov committed
421
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
422
423
424
425
426
427
428
429

		for(int i = n-1; i >= 0; i--) //iterate over rows of U
			for(int j = i+1; j < n; j++) 
			{
				b[i] -= b[j] * A[i*n+j];
			}
		for(int i = 0; i < n; i++)
			x[order[i]] = b[i];
Kirill Terekhov's avatar
Kirill Terekhov committed
430
	
Kirill Terekhov's avatar
Kirill Terekhov committed
431
432
		return 0;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
433
#endif //PSEUDOINVERSE
Kirill Terekhov's avatar
Kirill Terekhov committed
434
435
436
437
438
439
	class BCGSL_solver : public IterativeMethod
	{
		INMOST_DATA_REAL_TYPE rtol, atol, divtol, last_resid;
		INMOST_DATA_ENUM_TYPE iters, maxits, l, last_it;
		INMOST_DATA_REAL_TYPE resid;
		INMOST_DATA_REAL_TYPE * tau, * sigma, * gamma, *theta1, * theta2, * theta3;
Kirill Terekhov's avatar
Kirill Terekhov committed
440
		Solver::Vector r_tilde, x0, t, * u, * r;
Kirill Terekhov's avatar
Kirill Terekhov committed
441
442
		Solver::Matrix * Alink;
		Method * prec;
Kirill Terekhov's avatar
Kirill Terekhov committed
443
		std::string reason;
Kirill Terekhov's avatar
Kirill Terekhov committed
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
		Solver::OrderInfo * info;
		bool init;
	public:
		INMOST_DATA_ENUM_TYPE GetIterations() {return last_it;}
		INMOST_DATA_REAL_TYPE GetResidual() {return last_resid;}
		INMOST_DATA_REAL_TYPE & RealParameter(std::string name)
		{
			if (name[0] == ':')
			{
				if (prec != NULL) return prec->RealParameter(name.substr(1, name.size() - 1));
			}
			if (name == "rtol") return rtol;
			else if (name == "atol") return atol;
			else if (name == "divtol") return divtol;
			else if (prec != NULL) return prec->RealParameter(name);
			throw - 1;
		}
		INMOST_DATA_ENUM_TYPE & EnumParameter(std::string name)
		{
			if (name[0] == ':')
			{
				if (prec != NULL) return prec->EnumParameter(name.substr(1, name.size() - 1));
			}
			if (name == "maxits") return maxits;
468
469
470
471
472
			else if (name == "levels") 
			{
				if( init ) throw - 1; //solver was already initialized, value should not be changed
				return l;
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
473
474
475
476
477
478
479
480
481
482
483
484
485
			else if (prec != NULL) return prec->EnumParameter(name);
			throw - 1;
		}
		BCGSL_solver(Method * prec, Solver::OrderInfo & info)
			:rtol(1e-8), atol(1e-9), divtol(1e+40), maxits(1500),l(2),prec(prec),info(&info)
		{
			Alink = NULL;
			init = false;
		}
		bool Initialize()
		{
			if (isInitialized()) Finalize();
			if (prec != NULL && !prec->isInitialized()) prec->Initialize();
Kirill Terekhov's avatar
Kirill Terekhov committed
486
487
			info->PrepareVector(r_tilde);
			info->PrepareVector(x0);
Kirill Terekhov's avatar
Kirill Terekhov committed
488
			info->PrepareVector(t);
Kirill Terekhov's avatar
Kirill Terekhov committed
489
490
491
492
			tau = new INMOST_DATA_REAL_TYPE[l * 3 + (l+1)*(l+1) + (l+1)*2];
			sigma = tau + (l+1)*(l+1);
			gamma = sigma + l+1;
			theta1 = gamma + l+1;
Kirill Terekhov's avatar
Kirill Terekhov committed
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
			theta2 = theta1 + l;
			theta3 = theta2 + l;
			u = new Solver::Vector[l * 2 + 2];
			r = u + l + 1;
			for (INMOST_DATA_ENUM_TYPE k = 0; k < l + 1; k++)
			{
				info->PrepareVector(r[k]);
				info->PrepareVector(u[k]);
			}
			init = true;
			return true;
		}
		bool isInitialized() { return init && (prec == NULL || prec->isInitialized()); }
		bool Finalize()
		{
			if (isInitialized())
			{
				if (!prec->isFinalized()) prec->Finalize();
				delete[] u;
				delete[] tau;
				init = false;
			}
			return true;
		}
		bool isFinalized() { return !init && (prec == NULL || prec->isFinalized()); }
		void Copy(const Method * other)
		{
			const BCGSL_solver * b = dynamic_cast<const BCGSL_solver *>(other);
			assert(b != NULL);
			rtol = b->rtol;
			atol = b->atol;
			divtol = b->divtol;
			last_resid = b->last_resid;
			iters = b->iters;
			maxits = b->maxits;
			l = b->l;
			last_it = b->last_it;
			resid = b->resid;
			Alink = b->Alink;
			info = b->info;
			if (init) Finalize();
			if (b->prec != NULL)
			{
				if (prec == NULL) prec = b->prec->Duplicate();
				else prec->Copy(b->prec);
			}
			if (b->init) Initialize();
		}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
541
		BCGSL_solver(const BCGSL_solver & other) :IterativeMethod(other)
Kirill Terekhov's avatar
Kirill Terekhov committed
542
543
544
545
546
547
548
549
550
551
552
553
554
		{
			Copy(&other);
		}
		BCGSL_solver & operator =(BCGSL_solver const & other)
		{
			Copy(&other);
			return *this;
		}
		~BCGSL_solver()
		{
			if (!isFinalized()) Finalize();
			if (prec != NULL) delete prec;
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
555
556
557
558
559
560
561
562
563
564
565
		void ApplyOperator(Solver::Vector & Input, Solver::Vector & Output)
		{
			if (prec != NULL) //right preconditioning here! for left preconditioner have to reverse order
			{
				prec->Solve(Input, t); 
				info->Update(t);
				Alink->MatVec(1.0,t,0,Output);
				info->Update(Output);
			}
			else
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
566
				Alink->MatVec(1.0,Input,0,Output);
Kirill Terekhov's avatar
Kirill Terekhov committed
567
568
569
				info->Update(Output);
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
570
571
572
573
		bool Solve(Solver::Vector & RHS, Solver::Vector & SOL)
		{
			assert(isInitialized());
			INMOST_DATA_ENUM_TYPE vbeg,vend, vlocbeg, vlocend;
Kirill Terekhov's avatar
Kirill Terekhov committed
574
			INMOST_DATA_INTEGER_TYPE ivbeg, ivend, ivlocbeg, ivlocend;
Kirill Terekhov's avatar
Kirill Terekhov committed
575
			INMOST_DATA_REAL_TYPE rho0 = 1, rho1, alpha = 0, beta, omega = 1, eta;
Kirill Terekhov's avatar
Kirill Terekhov committed
576
			INMOST_DATA_REAL_TYPE resid0, resid, rhs_norm, tau_sum, sigma_sum;//, temp[2];
Kirill Terekhov's avatar
Kirill Terekhov committed
577
578
579
			iters = 0;
			info->PrepareVector(SOL);
			info->PrepareVector(RHS);
Kirill Terekhov's avatar
Kirill Terekhov committed
580
581
			info->Update(SOL);
			info->Update(RHS);
Kirill Terekhov's avatar
Kirill Terekhov committed
582
583
584
585
			if( prec != NULL ) prec->ReplaceSOL(SOL);
			if( prec != NULL ) prec->ReplaceRHS(RHS);
			info->GetLocalRegion(info->GetRank(),vlocbeg,vlocend);
			info->GetVectorRegion(vbeg,vend);
Kirill Terekhov's avatar
Kirill Terekhov committed
586
587
588
589
590
			ivbeg = vbeg;
			ivend = vend;
			ivlocbeg = vlocbeg;
			ivlocend = vlocend;
			//info->ScalarProd(RHS,RHS,vlocbeg,vlocend,rhs_norm);
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
591
			rhs_norm = 1;
Kirill Terekhov's avatar
Kirill Terekhov committed
592
			//r[0] = b
Kirill Terekhov's avatar
Kirill Terekhov committed
593
			std::copy(RHS.Begin(),RHS.End(),r[0].Begin());
Kirill Terekhov's avatar
Kirill Terekhov committed
594
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
595
				// r[0] = r[0] - A x
Kirill Terekhov's avatar
Kirill Terekhov committed
596
				Alink->MatVec(-1,SOL,1,r[0]); //global multiplication, r probably needs an update
Kirill Terekhov's avatar
Kirill Terekhov committed
597
				info->Update(r[0]); // r is good
598
				std::copy(SOL.Begin(),SOL.End(),x0.Begin()); //x0 = x
Kirill Terekhov's avatar
Kirill Terekhov committed
599
				std::fill(SOL.Begin(),SOL.End(),0.0); //x = 0
Kirill Terekhov's avatar
Kirill Terekhov committed
600
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
601
602
			std::copy(r[0].Begin(),r[0].End(),r_tilde.Begin()); // r_tilde = r[0]
			std::fill(u[0].Begin(),u[0].End(),0); // u[0] = 0
Kirill Terekhov's avatar
Kirill Terekhov committed
603
604
605
606
607
608
609
610
611
612
613
614
			resid = 0;
#if defined(USE_OMP)
#pragma omp parallel for reduction(+:resid)
#endif
			for(INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; ++k)
				resid += r[0][k]*r[0][k];
			info->Integrate(&resid,1);
			//info->ScalarProd(r[0],r[0],vlocbeg,vlocend,resid); //resid = dot(r[0],r[0])
#if defined(USE_OMP)
#pragma omp parallel for
#endif
			for(INMOST_DATA_INTEGER_TYPE k = ivbeg; k < ivend; ++k) // r_tilde = r[0] / dot(r[0],r[0])
Kirill Terekhov's avatar
Kirill Terekhov committed
615
				r_tilde[k] /= resid;
616
			last_resid = resid = resid0 = sqrt(resid/rhs_norm); //resid = sqrt(dot(r[0],r[0])
Kirill Terekhov's avatar
Kirill Terekhov committed
617
618
619
620
621
622
623
			last_it = 0;
#if defined(REPORT_RESIDUAL)
			if( info->GetRank() == 0 ) 
			{
				//std::cout << "iter " << last_it << " residual " << resid << std::endl;
				//std::cout << "iter " << last_it << " resid " << resid << "\r";
				//printf("iter %3d resid %12g | %12g relative %12g | %12g\r", last_it, resid, atol, resid / resid0, rtol);
Kirill Terekhov's avatar
Kirill Terekhov committed
624
				printf("iter %3d resid %12g | %g\r", last_it, resid, atol);
Kirill Terekhov's avatar
Kirill Terekhov committed
625
626
627
				fflush(stdout);
			}
#endif
Alexander Danilov's avatar
Alexander Danilov committed
628
			INMOST_DATA_ENUM_TYPE i = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
629
			bool halt = false;
Kirill Terekhov's avatar
Kirill Terekhov committed
630
631
632
			if( last_resid < atol || last_resid < rtol*resid0 ) 
			{
				reason = "initial solution satisfy tolerances";
Kirill Terekhov's avatar
Kirill Terekhov committed
633
				halt = true;
Kirill Terekhov's avatar
Kirill Terekhov committed
634
			}
635

Kirill Terekhov's avatar
Kirill Terekhov committed
636
637
638
#if defined(USE_OMP)
#pragma omp parallel
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
639
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
640
641
				long double tt, ts, tp;
				while( !halt )
Kirill Terekhov's avatar
Kirill Terekhov committed
642
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
643
					ts = tp = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
644

Kirill Terekhov's avatar
Kirill Terekhov committed
645
646
647
#if defined(USE_OMP)
#pragma omp single
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
648
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
649
						rho0 = -omega*rho0;
Kirill Terekhov's avatar
Kirill Terekhov committed
650
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
651
652
653
				
					tt = Timer();
					for(INMOST_DATA_ENUM_TYPE j = 0; j < l; j++)
Kirill Terekhov's avatar
Kirill Terekhov committed
654
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
#if defined(USE_OMP)
#pragma omp single
#endif
						rho1 = 0;
#if defined(USE_OMP)
#pragma omp for reduction(+:rho1)
#endif
						for(INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; ++k) 
							rho1+= r[j][k]*r_tilde[k];
						info->Integrate(&rho1,1);
						//info->ScalarProd(r[j],r_tilde,vlocbeg,vlocend,rho1); // rho1 = dot(r[j],r_tilde)
#if defined(USE_OMP)
#pragma omp single
#endif
						{
							beta = alpha * (rho1/rho0);
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
672

Kirill Terekhov's avatar
Kirill Terekhov committed
673
674
675
676
677
678
679
680
681
682
						if( fabs(beta) > 1.0e+100 ) 
						{
							//std::cout << "alpha " << alpha << " rho1 " << rho1 << " rho0 " << rho0 << " beta " << beta << std::endl;
#if defined(USE_OMP)
#pragma omp single
#endif
							reason = "multiplier(1) is too large";
							halt = true;
							break;
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
683

Kirill Terekhov's avatar
Kirill Terekhov committed
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
						if( beta != beta )
						{
#if defined(USE_OMP)
#pragma omp single
#endif
							reason = "multiplier(1) is NaN";
							halt = true;
							break;
						}
#if defined(USE_OMP)
#pragma omp single
#endif
						{
							rho0 = rho1;
						}
						for(INMOST_DATA_ENUM_TYPE i = 0; i < j+1; i++)
						{
#if defined(USE_OMP)
#pragma omp for
#endif
							for(INMOST_DATA_INTEGER_TYPE k = ivbeg; k < ivend; ++k)
								u[i][k] = r[i][k] - beta*u[i][k];
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
707

Kirill Terekhov's avatar
Kirill Terekhov committed
708

Kirill Terekhov's avatar
Kirill Terekhov committed
709
710
711
712
713
714
715
716
717
718
719
720
						ApplyOperator(u[j],u[j+1]); // u[j+1] = A*R*u[j]
#if defined(USE_OMP)
#pragma omp single
#endif
						eta = 0;
#if defined(USE_OMP)
#pragma omp for reduction(+:eta)
#endif
						for(INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; ++k) 
							eta += u[j+1][k]*r_tilde[k];
						info->Integrate(&eta,1);
						//info->ScalarProd(u[j+1],r_tilde,vlocbeg,vlocend,eta); //eta = dot(u[j+1],r_tilde)
Kirill Terekhov's avatar
Kirill Terekhov committed
721

Kirill Terekhov's avatar
Kirill Terekhov committed
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
#if defined(USE_OMP)
#pragma omp single
#endif
						alpha = rho0 / eta;

						if( fabs(alpha) > 1.0e+100 ) 
						{
#if defined(USE_OMP)
#pragma omp single
#endif
							reason = "multiplier(2) is too large";
							halt = true;
							break;
						}
						if( alpha != alpha )
						{
#if defined(USE_OMP)
#pragma omp single
#endif
							reason = "multiplier(2) is NaN";
							halt = true;
							break;
						}

#if defined(USE_OMP)
#pragma omp for
#endif
						for(INMOST_DATA_INTEGER_TYPE k = ivbeg; k < ivend; ++k)
							SOL[k] += alpha*u[0][k];

						for(INMOST_DATA_ENUM_TYPE i = 0; i < j+1; i++)
						{
#if defined(USE_OMP)
#pragma omp for
#endif
							for(INMOST_DATA_INTEGER_TYPE k = ivbeg; k < ivend; ++k) //r[i] = r[i] - alpha * u[i+1]
								r[i][k] -= alpha*u[i+1][k];
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
760
761

					
Kirill Terekhov's avatar
Kirill Terekhov committed
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
#if defined(USE_OMP)
#pragma omp single
#endif
						resid = 0;
#if defined(USE_OMP)
#pragma omp for reduction(+:resid)
#endif
						for(INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; ++k) 
							resid += r[0][k]*r[0][k];
						info->Integrate(&resid,1);
						//info->ScalarProd(r[0],r[0],vlocbeg,vlocend,resid); // resid = dot(r[j],r[j])
#if defined(USE_OMP)
#pragma omp single
#endif
						resid = sqrt(resid/rhs_norm); // resid = sqrt(dot(r[j],r[j]))
Kirill Terekhov's avatar
Kirill Terekhov committed
777

Kirill Terekhov's avatar
Kirill Terekhov committed
778
					
Kirill Terekhov's avatar
Kirill Terekhov committed
779
780
781
782
783
784
785
786
787
788
789
						if( resid < atol || resid < rtol*resid0 ) 
						{
#if defined(USE_OMP)
#pragma omp single
#endif
							reason = "early exit in bi-cg block";
							last_resid = resid;
							halt = true;
							break;
						}
						ApplyOperator(r[j],r[j+1]); // r[j+1] = A*R*r[j]		
Kirill Terekhov's avatar
Kirill Terekhov committed
790
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
791

Kirill Terekhov's avatar
Kirill Terekhov committed
792
793
794
795
796
797
798
					if( halt ) break;
					INMOST_DATA_ENUM_TYPE size = l;
#if defined(CONVEX_COMBINATION)
					size = l+1;
#endif
					// Penalization for convex combination for update below
					for(INMOST_DATA_ENUM_TYPE j = 1; j < l+1; j++)
Kirill Terekhov's avatar
Kirill Terekhov committed
799
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
						for(INMOST_DATA_ENUM_TYPE m = 1; m < j+1; m++)
						{
#if defined(USE_OMP)
#pragma omp single
#endif
							tau_sum = 0.0;
#if defined(USE_OMP)
#pragma omp for reduction(+:tau_sum)
#endif
							for(INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; ++k)
								tau_sum += r[j][k]*r[m][k];
#if defined(USE_OMP)
#pragma omp single
#endif
							tau[(j-1) + (m-1)*size] = tau[(m-1) + (j-1)*size] = tau_sum;
						}
#if defined(USE_OMP)
#pragma omp single
#endif
						sigma_sum = 0;
#if defined(USE_OMP)
#pragma omp for reduction(+:sigma_sum)
#endif
						for(INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; ++k)
							sigma_sum += r[0][k]*r[j][k];
#if defined(USE_OMP)
#pragma omp single
#endif
						sigma[j-1] = sigma_sum;
Kirill Terekhov's avatar
Kirill Terekhov committed
829
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
830
#if defined(CONVEX_COMBINATION)
Kirill Terekhov's avatar
Kirill Terekhov committed
831
832
833
#if defined(USE_OMP)
#pragma omp single
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
834
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
835
836
837
838
839
840
841
842
843
						INMOST_DATA_REAL_TYPE lagrangian = 0.0;
						for(INMOST_DATA_ENUM_TYPE j = 0; j < l; j++) lagrangian += tau[j+size*j];
						sigma[l] = lagrangian;
						tau[(l+1)*(l+1)-1] = 0.0;
						for(INMOST_DATA_ENUM_TYPE j = 0; j < l; j++)
						{
							tau[l + j*(l+1)] = -lagrangian;
							tau[l*(l+1)+j] = lagrangian;
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
844
845
					}
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
846
847
848
					info->Integrate(tau,(l+2)*(l+1)); //sigma is updated with tau
          //info->Integrate(tau,size*size);
          //info->Integrate(sigma,size);
Kirill Terekhov's avatar
Kirill Terekhov committed
849

Kirill Terekhov's avatar
Kirill Terekhov committed
850
851
852
#if defined(USE_OMP)
#pragma omp single
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
853
#if defined(PSEUDOINVERSE)
Kirill Terekhov's avatar
Kirill Terekhov committed
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
					{
						int dgesvd_info = 0;
#if defined(USE_LAPACK_SVD)
						char c = 'A';
						INMOST_DATA_REAL_TYPE U[128*128], V[128*128], w[128];
						INMOST_DATA_REAL_TYPE work[5*128];
						int lwork = 5*128;
						int n = static_cast<int>(size);
						dgesvd_(&c,&c,&n,&n,tau,&n,w,U,&n,V,&n,work,&lwork,&dgesvd_info);
#else
						/*
						char c = 'A';
						INMOST_DATA_REAL_TYPE U2[128*128], V2[128*128], w2[128], tau2[128*128];
						INMOST_DATA_REAL_TYPE work[5*128];
						int lwork = 5*128;
						int n = l;
						memcpy(tau2,tau,sizeof(INMOST_DATA_REAL_TYPE)*l*l);
						dgesvd_(&c,&c,&n,&n,tau2,&n,w2,U2,&n,V2,&n,work,&lwork,&dgesvd_info);
						printf("dgesvd\n");
						printf("w\n");
						for(int q = 0; q < l; ++q) printf("%g ",w2[q]);
						printf("\nU\n");
						for(int q = 0; q < l*l; ++q) 
						{
							printf("%g ",U2[q]);
							if( (q+1)%l == 0 ) printf("\n");
						}
						printf("V\n");
						for(int q = 0; q < l*l; ++q) 
						{
							printf("%g ",V2[q]);
							if( (q+1)%l == 0 ) printf("\n");
						}
						*/
						INMOST_DATA_REAL_TYPE U[128*128], V[128*128], w[128];
						dgesvd_info = svdnxn(tau,U,w,V,size);
						//for(INMOST_DATA_ENUM_TYPE j = 0; j < l; j++) w[j] = S[j*l+j];
						/*
						printf("svdnxn\n");
						printf("w\n");
						for(int q = 0; q < l; ++q) printf("%g ",w[q]);
						printf("\nU\n");
						for(int q = 0; q < l*l; ++q) 
						{
							printf("%g ",U[q]);
							if( (q+1)%l == 0 ) printf("\n");
						}
						printf("V\n");
						for(int q = 0; q < l*l; ++q) 
						{
							printf("%g ",V[q]);
							if( (q+1)%l == 0 ) printf("\n");
						}
						*/
#endif		
						/*
						printf("w ");
						for(INMOST_DATA_ENUM_TYPE j = 0; j < l; j++) printf("%20g ",w[j]);
						printf("\n");
Kirill Terekhov's avatar
Kirill Terekhov committed
913

Kirill Terekhov's avatar
Kirill Terekhov committed
914
915
916
917
918
919
920
						printf("U\n");
						for(INMOST_DATA_ENUM_TYPE j = 0; j < l*l; j++) 
						{
							printf("%20g ",U[j]);
							if( (j+1) % l == 0 ) printf("\n");
						}
						printf("\n");
Kirill Terekhov's avatar
Kirill Terekhov committed
921

Kirill Terekhov's avatar
Kirill Terekhov committed
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
						printf("VT\n");
						for(INMOST_DATA_ENUM_TYPE j = 0; j < l*l; j++) 
						{
							printf("%20g ",V[j]);
							if( (j+1) % l == 0 ) printf("\n");
						}
						printf("\n");
						*/
						if( dgesvd_info != 0 )
						{
							printf("(%s:%d) dgesvd %d\n",__FILE__,__LINE__,dgesvd_info);
							exit(-1);
						}
					
						INMOST_DATA_REAL_TYPE maxw = w[0], tol;
						for(INMOST_DATA_ENUM_TYPE j = 1; j < size; j++) if(w[j]>maxw) maxw = w[j];
						tol = size*maxw*1.0e-14;
						memset(gamma,0,sizeof(INMOST_DATA_REAL_TYPE)*size);
						for(INMOST_DATA_ENUM_TYPE j = 0; j < size; j++)
						{
							if( w[j] > tol )
							{
								INMOST_DATA_REAL_TYPE sum = 0;
								for(INMOST_DATA_ENUM_TYPE k = 0; k < size; ++k)
									sum += sigma[k]*U[j*size+k];
								for(INMOST_DATA_ENUM_TYPE k = 0; k < size; ++k)
									gamma[k] += sum/w[j]*V[k*size+j];
							}
						}
					}

					//svdnxn(tau,U,S,V,l);
					//INMOST_DATA_REAL_TYPE inv_tau[64];
					//pseudoinverse(tau,inv_tau,l);
					//matmul(inv_tau,sigma,gamma,l,l,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
957
#else
Kirill Terekhov's avatar
Kirill Terekhov committed
958
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
						int order[128];
						int row = solvenxn(tau,gamma,sigma,size,order);
						/*
						double sum = 0.0;
						for(int j = 0; j < l; ++j) 
						{
							sum += gamma[j];
							std::cout << gamma[j] << " ";
						}
						std::cout << "sum: " << sum;
						//std::cout << " lagrangian: " << gamma[l];
						std::cout << std::endl;
						*/
						if( row != 0 )
						{
							std::cout << "breakdown on row " << row << std::endl;
							reason = "breakdown in matrix inversion in polynomial part";
							break;
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
978
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
979
980
981
982
983
984
#endif
#if defined(USE_OMP)
#pragma omp single
#endif
					omega = gamma[l-1];
					if( fabs(omega) > 1.0e+100 )
Kirill Terekhov's avatar
Kirill Terekhov committed
985
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
986
987
988
989
990
991
#if defined(USE_OMP)
#pragma omp single
#endif
						reason = "multiplier(3) is too large";
						halt = true;
						break;
Kirill Terekhov's avatar
Kirill Terekhov committed
992
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
993
					if( omega != omega )
Kirill Terekhov's avatar
Kirill Terekhov committed
994
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
995
996
997
998
999
1000
#if defined(USE_OMP)
#pragma omp single
#endif
						reason = "multiplier(3) is NaN";
						halt = true;
						break;
Kirill Terekhov's avatar
Kirill Terekhov committed
1001
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
1002
					for(INMOST_DATA_ENUM_TYPE j = 1; j < l+1; ++j)
Kirill Terekhov's avatar
Kirill Terekhov committed
1003
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1004
1005
1006
1007
1008
1009
1010
1011
1012
#if defined(USE_OMP)
#pragma omp for
#endif
						for(INMOST_DATA_INTEGER_TYPE k = ivbeg; k < ivend; ++k)
						{
							u[0][k] -= gamma[j-1]*u[j][k];
							SOL[k]  += gamma[j-1]*r[j-1][k];
							r[0][k] -= gamma[j-1]*r[j][k];
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
1013
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
1014
1015
				
				
Kirill Terekhov's avatar
Kirill Terekhov committed
1016
					/*
Kirill Terekhov's avatar
Kirill Terekhov committed
1017
					for(INMOST_DATA_ENUM_TYPE j = 1; j < l+1; j++)
Kirill Terekhov's avatar
Kirill Terekhov committed
1018
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
						for(INMOST_DATA_ENUM_TYPE i = 1; i < j; i++)
						{
							tau[i-1 + (j-1)*l] = 0;
							for(INMOST_DATA_ENUM_TYPE k = vlocbeg; k < vlocend; ++k)
								tau[i-1 + (j-1)*l] += r[j][k]*r[i][k];
							info->Integrate(&tau[i-1 + (j-1)*l],1);
							tau[i-1 + (j-1)*l] /= sigma[i-1];
							for(INMOST_DATA_ENUM_TYPE k = vbeg; k < vend; ++k)
								r[j][k] -= tau[i-1 + (j-1)*l]*r[i][k];
						}
						INMOST_DATA_REAL_TYPE temp[2] = {0,0};
						for(INMOST_DATA_ENUM_TYPE k = vlocbeg; k < vlocend; ++k)
						{
							temp[0] += r[j][k]*r[j][k];
							temp[1] += r[0][k]*r[j][k];
						}
						info->Integrate(temp,2);
						sigma[j-1] = temp[0];//+1.0e-35; //REVIEW
						theta2[j-1] = temp[1]/sigma[j-1];
Kirill Terekhov's avatar
Kirill Terekhov committed
1038
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
					omega = theta1[l-1] = theta2[l-1];
					for(INMOST_DATA_ENUM_TYPE j = l-1; j > 0; j--)
					{
						eta = 0;
						for(INMOST_DATA_ENUM_TYPE i = j+1; i < l+1; i++)
							eta += tau[j-1 + (i-1)*l] * theta1[i-1];
						theta1[j-1] = theta2[j-1] - eta;
					}
					for(INMOST_DATA_ENUM_TYPE j = 1; j < l; j++)
					{
						eta = 0;
						for(INMOST_DATA_ENUM_TYPE i = j+1; i < l; i++)
							eta += tau[j-1 + (i-1)*l] * theta1[i];
						theta3[j-1] = theta1[j] + eta;
					}
					for(INMOST_DATA_ENUM_TYPE k = vbeg; k < vend; ++k)
Kirill Terekhov's avatar
Kirill Terekhov committed
1055
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
						SOL[k] += theta1[0]*r[0][k];
						r[0][k] -= theta2[l-1]*r[l][k];
						u[0][k] -= theta1[l-1]*u[l][k];
					}
					for(INMOST_DATA_ENUM_TYPE j = 1; j < l; j++)
					{
						for(INMOST_DATA_ENUM_TYPE k = vbeg; k < vend; ++k)
						{
							u[0][k] -= theta1[j-1]*u[j][k];
							SOL[k] += theta3[j-1]*r[j][k];
							r[0][k] -= theta2[j-1]*r[j][k];
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
1068
1069
					}
					*/
Kirill Terekhov's avatar
Kirill Terekhov committed
1070
					last_it = i+1;
Kirill Terekhov's avatar
Kirill Terekhov committed
1071
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
#if defined(USE_OMP)
#pragma omp single
#endif
						resid = 0;
#if defined(USE_OMP)
#pragma omp for reduction(+:resid)
#endif
						for(INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; ++k)
							resid += r[0][k]*r[0][k];
						info->Integrate(&resid,1);
						//info->ScalarProd(r[0],r[0],vlocbeg,vlocend,resid);
#if defined(USE_OMP)
#pragma omp single
#endif
						resid = sqrt(resid/rhs_norm);
Kirill Terekhov's avatar
Kirill Terekhov committed
1087
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
1088
1089
1090
					tt = Timer() - tt;
#if defined(REPORT_RESIDUAL)
					if( info->GetRank() == 0 ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
1091
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1092
1093
1094
1095
1096
1097
						//std::cout << "iter " << last_it << " residual " << resid << " time " << tt << " matvec " << ts*0.5/l << " precond " << tp*0.5/l << std::endl;
						//std::cout << "iter " << last_it << " resid " << resid << "\r";
						//printf("iter %3d resid %12g | %12g relative %12g | %12g\r", last_it, resid, atol, resid / resid0, rtol);
#if defined(USE_OMP)
#pragma omp single
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
1098
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
1099
1100
							printf("iter %3d resid %12g | %g\r", last_it, resid, atol);
							fflush(stdout);
Kirill Terekhov's avatar
Kirill Terekhov committed
1101
1102
1103
						}
					}
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
1104
1105
					last_resid = resid;
					if( resid != resid )
Kirill Terekhov's avatar
Kirill Terekhov committed
1106
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1107
1108
1109
1110
1111
#if defined(USE_OMP)
#pragma omp single
#endif
						reason = "residual is NAN";
						break;
Kirill Terekhov's avatar
Kirill Terekhov committed
1112
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
1113
					if( resid < atol )
Kirill Terekhov's avatar
Kirill Terekhov committed
1114
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1115
1116
1117
1118
1119
#if defined(USE_OMP)
#pragma omp single
#endif
						reason = "converged due to absolute tolerance";
						break;
Kirill Terekhov's avatar
Kirill Terekhov committed
1120
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
1121
					if( resid < rtol*resid0 )
Kirill Terekhov's avatar
Kirill Terekhov committed
1122
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1123
1124
1125
1126
1127
#if defined(USE_OMP)
#pragma omp single
#endif
						reason = "converged due to relative tolerance";
						break;
Kirill Terekhov's avatar
Kirill Terekhov committed
1128
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
1129
					if( resid > divtol )
Kirill Terekhov's avatar
Kirill Terekhov committed
1130
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1131
1132
1133
1134
1135
#if defined(USE_OMP)
#pragma omp single
#endif
						reason = "diverged due to divergence tolerance";
						break;
Kirill Terekhov's avatar
Kirill Terekhov committed
1136
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
1137
1138
1139
1140
					if( i == maxits )
					{
#if defined(USE_OMP)
#pragma omp single
Kirill Terekhov's avatar
Kirill Terekhov committed
1141
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
1142
1143
1144
1145
						reason = "reached maximum iteration number";
						break;
					}
					i++;
Kirill Terekhov's avatar
Kirill Terekhov committed
1146
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
1147
1148
1149
			}
			if (prec != NULL)
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1150
1151
				prec->Solve(SOL, r_tilde); //undo right preconditioner
				std::copy(r_tilde.Begin(), r_tilde.End(), SOL.Begin());
Kirill Terekhov's avatar
Kirill Terekhov committed
1152
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1153
1154
1155
1156
#if defined(USE_OMP)
#pragma omp parallel for
#endif
			for(INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; ++k) //undo shift
Kirill Terekhov's avatar
Kirill Terekhov committed
1157
				SOL[k] += x0[k];
Kirill Terekhov's avatar
Kirill Terekhov committed
1158
1159
1160
1161
1162
1163
1164
			//info->RestoreMatrix(A);
			info->RestoreVector(SOL);
			info->RestoreVector(RHS);
			if( last_resid < atol || last_resid < rtol*resid0 ) return true;
			return false;
		}
		bool ReplaceMAT(Solver::Matrix & A) { if (isInitialized()) Finalize(); if (prec != NULL) prec->ReplaceMAT(A);  Alink = &A; return true; }
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1165
1166
		bool ReplaceRHS(Solver::Vector & RHS) { (void) RHS; return true; }
		bool ReplaceSOL(Solver::Vector & SOL) { (void) SOL; return true; }
Kirill Terekhov's avatar
Kirill Terekhov committed
1167
		Method * Duplicate() { return new BCGSL_solver(*this);}
Kirill Terekhov's avatar
Kirill Terekhov committed
1168
		std::string GetReason() {return reason;}
Kirill Terekhov's avatar
Kirill Terekhov committed
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
	};


	class BCGS_solver : public IterativeMethod
	{
		INMOST_DATA_REAL_TYPE rtol, atol, divtol, last_resid;
		INMOST_DATA_ENUM_TYPE iters, maxits, last_it;
		INMOST_DATA_REAL_TYPE resid;
		Solver::Vector r0, p, y, s, t, z, r, v;
		Solver::Matrix * Alink;
		Method * prec;
		Solver::OrderInfo * info;
		bool init;
Kirill Terekhov's avatar
Kirill Terekhov committed
1182
		std::string reason;
Kirill Terekhov's avatar
Kirill Terekhov committed
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
	public:
		INMOST_DATA_ENUM_TYPE GetIterations() {return last_it;}
		INMOST_DATA_REAL_TYPE GetResidual() {return last_resid;}
		INMOST_DATA_REAL_TYPE & RealParameter(std::string name)
		{
			if (name[0] == ':')
			{
				if (prec != NULL) return prec->RealParameter(name.substr(1, name.size() - 1));
			}
			if (name == "rtol") return rtol;
			else if (name == "atol") return atol;
			else if (name == "divtol") return divtol;
			else if( prec != NULL ) return prec->RealParameter(name);
			throw - 1;
		}
		INMOST_DATA_ENUM_TYPE & EnumParameter(std::string name)
		{
			if (name[0] == ':')
			{
				if (prec != NULL) return prec->EnumParameter(name.substr(1, name.size() - 1));
			}
			if (name == "maxits") return maxits;
			else if (prec != NULL) return prec->EnumParameter(name);
			throw - 1;
		}
		BCGS_solver(Method * prec, Solver::OrderInfo & info)
			:rtol(1e-8), atol(1e-11), divtol(1e+40), iters(0), maxits(1500),prec(prec),info(&info)
		{
			init = false;
		}
		bool Initialize()
		{
			assert(Alink != NULL);
			if (isInitialized()) Finalize();
			if (prec != NULL && !prec->isInitialized()) prec->Initialize();
			info->PrepareVector(r);
			info->PrepareVector(v);
			info->PrepareVector(p);
			info->PrepareVector(y);
			info->PrepareVector(s);
			info->PrepareVector(t);
			info->PrepareVector(z);
			info->PrepareVector(r0);
			init = true;
			return true;
		}
		bool isInitialized() { return init && (prec == NULL || prec->isInitialized()); }
		bool Finalize()
		{
			if (prec != NULL && !prec->isFinalized()) prec->Finalize();
			init = false;
			return true;
		}
		bool isFinalized() { return !init && (prec == NULL || prec->isFinalized()); }
		void Copy(const Method * other)
		{
			const BCGS_solver * b = dynamic_cast<const BCGS_solver *>(other);
			assert(b != NULL);
			info = b->info;
			rtol = b->rtol;
			atol = b->atol;
			divtol = b->divtol;
			maxits = b->maxits;
			last_resid = b->last_resid;
			iters = b->iters;
			last_it = b->last_it;
			resid = b->resid;
			Alink = b->Alink;
			if (b->prec != NULL)
			{
				if (prec == NULL) prec = b->prec->Duplicate();
				else prec->Copy(b->prec);
			}
			if (b->init) Initialize();
		}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1258
		BCGS_solver(const BCGS_solver & other) : IterativeMethod(other)
Kirill Terekhov's avatar
Kirill Terekhov committed
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
		{
			Copy(&other);
		}
		BCGS_solver & operator =(BCGS_solver const & other)
		{
			Copy(&other);
			return *this;
		}
		~BCGS_solver()
		{
			if (!isFinalized()) Finalize();
			if (prec != NULL) delete prec;
		}
		bool Solve(Solver::Vector & RHS, Solver::Vector & SOL)
		{
			assert(isInitialized());
Kirill Terekhov's avatar
Kirill Terekhov committed
1275
			INMOST_DATA_REAL_TYPE tempa = 0.0, tempb=0.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
1276
			INMOST_DATA_ENUM_TYPE vbeg,vend, vlocbeg, vlocend;
Kirill Terekhov's avatar
Kirill Terekhov committed
1277
			INMOST_DATA_INTEGER_TYPE ivbeg,ivend, ivlocbeg, ivlocend;
Kirill Terekhov's avatar
Kirill Terekhov committed
1278
1279
1280
1281
			INMOST_DATA_REAL_TYPE rho = 1, alpha = 1, beta, omega = 1;
			INMOST_DATA_REAL_TYPE resid0, resid, temp[2];
			info->PrepareVector(SOL);
			info->PrepareVector(RHS);
Kirill Terekhov's avatar
Kirill Terekhov committed
1282
1283
			info->Update(SOL);
			info->Update(RHS);
Kirill Terekhov's avatar
Kirill Terekhov committed
1284
1285
1286
1287
			if (prec != NULL)prec->ReplaceSOL(SOL);
			if (prec != NULL)prec->ReplaceRHS(RHS);
			info->GetLocalRegion(info->GetRank(),vlocbeg,vlocend);
			info->GetVectorRegion(vbeg,vend);
Kirill Terekhov's avatar
Kirill Terekhov committed
1288
1289
1290
1291
			ivbeg = vbeg;
			ivend = vend;
			ivlocbeg = vlocbeg;
			ivlocend = vlocend;
Kirill Terekhov's avatar
Kirill Terekhov committed
1292
			std::copy(RHS.Begin(),RHS.End(),r.Begin());
Kirill Terekhov's avatar
Kirill Terekhov committed
1293
1294
1295
1296
			{
				Alink->MatVec(-1,SOL,1,r); //global multiplication, r probably needs an update
				info->Update(r); // r is good
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1297
			std::copy(r.Begin(),r.End(),r0.Begin());
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1298
1299
			std::fill(v.Begin(),v.End(),0.0);
			std::fill(p.Begin(),p.End(),0.0);
Kirill Terekhov's avatar
Kirill Terekhov committed
1300
1301
			{
				resid = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
1302
1303
1304
1305
#if defined(USE_OMP)
#pragma omp parallel for
#endif
				for(INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; k++) 
Kirill Terekhov's avatar
Kirill Terekhov committed
1306
					resid += r[k]*r[k];
Kirill Terekhov's avatar
Kirill Terekhov committed
1307
				info->Integrate(&resid,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
1308
1309
1310
1311
1312
1313
1314
1315
1316
			}
			last_resid = resid = resid0 = sqrt(resid);
			last_it = 0;
#if defined(REPORT_RESIDUAL)
			if( info->GetRank() == 0 ) 
			{
				//std::cout << "iter " << last_it << " residual " << resid << std::endl;
				//std::cout << "iter " << last_it << " resid " << resid << "\r";
				//printf("iter %3d resid %12g | %12g relative %12g | %12g\r",last_it,resid,atol,resid/resid0,rtol);
Kirill Terekhov's avatar
Kirill Terekhov committed
1317
				printf("iter %3d resid %12g | %g\r", last_it, resid, atol);
Kirill Terekhov's avatar
Kirill Terekhov committed
1318
1319
1320
				fflush(stdout);
			}
#endif
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1321
#if defined(USE_OMP)
Kirill Terekhov's avatar
Kirill Terekhov committed
1322
#pragma omp parallel
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1323
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
1324
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1325
1326
				INMOST_DATA_ENUM_TYPE i = 0;
				while(true)
Kirill Terekhov's avatar
Kirill Terekhov committed
1327
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
1328
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1329
						/*
Kirill Terekhov's avatar
Kirill Terekhov committed
1330
1331
						if( fabs(rho) < 1.0e-31 )
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
1332
							std::cout << "rho " << rho << " alpha " << alpha << " omega " << omega << " beta " << 1.0 /rho * alpha / omega << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
1333
1334
1335
1336
1337
							reason = "denominator(1) is zero";
							break;
						}
						if( fabs(omega) < 1.0e-31 )
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
1338
							std::cout << "rho " << rho << " alpha " << alpha << " omega " << omega << " beta " << 1.0 /rho * alpha / omega << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
1339
1340
1341
							reason = "denominator(2) is zero";
							break;
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
1342
1343
						*/
						//std::cout << "rho " << rho << " alpha " << alpha << " omega " << omega << " beta " << 1.0 /rho * alpha / omega << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
1344
1345
1346
1347
1348
1349
1350
#if defined(USE_OMP)
#pragma omp single
#endif
						{
							beta = 1.0 /rho * alpha / omega;
							rho = 0;
						}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1351
#if defined(USE_OMP)
Kirill Terekhov's avatar
Kirill Terekhov committed
1352
#pragma omp for reduction(+:rho)
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1353
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
1354
						for(INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; ++k) 
Kirill Terekhov's avatar
Kirill Terekhov committed
1355
							rho += r0[k]*r[k];
Kirill Terekhov's avatar
Kirill Terekhov committed
1356
1357
						info->Integrate(&rho,1);
						//info->ScalarProd(r0,r,ivlocbeg,ivlocend,rho);
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1358
#if defined(USE_OMP)
Kirill Terekhov's avatar
Kirill Terekhov committed
1359
#pragma omp single
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1360
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
1361
1362
						{
							beta *= rho;
Kirill Terekhov's avatar
Kirill Terekhov committed
1363
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
1364

Kirill Terekhov's avatar
Kirill Terekhov committed
1365
						if( fabs(beta) > 1.0e+100 )
Kirill Terekhov's avatar
Kirill Terekhov committed
1366
1367
						{
							//std::cout << "rho " << rho << " alpha " << alpha << " omega " << omega << " beta " << 1.0 /rho * alpha / omega << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
1368
1369
1370
#if defined(USE_OMP)
#pragma omp single
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
1371
1372
1373
							reason = "multiplier(1) is too large";
							break;
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
1374
1375
						if( beta != beta )
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
1376
1377
1378
#if defined(USE_OMP)
#pragma omp single
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
1379
1380
1381
							reason = "multiplier(1) is NaN";
							break;
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
1382
1383
					}
					{
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1384
#if defined(USE_OMP)
Kirill Terekhov's avatar
Kirill Terekhov committed
1385
#pragma omp for
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1386
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
1387
1388
1389
						for(INMOST_DATA_INTEGER_TYPE k = ivbeg; k < ivend; ++k) 
							p[k] = r[k] + beta*(p[k] - omega*v[k]); //global indexes r, p, v
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
1390

Kirill Terekhov's avatar
Kirill Terekhov committed
1391
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1392
						if (prec != NULL) 
Kirill Terekhov's avatar
Kirill Terekhov committed
1393
1394
1395
						{
							prec->Solve(p, y);
							info->Update(y);
Kirill Terekhov's avatar
Kirill Terekhov committed
1396
1397
							Alink->MatVec(1,y,0,v); // global multiplication, y should be updated, v probably needs an update
							info->Update(v);
Kirill Terekhov's avatar
Kirill Terekhov committed
1398
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
1399
						else
Kirill Terekhov's avatar
Kirill Terekhov committed
1400
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
1401
							Alink->MatVec(1,p,0,v); // global multiplication, y should be updated, v probably needs an update
Kirill Terekhov's avatar
Kirill Terekhov committed
1402
1403
1404
1405
							info->Update(v);
						}
					}
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1406
1407
1408
#if defined(USE_OMP)
#pragma omp single
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
1409
						alpha = 0;
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1410
#if defined(USE_OMP)
Kirill Terekhov's avatar
Kirill Terekhov committed