solver_ilu2.hpp 15.1 KB
Newer Older
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1

Kirill Terekhov's avatar
Kirill Terekhov committed
2 3 4 5 6 7 8
#ifndef __SOLVER_ILU2__
#define __SOLVER_ILU2__
#include <iomanip>

#include "inmost_solver.h"
#include "solver_prototypes.hpp"
//#define REPORT_ILU
Kirill Terekhov's avatar
Kirill Terekhov committed
9
//#define REPORT_ILU_PROGRESS
Kirill Terekhov's avatar
Kirill Terekhov committed
10 11 12 13
using namespace INMOST;

#define DEFAULT_TAU 0.005
#define DEFAULT_TAU2 0.00001
Kirill Terekhov's avatar
Kirill Terekhov committed
14 15
//#define LFILL //control, that factorization is not less then fill for ilu2

Kirill Terekhov's avatar
Kirill Terekhov committed
16 17 18 19 20 21


class ILU2_preconditioner : public Method
{
private:
	
Kirill Terekhov's avatar
Kirill Terekhov committed
22
	Sparse::Matrix * Alink;
Kirill Terekhov's avatar
Kirill Terekhov committed
23
	Solver::OrderInfo * info;
Kirill Terekhov's avatar
Kirill Terekhov committed
24 25
	//Sparse::Matrix L,U;
	//Sparse::Vector div;
Kirill Terekhov's avatar
Kirill Terekhov committed
26 27 28
	std::vector<INMOST_DATA_REAL_TYPE> luv;
	std::vector<INMOST_DATA_ENUM_TYPE> lui;
	interval<INMOST_DATA_ENUM_TYPE,INMOST_DATA_ENUM_TYPE> ilu,iu;
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
29
	INMOST_DATA_ENUM_TYPE Lfill;
Kirill Terekhov's avatar
Kirill Terekhov committed
30
	INMOST_DATA_REAL_TYPE tau, tau2;
Kirill Terekhov's avatar
Kirill Terekhov committed
31
	Sparse::Vector DL, DR;
Kirill Terekhov's avatar
Kirill Terekhov committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
	INMOST_DATA_ENUM_TYPE nnz, sciters;
	bool init;
public:
	INMOST_DATA_REAL_TYPE & RealParameter(std::string name)
	{
		if( name == "tau" ) return tau;
		else if( name == "tau2" ) return tau2;
		throw -1;
	}
	INMOST_DATA_ENUM_TYPE & EnumParameter(std::string name)
	{
		if (name == "fill") return Lfill;
		else if (name == "scale_iters") return sciters;
		throw -1;
	}
	ILU2_preconditioner(Solver::OrderInfo & info)
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
48
		:info(&info),tau(DEFAULT_TAU), tau2(DEFAULT_TAU2)
Kirill Terekhov's avatar
Kirill Terekhov committed
49 50 51 52 53 54 55 56 57 58 59
	{
		Alink = NULL;
		init = false;
		sciters = 12;
		Lfill = 1;
	}
	bool Initialize()
	{
		if (isInitialized()) Finalize();
		assert(Alink != NULL);
		nnz = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
60
		for (Sparse::Matrix::iterator it = (*Alink).Begin(); it != (*Alink).End(); ++it) nnz += it->Size();
Kirill Terekhov's avatar
Kirill Terekhov committed
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
#if defined(LFILL)
		std::vector<INMOST_DATA_ENUM_TYPE> lfill;
		lfill.reserve(nnz * 4);
#endif
		luv.reserve(nnz * 4);
		lui.reserve(nnz * 4);
		
		
		std::vector<INMOST_DATA_REAL_TYPE> rv;
		std::vector<INMOST_DATA_ENUM_TYPE> ri;
		rv.reserve(nnz * 16);
		ri.reserve(nnz * 16);
		INMOST_DATA_ENUM_TYPE mobeg, moend, vlocbeg, vlocend, vbeg, vend, k, r, end, iter, j;
		INMOST_DATA_REAL_TYPE leabs, flin, ldiag, udiag, mva;
		INMOST_DATA_ENUM_TYPE curr, foll;
		INMOST_DATA_ENUM_TYPE  ind, jn;
		INMOST_DATA_INTEGER_TYPE prev, ipred;
		const INMOST_DATA_REAL_TYPE tol_modif = 1e-12, eps = 1.0e-54, subst = 1.0;
		const INMOST_DATA_ENUM_TYPE UNDEF = ENUMUNDEF, EOL = ENUMUNDEF - 1;
		//Calculate scaling vectors for matrix (from genebs)
		info->GetOverlapRegion(info->GetRank(), mobeg, moend);
		info->GetLocalRegion(info->GetRank(), vlocbeg, vlocend);
		info->GetVectorRegion(vbeg, vend);
		interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> ir(mobeg, moend + 1);
		interval<INMOST_DATA_INTEGER_TYPE, INMOST_DATA_REAL_TYPE> RowValues(vbeg, vend);
#if defined(LFILL)
		interval<INMOST_DATA_INTEGER_TYPE, INMOST_DATA_ENUM_TYPE> RowFill(vbeg, vend);
Kirill Terekhov's avatar
Kirill Terekhov committed
88
		//std::fill(RowFill.begin(),RowFill.end(),ENUMUNDEF);
Kirill Terekhov's avatar
Kirill Terekhov committed
89 90
#endif
		interval<INMOST_DATA_INTEGER_TYPE, INMOST_DATA_ENUM_TYPE> RowIndeces(vbeg - 1, vend);
Kirill Terekhov's avatar
Kirill Terekhov committed
91
		
Kirill Terekhov's avatar
Kirill Terekhov committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
		ilu.set_interval_beg(mobeg);
		ilu.set_interval_end(moend + 1);
		iu.set_interval_beg(mobeg);
		iu.set_interval_end(moend);
		ilu[mobeg] = 0;
		ir[mobeg] = 0;
#if defined(REPORT_ILU)
		std::cout << "Matrix overlap    " << mobeg << ".." << moend << std::endl;
		std::cout << "Local vector part " << vlocbeg << ".." << vlocend << std::endl;
		std::cout << "Entire vector     " << vbeg << ".." << vend << std::endl;
#endif
		//Rescale Matrix
		DL.SetInterval(mobeg, moend);
		info->PrepareVector(DR);
		for (k = mobeg; k < moend; k++)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
108
			for (Sparse::Row::iterator rit = (*Alink)[k].Begin(); rit != (*Alink)[k].End(); ++rit) DL[k] += rit->second*rit->second;
Kirill Terekhov's avatar
Kirill Terekhov committed
109 110 111 112
			if (DL[k] < eps) DL[k] = 1.0 / subst; else DL[k] = 1.0 / DL[k];
		}
		for (iter = 0; iter < sciters; iter++)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
113
			for(Sparse::Vector::iterator rit = DR.Begin(); rit != DR.End(); ++rit) *rit = 0.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
114
			for (k = vlocbeg; k < vlocend; k++)
Kirill Terekhov's avatar
Kirill Terekhov committed
115
				for (Sparse::Row::iterator rit = (*Alink)[k].Begin(); rit != (*Alink)[k].End(); ++rit) DR[rit->first] += DL[k] * rit->second*rit->second;
Kirill Terekhov's avatar
Kirill Terekhov committed
116 117 118
			info->Accumulate(DR);
			info->Update(DR);
			for (k = vlocbeg; k < vlocend; k++) if (DR[k] < eps) DR[k] = 1.0 / subst; else DR[k] = 1.0 / DR[k];
Kirill Terekhov's avatar
Kirill Terekhov committed
119
			for(Sparse::Vector::iterator rit = DL.Begin(); rit != DL.End(); ++rit) *rit = 0.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
120
			for (k = mobeg; k < moend; k++)
Kirill Terekhov's avatar
Kirill Terekhov committed
121
				for (Sparse::Row::iterator rit = (*Alink)[k].Begin(); rit != (*Alink)[k].End(); ++rit) DL[k] += DR[rit->first] * rit->second*rit->second;
Kirill Terekhov's avatar
Kirill Terekhov committed
122 123 124 125 126
			for (k = mobeg; k < moend; k++) if (DL[k] < eps) DL[k] = 1.0 / subst; else DL[k] = 1.0 / DL[k];
		}
		for (k = mobeg; k < moend; k++) DL[k] = sqrt(DL[k]);
		for (k =  vbeg; k <  vend; k++) DR[k] = sqrt(DR[k]);
		for (k = mobeg; k < moend; k++)
