solver_bcgsl.hpp 19.6 KB
Newer Older
Kirill Terekhov's avatar
Kirill Terekhov committed
1 2 3 4 5 6 7 8 9 10
#pragma once
#ifndef __SOLVER_BCGS__
#define __SOLVER_BCGS__

//TODO:
// comply solvers with Method prototype, after TODO in solver_prototypes.hpp is done


#include "inmost_solver.h"

Kirill Terekhov's avatar
Kirill Terekhov committed
11
//#if !defined(NDEBUG)
Kirill Terekhov's avatar
Kirill Terekhov committed
12
#define REPORT_RESIDUAL
Kirill Terekhov's avatar
Kirill Terekhov committed
13
//#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
14 15 16 17 18 19 20 21 22 23 24 25

namespace INMOST
{
	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;
		Solver::Vector r0, t, * u, * r;
		Solver::Matrix * Alink;
		Method * prec;
Kirill Terekhov's avatar
Kirill Terekhov committed
26
		std::string reason;
Kirill Terekhov's avatar
Kirill Terekhov committed
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
		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;
			else if (name == "levels") return l;
			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();
			info->PrepareVector(r0);
			info->PrepareVector(t);
			tau = new INMOST_DATA_REAL_TYPE[l * 5 + l*l];
			sigma = tau + l*l;
			gamma = sigma + l;
			theta1 = gamma + l;
			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();
		}
		BCGSL_solver(const BCGSL_solver & other)
		{
			Copy(&other);
		}
		BCGSL_solver & operator =(BCGSL_solver const & other)
		{
			Copy(&other);
			return *this;
		}
		~BCGSL_solver()
		{
			if (!isFinalized()) Finalize();
			if (prec != NULL) delete prec;
		}
		bool Solve(Solver::Vector & RHS, Solver::Vector & SOL)
		{
			assert(isInitialized());
			INMOST_DATA_ENUM_TYPE vbeg,vend, vlocbeg, vlocend;
			INMOST_DATA_REAL_TYPE rho0 = 1, rho1, alpha = 0, beta, omega = 1;
			INMOST_DATA_REAL_TYPE resid0, resid, temp[2];
			bool is_parallel = info->GetSize() > 1;
			iters = 0;
			info->PrepareVector(SOL);
			info->PrepareVector(RHS);
Kirill Terekhov's avatar
Kirill Terekhov committed
143 144
			if( is_parallel ) info->Update(SOL);
			if( is_parallel ) info->Update(RHS);
Kirill Terekhov's avatar
Kirill Terekhov committed
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
			if( prec != NULL ) prec->ReplaceSOL(SOL);
			if( prec != NULL ) prec->ReplaceRHS(RHS);
			info->GetLocalRegion(info->GetRank(),vlocbeg,vlocend);
			info->GetVectorRegion(vbeg,vend);
			std::copy(RHS.Begin(),RHS.End(),r[0].Begin());
			{
				Alink->MatVec(-1,SOL,1,r[0]); //global multiplication, r probably needs an update
				if( is_parallel ) info->Update(r[0]); // r is good
			}
			std::copy(r[0].Begin(),r[0].End(),r0.Begin());
			std::fill(u[0].Begin(),u[0].End(),0);
			{
				resid = 0;
				for(INMOST_DATA_ENUM_TYPE k = vlocbeg; k != vlocend; k++) 
					resid += r[0][k]*r[0][k];
				if( is_parallel ) info->Integrate(&resid,1);
			}
			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
170
				printf("iter %3d resid %12g | %g\r", last_it, resid, atol);
Kirill Terekhov's avatar
Kirill Terekhov committed
171 172 173 174
				fflush(stdout);
			}
#endif
			long double tt, ts, tp, ttt;
Kirill Terekhov's avatar
Kirill Terekhov committed
175 176
			INMOST_DATA_ENUM_TYPE i = 0;
			while( true )
