solver_mtiluc2.cpp 113 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_mtiluc2.hpp"
#include <sstream>
Kirill Terekhov's avatar
Kirill Terekhov committed
7 8 9
//#define REPORT_ILU
//#undef REPORT_ILU
//#define REPORT_ILU_PROGRESS
Kirill Terekhov's avatar
Kirill Terekhov committed
10 11
//#define REPORT_ILU_END
//#define REPORT_ILU_SUMMARY
Kirill Terekhov's avatar
Kirill Terekhov committed
12
//#undef REPORT_ILU_PROGRESS
Kirill Terekhov's avatar
Kirill Terekhov committed
13 14 15

//#define USE_OMP

Kirill Terekhov's avatar
Kirill Terekhov committed
16 17 18 19 20 21
using namespace INMOST;

#ifndef DEFAULT_TAU
#define DEFAULT_TAU 0.01
#endif

Kirill Terekhov's avatar
Kirill Terekhov committed
22 23

//#define REORDER_RCM
Kirill Terekhov's avatar
Kirill Terekhov committed
24 25 26 27 28 29 30 31 32 33
//#define REORDER_NNZ
#if defined(USE_SOLVER_METIS)
#define REORDER_METIS_ND
#endif
#if defined(USE_SOLVER_MONDRIAAN)
//#define REORDER_MONDRIAAN
#endif
//#define REORDER_DDPQ
//#define REORDER_ZOLTAN_HUND

Kirill Terekhov's avatar
Kirill Terekhov committed
34
#define ESTIMATOR
Kirill Terekhov's avatar
Kirill Terekhov committed
35
#define ESTIMATOR_REFINE
Kirill Terekhov's avatar
Kirill Terekhov committed
36
#define RESCALE_B
Kirill Terekhov's avatar
Kirill Terekhov committed
37 38 39

#define PREMATURE_DROPPING

Kirill Terekhov's avatar
Kirill Terekhov committed
40 41
//#define EQUALIZE_1NORM
#define EQUALIZE_2NORM
Kirill Terekhov's avatar
Kirill Terekhov committed
42 43
//#define EQUALIZE_IDOMINANCE

Kirill Terekhov's avatar
Kirill Terekhov committed
44
#define PIVOT_THRESHOLD
Kirill Terekhov's avatar
Kirill Terekhov committed
45 46 47 48
#define PIVOT_THRESHOLD_VALUE 1.0e-9
//#define DIAGONAL_PERTURBATION
#define DIAGONAL_PERTURBATION_REL 1.0e-10
#define DIAGONAL_PERTURBATION_ABS 1.0e-12
Kirill Terekhov's avatar
Kirill Terekhov committed
49
//#define DIAGONAL_PIVOT //probably there is some bug
Kirill Terekhov's avatar
Kirill Terekhov committed
50
#define DIAGONAL_PIVOT_TAU 0.01
Kirill Terekhov's avatar
Kirill Terekhov committed
51
//#define DIAGONAL_PIVOT_COND 1.0e+6
Kirill Terekhov's avatar
Kirill Terekhov committed
52
#define ILUC2
Kirill Terekhov's avatar
Kirill Terekhov committed
53 54 55 56 57 58
#define TRACK_DIAGONAL

#if defined(DIAGONAL_PIVOT) && defined(DIAGONAL_PIVOT_TAU) && !defined(TRACK_DIAGONAL)
#define TRACK_DIAGONAL
#endif

Kirill Terekhov's avatar
Kirill Terekhov committed
59 60 61 62

#if defined(REORDER_METIS_ND)
#include "metis.h"
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
#if defined(REORDER_ZOLTAN_HUND)
#include <zoltan.h>
#endif
#if defined(REORDER_MONDRIAAN)
#include <Mondriaan.h>
#endif

template<class T> struct make_integer;
template<> struct make_integer<float> {typedef int type;};
template<> struct make_integer<double> {typedef long long type;};

__INLINE static bool compare(INMOST_DATA_REAL_TYPE * a, INMOST_DATA_REAL_TYPE * b)
{
	return (*reinterpret_cast< make_integer<INMOST_DATA_REAL_TYPE>::type * >(a)) <=
		(*reinterpret_cast< make_integer<INMOST_DATA_REAL_TYPE>::type * >(b));
}


class BinaryHeap
{
	