Kirill Terekhov's avatar
Kirill Terekhov committed
127
			for (Sparse::Row::iterator rit = (*Alink)[k].Begin(); rit != (*Alink)[k].End(); ++rit)
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
128
				rit->second = DL[k] * rit->second * DR[rit->first];
Kirill Terekhov's avatar
Kirill Terekhov committed
129 130

		//timer = Timer();
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
131
		for(interval<INMOST_DATA_INTEGER_TYPE,INMOST_DATA_ENUM_TYPE>::iterator it = RowIndeces.begin(); it != RowIndeces.end(); ++it) *it = UNDEF;
Kirill Terekhov's avatar
Kirill Terekhov committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
		std::vector<INMOST_DATA_ENUM_TYPE> sort_indeces;
		//INMOST_DATA_ENUM_TYPE nza = 0, nzl = 0, nzu = 0, nzu2 = 0;
		//for(k = mobeg; k != moend; k++) nza += A[k].Size();
		for (k = mobeg; k != moend; k++)
		{
#if defined(REPORT_ILU_PROGRESS)
			if (k % 1000 == 0)
			{
				//std::cout << "precond: " << (double)(k-mobeg)/(double)(moend-mobeg)*100 << "\r";
				//printf("%6.2f nza %12d nzl %12d nzu %12d nzu2 %12d\r", (double)(k-mobeg)/(double)(moend-mobeg)*100,nza,nzl,nzu,nzu2);
				printf("precond: %6.2f\r", (double)(k - mobeg) / (double)(moend - mobeg) * 100);
				fflush(stdout);
			}
#endif
			//Uncompress row
			//row_uncompr
Kirill Terekhov's avatar
Kirill Terekhov committed
148
			Sparse::Row & Ak = (*Alink)[k];
Kirill Terekhov's avatar
Kirill Terekhov committed
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 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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
			end = Ak.Size();
			sort_indeces.clear();
			for (r = 0; r < end; r++) if (fabs(Ak.GetValue(r)) > eps)
			{
				RowValues[Ak.GetIndex(r)] = Ak.GetValue(r);
#if defined(LFILL)
				RowFill[Ak.GetIndex(r)] = 0;
#endif
				ind = Ak.GetIndex(r);
				sort_indeces.push_back(ind);
			}
			std::sort(sort_indeces.begin(), sort_indeces.end());
			prev = static_cast<INMOST_DATA_INTEGER_TYPE>(vbeg)-1;
			ipred = static_cast<INMOST_DATA_INTEGER_TYPE>(vbeg)-1;
			for (r = 0; r < sort_indeces.size(); r++)
			{
				ind = sort_indeces[r];
				RowIndeces[prev] = ind;
				prev = static_cast<INMOST_DATA_INTEGER_TYPE>(ind);
				if (ind <= k) ipred = ind;
			}
			RowIndeces[prev] = EOL;

			if (ipred != static_cast<INMOST_DATA_INTEGER_TYPE>(k))
			{
				RowValues[k] = 0.0;
#if defined(LFILL)
				RowFill[k] = 0;
#endif
				ind = RowIndeces[ipred];
				RowIndeces[ipred] = k;
				RowIndeces[k] = ind;
			}
#if defined(DIAGONAL_PERTURBATION)
			RowValues[k] = RowValues[k]*(1.0+DIAGONAL_PERTURBATION_REL) + (RowValues[k] < 0.0? -1.0 : 1.0)*DIAGONAL_PERTURBATION_REL;
#endif
			//Eliminate lower part
			//elim_lpart
			j = RowIndeces[static_cast<INMOST_DATA_INTEGER_TYPE>(vbeg)-1];
			while (j < k) //until diagonal entry
			{
				assert(lui[iu[j]] == j);
				RowValues[j] *= luv[iu[j]]; //scale by diagonal
				leabs = fabs(RowValues[j]);
#if defined(LFILL)
				if (leabs > tau2*tau2)// introduce a non-zero, if threshold permits
#else
				if (leabs > tau2)// introduce a non-zero, if threshold permits
#endif
				{
					curr = j;
					for (r = iu[j] + 1; r < ilu[j + 1]; r++)
					{
						ind = lui[r];
						if (RowIndeces[ind] != UNDEF) //update without pondering on thresholds
						{
							RowValues[ind] -= RowValues[j] * luv[r];
#if defined(LFILL)
							RowFill[ind] = std::min(lfill[r]+1,RowFill[ind]);
#endif
						}
						else 
						{
							flin = -RowValues[j] * luv[r];
							//insert new value
							foll = curr;
							while (foll < ind)
							{
								curr = foll;
								foll = RowIndeces[curr];
							}
							assert(curr < ind);
							assert(ind < foll);
							RowIndeces[curr] = ind;
							RowIndeces[ind] = foll;
							RowValues[ind] = flin;
#if defined(LFILL)
							RowFill[ind] = lfill[r] + 1;
#endif
						}
						curr = ind;
					}

					if (leabs > tau)
					{
						curr = j;
						for (r = ir[j]; r < ir[j + 1]; r++)
						{
							//ind = U2j.GetIndex(r);
							ind = ri[r];
							if (RowIndeces[ind] != UNDEF) //update without pondering on thresholds
								RowValues[ind] -= RowValues[j] * rv[r];
							else // introduce a non-zero if threshold permits
							{
								flin = -RowValues[j] * rv[r];
								//insert new value
								foll = curr;
								while (foll < ind)
								{
									curr = foll;
									foll = RowIndeces[curr];
								}
								assert(curr < ind);
								assert(ind < foll);
								RowIndeces[curr] = ind;
								RowIndeces[ind] = foll;
								RowValues[ind] = flin;
#if defined(LFILL)
								RowFill[ind] = ENUMUNDEF;
#endif
							}
							curr = ind;
						}
					}
				}
				j = RowIndeces[j];
			}
			// Compress row
			//row_compr
			j = RowIndeces[static_cast<INMOST_DATA_INTEGER_TYPE>(vbeg)-1];
			//find minimum value in row
			ldiag = 0;
			while (j != EOL)
			{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
273 274
				INMOST_DATA_REAL_TYPE temp = fabs(RowValues[j]);
				ldiag = std::max(ldiag, temp);
Kirill Terekhov's avatar
Kirill Terekhov committed
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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
				j = RowIndeces[j];
			}
			if (ldiag < tau2)
			{
				ldiag = 1.0 / tau2;
				//std::cout << "ldiag too small " << ldiag << std::endl;
			}
			else
				ldiag = 1.0 / ldiag;

			//if (ldiag > 1000) std::cout << "ldiag is big " << k << " " << ldiag << std::endl;
			//divide all entries on right from the diagonal
			j = k;
			while (j != EOL)
			{
				RowValues[j] *= ldiag;
				j = RowIndeces[j];
			}
			j = RowIndeces[static_cast<INMOST_DATA_INTEGER_TYPE>(vbeg)-1];
			while (j < k)
			{
				mva = fabs(RowValues[j]);
				if (mva > tau2*tau2 )
				{
					if (mva > tau
#if defined(LFILL)
						|| RowFill[j] <= Lfill
#endif
						)
					{
						//L[k][j] = RowValues[j];
						lui.push_back(j); //lui indicates column index of L matrix
						luv.push_back(RowValues[j]); //luv indicates corresponding value
#if defined(LFILL)
						lfill.push_back(RowFill[j]);
#endif
						//nzl++;
					}
				}
				jn = RowIndeces[j];
				RowIndeces[j] = UNDEF;
				j = jn;
			}
			//add last diagonal entry to L matrix
			lui.push_back(k);
			luv.push_back(ldiag);
#if defined(LFILL)
			lfill.push_back(0);
#endif
			//nzl++;

Kirill Terekhov's avatar
Kirill Terekhov committed
326
			iu[k] = static_cast<INMOST_DATA_ENUM_TYPE>(luv.size()); //iu points to the first entry of current line of U matrix
Kirill Terekhov's avatar
Kirill Terekhov committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
			// END of L-part
			if (fabs(RowValues[j]) > tol_modif)
				udiag = 1.0 / RowValues[j];
			else
			{
				//std::cout << "udiag too small " << RowValues[j] << std::endl;
				udiag = (RowValues[j] < 0.0 ? -1.0 : 1.0) / tol_modif;
			}

			//if (fabs(udiag) > 1000) std::cout << "udiag is big " << k << " " << udiag << std::endl;

			jn = RowIndeces[j];
			RowIndeces[j] = UNDEF;
			j = jn;
			//start of U matrix entries
			//add diagonal value for U matrix
			lui.push_back(k);
			luv.push_back(udiag);
#if defined(LFILL)
			lfill.push_back(RowFill[k]);
#endif
			//nzu++;
			while (j != EOL)
			{
				mva = fabs(RowValues[j]);
				if (mva > tau2*tau2)
				{
					if (mva > tau
#if defined(LFILL)
						|| RowFill[j] <= Lfill
#endif
						)
					{
						//add values to U matrix
						lui.push_back(j);
						luv.push_back(RowValues[j]);
#if defined(LFILL)
						lfill.push_back(RowFill[j]);
#endif
						//nzu++;
					}
					else if (mva > tau2)
					{
						//add values to U2 matrix
						ri.push_back(j);
						rv.push_back(RowValues[j]);
						//nzu2++;
					}
				}
				jn = RowIndeces[j];
				RowIndeces[j] = UNDEF;
				j = jn;
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
380
			ilu[k + 1] = static_cast<INMOST_DATA_ENUM_TYPE>(luv.size()); //next first entry for L
Kirill Terekhov's avatar
Kirill Terekhov committed
381

Kirill Terekhov's avatar
Kirill Terekhov committed
382
			ir[k + 1] = static_cast<INMOST_DATA_ENUM_TYPE>(rv.size()); //next first entry for U2
Kirill Terekhov's avatar
Kirill Terekhov committed
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
			//END U-part
		}
		//printf("\n");
		//std::cout << "iluoo_solve: " << Timer() - timer << std::endl;
		//timer = Timer();
		//Rescale LU
		//xxlusc
		for (k = mobeg; k < moend; k++)
		{
			for (r = iu[k] - 1; r > ilu[k]; r--)
			{
				luv[r - 1] /= DL[k];
				//LFNORM += luv[r-1]*luv[r-1];
			}
			luv[iu[k] - 1] *= DL[k]; // L diagonal entry
			//LFNORM += luv[iu[k]-1]*luv[iu[k]-1];
		}
		for (k = mobeg; k < moend; k++)
		{
			for (r = iu[k] + 1; r < ilu[k + 1]; r++)
			{
				luv[r] /= DR[lui[r]];
				//UFNORM += luv[r]*luv[r];
			}
			luv[iu[k]] *= DR[k]; // U diagonal entry
			//UFNORM += luv[iu[k]]*luv[iu[k]];
		}
		//std::cout << "xxlusc: " << Timer() - timer << " LFNORM " << sqrt(LFNORM) << " UFNORM " << sqrt(UFNORM) << std::endl;
		//timer = Timer();


		//Rescale matrix back
		//matisc
		for (k = mobeg; k < moend; k++)
Kirill Terekhov's avatar
Kirill Terekhov committed
417
			for (Sparse::Row::iterator rit = (*Alink)[k].Begin(); rit != (*Alink)[k].End(); ++rit)
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
418
				rit->second = rit->second / DL[k] / DR[rit->first];
Kirill Terekhov's avatar
Kirill Terekhov committed
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 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
		//std::cout << "matisc: " << Timer() - timer << std::endl;

#if defined(REPORT_ILU)
		INMOST_DATA_ENUM_TYPE nzu,nzl, nza;
		nzu = 0;
		nzl = 0;
		nza = 0;
		for(INMOST_DATA_ENUM_TYPE k = mobeg; k < moend; k++)
		{
			nzl += iu[k] - ilu[k];
			nzu += ilu[k+1] - iu[k] - 1;
			nza += (*Alink)[k].Size();
		}
		std::cout << "      nonzeros in A = " << nza << std::endl;
		std::cout << "      nonzeros in L = " << nzl - (moend-mobeg) << std::endl;
		std::cout << "      nonzeros in U = " << nzu << std::endl;
		std::cout << "     nonzeros in LU = " << ilu[moend] - 1 << std::endl;
		std::cout << "     nonzeros in U2 = " << ir[moend] - 1 << std::endl;
		//std::cout << __FUNCTION__ << " done" << std::endl;
#endif

		/*
		info.PrepareVector(div);
		std::fill(div.Begin(),div.End(),0);
		for(k = mobeg; k < moend; k++) div[k] = 1.0;
		info.Accumulate(div);
		for(k = mobeg; k < moend; k++) div[k] = 1.0/div[k];
		*/
		init = true;
		return true;
	}
	bool isInitialized(){ return init; }
	bool Finalize()
	{
		if (!isFinalized())
		{
			luv.clear();
			lui.clear();
			init = false;
		}
		return true;
	}
	bool isFinalized() { return !init; }
	~ILU2_preconditioner()
	{
		if (!isFinalized()) Finalize();
	}
	void Copy(const Method * other)
	{
		const ILU2_preconditioner * b = dynamic_cast<const ILU2_preconditioner *>(other);
		assert(b != NULL);
		info = b->info;
		Alink = b->Alink;
		DL = b->DL;
		DR = b->DR;
		nnz = b->nnz;
		luv = b->luv;
		lui = b->lui;
		iu = b->iu;
		ilu = b->ilu;
	}
	ILU2_preconditioner(const ILU2_preconditioner & other)
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
481
		:Method(other)
