solver_ddpqiluc2.cpp 129 KB
Newer Older
Kirill Terekhov's avatar
Kirill Terekhov committed
1 2 3
#define _CRT_SECURE_NO_WARNINGS
#include <iomanip>
#include "inmost_solver.h"
4
#if defined(USE_SOLVER)
Kirill Terekhov's avatar
Kirill Terekhov committed
5 6
#include "solver_ddpqiluc2.hpp"

Kirill Terekhov's avatar
Kirill Terekhov committed
7 8 9 10 11

/// \todo
/// 1. Correct estimator (as in MPTILUC)
/// 2. Remove premature dropping.

Kirill Terekhov's avatar
Kirill Terekhov committed
12 13 14 15 16
//#define PRINT_DDPQ
//#define PRINT_HISTOGRAM
//#define REPORT_ILU
//#undef REPORT_ILU
//#define REPORT_ILU_PROGRESS
Kirill Terekhov's avatar
Kirill Terekhov committed
17 18
//#define REPORT_ILU_END
//#define REPORT_ILU_SUMMARY
Kirill Terekhov's avatar
Kirill Terekhov committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
//#undef REPORT_ILU_PROGRESS
//#define REPORT_SCHUR
using namespace INMOST;

#ifndef DEFAULT_TAU
#define DEFAULT_TAU 0.01
#endif

//#define REORDER_METIS_ND
#define REORDER_DDPQ
#define DDPQ_TAU 0.8 //ratio is time consumed for factorization by time for schur complement calculation
//#define DDPQ_TAU std::max(0.1,1.0/(1.0+exp(-ratio*5)))
#define DDPQ_ADAPT_TAU
#define ESTIMATOR
#define RESCALE_B
#define PIVOT_THRESHOLD
Kirill Terekhov's avatar
Kirill Terekhov committed
35
#define DIAGONAL_PERTURBATION
Kirill Terekhov's avatar
Kirill Terekhov committed
36 37
#define DIAGONAL_PERTURBATION_REL 1.0e-6
#define DIAGONAL_PERTURBATION_ABS 1.0e-15
Kirill Terekhov's avatar
Kirill Terekhov committed
38
//#define DIAGONAL_PIVOT //probably there is some bug
Kirill Terekhov's avatar
Kirill Terekhov committed
39 40 41 42 43 44 45 46 47 48 49 50 51
#define DIAGONAL_PIVOT_TAU 0.01
//#define DIAGONAL_PIVOT_COND 5.0
#define ILUC2

#if defined(REORDER_METIS_ND)
#include "metis.h"
#endif