Kirill Terekhov's avatar
Kirill Terekhov committed
177 178 179 180 181 182 183 184 185 186 187
			{
				ts = tp = 0;
				rho0 = -omega*rho0;
				
				tt = Timer();
				for(INMOST_DATA_ENUM_TYPE j = 0; j < l; j++)
				{
					rho1 = 0.0;
					for(INMOST_DATA_ENUM_TYPE k = vlocbeg; k < vlocend; ++k)
						rho1 += r[j][k]*r0[k];
					if( is_parallel ) info->Integrate(&rho1,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
188 189 190 191 192
					if( fabs(rho0) < 1.0e-54 ) 
					{
						reason = "denominator(1) is zero";
						goto exit;
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
					beta = alpha * rho1/rho0;
					rho0 = rho1;
					for(INMOST_DATA_ENUM_TYPE i = 0; i < j+1; i++)
						for(INMOST_DATA_ENUM_TYPE k = vbeg; k < vend; ++k)
							u[i][k] = r[i][k] - beta*u[i][k];
					{
						ttt = Timer();
						if (prec != NULL) prec->Solve(u[j], t);
						tp += Timer() - ttt;
						if( is_parallel ) info->Update(t);
						ttt = Timer();
						Alink->MatVec(1.0,t,0,u[j+1]);
						ts += Timer() - ttt;
						if( is_parallel ) info->Update(u[j+1]);
					}
					{
						temp[0] = 0;
						for(INMOST_DATA_ENUM_TYPE k = vlocbeg; k < vlocend; ++k)
							temp[0] += u[j+1][k]*r0[k];
						if( is_parallel ) info->Integrate(temp,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
213 214 215 216 217 218
						if( fabs(temp[0]) < 1.0e-54 ) 
						{
							reason = "denominator(2) is zero";
							goto exit;
						}
						alpha = rho0 / temp[0];
Kirill Terekhov's avatar
Kirill Terekhov committed
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 254 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
					}
					for(INMOST_DATA_ENUM_TYPE i = 0; i < j+1; i++)
						for(INMOST_DATA_ENUM_TYPE k = vbeg; k < vend; ++k)
							r[i][k] -= alpha*u[i+1][k];
					{
						ttt = Timer();
						if (prec != NULL) prec->Solve(r[j], t);
						tp += Timer() - ttt;
						if( is_parallel ) info->Update(t);
						ttt = Timer();
						Alink->MatVec(1.0,t,0,r[j+1]);
						ts += Timer() - ttt;
						if( is_parallel ) info->Update(r[j+1]);
					}
					for(INMOST_DATA_ENUM_TYPE k = vbeg; k < vend; ++k)
						SOL[k] += alpha*u[0][k];
				}

				for(INMOST_DATA_ENUM_TYPE j = 1; j < l+1; j++)
				{
					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];
						if(is_parallel) 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];
					}
					temp[0] = 0;
					temp[1] = 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];
					}
					if(is_parallel) info->Integrate(temp,2);
					sigma[j-1] = temp[0]+1.0e-35; //REVIEW
					theta2[j-1] = temp[1]/sigma[j-1];
				}
				omega = theta1[l-1] = theta2[l-1];
				for(INMOST_DATA_ENUM_TYPE j = l-1; j > 0; j--)
				{
					temp[0] = 0;
					for(INMOST_DATA_ENUM_TYPE i = j+1; i < l+1; i++)
						temp[0] += tau[j-1 + (i-1)*l] * theta1[i-1];
					theta1[j-1] = theta2[j-1] - temp[0];
				}
				for(INMOST_DATA_ENUM_TYPE j = 1; j < l; j++)
				{
					temp[0] = 0;
					for(INMOST_DATA_ENUM_TYPE i = j+1; i < l; i++)
						temp[0] += tau[j-1 + (i-1)*l] * theta1[i];
					theta3[j-1] = theta1[j] + temp[0];
				}
				for(INMOST_DATA_ENUM_TYPE k = vbeg; k < vend; ++k)
				{
					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];
					}
				}
				last_it = i+1;
				{
					resid = 0;
					for(INMOST_DATA_ENUM_TYPE k = vlocbeg; k != vlocend; k++) 
						resid += r[0][k]*r[0][k];
					if( is_parallel ) info->Integrate(&resid,1);
					resid = sqrt(resid);
				}
				tt = Timer() - tt;
#if defined(REPORT_RESIDUAL)
				if( info->GetRank() == 0 ) 
				{
					//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);
Kirill Terekhov's avatar
Kirill Terekhov committed
305
					printf("iter %3d resid %12g | %g\r", last_it, resid, atol);
Kirill Terekhov's avatar
Kirill Terekhov committed
306 307 308 309
					fflush(stdout);
				}
#endif
				last_resid = resid;
Kirill Terekhov's avatar
Kirill Terekhov committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
				if( resid != resid )
				{
					reason = "residual is NAN";
					break;
				}
				if( resid < atol )
				{
					reason = "converged due to absolute tolerance";
					break;
				}
				if( resid < rtol*resid0 )
				{
					reason = "converged due to relative tolerance";
					break;
				}
				if( resid > divtol )
				{
					reason = "diverged due to divergence tolerance";
					break;
				}
				if( i == maxits )
				{
					reason = "reached maximum iteration number";
					break;
				}
				i++;