Kirill Terekhov's avatar
Kirill Terekhov committed
482 483 484 485 486 487 488 489
	{
		Copy(&other);
	}
	ILU2_preconditioner & operator =(ILU2_preconditioner const & other)
	{
		Copy(&other);
		return *this;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
490
	bool Solve(Sparse::Vector & input, Sparse::Vector & output)
Kirill Terekhov's avatar
Kirill Terekhov committed
491 492
	{
		assert(isInitialized());
Kirill Terekhov's avatar
Kirill Terekhov committed
493 494 495
#if defined(USE_OMP)
#pragma omp single
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
496
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
			INMOST_DATA_ENUM_TYPE mobeg, moend, r, k, vbeg,vend; //, end;
			info->GetOverlapRegion(info->GetRank(),mobeg,moend);
			info->GetVectorRegion(vbeg,vend);
			for(k = vbeg; k < mobeg; k++) output[k] = 0; //Restrict additive schwartz (maybe do it outside?)
			for(k = mobeg; k < moend; k++) output[k] = input[k];
			for(k = moend; k < vend; k++) output[k] = 0; //Restrict additive schwartz (maybe do it outside?)
			for(k = mobeg; k < moend; k++) //iterate over L part
			{
				for(r = iu[k]-1; r > ilu[k]; r--) 
					output[k] -= luv[r-1]*output[lui[r-1]];
				output[k] *= luv[iu[k]-1];
			}
			for(k = moend; k > mobeg; k--) //iterate over U part
			{
				for(r = iu[k-1]+1; r < ilu[k]; r++)
					output[k-1] -= luv[r]*output[lui[r]];
				output[k-1] *= luv[iu[k-1]];
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
515
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
516 517
		//May assemble partition of unity instead of restriction before accumulation
		//assembly should be done instead of initialization
Kirill Terekhov's avatar
Kirill Terekhov committed
518 519 520
		info->Accumulate(output);
		return true;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
521 522 523
	bool ReplaceMAT(Sparse::Matrix & A) { if (isInitialized()) Finalize();  Alink = &A; return true; };
	bool ReplaceSOL(Sparse::Vector & x) {(void)x;return true;}
	bool ReplaceRHS(Sparse::Vector & b) {(void)b;return true;}
Kirill Terekhov's avatar
Kirill Terekhov committed
524 525 526 527 528 529 530
	Method * Duplicate() { return new ILU2_preconditioner(*this); }
};




#endif //__SOLVER_ILU2__