#define ADDR(row,col) ((row)*size+(col))


	void ILUC_preconditioner::DumpMatrix(interval<INMOST_DATA_ENUM_TYPE, Interval> & Address, 
Kirill Terekhov's avatar
Kirill Terekhov committed
52
									std::vector<Sparse::Row::entry> & Entries,
Kirill Terekhov's avatar
Kirill Terekhov committed
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
									INMOST_DATA_ENUM_TYPE wmbeg, INMOST_DATA_ENUM_TYPE wmend,
									std::string file_name)
	{
		INMOST_DATA_REAL_TYPE norm1 = 0, norm2 = 0, max = -1.0e54, min = 1.0e54, minabs = 1.0e54;
		INMOST_DATA_ENUM_TYPE nnz = 0;
		for (INMOST_DATA_ENUM_TYPE k = wmbeg; k < wmend; ++k) 
		{
			nnz += Address[k].Size();

			for (INMOST_DATA_ENUM_TYPE it = Address[k].first; it != Address[k].last; ++it)
			{
				norm1 += fabs(Entries[it].second);
				norm2 += Entries[it].second*Entries[it].second;
				if( Entries[it].second > max ) max = Entries[it].second;
				if( Entries[it].second < min ) min = Entries[it].second;
				if( fabs(Entries[it].second) < minabs ) minabs = Entries[it].second;
			}
		}

		std::cout << "Writing matrix to " << file_name.c_str() << std::endl;
		std::fstream fout(file_name.c_str(),std::ios::out);
		fout << "%%MatrixMarket matrix coordinate real general" << std::endl;
		fout << "% maximum " << max << std::endl;
		fout << "% minimum " << min << std::endl;
		fout << "% absolute minimum " << minabs << std::endl;
		fout << "% 1-norm  " << norm1 << std::endl;
		fout << "% 2-norm  " << sqrt(norm2) << std::endl;
		fout << "% mean 1-norm  " << norm1/nnz << std::endl;
		fout << "% mean 2-norm  " << sqrt(norm2/nnz) << std::endl;
		fout << std::scientific;
		
		fout << wmend-wmbeg << " " << wmend-wmbeg << " " << nnz << std::endl;;
		for (INMOST_DATA_ENUM_TYPE k = wmbeg; k < wmend; ++k)
		{
			for (INMOST_DATA_ENUM_TYPE it = Address[k].first; it != Address[k].last; ++it)
				fout << (k-wmbeg+1) << " " << (Entries[it].first-wmbeg+1) << " " << Entries[it].second << std::endl;
		}
		fout.close();
	}
	void ILUC_preconditioner::SwapEntries(interval<INMOST_DATA_ENUM_TYPE, Interval> & Address, 
Kirill Terekhov's avatar
Kirill Terekhov committed
93
					 std::vector<Sparse::Row::entry> & Entries, 
Kirill Terekhov's avatar
Kirill Terekhov committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 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 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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
					 INMOST_DATA_ENUM_TYPE rbeg, INMOST_DATA_ENUM_TYPE rend, 
					 INMOST_DATA_ENUM_TYPE k, INMOST_DATA_ENUM_TYPE j)
	{
		INMOST_DATA_ENUM_TYPE i;
		INMOST_DATA_REAL_TYPE u;
		for (i = rbeg; i < rend; i++)
		{
			INMOST_DATA_ENUM_TYPE pk = Address[i].last, pj = Address[i].last, r;
			for (r = Address[i].first; r < Address[i].last; r++)
			{
				if (Entries[r].first >= k) { pk = r; break; }
			}
			for (r = pk; r < Address[i].last; r++)
			{
				if (Entries[r].first >= j) { pj = r; break; }
			}
			if (pk < Address[i].last)
			{
				if (pj < Address[i].last && Entries[pj].first == j)
				{
					if (Entries[pk].first == k)
					{
						u = Entries[pj].second;
						Entries[pj].second = Entries[pk].second;
						Entries[pk].second = u;
					}
					else
					{
						u = Entries[pj].second;
						for (r = pj; r > pk; r--) Entries[r] = Entries[r - 1];
						Entries[pk].first = k;
						Entries[pk].second = u;
					}
				}
				else
				{
					if (Entries[pk].first == k)
					{
						u = Entries[pk].second;
						for (r = pk; r < pj - 1; r++) Entries[r] = Entries[r + 1];
						Entries[pj - 1].first = j;
						Entries[pj - 1].second = u;
					}
				}
			}
		}
	}
	void ILUC_preconditioner::SwapLine(interval<INMOST_DATA_ENUM_TYPE, Interval> & Line, INMOST_DATA_ENUM_TYPE i, INMOST_DATA_ENUM_TYPE j)
	{
		INMOST_DATA_ENUM_TYPE temp;
		temp = Line[i].first;
		Line[i].first = Line[j].first;
		Line[j].first = temp;
		temp = Line[i].last;
		Line[i].last = Line[j].last;
		Line[j].last = temp;
	}
	void ILUC_preconditioner::SwapE(INMOST_DATA_ENUM_TYPE i, INMOST_DATA_ENUM_TYPE j)
	{
		INMOST_DATA_ENUM_TYPE k;
		for (k = 0; k < E_Address.size(); ++k)
			SwapLine(*E_Address[k], i, j);
	}
	
	void ILUC_preconditioner::ReorderEF(INMOST_DATA_ENUM_TYPE mobeg, 
					INMOST_DATA_ENUM_TYPE cbeg,
					INMOST_DATA_ENUM_TYPE cend,
					INMOST_DATA_ENUM_TYPE wend, 
					interval<INMOST_DATA_ENUM_TYPE, bool> & donePQ, 
					interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> & localP,
					interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> & localQ)
	{
		(void)cend;
		INMOST_DATA_ENUM_TYPE i, k;
		//Reorder E rows block by P
		//for(interval<INMOST_DATA_ENUM_TYPE, bool>::iterator it = donePQ.begin() + cbeg - mobeg;
		//	it != donePQ.begin() + wend - mobeg; ++it) *it = false;
		std::fill(donePQ.begin() + cbeg - mobeg, donePQ.begin() + wend - mobeg, false);
		i = cbeg;
		while (i < wend)
		{
			if (donePQ[i]) i++;
			else
			{
				if (localP[i] != i)
				{
					k = i;
					do
					{
						SwapE(i, localP[k]);
						donePQ[localP[k]] = true;
						k = localP[k];
					} while (k != i);
				}
				donePQ[i] = true;
			}
		}
		//Reorder F columns by  Q
		/*
		std::fill(donePQ.begin() + cbeg - mobeg, donePQ.begin() + wend - mobeg, false);
		i = cbeg;
		while (i < wend)
		{
			if (donePQ[i]) i++;
			else
			{
				if (localQ[i] != i)
				{
					k = i;
					do
					{
						SwapF(i, localQ[k]);
						donePQ[localQ[k]] = true;
						k = localQ[k];
					} while (k != i);
				}
				donePQ[i] = true;
			}
		}
		*/
		for (i = mobeg; i < cbeg; i++)
		{
			for (k = F_Address[i].first; k < F_Address[i].last; ++k)
			{
				if (F_Entries[k].first >= cbeg)
					F_Entries[k].first = localQ[F_Entries[k].first];
			}
		}
	}
	void ILUC_preconditioner::inversePQ(INMOST_DATA_ENUM_TYPE wbeg,
				   INMOST_DATA_ENUM_TYPE wend, 
				   interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> & localP,
				   interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> & localQ,
				   interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> & invP,
				   interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> & invQ)
	{
		//inverse reordering
		// in invPQ numbers indicate where to get current column
		for (INMOST_DATA_ENUM_TYPE k = wbeg; k != wend; ++k) invP[localP[k]] = k;
		for (INMOST_DATA_ENUM_TYPE k = wbeg; k != wend; ++k) invQ[localQ[k]] = k;
	}
	void ILUC_preconditioner::applyPQ(INMOST_DATA_ENUM_TYPE wbeg,
				 INMOST_DATA_ENUM_TYPE wend,
				 interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> & localP,
				 interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> & localQ,
				 interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> & invP,
				 interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> & invQ)
	{
		INMOST_DATA_ENUM_TYPE k;
		// compute reordering in global P,Q, we need it to compute reordering in vector during solve phase
		for (k = wbeg; k != wend; ++k)
		{
			localP[k] = ddP[invP[k]];
			localQ[k] = ddQ[invQ[k]];
		}
		// update reordering in global P,Q
		for (k = wbeg; k != wend; ++k)
		{
			ddP[k] = localP[k];
			ddQ[k] = localQ[k];
		}
	}
	INMOST_DATA_ENUM_TYPE & ILUC_preconditioner::EnumParameter(std::string name)
	{
		if (name == "scale_iters") return sciters;
		else if( name == "reorder_nnz") return reorder_nnz;
		else if( name == "ddpq_tau_adapt" ) return ddpq_tau_adapt;
		else if( name == "estimator" ) return estimator;
		throw - 1;
	}
	INMOST_DATA_REAL_TYPE & ILUC_preconditioner::RealParameter(std::string name)
	{
		if (name == "tau") return tau;
		else if( name == "ddpq_tau" ) return ddpq_tau;
		else if( name == "tau2" ) return iluc2_tau;
		throw - 1;
	}
	void ILUC_preconditioner::Copy(const Method * other)
	{
		const ILUC_preconditioner * b = dynamic_cast<const ILUC_preconditioner *>(other);
		assert(b != NULL);
		tau = b->tau;
		Alink = b->Alink;
		info = b->info;
		sciters = b->sciters;
		eps = b->eps;
	}
	ILUC_preconditioner::ILUC_preconditioner(const ILUC_preconditioner & other) :Method(other)
	{
		Copy(&other);
	}
	ILUC_preconditioner & ILUC_preconditioner::operator =(ILUC_preconditioner const & other)
	{
		Copy(&other);
		return *this;
	}
	ILUC_preconditioner::ILUC_preconditioner(Solver::OrderInfo & info)
		:tau(DEFAULT_TAU),info(&info)
	{
		Alink = NULL;
		init = false;
		sciters = 8;
		eps = 1e-54;
#if defined(DDPQ_ADAPT_TAU)
		ddpq_tau_adapt = 1;
#else
		ddpq_tau_adapt = 0;
#endif
#if defined(ESTIMATOR)
		estimator = 1;
#else
		estimator = 0;
#endif
		ddpq_tau = DDPQ_TAU;
		reorder_nnz = 1;
		tau = 1.0e-3;
		iluc2_tau = tau*tau;
	}
	bool ILUC_preconditioner::isInitialized() { return init; }
	bool ILUC_preconditioner::isFinalized() { return !init; }
	bool ILUC_preconditioner::Initialize()
	{

#if defined(PRINT_HISTOGRAM)
		FILE * hist = fopen("histogram.csv","w");
#endif
#if defined(PRINT_DDPQ)
		FILE * fddpq = fopen("ddpq.csv","w");
#endif
#if defined(REPORT_SCHUR)
		FILE * fschur = fopen("schur.csv","w");
#endif
		const INMOST_DATA_REAL_TYPE subst = 1.0;
		const INMOST_DATA_REAL_TYPE tol_modif = 1e-12;
		const INMOST_DATA_ENUM_TYPE UNDEF = ENUMUNDEF, EOL = ENUMUNDEF - 1;

		
		INMOST_DATA_ENUM_TYPE wbeg, wend; //working interval
		INMOST_DATA_ENUM_TYPE mobeg, moend; // total interval
		
Kirill Terekhov's avatar
Kirill Terekhov committed
334
		INMOST_DATA_ENUM_TYPE k, i, j, Li, Ui, curr, next, mi, mj, Sbeg;
Kirill Terekhov's avatar
Kirill Terekhov committed
335 336
		INMOST_DATA_REAL_TYPE l,u,udiag, abs_udiag, max_diag, min_diag, mean_diag, tol_schur;
		INMOST_DATA_ENUM_TYPE nzA, nzLU = 0, nzEF = 0, nzS;
Kirill Terekhov's avatar
Kirill Terekhov committed
337
		Sparse::Vector DL, DR;
Kirill Terekhov's avatar
Kirill Terekhov committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
		info->GetOverlapRegion(info->GetRank(), mobeg, moend);
		
		
		//prepare temporal array
		temp.set_interval_beg(mobeg);
		temp.set_interval_end(moend);
		//for(interval<INMOST_DATA_ENUM_TYPE,INMOST_DATA_REAL_TYPE>::iterator rit = temp.begin();
		//	rit != temp.end(); ++rit) *rit = 0.0;
		std::fill(temp.begin(), temp.end(), 0.0);

		
		//prepare reordering vectors
		ddP.set_interval_beg(mobeg);
		ddP.set_interval_end(moend);
		ddQ.set_interval_beg(mobeg);
		ddQ.set_interval_end(moend);

		

		//prepare rescaling vectors
		DL.SetInterval(mobeg, moend);
		DR.SetInterval(mobeg, moend);
Kirill Terekhov's avatar
Kirill Terekhov committed
360
		for(Sparse::Vector::iterator ri = DL.Begin(); ri != DL.End(); ++ri) *ri = 0.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
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

		

		for (k = mobeg; k != moend; k++) ddP[k] = ddQ[k] = k;


		
		//preinitialize data
		level_size.clear();

		

		// supplementary data for ddPQ reordering
		interval<INMOST_DATA_ENUM_TYPE, bool> donePQ(mobeg, moend);

		

		interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> invP(mobeg, moend), invQ(mobeg,moend), localP(mobeg, moend), localQ(mobeg,moend);


		
#if defined(REORDER_DDPQ)
		wgt_coords sort_wgts(2 * (moend - mobeg));
#endif
		

		//supplimentary data structures for ILUC
		INMOST_DATA_ENUM_TYPE LU_Beg;
		U_Address.set_interval_beg(mobeg);
		U_Address.set_interval_end(moend);
		L_Address.set_interval_beg(mobeg);
		L_Address.set_interval_end(moend);
		F_Address.set_interval_beg(mobeg);
		F_Address.set_interval_end(moend);
		B_Address.set_interval_beg(mobeg);
		B_Address.set_interval_end(moend);
		LU_Diag.set_interval_beg(mobeg);
		LU_Diag.set_interval_end(moend);
Kirill Terekhov's avatar
Kirill Terekhov committed
399
		std::vector<Sparse::Row::entry> A_Entries, C_Entries, S_Entries, LF_Entries, LFt_Entries;
Kirill Terekhov's avatar
Kirill Terekhov committed
400 401 402 403 404 405 406 407
		//std::vector<INMOST_DATA_ENUM_TYPE> sort_indeces;
		interval<INMOST_DATA_ENUM_TYPE, Interval> A_Address(mobeg, moend), C_Address(mobeg,moend), S_Address(mobeg,moend), LF_Address(mobeg,moend), LFt_Address(mobeg,moend);
		interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> Ulist(mobeg, moend), Llist(mobeg, moend), Blist(mobeg,moend),Alist(mobeg,moend),Afirst(mobeg,moend), Flist(mobeg,moend);
		interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> Ubeg(mobeg, moend,EOL), Lbeg(mobeg, moend,EOL), Bbeg(mobeg,moend,EOL),Abeg(mobeg,moend,EOL), Fbeg(mobeg,moend,EOL);



		//supplimentary data structures for condition estimates of L^{-1}, U^{-1}
Kirill Terekhov's avatar
Kirill Terekhov committed
408 409 410 411
#if defined(ESTIMATOR)
		INMOST_DATA_REAL_TYPE mup, mum, smup, smum, NuU = 1, NuL = 1, NuD, NuL1 = 1, NuL2 = 1, NuU1 = 1, NuU2 = 1;
		INMOST_DATA_REAL_TYPE NuU1_old = 1, NuL1_old = 1, NuU2_old = 1, NuL2_old = 1;
		INMOST_DATA_REAL_TYPE NuU1_new, NuU2_new, NuL1_new, NuL2_new, vp, vm, v;
Kirill Terekhov's avatar
Kirill Terekhov committed
412
		INMOST_DATA_ENUM_TYPE np, nm;
Kirill Terekhov's avatar
Kirill Terekhov committed
413 414 415
		interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_REAL_TYPE> EstL1(mobeg, moend,0.0), EstU1(mobeg, moend,0.0), CondU(mobeg,moend,1.0);
		interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_REAL_TYPE> EstL2(mobeg, moend,0.0), EstU2(mobeg, moend,0.0), CondL(mobeg,moend,1.0);
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
416 417 418 419 420 421
		//supplimentary data structures for returning values of dropped elements
		//INMOST_DATA_REAL_TYPE DropLk, DropUk;
		//interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_REAL_TYPE> DropU(mobeg,moend,0.0), DropL(mobeg,moend,0.0);
		//data structure for linked list

		
Kirill Terekhov's avatar
Kirill Terekhov committed
422 423 424 425
		interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_REAL_TYPE> LineValuesL(mobeg, moend,0.0);
		interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> LineIndecesL(mobeg, moend+1,UNDEF);
		interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_REAL_TYPE> LineValuesU(mobeg, moend,0.0);
		interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> LineIndecesU(mobeg, moend+1,UNDEF);
Kirill Terekhov's avatar
Kirill Terekhov committed
426 427 428 429 430 431 432 433 434
		double tfactor = 0.0, trescale = 0.0, tschur = 0.0, treorder = 0.0, treassamble = 0.0, ttotal, tt;
		double tlfactor, tlrescale, tlschur, tlreorder, tlreassamble, ratio = 1.0, ratio2 = 1.0;
		ttotal = Timer();

		
		//calculate number of nonzeros
		nzA = 0;
		for (k = mobeg; k < moend; ++k)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
435
			for (Sparse::Row::iterator r = (*Alink)[k].Begin(); r != (*Alink)[k].End(); ++r)
Kirill Terekhov's avatar
Kirill Terekhov committed
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
				if (r->first >= mobeg && r->first < moend && fabs(r->second) > 0.0) nzA++;
		}

		//sort_indeces.reserve(256);
		A_Entries.resize(nzA);
		B_Entries.reserve(nzA);
		C_Entries.reserve(nzA);
		S_Entries.reserve(nzA);
		E_Entries.reserve(nzA*2);
		F_Entries.reserve(nzA*2);
		LU_Entries.reserve(nzA);
		LF_Entries.reserve(nzA);
		LFt_Entries.reserve(nzA);

		j = 0;
		for (k = mobeg; k < moend; ++k)
		{
			A_Address[k].first = j;
Kirill Terekhov's avatar
Kirill Terekhov committed
454
			for (Sparse::Row::iterator r = (*Alink)[k].Begin(); r != (*Alink)[k].End(); ++r)
Kirill Terekhov's avatar
Kirill Terekhov committed
455 456
			{
				if (r->first >= mobeg && r->first < moend && fabs(r->second) > 0.0)
Kirill Terekhov's avatar
Kirill Terekhov committed
457
					A_Entries[j++] = Sparse::Row::make_entry(r->first, r->second);
Kirill Terekhov's avatar
Kirill Terekhov committed
458 459
			}
			A_Address[k].last = j;
Kirill Terekhov's avatar
Kirill Terekhov committed
460
			//assert(A_Address[k].Size() != 0); //singular matrix
Kirill Terekhov's avatar
Kirill Terekhov committed
461 462 463 464 465 466
		}
		INMOST_DATA_REAL_TYPE  Anorm, Enorm, Fnorm;
		INMOST_DATA_ENUM_TYPE nzE,nzF;
#if defined(ILUC2)
		INMOST_DATA_ENUM_TYPE nzLU2 = 0;
		INMOST_DATA_REAL_TYPE tau2 = iluc2_tau;
Kirill Terekhov's avatar
Kirill Terekhov committed
467
		std::vector<Sparse::Row::entry> LU2_Entries;
Kirill Terekhov's avatar
Kirill Terekhov committed
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 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 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 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 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 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 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 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
		interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_REAL_TYPE> Fnorms(mobeg,moend);
		interval<INMOST_DATA_ENUM_TYPE, Interval> L2_Address(mobeg, moend+1), U2_Address(mobeg, moend+1);
		interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> U2list(mobeg, moend, UNDEF), L2list(mobeg, moend, UNDEF);
		interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> U2beg(mobeg, moend, EOL), L2beg(mobeg, moend, EOL);
		LU2_Entries.reserve(nzA * 4);
#else
		INMOST_DATA_REAL_TYPE tau2 = tau;
#endif

#if defined(REPORT_ILU)
		std::cout << "nonzeros in A " << nzA << std::endl;