	INMOST_DATA_REAL_TYPE * Base;
	std::vector<INMOST_DATA_REAL_TYPE *> Array;
	std::vector<INMOST_DATA_ENUM_TYPE> Position;
public:
	void Clear()
	{
		while(!Array.empty())
		{
			Position[Array.back()-Base] = ENUMUNDEF;
			Array.pop_back();
		}
	}
	INMOST_DATA_REAL_TYPE * Get(INMOST_DATA_ENUM_TYPE pos) {return Array[pos];}
	INMOST_DATA_ENUM_TYPE GetSize() {return static_cast<INMOST_DATA_ENUM_TYPE>(Array.size());}
	INMOST_DATA_ENUM_TYPE GetPosition(INMOST_DATA_ENUM_TYPE pos)
	{
		return Position[pos];
	}
	INMOST_DATA_ENUM_TYPE DecreaseKey(INMOST_DATA_ENUM_TYPE pos)
	{
		INMOST_DATA_ENUM_TYPE i = Position[pos];
		++i;
		while(i > 1)
		{
			//if((*Array[i-1]) <= (*Array[i/2-1]))
			if( compare(Array[i-1],Array[i/2-1]) )
			{
				Position[(Array[i/2-1]-Base)] = i-1; 
				Position[(Array[i-1]-Base)] = i/2-1; 
				std::swap(Array[i/2-1],Array[i-1]);
			}
			else break;
			i = i/2;
		}
		return i;
	}
	INMOST_DATA_ENUM_TYPE PushHeap(INMOST_DATA_REAL_TYPE * key)
	{
		INMOST_DATA_ENUM_TYPE i = GetSize();
		Array.push_back(key);
		Position[(key-Base)] = i;
		++i;
		while(i > 1)
		{
			//if((*Array[i-1]) <= (*Array[i/2-1]) )
			if( compare(Array[i-1],Array[i/2-1]) )
			{
				Position[(Array[i-1]-Base)] = i/2-1; 
				Position[(Array[i/2-1]-Base)] = i-1; 
				std::swap(Array[i-1],Array[i/2-1]);
			}
			else break;
			i = i/2;
		}
		return i;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
140

Kirill Terekhov's avatar
Kirill Terekhov committed
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
	void BalanceHeap(INMOST_DATA_ENUM_TYPE i)
	{
		INMOST_DATA_ENUM_TYPE Index;
		++i;
		while(i <= Array.size()/2)
		{
			if( 2*i+1 > Array.size() )
				Index = 2*i;
			//else if( (*Array[2*i-1]) <= (*Array[2*i+1-1]) )
			else if( compare(Array[2*i-1],Array[2*i+1-1]) )
				Index = 2*i;
			else
				Index = 2*i+1;
			//if(!((*Array[i-1]) <= (*Array[Index-1])))
			if(!compare(Array[i-1],Array[Index-1]))
			{ 
				Position[(Array[i-1]-Base)] = Index-1;
				Position[(Array[Index-1]-Base)] = i-1; 
				std::swap(Array[i-1],Array[Index-1]);
			}
			else break;
			i = Index;
		}
	}
	INMOST_DATA_ENUM_TYPE PopHeap()
	{
		INMOST_DATA_ENUM_TYPE Ret = ENUMUNDEF;
		if(Array.empty()) return Ret;
		Ret = static_cast<INMOST_DATA_ENUM_TYPE>(Array[0]-Base);
		Array[0] = Array.back();
171
		Position[Array[0] - Base] = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
		Array.pop_back();
		Position[Ret] = ENUMUNDEF;
		BalanceHeap(0);	
		return Ret;
	}
	BinaryHeap(INMOST_DATA_REAL_TYPE * Base, INMOST_DATA_ENUM_TYPE Size) 
		: Base(Base)
	{
		Position.resize(Size,ENUMUNDEF);
		Array.reserve(4096);
	}
	~BinaryHeap()
	{
	}
};
Kirill Terekhov's avatar
Kirill Terekhov committed
187 188 189



Kirill Terekhov's avatar
Kirill Terekhov committed
190 191
	void MTILUC_preconditioner::DumpMatrix(interval<INMOST_DATA_ENUM_TYPE, Interval> & Address, 
									std::vector<Sparse::Row::entry> & Entries,
Kirill Terekhov's avatar
Kirill Terekhov committed
192 193 194
									INMOST_DATA_ENUM_TYPE wmbeg, INMOST_DATA_ENUM_TYPE wmend,
									std::string file_name)
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
195 196 197 198
		INMOST_DATA_REAL_TYPE norm1 = 0, norm2 = 0, max = -1.0e54, min = 1.0e54, minabs = 1.0e54, vrowmax, diag, mindiag = 1.0e54, maxdiag = -1.0e54, maxabsdiag = -1.0e54, minabsdiag = 1.0e54;
		INMOST_DATA_ENUM_TYPE nnz = 0, dominant_rows = 0, dominant_cols = 0, irowmax = 0, nodiag = 0;
		interval<INMOST_DATA_ENUM_TYPE,INMOST_DATA_REAL_TYPE> vcolmax(wmbeg,wmend,0);
		interval<INMOST_DATA_ENUM_TYPE,INMOST_DATA_ENUM_TYPE> icolmax(wmbeg,wmend,ENUMUNDEF);
Kirill Terekhov's avatar
Kirill Terekhov committed
199 200 201
		for (INMOST_DATA_ENUM_TYPE k = wmbeg; k < wmend; ++k) 
		{
			nnz += Address[k].Size();
Kirill Terekhov's avatar
Kirill Terekhov committed
202
			vrowmax = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
203

Kirill Terekhov's avatar
Kirill Terekhov committed
204 205
			bool diag_found = false;
			diag = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
206 207 208 209
			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;
Kirill Terekhov's avatar
Kirill Terekhov committed
210 211 212 213 214 215 216 217 218 219 220
				if( fabs(Entries[it].second) > vrowmax ) 
				{
					vrowmax = fabs(Entries[it].second);
					irowmax = Entries[it].first;
				}

				if( fabs(Entries[it].second) > vcolmax[Entries[it].first] ) 
				{
					vcolmax[Entries[it].first] = fabs(Entries[it].second);
					icolmax[Entries[it].first] = k;
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
221 222
				if( Entries[it].second > max ) max = Entries[it].second;
				if( Entries[it].second < min ) min = Entries[it].second;
Kirill Terekhov's avatar
Kirill Terekhov committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
				if( fabs(Entries[it].second) < fabs(minabs) ) minabs = Entries[it].second;

				if( Entries[it].first == k )
				{
					diag_found = true;
					diag = Entries[it].second;
				}
			}

			if( diag_found )
			{
				if( mindiag > diag ) mindiag = diag;
				if( maxdiag < diag ) maxdiag = diag;
				if( minabsdiag > fabs(diag) ) minabsdiag = fabs(diag);
				if( maxabsdiag < fabs(diag) ) maxabsdiag = fabs(diag);
Kirill Terekhov's avatar
Kirill Terekhov committed
238
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
239 240 241
			else nodiag++;

			if( irowmax == k ) ++dominant_rows;
Kirill Terekhov's avatar
Kirill Terekhov committed
242 243
		}

Kirill Terekhov's avatar
Kirill Terekhov committed
244 245
		for (INMOST_DATA_ENUM_TYPE k = wmbeg; k < wmend; ++k) if( icolmax[k] == k ) ++dominant_cols;

Kirill Terekhov's avatar
Kirill Terekhov committed
246 247 248 249 250 251
		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;
Kirill Terekhov's avatar
Kirill Terekhov committed
252 253 254 255 256 257 258 259 260 261 262 263
		fout << "% A 1-norm  " << norm1 << std::endl;
		fout << "% A 2-norm  " << sqrt(norm2) << std::endl;
		fout << "% mean 1-norm  " << norm1/(wmend-wmbeg) << std::endl;
		fout << "% mean 2-norm  " << sqrt(norm2/(wmend-wmbeg)) << std::endl;
		fout << "% dominant rows  " << dominant_rows << std::endl;
		fout << "% dominant cols  " << dominant_cols << std::endl;
		fout << "% maximal diagonal value " << maxdiag << std::endl;
		fout << "% minimal diagonal value " << mindiag << std::endl;
		fout << "% absolute maximal diagonal value " << maxabsdiag << std::endl;
		fout << "% absolute minimal diagonal value " << minabsdiag << std::endl;
		fout << "% no diagonal value  " << nodiag << std::endl;
		fout << "% true matrix indices interval " << wmbeg << ":" << wmend << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
264 265 266 267 268 269 270 271 272 273
		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();
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
274 275
	void MTILUC_preconditioner::SwapEntries(interval<INMOST_DATA_ENUM_TYPE, Interval> & Address, 
					 std::vector<Sparse::Row::entry> & Entries, 
Kirill Terekhov's avatar
Kirill Terekhov committed
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
					 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;
					}
				}
			}
		}
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
323
	void MTILUC_preconditioner::SwapLine(interval<INMOST_DATA_ENUM_TYPE, Interval> & Line, INMOST_DATA_ENUM_TYPE i, INMOST_DATA_ENUM_TYPE j)
Kirill Terekhov's avatar
Kirill Terekhov committed
324 325 326 327 328 329 330 331 332 333
	{
		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;
	}
	
Kirill Terekhov's avatar
Kirill Terekhov committed
334
	void MTILUC_preconditioner::inversePQ(INMOST_DATA_ENUM_TYPE wbeg,
Kirill Terekhov's avatar
Kirill Terekhov committed
335 336 337 338 339 340 341 342 343 344 345
				   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;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
346
	void MTILUC_preconditioner::applyPQ(INMOST_DATA_ENUM_TYPE wbeg,
Kirill Terekhov's avatar
Kirill Terekhov committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
				 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];
		}
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
367
	INMOST_DATA_ENUM_TYPE & MTILUC_preconditioner::EnumParameter(std::string name)