Kirill Terekhov's avatar
Kirill Terekhov committed
336 337 338 339 340 341
			}
			if (prec != NULL)
			{
				prec->Solve(SOL, r0);
				std::copy(r0.Begin(), r0.End(), SOL.Begin());
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
342
exit:
Kirill Terekhov's avatar
Kirill Terekhov committed
343 344 345 346 347 348 349 350 351 352
			//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; }
		bool ReplaceRHS(Solver::Vector & RHS) { return true; }
		bool ReplaceSOL(Solver::Vector & SOL) { return true; }
		Method * Duplicate() { return new BCGSL_solver(*this);}
Kirill Terekhov's avatar
Kirill Terekhov committed
353
		std::string GetReason() {return reason;}
Kirill Terekhov's avatar
Kirill Terekhov committed
354 355 356 357 358 359 360 361 362 363 364 365 366
	};


	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
367
		std::string reason;
Kirill Terekhov's avatar
Kirill Terekhov committed
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 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
	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();
		}
		BCGS_solver(const BCGS_solver & other)
		{
			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
460
			INMOST_DATA_REAL_TYPE tempa = 0.0, tempb=0.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
461
			INMOST_DATA_ENUM_TYPE vbeg,vend, vlocbeg, vlocend;
Kirill Terekhov's avatar
Kirill Terekhov committed
462
			INMOST_DATA_INTEGER_TYPE ivbeg,ivend, ivlocbeg, ivlocend;
Kirill Terekhov's avatar
Kirill Terekhov committed
463 464 465 466 467
			INMOST_DATA_REAL_TYPE rho = 1, alpha = 1, beta, omega = 1;
			INMOST_DATA_REAL_TYPE resid0, resid, temp[2];
			bool is_parallel = info->GetSize() > 1;
			info->PrepareVector(SOL);
			info->PrepareVector(RHS);
Kirill Terekhov's avatar
Kirill Terekhov committed
468 469
			if( is_parallel ) info->Update(SOL);
			if( is_parallel ) info->Update(RHS);
Kirill Terekhov's avatar
Kirill Terekhov committed
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
			if (prec != NULL)prec->ReplaceSOL(SOL);
			if (prec != NULL)prec->ReplaceRHS(RHS);
			info->GetLocalRegion(info->GetRank(),vlocbeg,vlocend);
			info->GetVectorRegion(vbeg,vend);
			std::copy(RHS.Begin(),RHS.End(),r.Begin());
			{
				Alink->MatVec(-1,SOL,1,r); //global multiplication, r probably needs an update
				info->Update(r); // r is good
			}
			std::copy(r.Begin(),r.End(),r0.Begin());
			std::fill(v.Begin(),v.End(),0);
			std::fill(p.Begin(),p.End(),0);
			{
				resid = 0;
				for(INMOST_DATA_ENUM_TYPE k = vlocbeg; k != vlocend; k++) 
					resid += r[k]*r[k];
				if( is_parallel ) info->Integrate(&resid,1);
			}
			last_resid = resid = resid0 = sqrt(resid);
			last_it = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
490 491 492 493
			ivbeg = vbeg;
			ivend = vend;
			ivlocbeg = vlocbeg;
			ivlocend = vlocend;
Kirill Terekhov's avatar
Kirill Terekhov committed
494 495 496 497 498 499
#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
500
				printf("iter %3d resid %12g | %g\r", last_it, resid, atol);
Kirill Terekhov's avatar
Kirill Terekhov committed
501 502 503
				fflush(stdout);
			}
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
504
#pragma omp parallel
Kirill Terekhov's avatar
Kirill Terekhov committed
505
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
506 507 508
				long double tt, ts, tp, ttt;
				INMOST_DATA_ENUM_TYPE i = 0;
				while(true)