#endif
		//set up working interval
		wbeg = mobeg;
		wend = moend;
		while (wbeg < wend) //iterate into levels until all matrix is factored
		{
			//ddPQ reordering on current Schur complement
			INMOST_DATA_ENUM_TYPE cbeg = wbeg, cend = wend; //next size of factored B block
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////// REORDERING                /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			tt = Timer();
			//for(interval<INMOST_DATA_ENUM_TYPE,INMOST_DATA_ENUM_TYPE>::iterator it = localP.begin()+(wbeg-mobeg);
			//	it != localP.begin() + (wend-mobeg); ++it) *it = ENUMUNDEF;
			//for(interval<INMOST_DATA_ENUM_TYPE,INMOST_DATA_ENUM_TYPE>::iterator it = localQ.begin()+(wbeg-mobeg);
			//	it != localQ.begin() + (wend-mobeg); ++it) *it = ENUMUNDEF;
			std::fill(localP.begin() + (wbeg - mobeg), localP.begin() + (wend - mobeg), ENUMUNDEF);
			std::fill(localQ.begin() + (wbeg - mobeg), localQ.begin() + (wend - mobeg), ENUMUNDEF);

#if defined(REORDER_DDPQ)

#if defined(REPORT_ILU)
			std::cout << level_size.size() << " calculate weights" << std::endl;
#endif
			std::fill(DL.Begin() + wbeg - mobeg, DL.Begin() + wend - mobeg, 0.0);
			std::fill(DR.Begin() + wbeg - mobeg, DR.Begin() + wend - mobeg, 0.0);
			std::fill(Ulist.begin() + wbeg - mobeg, Ulist.begin() + wend - mobeg, 0);
			std::fill(Llist.begin() + wbeg - mobeg, Llist.begin() + wend - mobeg, 0);
			for (k = wbeg; k < wend; ++k)
			{
				for (INMOST_DATA_ENUM_TYPE it = A_Address[k].first; it != A_Address[k].last; ++it)
				{
					i = A_Entries[it].first;
					u = fabs(A_Entries[it].second);
					//row-col norms
					DL[k] += u; //row norm
					DR[i] += u; //col norm
					//nonzeros
					Ulist[k]++; //row nnz
					Llist[i]++; //col nnz
				}
			}

			INMOST_DATA_REAL_TYPE ddmaxall = 0, ddmaxcur, ddcur, ddmaxmean = 0.0, ddmaxmin = 1.0e+20;
			for (k = wbeg; k < wend; ++k)
			{
				ddmaxcur = 0.0;
				Blist[k] = ENUMUNDEF;
				for (INMOST_DATA_ENUM_TYPE it = A_Address[k].first; it != A_Address[k].last; ++it)
				{
					i = A_Entries[it].first;
					u = fabs(A_Entries[it].second);
					ddcur = u / (DL[k] + DR[i] - u);
					if (ddmaxcur < ddcur)
					{
						ddmaxcur = ddcur;
						Blist[k] = i;
					}
				}
				temp[k] = ddmaxcur;
				if (ddmaxall < ddmaxcur) ddmaxall = ddmaxcur;
				if (ddmaxmin > ddmaxcur) ddmaxmin = ddmaxcur;
				ddmaxmean += ddmaxcur*ddmaxcur;
			}

			ddmaxmean = sqrt(ddmaxmean / (1.0*(wend - wbeg)));

#if defined(PRINT_DDPQ)
			int histogram[32];
			memset(histogram,0,sizeof(int)*32);
			for (k = wbeg; k < wend; ++k)
			{
				//histogram[std::max(std::min(31-(int)floor((temp[k]-ddmaxmin)/(ddmaxall-ddmaxmin)*31),31),0)]++;
				int pos = std::min((int)ceil(-log((fabs(temp[k])-ddmaxmin)/(ddmaxall-ddmaxmin))),31);
				if( pos < 0 ) pos = 31;
				++histogram[ pos ];
			}
			fprintf(fddpq,"%g; %g; %g; %d",ddmaxall,ddmaxmin,ddmaxmean,histogram[0]);
			for(int hi = 1; hi < 32; ++hi)
				fprintf(fddpq,"; %d",histogram[hi]);
			fprintf(fddpq,"\n");
#endif
			
			//first select those elements that are largest over row and column simultaneously
			sort_wgts.clear();
			for (k = wbeg; k < wend; ++k)
			{
				if (Blist[k] != ENUMUNDEF)
				{
					//if ((temp[k]-ddmaxmin)/(ddmaxall-ddmaxmin) > ddpq_tau)
					//	sort_wgts.push_back(wgt_coord((Ulist[k]+Llist[Blist[k]]-1), coord(k, Blist[k])));
					sort_wgts.push_back(wgt_coord(temp[k], coord(k, Blist[k])));
					//sort_wgts.push_back(wgt_coord(pow(temp[k],0.25)/(Ulist[k]+Llist[Blist[k]]-1), coord(k, Blist[k])));
					//sort_wgts.push_back(wgt_coord(1.0/(Ulist[k]+Llist[Blist[k]]-1), coord(k, Blist[k])));
					//sort_wgts.push_back(wgt_coord(temp[k]/(Ulist[k]+Llist[Blist[k]]-1), coord(k, Blist[k])));
				}
			}
			//if(reorder_nnz) 
				std::sort(sort_wgts.rbegin(), sort_wgts.rend());
#if defined(REPORT_ILU)
			std::cout << level_size.size() << " fill reordering" << std::endl;
#endif
			i = wbeg;
			for (wgt_coords::iterator it = sort_wgts.begin(); it != sort_wgts.end(); ++it)
			{
				if (localP[it->second.first] == ENUMUNDEF && localQ[it->second.second] == ENUMUNDEF)
				{
					localP[it->second.first] = i;
					localQ[it->second.second] = i;
					i++;
				}

				if( it->first / sort_wgts.begin()->first < ddpq_tau ) break;
				//if( it-sort_wgts.begin() > 0.01*(moend-mobeg) ) break; //5% of all values
			}
			cend = i;
#elif defined(REORDER_METIS_ND)
			idx_t nvtxs = wend-wbeg;
			std::vector<idx_t> xadj(nvtxs+1), adjncy, perm(nvtxs),iperm(nvtxs), sizes(nvtxs), vwgt(nvtxs);
			adjncy.reserve(nvtxs*5);
			xadj[0] = 0;

///////////////////////////////////////////////////////////////////////////////////
//       setup indices for transposed traversal of B block                       //
///////////////////////////////////////////////////////////////////////////////////
			
			for (k = wend; k > wbeg; --k)
			{
				vwgt[k-1] = 1;
				if (A_Address[k-1].Size() > 0)
				{
					i = A_Entries[A_Address[k-1].first].first;
					Alist[k-1] = Abeg[i];
					Abeg[i] = k-1;
				}
				Afirst[k-1] = A_Address[k-1].first;
			}
			
			for(i = wbeg; i < wend; ++i)
			{
				for (INMOST_DATA_ENUM_TYPE jt = A_Address[i].first; jt != A_Address[i].last; ++jt)
				{
					adjncy.push_back(static_cast<idx_t>(A_Entries[jt].first-wbeg));
				}
				
				Li = Abeg[i];
				while (Li != EOL)
				{
					adjncy.push_back(static_cast<idx_t>(Li-wbeg));
					Li = Alist[Li];
				}
				Li = Abeg[i];
				while (Li != EOL)
				{
					Ui = Alist[Li];
					Afirst[Li]++;
					if (A_Address[Li].last - Afirst[Li] > 0)
					{
						k = A_Entries[Afirst[Li]].first;
						Alist[Li] = Abeg[k];
						Abeg[k] = Li;
					}

					Li = Ui;
				}
				std::sort(adjncy.begin()+xadj[i],adjncy.end());
				adjncy.resize(std::unique(adjncy.begin()+xadj[i],adjncy.end())-adjncy.begin());
				
				xadj[i+1] = adjncy.size();
			}
			//A_Address[0].first = 0;
			//for(k = wbeg+1; k < wend; ++k)
			//	A_Address[k].first = A_Address[k-1].last;
			idx_t options[METIS_NOPTIONS];
			METIS_SetDefaultOptions(options);
			options[METIS_OPTION_NUMBERING] = 0;
			options[METIS_OPTION_IPTYPE] = METIS_IPTYPE_NODE;
			options[METIS_OPTION_DBGLVL] = METIS_DBG_INFO | METIS_DBG_TIME | METIS_DBG_COARSEN | METIS_DBG_CONNINFO | METIS_DBG_CONTIGINFO | METIS_DBG_IPART | METIS_DBG_MEMORY | METIS_DBG_MOVEINFO | METIS_DBG_REFINE | METIS_DBG_SEPINFO;
			printf("before metis\n");
			METIS_NodeNDP(nvtxs,&xadj[0],&adjncy[0],&vwgt[0],4,options,&perm[0],&iperm[0],&sizes[0]);
			//METIS_NodeND(&nvtxs,&xadj[0],&adjncy[0],&vwgt[0],options,&perm[0],&iperm[0]);
			printf("after metis\n");
			for(k = wbeg; k < wend; ++k)
			{
				localP[k] = perm[k-wbeg]+wbeg;
				localQ[k] = perm[k-wbeg]+wbeg;
			}
			cend = wend;
			i = wend;
#else
			cend = wend;
			i = wbeg;
#endif
			if (cbeg == cend && cbeg != wend)
			{
				//std::cout << __FILE__ << ":" << __LINE__ << " singular matrix, factored " << mobeg << ".." << cend << " out of " << mobeg << ".." << moend << std::endl;
				for (k = cbeg; k < wend; ++k)
					LU_Diag[k] = tol_modif;
				break;
			}
#if defined(REORDER_DDPQ)
#if defined(REPORT_ILU)
			//std::cout << level_size.size() << " new level " << cbeg << ".." << cend << " out of " << wbeg << ".." << wend << std::endl;
			std::cout << std::setw(8) << level_size.size() << " total " << std::setw(8) << wend - wbeg << " after tau filtering " << std::setw(8) << sort_wgts.size() << " selected " << std::setw(8) << cend - wbeg;
			std::cout << std::scientific << " max " << std::setw(8) << ddmaxall << " mean " << std::setw(8) << ddmaxmean << " min " << std::setw(8) << ddmaxmin << " ratio " << ddmaxall/ddmaxmean;
			std::cout << std::endl;
#endif
#endif
			//finish reordering
			if (i < wend)
			{
				j = i;
				for (k = wbeg; k < wend; ++k)
				{
					if (localP[k] == ENUMUNDEF) localP[k] = i++;
					if (localQ[k] == ENUMUNDEF) localQ[k] = j++;
				}
			}
			tlreorder = Timer() - tt;
			treorder += tlreorder;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////// REASSAMBLE             ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			tt = Timer();
			double tt1, tt2, tt3,ttt;
			//in localPQ numbers indicate where to put current row/column
			//reorder E,F blocks by swaps
			tt1 = Timer();
#if defined(REPORT_ILU)
			std::cout << level_size.size() << " applying permutations to EF blocks " << std::endl;
#endif
			//reorder blocks that we have
			ReorderEF(mobeg, cbeg, cend, wend, donePQ, localP, localQ);
			//allocate space for E,F blocks for current level
			E_Address.push_back(new interval<INMOST_DATA_ENUM_TYPE, Interval>(cend, wend));
			//inverse ordering
			inversePQ(wbeg,wend,localP,localQ, invP,invQ);
			tt1 = Timer() - tt1;

			//std::cout << "reorder: " << tt1 << std::endl;
#if defined(REPORT_ILU)
			std::cout << level_size.size() << " rebuilding matrix, B,F blocks " << std::endl;
#endif
			tt2 = Timer();
			//Create B block from current Schur complement for ILUC
			//      | B   F |
			//  A = |       |
			//      | E   C |
			//compute B,F
			Anorm = 0;
			nzA = 0;
			for (k = cbeg; k < cend; ++k)
			{
				F_Address[k].first = static_cast<INMOST_DATA_ENUM_TYPE>(F_Entries.size());
				B_Address[k].first = static_cast<INMOST_DATA_ENUM_TYPE>(B_Entries.size());
				for (INMOST_DATA_ENUM_TYPE jt = A_Address[invP[k]].first; jt != A_Address[invP[k]].last; ++jt)
				{
					i = localQ[A_Entries[jt].first];
					u = A_Entries[jt].second;
Kirill Terekhov's avatar
Kirill Terekhov committed
738 739
					if (i < cend) B_Entries.push_back(Sparse::Row::make_entry(i, u));
					else F_Entries.push_back(Sparse::Row::make_entry(i,u)); //count nonzero in column of F
Kirill Terekhov's avatar
Kirill Terekhov committed
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
					Anorm += u*u;
				}
				F_Address[k].last = static_cast<INMOST_DATA_ENUM_TYPE>(F_Entries.size());
				B_Address[k].last = static_cast<INMOST_DATA_ENUM_TYPE>(B_Entries.size());
				std::sort(B_Entries.begin() + B_Address[k].first, B_Entries.end());
				std::sort(F_Entries.begin() + F_Address[k].first, F_Entries.end());
				nzEF += F_Address[k].Size();
				nzA += A_Address[k].Size();
			}
			//Setup column addressing for B,F, in descending order to keep list ordered
			for (k = cend; k > cbeg; --k)
			{
				if (B_Address[k - 1].Size() > 0)
				{
					i = B_Entries[B_Address[k - 1].first].first;
					if (i < k-1)
					{
						Blist[k-1] = Bbeg[i];
						Bbeg[i] = k-1;
					}
				}
			}

			tt2 = Timer() - tt2;
#if defined(REPORT_ILU)
			std::cout << level_size.size() << " rebuilding matrix, E,C blocks " << std::endl;
#endif

			tt3 = Timer();
			//compute E,C
			C_Entries.clear();
			for (k = cend; k < wend; ++k)
			{
				E_Address.back()->at(k).first = static_cast<INMOST_DATA_ENUM_TYPE>(E_Entries.size());
				C_Address[k].first = static_cast<INMOST_DATA_ENUM_TYPE>(C_Entries.size());
				for (INMOST_DATA_ENUM_TYPE jt = A_Address[invP[k]].first; jt != A_Address[invP[k]].last; ++jt)
				{
					i = localQ[A_Entries[jt].first];
					u = A_Entries[jt].second;
Kirill Terekhov's avatar
Kirill Terekhov committed
779 780
					if (i < cend) E_Entries.push_back(Sparse::Row::make_entry(i, u)); //put to row of E
					else C_Entries.push_back(Sparse::Row::make_entry(i, u)); //form new Schur complement
Kirill Terekhov's avatar
Kirill Terekhov committed
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
					Anorm += u*u;
				}
				E_Address.back()->at(k).last = static_cast<INMOST_DATA_ENUM_TYPE>(E_Entries.size());
				C_Address[k].last = static_cast<INMOST_DATA_ENUM_TYPE>(C_Entries.size());
				// sort entries added to E, because it will be very usefull later
				std::sort(E_Entries.begin() + E_Address.back()->at(k).first, E_Entries.end());
				nzEF += E_Address.back()->at(k).Size();
				nzA += A_Address[k].Size();
			}
			Anorm = sqrt(Anorm/static_cast<INMOST_DATA_REAL_TYPE>(nzA));
			tt3 = Timer() - tt3;
			ttt = Timer();
			applyPQ(wbeg, wend, localP, localQ, invP, invQ);
			tt1 += Timer() - ttt;

#if defined(REPORT_ILU)
			std::cout << level_size.size() << " finished permutation, E,F " << tt1 << " B,F " << tt2 << " E,C " << tt3 << std::endl;
#endif
			tlreassamble = Timer() - tt;
			treassamble += tlreassamble;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////// RESCALING                 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			//Rescale current B block by Row-Column alternating scaling
			tt = Timer();
#if defined(RESCALE_B)
#if defined(REPORT_ILU)
			std::cout << level_size.size() << " rescaling block B " << std::endl;
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
810
			//for(Sparse::Vector::iterator it = DL.Begin() + cbeg - mobeg;
Kirill Terekhov's avatar
Kirill Terekhov committed
811 812 813 814 815
			//	it != DL.Begin() + cend - mobeg; ++it) *it = 0.0;
			std::fill(DL.Begin() + cbeg - mobeg, DL.Begin() + cend - mobeg, 0.0);
			for (k = cbeg; k < cend; k++)
			{
				for (INMOST_DATA_ENUM_TYPE r = B_Address[k].first; r < B_Address[k].last; ++r) 
Kirill Terekhov's avatar
Kirill Terekhov committed
816
					DL[k] += fabs(B_Entries[r].second);
Kirill Terekhov's avatar
Kirill Terekhov committed
817 818 819 820
			}
			for (k = cbeg; k < cend; k++) if (DL[k] < eps) DL[k] = 1.0 / subst; else DL[k] = 1.0 / DL[k];
			for (INMOST_DATA_ENUM_TYPE iter = 0; iter < sciters; iter++)
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
821
				//for(Sparse::Vector::iterator it = DR.Begin()+cbeg-mobeg; 
Kirill Terekhov's avatar
Kirill Terekhov committed
822 823 824 825 826
				//	it != DR.Begin()+cend-mobeg; ++it) *it = 0.0;
				std::fill(DR.Begin()+cbeg-mobeg, DR.Begin()+cend-mobeg, 0.0);
				for (k = cbeg; k < cend; k++)
				{
					for (INMOST_DATA_ENUM_TYPE r = B_Address[k].first; r < B_Address[k].last; ++r) 
Kirill Terekhov's avatar
Kirill Terekhov committed
827
						DR[B_Entries[r].first] += DL[k] * fabs(B_Entries[r].second);
Kirill Terekhov's avatar
Kirill Terekhov committed
828 829
				}
				for (k = cbeg; k < cend; k++) if (DR[k] < eps) DR[k] = 1.0 / subst; else DR[k] = 1.0 / DR[k];
Kirill Terekhov's avatar
Kirill Terekhov committed
830
				//for(Sparse::Vector::iterator it = DL.Begin()+cbeg-mobeg;
Kirill Terekhov's avatar
Kirill Terekhov committed
831 832 833 834 835
				//	it != DL.Begin()+cend-mobeg; ++it) *it = 0.0;
				std::fill(DL.Begin()+cbeg-mobeg, DL.Begin()+cend-mobeg, 0.0);
				for (k = cbeg; k < cend; k++)
				{
					for (INMOST_DATA_ENUM_TYPE r = B_Address[k].first; r < B_Address[k].last; ++r) 
Kirill Terekhov's avatar
Kirill Terekhov committed
836
						DL[k] += DR[B_Entries[r].first] * fabs(B_Entries[r].second);
Kirill Terekhov's avatar
Kirill Terekhov committed
837 838 839
				}
				for (k = cbeg; k < cend; k++) if (DL[k] < eps) DL[k] = 1.0 / subst; else DL[k] = 1.0 / DL[k];
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
840 841
			//for (k = cbeg; k < cend; k++) DL[k] = sqrt(DL[k]);
			//for (k = cbeg; k < cend; k++) DR[k] = sqrt(DR[k]);
Kirill Terekhov's avatar
Kirill Terekhov committed
842 843 844 845 846 847 848 849 850 851 852 853
			for (k = cbeg; k < cend; k++)
			{
				for (INMOST_DATA_ENUM_TYPE r = B_Address[k].first; r < B_Address[k].last; ++r)
					B_Entries[r].second *= DL[k] * DR[B_Entries[r].first];
			}
#endif
			//End rescale B block
			trescale += Timer() - tt;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////// FACTORIZATION BEGIN ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
854
#if defined(REPORT_ILU)
Kirill Terekhov's avatar
Kirill Terekhov committed
855
        int swaps = 0;
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
856
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
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 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
#if defined(ILUC2)
				nzLU2 = 0;
				LU2_Entries.clear();
#endif
				mean_diag = 0.0;
///////////////////////////////////////////////////////////////////////////////////
//       setup diagonal values and memorize indices                              //
///////////////////////////////////////////////////////////////////////////////////
				//initialize diagonal values
				for (k = cbeg; k < cend; k++)
				{
          LU_Diag[k] = 0.0;
					localP[k] = F_Address[k].first; //remember F block starts permutation
					localQ[k] = B_Address[k].first; //remember B block starts permutation for pivoting
					for (i = B_Address[k].first; i < B_Address[k].last; i++)
					{
						if (B_Entries[i].first == k)
						{
							LU_Diag[k] = B_Entries[i].second;
#if defined(DIAGONAL_PIVOT)
							mean_diag += fabs(LU_Diag[k]);
#endif
							break;
						}
					}
				}
				//initialize first row and column
///////////////////////////////////////////////////////////////////////////////////
//       get diagonal value                                                      //
///////////////////////////////////////////////////////////////////////////////////
				assert(B_Entries[B_Address[cbeg].first].first == cbeg);
				if (B_Entries[B_Address[cbeg].first].first == cbeg)
					LU_Diag[cbeg] = B_Entries[B_Address[cbeg].first].second;
				else
				{
					std::cout << __LINE__ << " No diagonal value! " << cbeg << " " << B_Entries[B_Address[cbeg].first].first << std::endl;
					LU_Diag[cbeg] = 0.0;
				}
///////////////////////////////////////////////////////////////////////////////////
//       update for diagonal pivoting                                            //
///////////////////////////////////////////////////////////////////////////////////
#if defined(DIAGONAL_PIVOT)
				mean_diag -= fabs(LU_Diag[cbeg]);
#endif
///////////////////////////////////////////////////////////////////////////////////
//       modify diagonal value according to rules                                //
///////////////////////////////////////////////////////////////////////////////////
#if defined(PIVOT_THRESHOLD)
				if (fabs(LU_Diag[cbeg]) < tol_modif)
				{
					LU_Diag[cbeg] = LU_Diag[cbeg] < 0.0 ? -tol_modif : tol_modif;
				}
				else
#endif
#if defined(DIAGONAL_PERTURBATION)
				{
					LU_Diag[cbeg] = LU_Diag[cbeg] * (1.0 + DIAGONAL_PERTURBATION_REL) + (LU_Diag[cbeg] < 0.0 ? -1.0 : 1.0)*DIAGONAL_PERTURBATION_ABS;
				}
#endif
///////////////////////////////////////////////////////////////////////////////////
//      form first line of U and second-order U                                  //
///////////////////////////////////////////////////////////////////////////////////
				LU_Beg = static_cast<INMOST_DATA_ENUM_TYPE>(LU_Entries.size());
				U_Address[cbeg].first = static_cast<INMOST_DATA_ENUM_TYPE>(LU_Entries.size());
#if defined(ILUC2)
        U2_Address[cbeg].first = 0;
#endif
				for (INMOST_DATA_ENUM_TYPE r = B_Address[cbeg].first + (B_Entries[B_Address[cbeg].first].first == cbeg ? 1 : 0); r != B_Address[cbeg].last; ++r) 
        {
          u = B_Entries[r].second / LU_Diag[cbeg];
          if( fabs(u) > tau )
Kirill Terekhov's avatar
Kirill Terekhov committed
928
					  LU_Entries.push_back(Sparse::Row::make_entry(B_Entries[r].first, u));
Kirill Terekhov's avatar
Kirill Terekhov committed
929 930
#if defined(ILUC2)
          else if( fabs(u) > tau2 )
Kirill Terekhov's avatar
Kirill Terekhov committed
931
            LU2_Entries.push_back(Sparse::Row::make_entry(B_Entries[r].first, u));
Kirill Terekhov's avatar
Kirill Terekhov committed
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
#endif
        }
        U_Address[cbeg].last = static_cast<INMOST_DATA_ENUM_TYPE>(LU_Entries.size());				
#if defined(ILUC2)
        U2_Address[cbeg].last = static_cast<INMOST_DATA_ENUM_TYPE>(LU2_Entries.size());				
#endif
///////////////////////////////////////////////////////////////////////////////////
//       form first line of L and second-order L                                 //
///////////////////////////////////////////////////////////////////////////////////
        L_Address[cbeg].first = static_cast<INMOST_DATA_ENUM_TYPE>(LU_Entries.size());
#if defined(ILUC2)
        L2_Address[cbeg].first = static_cast<INMOST_DATA_ENUM_TYPE>(LU2_Entries.size());				
#endif
				Li = Bbeg[cbeg];
				while (Li != EOL)
				{
          l = B_Entries[B_Address[Li].first].second / LU_Diag[cbeg];
          if( fabs(l) > tau )
Kirill Terekhov's avatar
Kirill Terekhov committed
950
					  LU_Entries.push_back(Sparse::Row::make_entry(Li, l));
Kirill Terekhov's avatar
Kirill Terekhov committed
951 952
#if defined(ILUC2)
          else if( fabs(l) > tau2 )
Kirill Terekhov's avatar
Kirill Terekhov committed
953
            LU2_Entries.push_back(Sparse::Row::make_entry(Li, l));
Kirill Terekhov's avatar
Kirill Terekhov committed
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
#endif
					Li = Blist[Li];
				}
				L_Address[cbeg].last = static_cast<INMOST_DATA_ENUM_TYPE>(LU_Entries.size());
#if defined(ILUC2)
        L2_Address[cbeg].last = static_cast<INMOST_DATA_ENUM_TYPE>(LU2_Entries.size());				
#endif
				//update diagonal by factors
///////////////////////////////////////////////////////////////////////////////////
//       update whole diagonal with calculated L and U lines                     //
///////////////////////////////////////////////////////////////////////////////////
				i = U_Address[cbeg].first;
				j = L_Address[cbeg].first;
				while (i != U_Address[cbeg].last && j != L_Address[cbeg].last)
				{
					Ui = LU_Entries[i].first;
					Li = LU_Entries[j].first;
					if (Ui > Li) j++;
					else if (Ui < Li) i++;
					else
					{
						LU_Diag[Ui] -= LU_Entries[i].second * LU_Entries[j].second * LU_Diag[cbeg];
						i++;
						j++;
					}
				}
				//update column indexes
///////////////////////////////////////////////////////////////////////////////////
//       setup indices for transposed traversal of B block                       //
///////////////////////////////////////////////////////////////////////////////////
				Li = Bbeg[cbeg];
				while (Li != EOL)
				{
					B_Address[Li].first++;
					Ui = Blist[Li];
					if (B_Address[Li].Size() > 0)
					{
						i = B_Entries[B_Address[Li].first].first;
						if (i < Li)
						{
							if (Bbeg[i] > Li)
							{
								Blist[Li] = Bbeg[i];
								Bbeg[i] = Li;
							}
							else
							{
								curr = next = Bbeg[i];
								while (next < Li)
								{
									curr = next;
									next = Blist[curr];
								}
								assert(curr < Li);
								assert(Li < next);
								Blist[curr] = Li;
								Blist[Li] = next;
							}
						}
					}
					Li = Ui;
				}
///////////////////////////////////////////////////////////////////////////////////
//       setup indices for transposed traversal of U                             //
///////////////////////////////////////////////////////////////////////////////////
				//form partial list from this row
				if (U_Address[cbeg].Size() > 0)
				{
					i = LU_Entries[U_Address[cbeg].first].first;
					Ubeg[i] = cbeg;
					Ulist[cbeg] = EOL;
				}
///////////////////////////////////////////////////////////////////////////////////
//       setup indices for transposed traversal of L                             //
///////////////////////////////////////////////////////////////////////////////////
				if (L_Address[cbeg].Size() > 0)
				{
					i = LU_Entries[L_Address[cbeg].first].first;
					Lbeg[i] = cbeg;
					Llist[cbeg] = EOL;
				}
				//Initialize data for condition estimator
				tt = Timer();
///////////////////////////////////////////////////////////////////////////////////
//              initialize condition estimator                                   //
///////////////////////////////////////////////////////////////////////////////////
Kirill Terekhov's avatar
Kirill Terekhov committed
1040
#if defined(ESTIMATOR)
Kirill Terekhov's avatar
Kirill Terekhov committed
1041 1042 1043 1044 1045
        if( estimator )
        {
				  NuU = NuL = 1.0;
				  if( estimator )
				  {
Kirill Terekhov's avatar
Kirill Terekhov committed
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
					  EstU1[cbeg] = EstL1[cbeg] = 0.0;
						EstU2[cbeg] = EstL2[cbeg] = 0.0;
					  for (INMOST_DATA_ENUM_TYPE it = U_Address[cbeg].first; it != U_Address[cbeg].last; ++it) 
						{
							EstU1[LU_Entries[it].first] = LU_Entries[it].second;
							EstU2[LU_Entries[it].first] = LU_Entries[it].second;
						}
					  for (INMOST_DATA_ENUM_TYPE it = L_Address[cbeg].first; it != L_Address[cbeg].last; ++it) 
						{
							EstL1[LU_Entries[it].first] = LU_Entries[it].second;
							EstL2[LU_Entries[it].first] = LU_Entries[it].second;
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
1058 1059
				  }
        }
Kirill Terekhov's avatar
Kirill Terekhov committed
1060
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
				nzLU += L_Address[cbeg].Size() + U_Address[cbeg].Size() + 1;
				max_diag = min_diag = fabs(LU_Diag[cbeg]);
				NuD = 1.0;
#if defined(REPORT_ILU)
				std::cout << level_size.size() << " starting factorization " << std::endl;
#endif
				for (k = cbeg + 1; k < cend; ++k)
				{
///////////////////////////////////////////////////////////////////////////////////
//              starting factorization step                                      //
///////////////////////////////////////////////////////////////////////////////////
					//observe that there is a better diagonal pivot
#if defined(DIAGONAL_PIVOT)
					/*
					double mean_diag2 = 0.0;
					for (i = k; i < cend; i++)
					{
						mean_diag2 += fabs(LU_Diag[i]);
					}
					std::cout << mean_diag << " " << mean_diag2 << std::endl;
					*/
///////////////////////////////////////////////////////////////////////////////////
//              diagonal pivoting algorithm                                      //
///////////////////////////////////////////////////////////////////////////////////
#if defined(DIAGONAL_PIVOT_COND)
          bool no_swap_algorithm = false;
#endif
					if ( k < cend-1 &&  fabs(LU_Diag[k])*(cend-k) < DIAGONAL_PIVOT_TAU*mean_diag)
					{
#if defined(DIAGONAL_PIVOT_COND)
            if( false )
            {
swap_algorithm:
              no_swap_algorithm = true;
            }
#endif
						j = k+1;
						for (i = k + 1; i < cend; i++) if (fabs(LU_Diag[j]) < fabs(LU_Diag[i])) j = i;

            if( j > cend-1 )
            {
              std::cout << "Oops! asking to exchange beyond matrix! k = " << k << " j = " << j << " cend = " << cend << std::endl;
            }

            if( fabs(LU_Diag[j]) < fabs(LU_Diag[k]) )
            {
              std::cout << "Oops! I'm the best pivot!" << std::endl;
              
            }
            else
						{
							//TODO!!!
              ++swaps;
#if defined(REPORT_ILU)
							//std::cout << "Detected, that there is a much better pivot, i'm " << k << " " << LU_Diag[k] << " other " << j << " " << LU_Diag[j] << std::endl;
							//std::cout << "Condition numbers: L " << NuL << " D " << NuD << " U " << NuU << std::endl;
#endif
							//scanf("%*c");
							//std::cout << "But repivoting algorithm not implemented" << std::endl;
							//This algorithm may be quite costly, but the effect is miraculous
							//First correct E
							SwapEntries(*E_Address.back(), E_Entries, cend, wend, k, j);
							//Then correct F
							SwapLine(F_Address,k, j);
							//Swap lines of B
							SwapLine(B_Address, k, j);
							//Correct B the same way E is corrected
							SwapEntries(B_Address, B_Entries, cbeg, cend, k, j);
							//now reintialize entrie column linked list
							for (i = k; i < cend; ++i) Bbeg[i] = EOL;
							for (i = cend; i > k; --i)
							{
								Li = B_Entries[B_Address[i - 1].first].first;
								if (Li < i - 1)
								{
									Blist[i - 1] = Bbeg[Li];
									Bbeg[Li] = i - 1;
								}
							}
							//now update L
							SwapEntries(L_Address, LU_Entries, cbeg, cend, k, j);
							//reinitialize linked list
							//now update U
							SwapEntries(U_Address, LU_Entries, cbeg, cend, k, j);
							//reinitialize linked list
#if defined(ILUC2)
							//now update L2
							SwapEntries(U2_Address, LU2_Entries, cbeg, cend, k, j);
							//reinitialize linked list
							//now update U2
							SwapEntries(L2_Address, LU2_Entries, cbeg, cend, k, j);
							//reinitialize linked list
#endif
							for (i = k; i < cend; i++)
							{
								Ubeg[i] = EOL;
								Lbeg[i] = EOL;
#if defined(ILUC2)
								U2beg[i] = EOL;
								L2beg[i] = EOL;
#endif
							}
							for (i = cbeg; i < k; i++)
							{
								if (U_Address[i].Size() > 0)
								{
									Li = LU_Entries[U_Address[i].first].first;
									Ulist[i] = Ubeg[Li];
									Ubeg[Li] = i;
								}
								if (L_Address[i].Size() > 0)
								{
									Li = LU_Entries[L_Address[i].first].first;
									Llist[i] = Lbeg[Li];
									Lbeg[Li] = i;
								}
#if defined(ILUC2)
								if (U2_Address[i].Size() > 0)
								{
									Li = LU2_Entries[U2_Address[i].first].first;
									U2list[i] = U2beg[Li];
									U2beg[Li] = i;
								}
								if (L2_Address[i].Size() > 0)
								{
									Li = LU2_Entries[L2_Address[i].first].first;
									L2list[i] = L2beg[Li];
									L2beg[Li] = i;
								}
#endif
							}
							//swap diagonal values
							u = LU_Diag[k];
							LU_Diag[k] = LU_Diag[j];
							LU_Diag[j] = u;
							//swap values in estimator
Kirill Terekhov's avatar
Kirill Terekhov committed
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
#if defined(ESTIMATOR)
							if( estimator )
							{
								u = EstL1[k];
								EstL1[k] = EstL1[j];
								EstL1[j] = u;
								u = EstL2[k];
								EstL2[k] = EstL2[j];
								EstL2[j] = u;
								u = EstU1[k];
								EstU1[k] = EstU1[j];
								EstU1[j] = u;
								u = EstU2[k];
								EstU2[k] = EstU2[j];
								EstU2[j] = u;
							}
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
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 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
							//swap rescaling vectors
							u = DL[k];
							DL[k] = DL[j];
							DL[j] = u;

							u = DR[k];
							DR[k] = DR[j];
							DR[j] = u;
							//update indeces in ddPQ
							Ui = ddP[k];
							ddP[k] = ddP[j];
							ddP[j] = Ui;
							Ui = ddQ[k];
							ddQ[k] = ddQ[j];
							ddQ[j] = Ui;

							//update interval starts for B block to correctly restore intervals
							Ui = localQ[k];
							localQ[k] = localQ[j];
							localQ[j] = Ui;

							Ui = localP[k];
							localP[k] = localP[j];
							localP[j] = Ui;
						}
						/*
						else
						{
							std::cout << "Detected that there are no good pivots anymore! interval " << cbeg << "..." << cend << " current element " << k << std::endl;
							std::cout << "But level rejection algorithm is not implemented" << std::endl;
							//probably should reject all unfactored parts for the next level
						}
						*/
					}
					
#endif
///////////////////////////////////////////////////////////////////////////////////
//            Prepare diagonal value                                             //
///////////////////////////////////////////////////////////////////////////////////
					udiag = LU_Diag[k]; // LU_Diag is calculated with less dropping
///////////////////////////////////////////////////////////////////////////////////
// Prevent small pivot, hopefully it would work with correct right hand side     //
///////////////////////////////////////////////////////////////////////////////////
#if defined(PIVOT_THRESHOLD)
					if (fabs(udiag) < tol_modif)
					{
						udiag = udiag < 0.0 ? -tol_modif : tol_modif;
					}
#endif
///////////////////////////////////////////////////////////////////////////////////
// Perturb diagonal value, this will shift eigenvalues                           //
///////////////////////////////////////////////////////////////////////////////////
#if defined(DIAGONAL_PERTURBATION)
					udiag = udiag * (1.0 + DIAGONAL_PERTURBATION_REL) + (udiag < 0.0 ? -1.0 : 1.0)*DIAGONAL_PERTURBATION_ABS;
#endif
					abs_udiag = fabs(LU_Diag[k]);
///////////////////////////////////////////////////////////////////////////////////
//            Prepare linked list for U-part                                     //
///////////////////////////////////////////////////////////////////////////////////
					//DropLk = DropUk = 0.0;
					//uncompress k-th row
					// add diagonal value first, there shouldn't be values on left from diagonal
					assert(B_Entries[B_Address[k].first].first == k);
Kirill Terekhov's avatar
Kirill Terekhov committed
1277
					LineIndecesU[cbeg] = k;
Kirill Terekhov's avatar
Kirill Terekhov committed
1278
					if (B_Entries[B_Address[k].first].first == k)
Kirill Terekhov's avatar
Kirill Terekhov committed
1279
						LineValuesU[k] = B_Entries[B_Address[k].first].second;
Kirill Terekhov's avatar
Kirill Terekhov committed
1280 1281 1282
					else
					{
						std::cout << __LINE__ << " No diagonal value! " << k << " " << B_Entries[B_Address[k].first].first << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
1283
						LineValuesU[k] = 0.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
1284 1285 1286 1287 1288
					}

					Ui = k;
					for (INMOST_DATA_ENUM_TYPE it = B_Address[k].first + (B_Entries[B_Address[k].first].first == k ? 1 : 0); it != B_Address[k].last; ++it)
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1289 1290
						LineValuesU[B_Entries[it].first] = B_Entries[it].second;
						Ui = LineIndecesU[Ui] = B_Entries[it].first;
Kirill Terekhov's avatar
Kirill Terekhov committed
1291
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
1292
					LineIndecesU[Ui] = EOL;
Kirill Terekhov's avatar
Kirill Terekhov committed
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
///////////////////////////////////////////////////////////////////////////////////
//                    U-part elimination with L                                  //
///////////////////////////////////////////////////////////////////////////////////
					// perform the elimination from row
					i = Lbeg[k];
					while (i != EOL)
					{
						assert(i != UNDEF);
						assert(LU_Entries[L_Address[i].first].first == k);
						l = LU_Entries[L_Address[i].first].second*LU_Diag[i];
Kirill Terekhov's avatar
Kirill Terekhov committed
1303
            //if( fabs(l)*NuU*NuL_old > tau2*abs_udiag )//global check
Kirill Terekhov's avatar
Kirill Terekhov committed
1304 1305 1306 1307 1308 1309 1310
            {
						  curr = cbeg;
						  for (INMOST_DATA_ENUM_TYPE it = U_Address[i].first; it < U_Address[i].last; ++it)
						  {
							  j = LU_Entries[it].first;
							  u = LU_Entries[it].second;
							  //eliminate values
Kirill Terekhov's avatar
Kirill Terekhov committed
1311 1312 1313
							  if (LineIndecesU[j] != UNDEF) //there is an entry in the list
								  LineValuesU[j] -= l*u;
							  else //if (fabs(u)*NuU*NuL_old > tau2*abs_udiag )//add new entry
Kirill Terekhov's avatar
Kirill Terekhov committed
1314 1315 1316 1317 1318
							  {
								  next = curr;
								  while (next < j)
								  {
									  curr = next;
Kirill Terekhov's avatar
Kirill Terekhov committed
1319
									  next = LineIndecesU[curr];
Kirill Terekhov's avatar
Kirill Terekhov committed
1320 1321 1322
								  }
								  assert(curr < j);
								  assert(j < next);
Kirill Terekhov's avatar
Kirill Terekhov committed
1323 1324 1325
								  LineIndecesU[curr] = j;
								  LineIndecesU[j] = next;
								  LineValuesU[j] = -l*u;
Kirill Terekhov's avatar
Kirill Terekhov committed
1326 1327 1328 1329 1330 1331 1332
							  }
						  }
            }
#if defined(ILUC2)
///////////////////////////////////////////////////////////////////////////////////
//                 second order U-part elimination with L                        //
///////////////////////////////////////////////////////////////////////////////////
Kirill Terekhov's avatar
Kirill Terekhov committed
1333
						//if( fabs(l)*NuU*NuL_old > tau*abs_udiag )//global check
Kirill Terekhov's avatar
Kirill Terekhov committed
1334 1335 1336 1337 1338 1339 1340
						{
							curr = cbeg;
							for (INMOST_DATA_ENUM_TYPE it = U2_Address[i].first; it < U2_Address[i].last; ++it)
							{
								j = LU2_Entries[it].first;
								u = l*LU2_Entries[it].second;
								//eliminate values
Kirill Terekhov's avatar
Kirill Terekhov committed
1341 1342 1343
								if (LineIndecesU[j] != UNDEF) //there is an entry in the list
									LineValuesU[j] -= u;
								else //if (fabs(u)*NuU*NuL_old > tau2*abs_udiag)//add new entry
Kirill Terekhov's avatar
Kirill Terekhov committed
1344 1345 1346 1347 1348
								{
									next = curr;
									while (next < j)
									{
										curr = next;
Kirill Terekhov's avatar
Kirill Terekhov committed
1349
										next = LineIndecesU[curr];
Kirill Terekhov's avatar
Kirill Terekhov committed
1350 1351 1352
									}
									assert(curr < j);
									assert(j < next);
Kirill Terekhov's avatar
Kirill Terekhov committed
1353 1354 1355
									LineIndecesU[curr] = j;
									LineIndecesU[j] = next;
									LineValuesU[j] = -u;
Kirill Terekhov's avatar
Kirill Terekhov committed
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
								}
							}
						}
#endif
						i = Llist[i];
					}
#if defined(ILUC2)
///////////////////////////////////////////////////////////////////////////////////
//                 U-part elimination with second-order L                        //
///////////////////////////////////////////////////////////////////////////////////
					i = L2beg[k];
					while (i != EOL)
					{
						assert(i != UNDEF);
						assert(LU2_Entries[L2_Address[i].first].first == k);
						l = LU2_Entries[L2_Address[i].first].second*LU_Diag[i];
Kirill Terekhov's avatar
Kirill Terekhov committed
1372
            //if( fabs(l)*NuU*NuL_old > tau2*abs_udiag )//global check
Kirill Terekhov's avatar
Kirill Terekhov committed
1373 1374 1375 1376 1377 1378 1379
            {
						  curr = cbeg;
						  for (INMOST_DATA_ENUM_TYPE it = U_Address[i].first; it < U_Address[i].last; ++it)
						  {
							  j = LU_Entries[it].first;
							  u = l*LU_Entries[it].second;
							  //eliminate values
Kirill Terekhov's avatar
Kirill Terekhov committed
1380 1381 1382
							  if (LineIndecesU[j] != UNDEF) //there is an entry in the list
								  LineValuesU[j] -= u;
							  else //if (fabs(u)*NuU*NuL_old > tau2*abs_udiag)//add new entry
Kirill Terekhov's avatar
Kirill Terekhov committed
1383 1384 1385 1386 1387
							  {
								  next = curr;
								  while (next < j)
								  {
									  curr = next;
Kirill Terekhov's avatar
Kirill Terekhov committed
1388
									  next = LineIndecesU[curr];
Kirill Terekhov's avatar
Kirill Terekhov committed
1389 1390 1391
								  }
								  assert(curr < j);
								  assert(j < next);
Kirill Terekhov's avatar
Kirill Terekhov committed
1392 1393 1394
								  LineIndecesU[curr] = j;
								  LineIndecesU[j] = next;
								  LineValuesU[j] = -u;
Kirill Terekhov's avatar
Kirill Terekhov committed
1395 1396 1397 1398 1399 1400
							  }
						  }
            }
///////////////////////////////////////////////////////////////////////////////////
//                 second order U-part elimination with second-order L           //
///////////////////////////////////////////////////////////////////////////////////
Kirill Terekhov's avatar
Kirill Terekhov committed
1401
            //if( fabs(l)*NuU*NuL_old > tau*abs_udiag ) //global check
Kirill Terekhov's avatar
Kirill Terekhov committed
1402 1403 1404 1405 1406 1407 1408
            {
              curr = cbeg;
						  for (INMOST_DATA_ENUM_TYPE it = U2_Address[i].first; it < U2_Address[i].last; ++it)
						  {
                j = LU2_Entries[it].first;
							  u = l*LU2_Entries[it].second;
							  //eliminate values
Kirill Terekhov's avatar
Kirill Terekhov committed
1409 1410 1411
							  if (LineIndecesU[j] != UNDEF) //there is an entry in the list
								  LineValuesU[j] -= u;
                else //if (fabs(u)*NuU*NuL_old > tau2*abs_udiag)//add new entry
Kirill Terekhov's avatar
Kirill Terekhov committed
1412 1413 1414 1415 1416
							  {
								  next = curr;
								  while (next < j)
								  {
									  curr = next;
Kirill Terekhov's avatar
Kirill Terekhov committed
1417
									  next = LineIndecesU[curr];
Kirill Terekhov's avatar
Kirill Terekhov committed
1418 1419 1420
								  }
								  assert(curr < j);
								  assert(j < next);
Kirill Terekhov's avatar
Kirill Terekhov committed
1421 1422 1423
								  LineIndecesU[curr] = j;
								  LineIndecesU[j] = next;
								  LineValuesU[j] = -u;
Kirill Terekhov's avatar
Kirill Terekhov committed
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469
							  }
						  }
            }

						i = L2list[i];
					}
#endif
					//std::cout << std::endl;
					//define the diagonal value
//this check will fail due to global check of tolerances, if global check will be removed then this will never fail
//#if defined(DIAGONAL_PIVOT) && !defined(DIAGONAL_PERTURBATION)
//					if (fabs(LU_Diag[k] - LineValues[k]) > 1e-6) std::cout << __LINE__ << " Diagonal value went wrong, good: " << LineValues[k] << " have " << LU_Diag[k] << " line " << k << std::endl;
//#endif
///////////////////////////////////////////////////////////////////////////////////
//                  Retrieve diagonal value                                      //
///////////////////////////////////////////////////////////////////////////////////
/*          
//#if defined(DIAGONAL_PIVOT) 
          udiag = LU_Diag[k]; // LU_Diag is calculated with less dropping
//#else
//					udiag = LineValues[k];
//#endif
          
          //udiag = LineValues[k];

///////////////////////////////////////////////////////////////////////////////////
// Prevent small pivot, hopefully it would work with correct right hand side     //
///////////////////////////////////////////////////////////////////////////////////
#if defined(PIVOT_THRESHOLD)
					if (fabs(udiag) < tol_modif)
					{
						//std::cout << "udiag too small " << 1.0 /udiag << std::endl;
						udiag = udiag < 0.0 ? -tol_modif : tol_modif;
					}
#endif
					*/
///////////////////////////////////////////////////////////////////////////////////
//                    Condition estimator for diagonal D                         //
///////////////////////////////////////////////////////////////////////////////////
          if (fabs(udiag) > max_diag) max_diag = fabs(udiag);
					if (fabs(udiag) < min_diag) min_diag = fabs(udiag);
					NuD = max_diag / min_diag;
///////////////////////////////////////////////////////////////////////////////////
//                    Rescale with diagonal                                     //
///////////////////////////////////////////////////////////////////////////////////
          //rescale elements next after diagonal position
Kirill Terekhov's avatar
Kirill Terekhov committed
1470
					Ui = LineIndecesU[k];
Kirill Terekhov's avatar
Kirill Terekhov committed
1471 1472
					while (Ui != EOL)
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1473 1474
						LineValuesU[Ui] /= udiag;
						Ui = LineIndecesU[Ui];
Kirill Terekhov's avatar
Kirill Terekhov committed
1475 1476 1477 1478 1479 1480
					}
///////////////////////////////////////////////////////////////////////////////////
//                 prepearing linked list for L-part                             //
///////////////////////////////////////////////////////////////////////////////////
					//uncompress column
					//insert diagonal value first
Kirill Terekhov's avatar
Kirill Terekhov committed
1481
					LineIndecesL[cbeg] = k;
Kirill Terekhov's avatar
Kirill Terekhov committed
1482
					if (B_Entries[B_Address[k].first].first == k)
Kirill Terekhov's avatar
Kirill Terekhov committed
1483
						LineValuesL[k] = B_Entries[B_Address[k].first].second;
Kirill Terekhov's avatar
Kirill Terekhov committed
1484 1485 1486
					else
					{
						std::cout << __LINE__ << " No diagonal value! " << k << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
1487
						LineValuesL[k] = 0.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
1488 1489 1490 1491 1492 1493 1494 1495 1496
					}
					//start from diagonal
					//sort_indeces.clear();
					Ui = k;
					Li = Bbeg[k];
					while (Li != EOL)
					{
						assert(B_Entries[B_Address[Li].first].first == k);
						//sort_indeces.push_back(Li);
Kirill Terekhov's avatar
Kirill Terekhov committed
1497 1498
						LineValuesL[Li] = B_Entries[B_Address[Li].first].second;
						Ui = LineIndecesL[Ui] = Li;
Kirill Terekhov's avatar
Kirill Terekhov committed
1499 1500 1501 1502 1503 1504 1505
						Li = Blist[Li];
					}
					/*
					std::sort(sort_indeces.begin(), sort_indeces.end());
					for (Li = 0; Li < sort_indeces.size(); Li++)
					Ui = LineIndeces[Ui] = sort_indeces[Li];
					*/
Kirill Terekhov's avatar
Kirill Terekhov committed
1506
					LineIndecesL[Ui] = EOL;
Kirill Terekhov's avatar
Kirill Terekhov committed
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
///////////////////////////////////////////////////////////////////////////////////
//                 L-part elimination with U                                     //
///////////////////////////////////////////////////////////////////////////////////
					// perform the elimination from column
					i = Ubeg[k];
					while (i != EOL)
					{
						assert(i != UNDEF);
						assert(LU_Entries[U_Address[i].first].first == k);
						u = LU_Entries[U_Address[i].first].second*LU_Diag[i];
Kirill Terekhov's avatar
Kirill Terekhov committed
1517
            //if( fabs(u)*NuL*NuU_old > tau2*abs_udiag )//global check
Kirill Terekhov's avatar
Kirill Terekhov committed
1518 1519 1520 1521 1522 1523 1524
            {
						  curr = cbeg;
						  for (INMOST_DATA_ENUM_TYPE it = L_Address[i].first; it < L_Address[i].last; ++it)
						  {
							  j = LU_Entries[it].first;
							  l = LU_Entries[it].second;
							  //eliminate values
Kirill Terekhov's avatar
Kirill Terekhov committed
1525 1526 1527
							  if (LineIndecesL[j] != UNDEF) //there is an entry in the list
								  LineValuesL[j] -= l*u;
							  else //if (fabs(l)*NuL*NuU_old > tau2*abs_udiag)//add new entry
Kirill Terekhov's avatar
Kirill Terekhov committed
1528 1529 1530 1531 1532
							  {
								  next = curr;
								  while (next < j)
								  {
									  curr = next;
Kirill Terekhov's avatar
Kirill Terekhov committed
1533
									  next = LineIndecesL[curr];
Kirill Terekhov's avatar
Kirill Terekhov committed
1534 1535 1536
								  }
								  assert(curr < j);
								  assert(j < next);
Kirill Terekhov's avatar
Kirill Terekhov committed
1537 1538 1539
								  LineIndecesL[curr] = j;
								  LineIndecesL[j] = next;
								  LineValuesL[j] = -l*u;
Kirill Terekhov's avatar
Kirill Terekhov committed
1540 1541 1542 1543 1544 1545 1546
							  }
						  }
            }
#if defined(ILUC2)
///////////////////////////////////////////////////////////////////////////////////
//                 second-order L-part elimination with U                        //
///////////////////////////////////////////////////////////////////////////////////
Kirill Terekhov's avatar
Kirill Terekhov committed
1547
						//if( fabs(u)*NuL*NuU_old > tau*abs_udiag )//global check
Kirill Terekhov's avatar
Kirill Terekhov committed
1548 1549 1550 1551 1552 1553
						{
							curr = cbeg;
							for (INMOST_DATA_ENUM_TYPE it = L2_Address[i].first; it < L2_Address[i].last; ++it)
							{
								j = LU2_Entries[it].first;
								l = u*LU2_Entries[it].second;
Kirill Terekhov's avatar
Kirill Terekhov committed
1554 1555 1556
								if (LineIndecesL[j] != UNDEF) //there is an entry in the list
									LineValuesL[j] -= l;
								else //if (fabs(l)*NuL*NuU_old > tau2*abs_udiag)//add new entry
Kirill Terekhov's avatar
Kirill Terekhov committed
1557 1558 1559 1560 1561
								{
									next = curr;
									while (next < j)
									{
										curr = next;
Kirill Terekhov's avatar
Kirill Terekhov committed
1562
										next = LineIndecesL[curr];
Kirill Terekhov's avatar
Kirill Terekhov committed
1563 1564 1565
									}
									assert(curr < j);
									assert(j < next);
Kirill Terekhov's avatar
Kirill Terekhov committed
1566 1567 1568
									LineIndecesL[curr] = j;
									LineIndecesL[j] = next;
									LineValuesL[j] = -l;
Kirill Terekhov's avatar
Kirill Terekhov committed
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
								}
							}
						}
#endif
						i = Ulist[i];
					}
#if defined(ILUC2)
///////////////////////////////////////////////////////////////////////////////////
//                 L-part elimination with second-order U                        //
///////////////////////////////////////////////////////////////////////////////////
					i = U2beg[k];
					while (i != EOL)
					{
						assert(i != UNDEF);
						assert(LU2_Entries[U2_Address[i].first].first == k);
						u = LU2_Entries[U2_Address[i].first].second*LU_Diag[i];
Kirill Terekhov's avatar
Kirill Terekhov committed
1585
            //if( fabs(u)*NuL*NuU_old > tau2*abs_udiag )//global check
Kirill Terekhov's avatar
Kirill Terekhov committed
1586 1587 1588 1589 1590 1591 1592
            {
						  curr = cbeg;
						  for (INMOST_DATA_ENUM_TYPE it = L_Address[i].first; it != L_Address[i].last; ++it)
						  {
							  j = LU_Entries[it].first;
							  l = u*LU_Entries[it].second;
							  //eliminate values
Kirill Terekhov's avatar
Kirill Terekhov committed
1593 1594 1595
							  if (LineIndecesL[j] != UNDEF) //there is an entry in the list
								  LineValuesL[j] -= l;
							  else //if (fabs(l)*NuL*NuU_old > tau2*abs_udiag)//add new entry
Kirill Terekhov's avatar
Kirill Terekhov committed
1596 1597 1598 1599 1600
							  {
								  next = curr;
								  while (next < j)
								  {
									  curr = next;
Kirill Terekhov's avatar
Kirill Terekhov committed
1601
									  next = LineIndecesL[curr];
Kirill Terekhov's avatar
Kirill Terekhov committed
1602 1603 1604
								  }
								  assert(curr < j);
								  assert(j < next);
Kirill Terekhov's avatar
Kirill Terekhov committed
1605 1606 1607
								  LineIndecesL[curr] = j;
								  LineIndecesL[j] = next;
								  LineValuesL[j] = -l;
Kirill Terekhov's avatar
Kirill Terekhov committed
1608 1609 1610 1611 1612 1613
                }
						  }
            }
///////////////////////////////////////////////////////////////////////////////////
//           second-order L-part elimination with second-order U                 //
///////////////////////////////////////////////////////////////////////////////////
Kirill Terekhov's avatar
Kirill Terekhov committed
1614
            //if( fabs(u)*NuL*NuU_old > tau*abs_udiag )//global check
Kirill Terekhov's avatar
Kirill Terekhov committed
1615 1616 1617 1618 1619 1620 1621
            {
              curr = cbeg;
						  for (INMOST_DATA_ENUM_TYPE it = L2_Address[i].first; it != L2_Address[i].last; ++it)
						  {
							  j = LU2_Entries[it].first;
							  l = u*LU2_Entries[it].second;
							  //eliminate values
Kirill Terekhov's avatar
Kirill Terekhov committed
1622 1623 1624
							  if (LineIndecesL[j] != UNDEF) //there is an entry in the list
								  LineValuesL[j] -= l;
                else //if (fabs(l)*NuL*NuU_old > tau2*abs_udiag)//add new entry
Kirill Terekhov's avatar
Kirill Terekhov committed
1625 1626 1627 1628 1629
							  {
								  next = curr;
								  while (next < j)
								  {
									  curr = next;
Kirill Terekhov's avatar
Kirill Terekhov committed
1630
									  next = LineIndecesL[curr];
Kirill Terekhov's avatar
Kirill Terekhov committed
1631 1632 1633
								  }
								  assert(curr < j);
								  assert(j < next);
Kirill Terekhov's avatar
Kirill Terekhov committed
1634 1635 1636
								  LineIndecesL[curr] = j;
								  LineIndecesL[j] = next;
								  LineValuesL[j] = -l;
Kirill Terekhov's avatar
Kirill Terekhov committed
1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652
                }
						  }
            }
						i = U2list[i];
					}
#endif
					//check that diagonal value is the same(must be!)
					//assert(fabs(LineValues[k] / udiag - 1.0) < 1.0e-10);
//this check will fail due to global check of tolerances, if global check will be removed then this will never fail
//#if defined(DIAGONAL_PIVOT) && !defined(DIAGONAL_PERTURBATION)
//					if (fabs(LineValues[k] - udiag) > 1e-9) std::cout << __LINE__ << " Diagonal value went wrong, good: " << udiag << " have " << LineValues[k] << " line " << k << std::endl;
//#endif
///////////////////////////////////////////////////////////////////////////////////
//                    Rescale with diagonal                                      //
///////////////////////////////////////////////////////////////////////////////////
					//rescale line by diagonal
Kirill Terekhov's avatar
Kirill Terekhov committed
1653
					Li = LineIndecesL[k];
Kirill Terekhov's avatar
Kirill Terekhov committed
1654 1655
					while (Li != EOL)
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1656 1657
						LineValuesL[Li] /= udiag;
						Li = LineIndecesL[Li];
Kirill Terekhov's avatar
Kirill Terekhov committed
1658 1659
					}
///////////////////////////////////////////////////////////////////////////////////
Kirill Terekhov's avatar
Kirill Terekhov committed
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705
//                    Condition estimator for U part                             //
///////////////////////////////////////////////////////////////////////////////////
					//estimate condition for U^{-1}
#if defined(ESTIMATOR)
					if( estimator )
					{
						NuU1_old = NuU1;
						mup = 1.0 - EstU1[k];
						mum = -1.0 - EstU1[k];
						smup = smum = 0;
						Ui = LineIndecesU[k];
						while (Ui != EOL)
						{
							v = EstU1[Ui];
							vp = fabs(v + LineValuesU[Ui] * mup);
							vm = fabs(v + LineValuesU[Ui] * mum);
							smup += vp;
							smum += vm;
							Ui = LineIndecesU[Ui];
						}
						if (smup > smum) NuU1 = mup; else NuU1 = mum;
						NuU1_new = std::max(fabs(mup),fabs(mum));

						mup = 1.0 - EstU2[k];
						mum = -1.0 - EstU2[k];
						np = nm = 0;
						//start from the element next after diagonal position
						Ui = LineIndecesU[k];
						while (Ui != EOL)
						{
							v = EstU2[Ui];
							vp = fabs(v + LineValuesU[Ui] * mup);
							vm = fabs(v + LineValuesU[Ui] * mum);
							v = fabs(v);
							if (vp > std::max(2 * v, 0.5)) np++;
							if (std::max(2 * vp, 0.5) < v) np--;
							if (vm > std::max(2 * v, 0.5)) nm++;
							if (std::max(2 * vm, 0.5) < v) nm--;
							Ui = LineIndecesU[Ui];
						}
						NuU2_old = NuU2;
						if (np > nm) NuU2 = mup; else NuU2 = mum;
						NuU2_new = std::max(fabs(mup), fabs(mum));
					} //if( estimator )
#endif //ESTIMATOR
///////////////////////////////////////////////////////////////////////////////////
Kirill Terekhov's avatar
Kirill Terekhov committed
1706 1707 1708
//                 condition estimation for L part                               //
///////////////////////////////////////////////////////////////////////////////////
					//estimate condition for L^{-1}
Kirill Terekhov's avatar
Kirill Terekhov committed
1709
#if defined(ESTIMATOR)
Kirill Terekhov's avatar
Kirill Terekhov committed
1710 1711
					if( estimator )
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731
						NuL1_old = NuL1;
						mup = 1.0 - EstL1[k];
						mum = -1.0 - EstL1[k];
						smup = smum = 0;
						Li = LineIndecesL[k];
						while (Li != EOL)
						{
							v = EstL1[Li];
							vp = fabs(v + LineValuesL[Li] * mup);
							vm = fabs(v + LineValuesL[Li] * mum);
							smup += vp;
							smum += vm;
							Li = LineIndecesL[Li];
						}
						if (smup > smum) NuL1 = mup; else NuL1 = mum;
						NuL1_new = std::max(fabs(mup),fabs(mum));

						NuL2_old = NuL2;
						mup = 1.0 - EstL2[k];
						mum = -1.0 - EstL2[k];
Kirill Terekhov's avatar
Kirill Terekhov committed
1732 1733
						np = nm = 0;
						//start from the element next after diagonal position
Kirill Terekhov's avatar
Kirill Terekhov committed
1734
						Li = LineIndecesL[k];
Kirill Terekhov's avatar
Kirill Terekhov committed
1735 1736
						while (Li != EOL)
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
1737 1738 1739
							v = EstL2[Li];
							vp = fabs(v + LineValuesL[Li] * mup);
							vm = fabs(v + LineValuesL[Li] * mum);
Kirill Terekhov's avatar
Kirill Terekhov committed
1740 1741 1742 1743 1744
							v = fabs(v);
							if (vp > std::max(2 * v, 0.5)) np++;
							if (std::max(2 * vp, 0.5) < v) np--;
							if (vm > std::max(2 * v, 0.5)) nm++;
							if (std::max(2 * vm, 0.5) < v) nm--;
Kirill Terekhov's avatar
Kirill Terekhov committed
1745
							Li = LineIndecesL[Li];
Kirill Terekhov's avatar
Kirill Terekhov committed
1746
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
1747 1748 1749 1750 1751
						if (np > nm) NuL2 = mup; else NuL2 = mum;
						NuL2_new = std::max(fabs(mup), fabs(mum));
					}