Kirill Terekhov's avatar
Kirill Terekhov committed
368 369 370 371 372
	{
		if (name == "scale_iters") return sciters;
		else if( name == "estimator" ) return estimator;
		throw - 1;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
373
	INMOST_DATA_REAL_TYPE & MTILUC_preconditioner::RealParameter(std::string name)
Kirill Terekhov's avatar
Kirill Terekhov committed
374 375 376
	{
		if (name == "tau") return tau;
		else if( name == "tau2" ) return iluc2_tau;
Kirill Terekhov's avatar
Kirill Terekhov committed
377 378
    else if( name == "condition_number_L" ) return condestL;
    else if( name == "condition_number_U" ) return condestU;
Kirill Terekhov's avatar
Kirill Terekhov committed
379 380
		throw - 1;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
381
	void MTILUC_preconditioner::Copy(const Method * other)
Kirill Terekhov's avatar
Kirill Terekhov committed
382
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
383
		const MTILUC_preconditioner * b = dynamic_cast<const MTILUC_preconditioner *>(other);
Kirill Terekhov's avatar
Kirill Terekhov committed
384 385 386 387 388 389 390
		assert(b != NULL);
		tau = b->tau;
		Alink = b->Alink;
		info = b->info;
		sciters = b->sciters;
		eps = b->eps;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
391
	MTILUC_preconditioner::MTILUC_preconditioner(const MTILUC_preconditioner & other) :Method(other)
Kirill Terekhov's avatar
Kirill Terekhov committed
392 393 394
	{
		Copy(&other);
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
395
	MTILUC_preconditioner & MTILUC_preconditioner::operator =(MTILUC_preconditioner const & other)
Kirill Terekhov's avatar
Kirill Terekhov committed
396 397 398 399
	{
		Copy(&other);
		return *this;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
400
	MTILUC_preconditioner::MTILUC_preconditioner(Solver::OrderInfo & info)
Kirill Terekhov's avatar
Kirill Terekhov committed
401 402 403 404 405 406 407 408 409 410 411 412 413 414
		:tau(DEFAULT_TAU),info(&info)
	{
		Alink = NULL;
		init = false;
		sciters = 8;
		eps = 1e-54;
#if defined(ESTIMATOR)
		estimator = 1;
#else
		estimator = 0;
#endif
		tau = 1.0e-3;
		iluc2_tau = tau*tau;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
415 416 417
	bool MTILUC_preconditioner::isInitialized() { return init; }
	bool MTILUC_preconditioner::isFinalized() { return !init; }
	bool MTILUC_preconditioner::Initialize()
Kirill Terekhov's avatar
Kirill Terekhov committed
418 419 420
	{

		const INMOST_DATA_REAL_TYPE subst = 1.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
421
		const INMOST_DATA_REAL_TYPE tol_modif = PIVOT_THRESHOLD_VALUE;
Kirill Terekhov's avatar
Kirill Terekhov committed
422 423 424 425 426
		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
427
		INMOST_DATA_ENUM_TYPE vbeg, vend; // vector interval
Kirill Terekhov's avatar
Kirill Terekhov committed
428
		
Kirill Terekhov's avatar
Kirill Terekhov committed
429 430 431 432
		INMOST_DATA_ENUM_TYPE k, i, j, Li, Ui, curr, next;
		INMOST_DATA_REAL_TYPE l,u,udiag, abs_udiag, max_diag, min_diag, mean_diag;
		INMOST_DATA_ENUM_TYPE nzA, nzLU = 0;
		Sparse::Vector DL, DR;
Kirill Terekhov's avatar
Kirill Terekhov committed
433
		info->GetOverlapRegion(info->GetRank(), mobeg, moend);
Kirill Terekhov's avatar
Kirill Terekhov committed
434
		info->GetVectorRegion(vbeg,vend);
Kirill Terekhov's avatar
Kirill Terekhov committed
435 436
		
		//prepare temporal array
Kirill Terekhov's avatar
Kirill Terekhov committed
437 438
		temp.set_interval_beg(vbeg);
		temp.set_interval_end(vend);
Kirill Terekhov's avatar
Kirill Terekhov committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
		//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
455
		for(Sparse::Vector::iterator ri = DL.Begin(); ri != DL.End(); ++ri) *ri = 0.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
456 457 458 459 460 461

		

		for (k = mobeg; k != moend; k++) ddP[k] = ddQ[k] = k;
		// supplementary data for ddPQ reordering
		interval<INMOST_DATA_ENUM_TYPE, bool> donePQ(mobeg, moend);
Kirill Terekhov's avatar
Kirill Terekhov committed
462
		interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> invP(mobeg, moend), invQ(mobeg,moend), localP(mobeg, moend,ENUMUNDEF), localQ(mobeg,moend,ENUMUNDEF);		
Kirill Terekhov's avatar
Kirill Terekhov committed
463 464 465 466 467 468 469 470 471 472 473

		//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);
		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
474
		std::vector<Sparse::Row::entry> A_Entries;
Kirill Terekhov's avatar
Kirill Terekhov committed
475
		//std::vector<INMOST_DATA_ENUM_TYPE> sort_indeces;
Kirill Terekhov's avatar
Kirill Terekhov committed
476 477 478
		interval<INMOST_DATA_ENUM_TYPE, Interval> A_Address(mobeg, moend);
		interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> Ulist(mobeg, moend), Llist(mobeg, moend), Blist(mobeg,moend);
		interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> Ubeg(mobeg, moend,EOL), Lbeg(mobeg, moend,EOL), Bbeg(mobeg,moend,EOL);
Kirill Terekhov's avatar
Kirill Terekhov committed
479

Kirill Terekhov's avatar
Kirill Terekhov committed
480
		INMOST_DATA_REAL_TYPE NuU = 1, NuL = 1, NuD, NuU_max = 1.0, NuL_max = 1.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
481
#if defined(ESTIMATOR)
Kirill Terekhov's avatar
Kirill Terekhov committed
482 483
		//supplimentary data structures for condition estimates of L^{-1}, U^{-1}
		INMOST_DATA_REAL_TYPE mup, mum, smup, smum, NuL1 = 1, NuL2 = 1, NuU1 = 1, NuU2 = 1;
Kirill Terekhov's avatar
Kirill Terekhov committed
484
		INMOST_DATA_REAL_TYPE NuU1_old = 1, NuL1_old = 1, NuU2_old = 1, NuL2_old = 1;
Kirill Terekhov's avatar
Kirill Terekhov committed
485 486
		INMOST_DATA_REAL_TYPE NuU1_new = 1, NuL1_new = 1, vp, vm, v;
#if defined(ESTIMATOR_REFINE)
Kirill Terekhov's avatar
Kirill Terekhov committed
487
		INMOST_DATA_ENUM_TYPE np, nm;
Kirill Terekhov's avatar
Kirill Terekhov committed
488 489 490 491 492 493 494
		INMOST_DATA_REAL_TYPE NuU2_new, NuL2_new;
#endif
		interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_REAL_TYPE> EstL1(mobeg, moend,0.0), EstU1(mobeg, moend,0.0);
		interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_REAL_TYPE> EstL2(mobeg, moend,0.0), EstU2(mobeg, moend,0.0);
#endif
#if defined(ESTIMATOR) && defined(DIAGONAL_PIVOT_COND)
		INMOST_DATA_REAL_TYPE NuU_tmp, NuL_tmp;
Kirill Terekhov's avatar
Kirill Terekhov committed
495
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
496 497 498 499 500
		//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
501 502 503
		interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_REAL_TYPE> U(mobeg,moend,std::numeric_limits<INMOST_DATA_REAL_TYPE>::max());
		interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_REAL_TYPE> V(mobeg,moend,std::numeric_limits<INMOST_DATA_REAL_TYPE>::max());
		interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_REAL_TYPE> Dist(mobeg,moend,std::numeric_limits<INMOST_DATA_REAL_TYPE>::max());		
Kirill Terekhov's avatar
Kirill Terekhov committed
504
		
Kirill Terekhov's avatar
Kirill Terekhov committed
505 506 507
		interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_REAL_TYPE> LineValuesU(mobeg, moend,0.0), LineValuesL(mobeg,moend,0.0);
		interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> LineIndecesU(mobeg, moend+1,UNDEF), LineIndecesL(mobeg,moend+1,UNDEF);
		double tfactor = 0.0, trescale = 0.0, treorder = 0.0, treassamble = 0.0, ttotal, tt;
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
508
#if defined(REORDER_METIS_ND)
Kirill Terekhov's avatar
Kirill Terekhov committed
509
		double tmetisgraph = 0, tmetisnd = 0;
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
510 511
#endif
        double tlfactor, tlrescale, tlreorder, tlreassamble;
Kirill Terekhov's avatar
Kirill Terekhov committed
512 513 514 515 516 517 518
		ttotal = Timer();

		
		//calculate number of nonzeros
		nzA = 0;
		for (k = mobeg; k < moend; ++k)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
519
			for (Sparse::Row::iterator r = (*Alink)[k].Begin(); r != (*Alink)[k].End(); ++r)
Kirill Terekhov's avatar
Kirill Terekhov committed
520 521 522 523 524 525
				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);
Kirill Terekhov's avatar
Kirill Terekhov committed
526 527 528 529 530
		LU_Entries.reserve(nzA*4);

#if defined(REORDER_NNZ)
		wgt_coords sort_wgts(2*(moend - mobeg));
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
531

Kirill Terekhov's avatar
Kirill Terekhov committed
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
#if defined(REORDER_ZOLTAN_HUND)
#endif

#if defined(REORDER_MONDRIAAN)
		{
			sparsematrix sp;
			int numparts = 8;
			int * cols = new int[nzA];
			int * rows = new int[Alink->Size()+1];
			double * values = new double[nzA];
			int cnt = 0;
			rows[0] = 0;
			for (k = mobeg; k < moend; ++k)
			{
				for (Sparse::Row::iterator r = (*Alink)[k].Begin(); r != (*Alink)[k].End(); ++r)
				{
					if (r->first >= mobeg && r->first < moend && fabs(r->second) > 0.0)
					{
						cols[cnt] = r->first;
						values[cnt] = r->second;
						++cnt;
					}
				}
				rows[k-mobeg+1] = cnt;
			}
			CRSSparseMatrixInit(&sp,Alink->Size(),Alink->Size(),nzA,cols,rows,values,0);
			
			PstartInit(&sp,numparts);
			opts options;
			
			SetDefaultOptions(&options);
			//SetOption(&options,"SplitMethod","KLFM");
			SetOption(&options,"Permute","BBD");
			SetOption(&options,"SplitStrategy","onedimrow");
			DistributeMatrixMondriaan(&sp,numparts,0.1,&options,NULL);
			sp.

			j = 0;
			for (k = mobeg; k < moend; ++k)
			{
				A_Address[k].first = j;
				for (Sparse::Row::iterator r = (*Alink)[sp.row_perm[k-mobeg]].Begin(); r != (*Alink)[sp.row_perm[k-mobeg]].End(); ++r)
				{
					if (r->first >= mobeg && r->first < moend && fabs(r->second) > 0.0)
						A_Entries[j++] = Sparse::Row::make_entry(sp.col_perm_inv[r->first-mobeg], r->second);
				}
				A_Address[k].last = j;
				assert(A_Address[k].Size() != 0); //singular matrix
			}
			DumpMatrix(A_Address,A_Entries,mobeg,moend,"mondriaan.mtx");
			/*
			sp.MMTypeCode[0] = 'M';
			FILE * f = fopen("mondriaan.mtx","w");
			MMWriteSparseMatrix(&sp,f,"mondriaan",&options);
			fclose(f);
			*/
			MMDeleteSparseMatrix(&sp);
			delete [] cols;
			delete [] rows;
			delete [] values;
		}
#else
		
Kirill Terekhov's avatar
Kirill Terekhov committed
595 596 597 598
		j = 0;
		for (k = mobeg; k < moend; ++k)
		{
			A_Address[k].first = j;
Kirill Terekhov's avatar
Kirill Terekhov committed
599
			for (Sparse::Row::iterator r = (*Alink)[k].Begin(); r != (*Alink)[k].End(); ++r)
Kirill Terekhov's avatar
Kirill Terekhov committed
600 601
			{
				if (r->first >= mobeg && r->first < moend && fabs(r->second) > 0.0)
Kirill Terekhov's avatar
Kirill Terekhov committed
602
					A_Entries[j++] = Sparse::Row::make_entry(r->first, r->second);
Kirill Terekhov's avatar
Kirill Terekhov committed
603 604
			}
			A_Address[k].last = j;
Kirill Terekhov's avatar
Kirill Terekhov committed
605
			//assert(A_Address[k].Size() != 0); //singular matrix
Kirill Terekhov's avatar
Kirill Terekhov committed
606
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
607 608 609 610
#endif

		std::vector<INMOST_DATA_REAL_TYPE> C_Entries(A_Entries.size());

Kirill Terekhov's avatar
Kirill Terekhov committed
611 612 613
#if defined(ILUC2)
		INMOST_DATA_ENUM_TYPE nzLU2 = 0;
		INMOST_DATA_REAL_TYPE tau2 = iluc2_tau;
Kirill Terekhov's avatar
Kirill Terekhov committed
614
		std::vector<Sparse::Row::entry> LU2_Entries;
Kirill Terekhov's avatar
Kirill Terekhov committed
615 616 617
		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);
Kirill Terekhov's avatar
Kirill Terekhov committed
618
		LU2_Entries.reserve(nzA*4);
Kirill Terekhov's avatar
Kirill Terekhov committed
619 620 621 622 623 624 625 626 627 628 629 630
#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
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
631 632
			//std::fill(localP.begin() + (wbeg - mobeg), localP.begin() + (wend - mobeg), ENUMUNDEF);
			//std::fill(localQ.begin() + (wbeg - mobeg), localQ.begin() + (wend - mobeg), ENUMUNDEF);
Kirill Terekhov's avatar
Kirill Terekhov committed
633 634 635
			//ddPQ reordering on current Schur complement
			INMOST_DATA_ENUM_TYPE cbeg = wbeg, cend = wend; //next size of factored B block
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Kirill Terekhov's avatar
Kirill Terekhov committed
636
/////////// MAXIMUM TRANSVERSE REORDERING /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Kirill Terekhov's avatar
Kirill Terekhov committed
637
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Kirill Terekhov's avatar
Kirill Terekhov committed
638 639 640 641 642 643 644 645 646 647 648
			{
				tt = Timer();
				INMOST_DATA_ENUM_TYPE ColumnBegin;
				INMOST_DATA_ENUM_TYPE pop_heap_pos;
				INMOST_DATA_REAL_TYPE ShortestPath, AugmentPath;
				INMOST_DATA_ENUM_TYPE PathEnd, Trace, IPermPrev;
				//Sparse::Vector & U = DL;
				//Sparse::Vector & V = DR;
				interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_REAL_TYPE> Cmax(wbeg,wend,0.0);
				interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> & Perm = localQ;
				interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> & IPerm = localP;
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
649
				//interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> & UpdateStack = Ulist;
Kirill Terekhov's avatar
Kirill Terekhov committed
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 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
				interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> & ColumnList = Llist;//(wbeg,wend,ENUMUNDEF);
				interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> & Parent = Blist;
				interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> AugmentPosition(wbeg,wend,ENUMUNDEF);
				interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> ColumnPosition(wbeg,wend,ENUMUNDEF);
				
				BinaryHeap Heap(&Dist[wbeg],wend-wbeg);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////// Arrays initialization   ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
				
				//std::fill(U.Begin() + wbeg - mobeg, U.Begin() + wend - mobeg, std::numeric_limits<INMOST_DATA_REAL_TYPE>::max());
				//std::fill(V.Begin() + wbeg - mobeg, V.Begin() + wend - mobeg, std::numeric_limits<INMOST_DATA_REAL_TYPE>::max());
				//std::fill(Cmax.begin() + wbeg - mobeg, Cmax.begin() + wend - mobeg, 0.0);
				//std::fill(Dist.begin() + wbeg - mobeg, Dist.begin() + wend - mobeg, std::numeric_limits<INMOST_DATA_REAL_TYPE>::max());
				std::fill(Perm.begin() + wbeg - mobeg, Perm.begin() + wend - mobeg, ENUMUNDEF);
				std::fill(IPerm.begin() + wbeg - mobeg, IPerm.begin() + wend - mobeg, ENUMUNDEF);
				std::fill(Parent.begin() + wbeg - mobeg, Parent.begin() + wend - mobeg, ENUMUNDEF);
				std::fill(ColumnList.begin() + wbeg - mobeg, ColumnList.begin() + wend - mobeg, ENUMUNDEF);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////// Initial LOG transformation to dual problem and initial extreme match /////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
				//double T = Timer();
				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 = C_Entries[it] = fabs(A_Entries[it].second);
						if( u > Cmax[i] ) Cmax[i] = u;
						//C_Entries.push_back(Sparse::Row::make_entry(i,u));
					}
				}

				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;
						if( Cmax[i] == 0.0 )
							C_Entries[it] = std::numeric_limits<INMOST_DATA_REAL_TYPE>::max();
						else
						{
							C_Entries[it] = log(Cmax[i])-log(C_Entries[it]);
							//C_Entries[it] = log10(Cmax[i])-log10(C_Entries[it]);
							//C_Entries[it] = fabs(log10(Cmax[i]/C_Entries[it]));
							//C_Entries[it] = fabs(log(Cmax[i]/C_Entries[it]));
							if( C_Entries[it] < U[i] ) U[i] = C_Entries[it];
						}
					}
				}
				for(k = wbeg; k < wend; ++k)
				{
					for (INMOST_DATA_ENUM_TYPE it = A_Address[k].first; it != A_Address[k].last; ++it)
					{
						u = C_Entries[it] - U[A_Entries[it].first];
						if( u < V[k] ) V[k] = u;
					}
				}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////// Update cost and match ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
				for(k = wbeg; k < wend; ++k)
				{
					for (INMOST_DATA_ENUM_TYPE it = A_Address[k].first; it != A_Address[k].last; ++it)
					{
						u = fabs(C_Entries[it] - V[k] - U[A_Entries[it].first]);
						if( u < 1.0e-30 && Perm[A_Entries[it].first] == ENUMUNDEF && IPerm[k] == ENUMUNDEF )
						{
							 Perm[A_Entries[it].first] = k;
							 IPerm[k] = A_Entries[it].first;
							 ColumnPosition[k] = it;
						}
					}
				}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////// 1-step augmentation   /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

				for(k = wbeg; k < wend; ++k)
				{
					if( IPerm[k] == ENUMUNDEF ) //unmatched row
					{
						for (INMOST_DATA_ENUM_TYPE it = A_Address[k].first; it != A_Address[k].last && IPerm[k] == ENUMUNDEF; ++it)
						{
							u = fabs(C_Entries[it] - V[k] - U[A_Entries[it].first]);
							if( u <= 1.0e-30 )
							{
								Li = Perm[A_Entries[it].first];
								assert(Li != ENUMUNDEF);
								// Search other row in C for 0
								for (INMOST_DATA_ENUM_TYPE Lit = A_Address[Li].first; Lit != A_Address[Li].last; ++Lit)
								{
									u = fabs(C_Entries[Lit]- V[Li] - U[A_Entries[Lit].first]);
									if( u <= 1.0e-30 && Perm[A_Entries[Lit].first] == ENUMUNDEF )
									{
										Perm[A_Entries[it].first] = k;
										IPerm[k] = A_Entries[it].first;
										ColumnPosition[k] = it;
										Perm[A_Entries[Lit].first] = Li;
										IPerm[Li] = A_Entries[Lit].first;
										ColumnPosition[Li] = Lit;
										break;
									}
								}
							}
						}
					}
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
759

Kirill Terekhov's avatar
Kirill Terekhov committed
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 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 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
				
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////// Weighted bipartite matching ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
				for(k = wbeg; k < wend; ++k)
				{
					if( IPerm[k] != ENUMUNDEF )
						continue;
					Li = k;
					ColumnBegin = EOL;
					Parent[Li] = ENUMUNDEF;
					PathEnd = ENUMUNDEF;
					Trace = k;
					ShortestPath = 0;
					AugmentPath = std::numeric_limits<INMOST_DATA_REAL_TYPE>::max();
					while(true)
					{
						for (INMOST_DATA_ENUM_TYPE Lit = A_Address[Li].first; Lit != A_Address[Li].last; ++Lit)
						{
							Ui = A_Entries[Lit].first;
							//if( ColumnList[Ui] == k ) continue;
							if( ColumnList[Ui] != ENUMUNDEF ) continue;
							l = ShortestPath + C_Entries[Lit] - V[Li] - U[Ui];
							//if( l < 0.0 ) printf("row %d col %d negative l %g Shortest %lf C %lf V %lf U %lf\n",k,Ui,l,C_Entries[Lit],V[Li],U[Ui]);
							if( l < 0.0 && fabs(l) < 1.0e-12 ) l = 0;
							if( l < AugmentPath )
							{
								if( Perm[Ui] == ENUMUNDEF )
								{
									PathEnd = Ui;
									Trace = Li;
									AugmentPath = l;
									AugmentPosition[Ui] = Lit;
								}
								else if( l < Dist[Ui] )
								{
									Dist[Ui] = l;
									Parent[Perm[Ui]] = Li;
									AugmentPosition[Ui] = Lit;
									if( Heap.GetPosition(Ui-wbeg) != ENUMUNDEF )
										Heap.DecreaseKey(Ui-wbeg);
									else 
										Heap.PushHeap(&Dist[Ui]);
								}
							}
						}

						pop_heap_pos = Heap.PopHeap();
						if( pop_heap_pos == ENUMUNDEF ) break;
					
						Ui = pop_heap_pos+wbeg;
						ShortestPath = Dist[Ui];

						if( AugmentPath <= ShortestPath ) 
						{
							Dist[Ui] = std::numeric_limits<INMOST_DATA_REAL_TYPE>::max();
							break;
						}


						ColumnList[Ui] = ColumnBegin;
						ColumnBegin = Ui;

						Li = Perm[Ui];
						
					}
					if( PathEnd != ENUMUNDEF )
					{
						Ui = ColumnBegin;
						while(Ui != EOL)
						{
							U[Ui] += Dist[Ui] - AugmentPath;
							if( Perm[Ui] != ENUMUNDEF ) V[Perm[Ui]] = C_Entries[ColumnPosition[Perm[Ui]]] - U[Ui];
							Dist[Ui] = std::numeric_limits<INMOST_DATA_REAL_TYPE>::max();
							Li = ColumnList[Ui];
							ColumnList[Ui] = ENUMUNDEF;
							Ui = Li;
						}

						Ui = PathEnd;
						while(Trace != ENUMUNDEF)
						{
							IPermPrev = IPerm[Trace];
							Perm[Ui] = Trace;
							IPerm[Trace] = Ui;

							ColumnPosition[Trace] = AugmentPosition[Ui];
							V[Trace] = C_Entries[ColumnPosition[Trace]] - U[Ui];

							Ui = IPermPrev;
							Trace = Parent[Trace];

						}
						for(Ui = 0; Ui < Heap.GetSize(); ++Ui) *Heap.Get(Ui) = std::numeric_limits<INMOST_DATA_REAL_TYPE>::max();
						Heap.Clear();
					}
				}
				//printf("Maximum product transversal %lf\n",Timer()-T);
				//fclose(rec);

				//T = Timer();
				for (k = cbeg; k < cend; ++k)
				{
					//B_Address[k].first = static_cast<INMOST_DATA_ENUM_TYPE>(B_Entries.size());

					bool flip_sign = false;
					
					for (INMOST_DATA_ENUM_TYPE jt = A_Address[k].first; jt != A_Address[k].last; ++jt)
					{
						i = A_Entries[jt].first;
						j = Perm[A_Entries[jt].first];
						
						l = exp(V[k]);
						u = exp(U[i])/Cmax[i];
						DL[k] = l;
						DR[i] = u;
						//l = l*u;
						//l = l*u;//exp(U[i]+V[k])/Cmax[i];
						//l = pow(10,U[i]+V[k])/Cmax[i];
						//l = exp(U[i]+V[k])/Cmax[i];
						//A_Entries[jt].first = j;
						//A_Entries[jt].second *= l;

						
						
						//B_Entries.push_back(Sparse::Row::make_entry(j, l*A_Entries[jt].second));

						if( j == k && (l*A_Entries[jt].second*u) < 0.0 ) flip_sign = true;
					}

					//B_Address[k].last = static_cast<INMOST_DATA_ENUM_TYPE>(B_Entries.size());

					if( flip_sign )
					{
						DL[k] *= -1;
						//for(Li = B_Address[k].first; Li < B_Address[k].last; ++Li)
						//	B_Entries[Li].second *= -1;
					}
						

					//std::sort(B_Entries.begin() + B_Address[k].first, B_Entries.end());
					//std::sort(A_Entries.begin() + A_Address[k].first, A_Entries.begin() + A_Address[k].last);
				}
				//printf("Reorder matrix %lf\n",Timer()-T);

				//DumpMatrix(A_Address,A_Entries,wbeg,wend,"A.mtx");
				//DumpMatrix(B_Address,B_Entries,wbeg,wend,"MC64.mtx");

				std::fill(localP.begin() + (wbeg - mobeg), localP.begin() + (wend - mobeg), ENUMUNDEF);
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
                
                { //check that there are no gaps in Perm
                    for(k = cbeg; k < cend; ++k)
                    {
                        if( Perm[k] != ENUMUNDEF )
                            localP[Perm[k]] = 0;
                    }
                    std::vector<INMOST_DATA_ENUM_TYPE> gaps;
                    for(k = cbeg; k < cend; ++k)
                        if( localP[k] == ENUMUNDEF )
                            gaps.push_back(k);
                    
                    for(k = cbeg; k < cend; ++k)
                        if( Perm[k] == ENUMUNDEF )
                        {
                            Perm[k] = gaps.back();
                            gaps.pop_back();
                        }
                    std::fill(localP.begin() + (wbeg - mobeg), localP.begin() + (wend - mobeg), ENUMUNDEF);
                }
Kirill Terekhov's avatar
Kirill Terekhov committed
929 930 931 932 933 934 935 936 937 938
				
				//exit(-1);

				tt = Timer() - tt;

				treorder += tt;
			}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////// END MAXIMUM TRANSVERSE REORDERING /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Kirill Terekhov's avatar
Kirill Terekhov committed
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 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
#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)));

Kirill Terekhov's avatar
Kirill Terekhov committed
987 988 989
			
			//first select those elements that are largest over row and column simultaneously
			sort_wgts.clear();
Kirill Terekhov's avatar
Kirill Terekhov committed
990 991 992 993 994 995
			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])));