Kirill Terekhov's avatar
Kirill Terekhov committed
509
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
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 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
					ts = tp = 0;
					tt = Timer();
					{
						if( fabs(rho) < 1.0e-31 )
						{
							reason = "denominator(1) is zero";
							break;
						}
						if( fabs(omega) < 1.0e-31 )
						{
							reason = "denominator(2) is zero";
							break;
						}
						beta = 1.0 /rho * alpha / omega;
						rho = 0;
#pragma omp for reduction(+:rho)
						for(INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; k++) 
							rho += r0[k]*r[k];
						if( is_parallel ) 
						{
#pragma omp single
							info->Integrate(&rho,1);
						}
						beta *= rho;
					}
					{
#pragma omp for
						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
					}
					{
						ttt = Timer();
						if (prec != NULL)
						{
#pragma omp single
							prec->Solve(p, y);
						}
						tp += Timer() - ttt;
						if( is_parallel ) 
						{
#pragma omp single
							info->Update(y);
						}
						ttt = Timer();
						Alink->MatVec(1,y,0,v); // global multiplication, y should be updated, v probably needs an update
						ts += Timer() - ttt;
						if( is_parallel ) 
						{
#pragma omp single
							info->Update(v);
						}
					}
					{
						alpha = 0;
#pragma omp for reduction(+:alpha)
						for(INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; k++)  
							alpha += r0[k]*v[k];
						if( is_parallel ) 
						{
#pragma omp single
							info->Integrate(&alpha,1);
						}
						if( fabs(alpha) < 1.0e-31 )
						{
							reason = "denominator(3) is zero";
							break;
						}

						alpha = rho / alpha; //local indexes, r0, v
					}
					{
#pragma omp for
						for(INMOST_DATA_INTEGER_TYPE k = ivbeg; k < ivend; ++k) 
							s[k] = r[k] - alpha * v[k]; //global indexes r, v
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
585 586
				
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
						ttt = Timer();
						if (prec != NULL)
						{
#pragma omp single
							prec->Solve(s, z);
						}
						tp += Timer() - ttt;
						if( is_parallel ) 
						{
#pragma omp single
							info->Update(z);
						}
						ttt = Timer();
						Alink->MatVec(1.0,z,0,t); // global multiplication, z should be updated, t probably needs an update
						ts += Timer() - ttt;
						if( is_parallel ) 
						{
#pragma omp single
							info->Update(t);
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
607 608
					}
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
						
						temp[0] = temp[1] = 0;
#pragma omp for reduction(+:tempa) reduction(+:tempb)
						for(INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; k++)
						{
							tempa += t[k]*s[k];
							tempb += t[k]*t[k];
						}
						temp[0] = tempa;
						temp[1] = tempb;
						if( is_parallel ) 
						{
#pragma omp single
							info->Integrate(temp,2);
						}
						/*
						if (fabs(temp[0]) < 1.0e-35)
						{
							if (fabs(temp[1]) > 1.0e-35)
								break; //breakdown
							else omega = 0.0;
						}
						else 
						*/
						omega = temp[0] / (temp[1]+1e-35); //local indexes t, s
Kirill Terekhov's avatar
Kirill Terekhov committed
634
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
					{
#pragma omp for
						for(INMOST_DATA_INTEGER_TYPE k = ivbeg; k < ivend; ++k) 
							SOL[k] += alpha * y[k] + omega * z[k]; // global indexes SOL, y, z
					}
					{
#pragma omp for
						for(INMOST_DATA_INTEGER_TYPE k = ivbeg; k < ivend; ++k) 
							r[k] = s[k] - omega * t[k]; // global indexes r, s, t
					}
					last_it = i+1;
					{
						resid = 0;
#pragma omp for reduction(+:resid)
						for(INMOST_DATA_INTEGER_TYPE k = ivlocbeg; k < ivlocend; k++) 
							resid += r[k]*r[k];
						if( is_parallel ) 
						{
#pragma omp single
							info->Integrate(&resid,1);
						}
						resid = sqrt(resid);
					}
					tt = Timer() - tt;
Kirill Terekhov's avatar
Kirill Terekhov committed
659
#if defined(REPORT_RESIDUAL)
Kirill Terekhov's avatar
Kirill Terekhov committed
660 661 662 663 664 665 666 667 668 669 670
					if( info->GetRank() == 0 ) 
					{
						//std::cout << "iter " << last_it << " residual " << resid << " time " << tt << " matvec " << ts*0.5 << " precond " << tp*0.5 << 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);
#pragma omp single
						{
							printf("iter %3d resid %12g | %g\r", last_it, resid, atol);
							fflush(stdout);
						}
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
671
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
					last_resid = resid;
					if( resid != resid )
					{
						reason = "residual is NAN";
						break;
					}
					if( resid > divtol )
					{
						reason = "diverged due to divergence tolerance";
						break;
					}
					if( resid < atol )
					{
						reason = "converged due to absolute tolerance";
						break;
					}
					if( resid < rtol*resid0 )
					{
						reason = "converged due to relative tolerance";
						break;
					}
					if( i == maxits )
					{
						reason = "reached maximum iteration number";
						break;
					}
					i++;
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
700 701 702 703 704 705 706 707 708 709 710
			}
			//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; }
		bool ReplaceRHS(Solver::Vector & RHS) { return true; }
		bool ReplaceSOL(Solver::Vector & SOL) { return true; }
		Method * Duplicate() { return new BCGS_solver(*this);}
Kirill Terekhov's avatar
Kirill Terekhov committed
711
		std::string GetReason() {return reason;}
Kirill Terekhov's avatar
Kirill Terekhov committed
712 713 714 715 716
	};
}


#endif