#endif //ESTIMATOR

Kirill Terekhov's avatar
Kirill Terekhov committed
1752 1753 1754 1755 1756
///////////////////////////////////////////////////////////////////////////////////
//         discarding current iteration based on condition numbers               //
///////////////////////////////////////////////////////////////////////////////////
          //discard line if condition number is too large
#if defined(DIAGONAL_PIVOT) && defined(DIAGONAL_PIVOT_COND)
Kirill Terekhov's avatar
Kirill Terekhov committed
1757 1758 1759
          if( k != cend-1 && no_swap_algorithm && 
						(std::max(NuU1_new,NuU2_new) > DIAGONAL_PIVOT_COND || std::max(NuL1_new,NuL2_new) > DIAGONAL_PIVOT_COND) )
          {
Kirill Terekhov's avatar
Kirill Terekhov committed
1760
#if defined(REPORT_ILU)
Kirill Terekhov's avatar
Kirill Terekhov committed
1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799
            std::cout << "Requested pivoting based on condition estimator (L" << NuL1_new << " " << NuL2_new << " U " << NuU1_new << " " << NuU2_new << ")! row " << k << "/" << cend << std::endl;
#endif //REPORT_ILU
            //restore condition number
						NuL1 = NuU1_old;
            NuL2 = NuU2_old;
						NuU1 = NuU1_old;
            NuU2 = NuU2_old;
            //clean up values
            Li = cbeg;
						while (Li != EOL)
						{
							i = LineIndecesL[Li];
							LineValuesL[Li] = 0.0; //clean values after use
							LineIndecesL[Li] = UNDEF; //clean indeces after use
							Li = i;
						}
						Ui = cbeg;
						while (Ui != EOL)
						{
							i = LineIndecesU[Ui];
							LineValuesU[Ui] = 0.0; // clean values after use
							LineIndecesU[Ui] = UNDEF; //clean indeces after use
							Ui = i;
						}
            goto swap_algorithm;
          }
          else