Kirill Terekhov's avatar
Kirill Terekhov committed
996
					//sort_wgts.push_back(wgt_coord(temp[k], coord(k, Blist[k])));
Kirill Terekhov's avatar
Kirill Terekhov committed
997 998
					//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])));
Kirill Terekhov's avatar
Kirill Terekhov committed
999
					sort_wgts.push_back(wgt_coord(temp[k]/(Ulist[k]+Llist[Blist[k]]-1), coord(k, Blist[k])));
Kirill Terekhov's avatar
Kirill Terekhov committed
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
				}
			}
			//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;
Kirill Terekhov's avatar
Kirill Terekhov committed
1018
				if( it-sort_wgts.begin() > 0.01*(moend-mobeg) ) break; //5% of all values
Kirill Terekhov's avatar
Kirill Terekhov committed
1019 1020 1021
			}
			cend = i;
#elif defined(REORDER_METIS_ND)
Kirill Terekhov's avatar
Kirill Terekhov committed
1022
			tt = Timer();
Kirill Terekhov's avatar
Kirill Terekhov committed
1023
			idx_t nvtxs = wend-wbeg;
Kirill Terekhov's avatar
Kirill Terekhov committed
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
			std::vector<idx_t> xadj(nvtxs+1), adjncy, perm(nvtxs),iperm(nvtxs);
			adjncy.reserve(nzA*2);
			
			for (k = cbeg; k < cend; ++k) if( localQ[k] == ENUMUNDEF ) printf("%s:%d No column permutation for row %d. Matrix is structurally singular\n",__FILE__,__LINE__,k);

			for (k = cbeg; k < cend; ++k) localP[k] = k;
			for (k = cbeg; k < cend; ++k) V[localQ[k]] = DR[k]; //if you expirience a bug here then the matrix is structurally singular
			for (k = cbeg; k < cend; ++k) DR[k] = V[k];

			
			
			for(k = wbeg; k < wend; ++k)
			{
				for (INMOST_DATA_ENUM_TYPE jt = A_Address[k].first; jt != A_Address[k].last; ++jt)
				{
					A_Entries[jt].first = localQ[A_Entries[jt].first];
					//A_Entries[jt].second *= DL[k]*DR[A_Entries[jt].first];
					
				}
				std::sort(A_Entries.begin()+A_Address[k].first,A_Entries.begin()+A_Address[k].last);
			}

			inversePQ(wbeg,wend,localP,localQ, invP,invQ);
			applyPQ(wbeg, wend, localP, localQ, invP, invQ);

			//DumpMatrix(A_Address,A_Entries,wbeg,wend,"a.mtx");