#endif //DIAGONAL_PIVOT / DIAGONAL_PIVOT_COND
          {
						NuL = std::max(NuL1_new,NuL2_new);
						NuU = std::max(NuU1_new,NuU2_new);
          }
///////////////////////////////////////////////////////////////////////////////////
//                 reconstruct U-part from linked list                           //
///////////////////////////////////////////////////////////////////////////////////
          //insert line to U part
					U_Address[k].first = static_cast<INMOST_DATA_ENUM_TYPE>(LU_Entries.size());
#if defined(ILUC2)
          U2_Address[k].first = static_cast<INMOST_DATA_ENUM_TYPE>(LU2_Entries.size());
Kirill Terekhov's avatar
Kirill Terekhov committed
1800
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
1801 1802 1803 1804 1805
					Ui = LineIndecesU[k];
					while (Ui != EOL)
					{
						u = fabs(LineValuesU[Ui]);
						if (u*NuU > tau) // apply dropping rule
Kirill Terekhov's avatar
Kirill Terekhov committed
1806
							LU_Entries.push_back(Sparse::Row::make_entry(Ui, LineValuesU[Ui]));
Kirill Terekhov's avatar
Kirill Terekhov committed
1807 1808
#if defined(ILUC2)
						else if (u*NuU > tau2)
Kirill Terekhov's avatar
Kirill Terekhov committed
1809
							LU2_Entries.push_back(Sparse::Row::make_entry(Ui, LineValuesU[Ui]));
Kirill Terekhov's avatar
Kirill Terekhov committed
1810 1811
#endif
						Ui = LineIndecesU[Ui];
Kirill Terekhov's avatar
Kirill Terekhov committed
1812
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
1813 1814 1815 1816
					U_Address[k].last = static_cast<INMOST_DATA_ENUM_TYPE>(LU_Entries.size());
#if defined(ILUC2)
					U2_Address[k].last = static_cast<INMOST_DATA_ENUM_TYPE>(LU2_Entries.size());
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
1817 1818 1819 1820
///////////////////////////////////////////////////////////////////////////////////
//                 reconstruct L-part from linked list                           //
///////////////////////////////////////////////////////////////////////////////////
					//insert column to L part
Kirill Terekhov's avatar
Kirill Terekhov committed
1821
					Li = LineIndecesL[k];
Kirill Terekhov's avatar
Kirill Terekhov committed
1822 1823 1824 1825 1826 1827
					L_Address[k].first = static_cast<INMOST_DATA_ENUM_TYPE>(LU_Entries.size());
#if defined(ILUC2)
          L2_Address[k].first = static_cast<INMOST_DATA_ENUM_TYPE>(LU2_Entries.size());
#endif
					while (Li != EOL)
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1828 1829
						u = fabs(LineValuesL[Li]);
						if (u*NuL > tau) //apply dropping 
Kirill Terekhov's avatar
Kirill Terekhov committed
1830
							LU_Entries.push_back(Sparse::Row::make_entry(Li, LineValuesL[Li]));
Kirill Terekhov's avatar
Kirill Terekhov committed
1831
#if defined(ILUC2)
Kirill Terekhov's avatar
Kirill Terekhov committed
1832
						else if (u*NuL > tau2)
Kirill Terekhov's avatar
Kirill Terekhov committed
1833
							LU2_Entries.push_back(Sparse::Row::make_entry(Li, LineValuesL[Li]));
Kirill Terekhov's avatar
Kirill Terekhov committed
1834
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
1835
						Li = LineIndecesL[Li];
Kirill Terekhov's avatar
Kirill Terekhov committed
1836 1837 1838 1839 1840 1841
					}
					L_Address[k].last = static_cast<INMOST_DATA_ENUM_TYPE>(LU_Entries.size());