Kirill Terekhov's avatar
Kirill Terekhov committed
1050 1051 1052 1053 1054

///////////////////////////////////////////////////////////////////////////////////
//       setup indices for transposed traversal of B block                       //
///////////////////////////////////////////////////////////////////////////////////
			
Kirill Terekhov's avatar
Kirill Terekhov committed
1055 1056
			tmetisgraph = Timer();

Kirill Terekhov's avatar
Kirill Terekhov committed
1057 1058
			for (k = wend; k > wbeg; --k)
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1059
				//vwgt[k-1] = 1;
Kirill Terekhov's avatar
Kirill Terekhov committed
1060 1061 1062
				if (A_Address[k-1].Size() > 0)
				{
					i = A_Entries[A_Address[k-1].first].first;
Kirill Terekhov's avatar
Kirill Terekhov committed
1063 1064
					Blist[k-1] = Bbeg[i];
					Bbeg[i] = k-1;
Kirill Terekhov's avatar
Kirill Terekhov committed
1065
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
1066
				Ulist[k-1] = A_Address[k-1].first;
Kirill Terekhov's avatar
Kirill Terekhov committed
1067 1068
			}
			
Kirill Terekhov's avatar
Kirill Terekhov committed
1069 1070

			xadj[0] = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
1071 1072 1073 1074
			for(i = wbeg; i < wend; ++i)
			{
				for (INMOST_DATA_ENUM_TYPE jt = A_Address[i].first; jt != A_Address[i].last; ++jt)
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
1075 1076
					if( A_Entries[jt].first != i )
						adjncy.push_back(static_cast<idx_t>(A_Entries[jt].first-wbeg));
Kirill Terekhov's avatar
Kirill Terekhov committed
1077 1078
				}
				
Kirill Terekhov's avatar
Kirill Terekhov committed
1079
				Li = Bbeg[i];
Kirill Terekhov's avatar
Kirill Terekhov committed
1080 1081
				while (Li != EOL)
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
1082 1083 1084
					if( Li != i )
						adjncy.push_back(static_cast<idx_t>(Li-wbeg));
					Li = Blist[Li];
Kirill Terekhov's avatar
Kirill Terekhov committed
1085
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
1086 1087
				
				Li = Bbeg[i];
Kirill Terekhov's avatar
Kirill Terekhov committed
1088 1089
				while (Li != EOL)
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
1090 1091 1092
					Ui = Blist[Li];
					Ulist[Li]++;
					if (A_Address[Li].last - Ulist[Li] > 0)
Kirill Terekhov's avatar
Kirill Terekhov committed
1093
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1094 1095 1096
						k = A_Entries[Ulist[Li]].first;
						Blist[Li] = Bbeg[k];
						Bbeg[k] = Li;
Kirill Terekhov's avatar
Kirill Terekhov committed
1097 1098 1099 1100 1101
					}

					Li = Ui;
				}
				
Kirill Terekhov's avatar
Kirill Terekhov committed
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
				std::sort(adjncy.begin()+xadj[i-wbeg],adjncy.end());
				adjncy.resize(std::unique(adjncy.begin()+xadj[i-wbeg],adjncy.end())-adjncy.begin());
				
				xadj[i-wbeg+1] = static_cast<idx_t>(adjncy.size());
			}

			std::fill(Bbeg.begin(),Bbeg.end(),EOL);

			tmetisgraph = Timer() - tmetisgraph;
			/*
			{
				FILE * f = fopen("metis.mtx","w");
				fprintf(f,"%%MatrixMarket matrix coordinate real general\n");
				fprintf(f,"%d %d %d\n",nvtxs,nvtxs,adjncy.size());
				for(i = wbeg; i < wend; ++i)
				{
					for(j = xadj[i]; j < xadj[i+1]; ++j)
						fprintf(f,"%d %d 1\n",i+1,adjncy[j]+1);
				}
				fclose(f);
Kirill Terekhov's avatar
Kirill Terekhov committed
1122
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1123
			*/
Kirill Terekhov's avatar
Kirill Terekhov committed
1124 1125 1126 1127 1128 1129
			//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;
Kirill Terekhov's avatar
Kirill Terekhov committed
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
			
			//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;
			//options[METIS_OPTION_CTYPE] = METIS_CTYPE_RM;
			//options[METIS_OPTION_RTYPE] = METIS_RTYPE_SEP2SIDED;
			//options[METIS_OPTION_IPTYPE] = METIS_IPTYPE_NODE;
			//options[METIS_OPTION_PTYPE] = METIS_PTYPE_RB;
			//options[METIS_OPTION_NO2HOP] = 0;
			//options[METIS_OPTION_MINCONN] = 0;
			//options[METIS_OPTION_NITER] = 4;
			//printf("before metis\n");
			//METIS_NodeNDP(nvtxs,&xadj[0],&adjncy[0],NULL,4,options,&perm[0],&iperm[0],&sizes[0]);
			tmetisnd = Timer();
			METIS_NodeND(&nvtxs,&xadj[0],&adjncy[0],NULL,options,&perm[0],&iperm[0]);
			tmetisnd = Timer()-tmetisnd;
			//printf("after metis\n");