#if defined(ILUC2)
          L2_Address[k].last = static_cast<INMOST_DATA_ENUM_TYPE>(LU2_Entries.size());
#endif
///////////////////////////////////////////////////////////////////////////////////
Kirill Terekhov's avatar
Kirill Terekhov committed
1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888
//                 Update estimator vectors                                      //
///////////////////////////////////////////////////////////////////////////////////
#if defined(ESTIMATOR)
					if( estimator )
					{
						{ //U-estimator
							Ui = LineIndecesU[k];
							while (Ui != EOL)
							{
								EstU1[Ui] += LineValuesU[Ui] * NuU1;
								Ui = LineIndecesU[Ui];
							}
							//choose new condition number
							NuU1 = NuU1_new;//std::max(NuU_new,NuU_old);
							//update estimator vector
							Ui = LineIndecesU[k];
							while (Ui != EOL)
							{
								EstU2[Ui] += LineValuesU[Ui] * NuU2;
								Ui = LineIndecesU[Ui];
							}
							//choose new condition number
							NuU2 = NuU2_new;//std::max(NuU_new,NuU_old);
						}
						{ //L-estimator
							Li = LineIndecesL[k];
						  while (Li != EOL)
						  {
							  EstL1[Li] += LineValuesL[Li] * NuL1;
							  Li = LineIndecesL[Li];
						  }
							NuL1 = NuL1_new;
              //update estimator vector
              Li = LineIndecesL[k];
						  while (Li != EOL)
						  {
							  EstL2[Li] += LineValuesL[Li] * NuL2;
							  Li = LineIndecesL[Li];
						  }
              //choose new condition number
              NuL2 = NuL2_new;//std::max(NuL_new,NuL_old);
						}
						CondU[k] = NuU;
						CondL[k] = NuL;
					}
#endif
///////////////////////////////////////////////////////////////////////////////////
Kirill Terekhov's avatar
Kirill Terekhov committed
1889 1890 1891 1892 1893
//                 Cleaning data structures                                      //
///////////////////////////////////////////////////////////////////////////////////
					Li = cbeg;
					while (Li != EOL)
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1894 1895 1896
						i = LineIndecesL[Li];
						LineValuesL[Li] = 0.0; //clean values after use
						LineIndecesL[Li] = UNDEF; //clean indeces after use
Kirill Terekhov's avatar
Kirill Terekhov committed
1897 1898
						Li = i;
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
1899 1900 1901 1902 1903 1904 1905 1906
					Ui = cbeg;
					while (Ui != EOL)
					{
						i = LineIndecesU[Ui];
						LineValuesU[Ui] = 0.0; // clean values after use
						LineIndecesU[Ui] = UNDEF; //clean indeces after use
						Ui = i;
          }
Kirill Terekhov's avatar
Kirill Terekhov committed
1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029