Kirill Terekhov's avatar
Kirill Terekhov committed
1145 1146
			for(k = wbeg; k < wend; ++k)
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1147 1148 1149 1150
				//localP[k] = iperm[k-wbeg]+wbeg;
				//localQ[k] = iperm[k-wbeg]+wbeg;
				localP[perm[k-wbeg]+wbeg] = k;
				localQ[perm[k-wbeg]+wbeg] = k;
Kirill Terekhov's avatar
Kirill Terekhov committed
1151 1152 1153
			}
			cend = wend;
			i = wend;
Kirill Terekhov's avatar
Kirill Terekhov committed
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

			treorder += Timer() - tt;
#elif defined(REORDER_NNZ)
			//for (k = cbeg; k < cend; ++k) V[k] = DR[localQ[k]];
			//for (k = cbeg; k < cend; ++k) DR[k] = V[k];

			//DumpMatrix(A_Address,A_Entries,cbeg,cend,"mat_original.mtx");
			std::fill(U.begin() + wbeg - mobeg, U.begin() + wend - mobeg, 0.0);
			std::fill(V.begin() + wbeg - mobeg, V.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) Blist[localQ[k]] = k;

			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
					U[k] += u; //row norm
					V[i] += u; //col norm
					//nonzeros
					Ulist[k]++; //row nnz
					Llist[i]++; //col nnz

					if( Blist[i] == k ) temp[k] = u;
				}
			}

			sort_wgts.clear();
			for (k = wbeg; k < wend; ++k)
Kirill Terekhov's avatar
Kirill Terekhov committed
1189
                sort_wgts.push_back(wgt_coord(Ulist[k]+Llist[Blist[k]]-1, coord(k, Blist[k])));
Kirill Terekhov's avatar
Kirill Terekhov committed
1190 1191 1192
				//sort_wgts.push_back(wgt_coord((Ulist[k]+Llist[Blist[k]]-1)*(U[k]+V[Blist[k]]), coord(k, Blist[k])));
				//sort_wgts.push_back(wgt_coord((Ulist[k]+Llist[Blist[k]]-1)*(U[k]+V[Blist[k]]-temp[k])/temp[k], coord(k, Blist[k])));
				//sort_wgts.push_back(wgt_coord(1.0/temp[k], coord(k, Blist[k])));
Kirill Terekhov's avatar
Kirill Terekhov committed
1193
				//sort_wgts.push_back(wgt_coord((U[k]+V[Blist[k]])*(U[k]+V[Blist[k]])/(temp[k]), coord(k, Blist[k])));
Kirill Terekhov's avatar
Kirill Terekhov committed
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
			std::sort(sort_wgts.begin(), sort_wgts.end());

			i = wbeg;
			for (wgt_coords::iterator it = sort_wgts.begin(); it != sort_wgts.end(); ++it)
			{
				localP[it->second.first] = i;
				localQ[it->second.second] = i;
				++i;
			}
			cend = i;
Kirill Terekhov's avatar
Kirill Terekhov committed
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
#elif defined(REORDER_RCM)
            //compute order of each entry
            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;
                    //nonzeros
                    Ulist[k]++; //row nnz
                    Llist[i]++; //col nnz
                }
            }
            //find node with the lowest order
            INMOST_DATA_ENUM_TYPE start = wbeg;
            INMOST_DATA_ENUM_TYPE index = wbeg;
            for(k = wbeg; k < wend; ++k)
                if( Ulist[k] + Llist[Blist[k]] < Ulist[start] + Llist[Blist[start]] )
                    start = k;
            localP[start] = localQ[Blist[start]] = index++;
            
Kirill Terekhov's avatar
Kirill Terekhov committed
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
#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
Kirill Terekhov's avatar
Kirill Terekhov committed
1245
			tt = Timer();
Kirill Terekhov's avatar
Kirill Terekhov committed
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
			//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++;
				}
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270

			for (k = cbeg; k < cend; ++k) 
			{
				U[localP[k]] = DL[k];
				V[localQ[k]] = DR[k];

				//U[k] = DL[localP[k]];
				//V[k] = DR[localQ[k]];
			}
			for (k = cbeg; k < cend; ++k) 
			{
				DL[k] = U[k];
				DR[k] = V[k];
			}

Kirill Terekhov's avatar
Kirill Terekhov committed
1271 1272 1273 1274 1275 1276
			tlreorder = Timer() - tt;
			treorder += tlreorder;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////// REASSAMBLE             ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			tt = Timer();
Kirill Terekhov's avatar
Kirill Terekhov committed
1277
			double tt1, tt2,ttt;
Kirill Terekhov's avatar
Kirill Terekhov committed
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
			//in localPQ numbers indicate where to put current row/column
			//reorder E,F blocks by swaps
			tt1 = Timer();
			//inverse ordering
			inversePQ(wbeg,wend,localP,localQ, invP,invQ);
			tt1 = Timer() - tt1;

			//std::cout << "reorder: " << tt1 << std::endl;
			tt2 = Timer();
			nzA = 0;
			for (k = cbeg; k < cend; ++k)
			{
				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
1295
					B_Entries.push_back(Sparse::Row::make_entry(i, u));
Kirill Terekhov's avatar
Kirill Terekhov committed
1296 1297 1298 1299 1300
				}
				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());
				nzA += A_Address[k].Size();
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1301 1302

			//DumpMatrix(B_Address,B_Entries,cbeg,cend,"mat_reorder.mtx");
Kirill Terekhov's avatar
Kirill Terekhov committed
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
			//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;

			ttt = Timer();
			applyPQ(wbeg, wend, localP, localQ, invP, invQ);
			tt1 += Timer() - ttt;

			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)
Kirill Terekhov's avatar
Kirill Terekhov committed
1332
			std::cout << " rescaling block B " << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
1333
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
			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];

				U[k] = DL[k];
				V[k] = DR[k];
			}
			//DumpMatrix(B_Address,B_Entries,cbeg,cend,"MC64_MPTILUC2.mtx");
			/*
			{
				std::stringstream name;
				name << "mc64_" << info->GetRank() << ".mtx";
				DumpMatrix(B_Address,B_Entries,cbeg,cend,name.str());
			}
			*/
			/*
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////// COMPUTE GIRSCHGORIN RADIUS ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

			INMOST_DATA_REAL_TYPE radii = 0;
			for (k = cbeg; k < cend; k++)
			{
				INMOST_DATA_REAL_TYPE local_radii = 0;
				for (INMOST_DATA_ENUM_TYPE r = B_Address[k].first; r < B_Address[k].last; ++r)
					if( B_Entries[r].first != k ) local_radii += fabs(B_Entries[r].second);
				if( radii < local_radii ) radii = local_radii;
			}

			printf("Gershgorin's radius after mc64: %e\n",radii);
			*/


			//DumpMatrix(B_Address,B_Entries,cbeg,cend,"mat_mc64.mtx");
#if defined(EQUALIZE_1NORM)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////// ROW-COLUMN ALTERNATING SCALING FOR 1-NORM BALANCING////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			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) 
					DL[k] += fabs(B_Entries[r].second);//*B_Entries[r].second;
			}
			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++)
			{
				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) 
						DR[B_Entries[r].first] += DL[k] * fabs(B_Entries[r].second);//*B_Entries[r].second;
				}
				for (k = cbeg; k < cend; k++) if (DR[k] < eps) DR[k] = 1.0 / subst; else DR[k] = 1.0 / DR[k];
				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) 
						DL[k] += DR[B_Entries[r].first] * fabs(B_Entries[r].second);//*B_Entries[r].second;
				}
				for (k = cbeg; k < cend; k++) if (DL[k] < eps) DL[k] = 1.0 / subst; else DL[k] = 1.0 / DL[k];
			}
			//for (k = cbeg; k < cend; k++) DL[k] = sqrt(DL[k]);
			//for (k = cbeg; k < cend; k++) DR[k] = sqrt(DR[k]);
			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];
			}
#elif defined(EQUALIZE_2NORM)
Kirill Terekhov's avatar
Kirill Terekhov committed
1405 1406 1407 1408
			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
1409
					DL[k] += B_Entries[r].second*B_Entries[r].second;
Kirill Terekhov's avatar
Kirill Terekhov committed
1410 1411 1412 1413 1414 1415 1416 1417
			}
			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++)
			{
				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
1418
						DR[B_Entries[r].first] += DL[k] * B_Entries[r].second*B_Entries[r].second;
Kirill Terekhov's avatar
Kirill Terekhov committed
1419 1420 1421 1422 1423 1424
				}
				for (k = cbeg; k < cend; k++) if (DR[k] < eps) DR[k] = 1.0 / subst; else DR[k] = 1.0 / DR[k];
				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
1425
						DL[k] += DR[B_Entries[r].first] * B_Entries[r].second*B_Entries[r].second;
Kirill Terekhov's avatar
Kirill Terekhov committed
1426 1427 1428
				}
				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
1429 1430
			//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
1431 1432 1433 1434 1435
			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];
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
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 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
#elif defined(EQUALIZE_IDOMINANCE)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////THIS VERSION OF RESCALING INCREASES DIAGONAL DOMINANCE///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

			for(k = cbeg; k < cend; ++k)
			{
				for (INMOST_DATA_ENUM_TYPE r = B_Address[k].first; r < B_Address[k].last; ++r) 
				{
					u = fabs(B_Entries[r].second);
					if( u > 0.0 )
						C_Entries[r] = -log(u);
					else 
						C_Entries[r] = std::numeric_limits<INMOST_DATA_REAL_TYPE>::max();
				}
			}

			
			std::fill(temp.begin() + cbeg - mobeg, temp.begin() + cend - mobeg, 0.0);

			for (INMOST_DATA_ENUM_TYPE iter = 0; iter < sciters; iter++)
			{
				std::fill(DL.Begin() + cbeg - mobeg, DL.Begin() + cend - mobeg, std::numeric_limits<INMOST_DATA_REAL_TYPE>::max());
				std::fill(DR.Begin() + cbeg - mobeg, DR.Begin() + cend - mobeg, std::numeric_limits<INMOST_DATA_REAL_TYPE>::max());	
				for (k = cbeg; k < cend; k++) //row number
				{
					for (INMOST_DATA_ENUM_TYPE r = B_Address[k].first; r < B_Address[k].last; ++r)
					{
						i = B_Entries[r].first; //column number
						if( i != k ) //out of diagonal
						{
							u = C_Entries[r] + temp[k] - temp[i];
							DL[k] = std::min(DL[k],u);// update Y1
							DR[i] = std::min(DR[i],u);// update Y2
						}
					}
				}

				for (k = cbeg; k < cend; k++)
				{
					if( DR[k] != std::numeric_limits<INMOST_DATA_REAL_TYPE>::max() && 
						  DL[k] != std::numeric_limits<INMOST_DATA_REAL_TYPE>::max() )
						temp[k] += (DR[k]-DL[k])*0.5;
				}

			}

			for (k = cbeg; k < cend; k++)
			{
				DL[k] = exp(-temp[k]);
				DR[k] = exp(temp[k]);
			}

			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
			
			/*
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////// COMPUTE GIRSCHGORIN RADIUS ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			radii = 0;
			for (k = cbeg; k < cend; k++)
			{
				INMOST_DATA_REAL_TYPE local_radii = 0;
				for (INMOST_DATA_ENUM_TYPE r = B_Address[k].first; r < B_Address[k].last; ++r)
					if( B_Entries[r].first != k ) local_radii += fabs(B_Entries[r].second);
				if( radii < local_radii ) radii = local_radii;
			}

			printf("Gershgorin's radius after equilibration: %e\n",radii);
			*/

#if defined(EQUALIZE_1NORM) || defined(EQUALIZE_2NORM) || defined(EQUALIZE_IDOMINANCE)
			for (k = cbeg; k < cend; k++)
			{
				DL[k] *= U[k];
				DR[k] *= V[k];
			}
#else
			for (k = cbeg; k < cend; k++)
			{
				DL[k] = U[k];
				DR[k] = V[k];
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1524
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
1525 1526 1527 1528 1529

			//DumpMatrix(B_Address,B_Entries,cbeg,cend,"mat_equilibration.mtx");
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////// RESCALING DONE ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Kirill Terekhov's avatar
Kirill Terekhov committed
1530 1531
			//End rescale B block
			trescale += Timer() - tt;
Kirill Terekhov's avatar
Kirill Terekhov committed
1532 1533 1534 1535
#endif
			

			
Kirill Terekhov's avatar
Kirill Terekhov committed
1536 1537 1538 1539
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////// FACTORIZATION BEGIN ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1540 1541
        //int discarded = 0;
#if defined(REPORT_ILU)
Kirill Terekhov's avatar
Kirill Terekhov committed
1542
        int swaps = 0;
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1543
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
#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;
					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)
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
1561
#if defined(TRACK_DIAGONAL)
Kirill Terekhov's avatar
Kirill Terekhov committed
1562
							LU_Diag[k] = B_Entries[i].second;
Kirill Terekhov's avatar
Kirill Terekhov committed
1563
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
#if defined(DIAGONAL_PIVOT)
							mean_diag += fabs(LU_Diag[k]);
#endif
							break;
						}
					}
				}
				//initialize first row and column
///////////////////////////////////////////////////////////////////////////////////
//       get diagonal value                                                      //
///////////////////////////////////////////////////////////////////////////////////
Kirill Terekhov's avatar
Kirill Terekhov committed
1575
				//assert(B_Entries[B_Address[cbeg].first].first == cbeg);
Kirill Terekhov's avatar
Kirill Terekhov committed
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
				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;
				}
#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
1615
					  LU_Entries.push_back(Sparse::Row::make_entry(B_Entries[r].first, u));
Kirill Terekhov's avatar
Kirill Terekhov committed
1616 1617
#if defined(ILUC2)
          else if( fabs(u) > tau2 )
Kirill Terekhov's avatar
Kirill Terekhov committed
1618
            LU2_Entries.push_back(Sparse::Row::make_entry(B_Entries[r].first, u));
Kirill Terekhov's avatar
Kirill Terekhov committed
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
#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
1637
					  LU_Entries.push_back(Sparse::Row::make_entry(Li, l));
Kirill Terekhov's avatar
Kirill Terekhov committed
1638 1639
#if defined(ILUC2)
          else if( fabs(l) > tau2 )
Kirill Terekhov's avatar
Kirill Terekhov committed
1640
            LU2_Entries.push_back(Sparse::Row::make_entry(Li, l));
Kirill Terekhov's avatar
Kirill Terekhov committed
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
#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                     //
///////////////////////////////////////////////////////////////////////////////////
Kirill Terekhov's avatar
Kirill Terekhov committed
1652
#if defined(TRACK_DIAGONAL)
Kirill Terekhov's avatar
Kirill Terekhov committed
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662
				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
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1663 1664 1665 1666
						assert(Ui > cbeg);
#if defined(DIAGONAL_PIVOT)
						mean_diag -= fabs(LU_Diag[Ui]);
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
1667
						LU_Diag[Ui] -= LU_Entries[i].second * LU_Entries[j].second * LU_Diag[cbeg];
Kirill Terekhov's avatar
Kirill Terekhov committed
1668 1669 1670
#if defined(DIAGONAL_PIVOT)
						mean_diag += fabs(LU_Diag[Ui]);
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
1671 1672 1673 1674
						i++;
						j++;
					}
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
1675
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
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 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
				//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
1736
#if defined(ESTIMATOR)
Kirill Terekhov's avatar
Kirill Terekhov committed
1737 1738 1739 1740 1741
        if( estimator )
        {
				  NuU = NuL = 1.0;
				  if( estimator )
				  {
Kirill Terekhov's avatar
Kirill Terekhov committed
1742
					  EstU1[cbeg] = EstL1[cbeg] = 0.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
1743
#if defined(ESTIMATOR_REFINE)
Kirill Terekhov's avatar
Kirill Terekhov committed
1744
						EstU2[cbeg] = EstL2[cbeg] = 0.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
1745
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
1746 1747 1748
					  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;
Kirill Terekhov's avatar
Kirill Terekhov committed
1749
#if defined(ESTIMATOR_REFINE)
Kirill Terekhov's avatar
Kirill Terekhov committed
1750
							EstU2[LU_Entries[it].first] = LU_Entries[it].second;
Kirill Terekhov's avatar
Kirill Terekhov committed
1751
#endif