solver_mtiluc2.cpp 123 KB
Newer Older
Kirill Terekhov's avatar
Kirill Terekhov committed
1
#define _CRT_SECURE_NO_WARNINGS
2

Kirill Terekhov's avatar
Kirill Terekhov committed
3
#include "inmost_solver.h"
4
#if defined(USE_SOLVER)
Kirill Terekhov's avatar
Kirill Terekhov committed
5
6
#include "solver_mtiluc2.hpp"
#include <sstream>
7
#include <deque>
8
#include <iomanip>
9
#include "../../../Misc/utils.h"
Kirill Terekhov's avatar
Kirill Terekhov committed
10
11
12

//#define USE_OMP

Kirill Terekhov's avatar
Kirill Terekhov committed
13
14
15
16
17
18
using namespace INMOST;

#ifndef DEFAULT_TAU
#define DEFAULT_TAU 0.01
#endif

Kirill Terekhov's avatar
Kirill Terekhov committed
19

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

Kirill Terekhov's avatar
Kirill Terekhov committed
31
#define ESTIMATOR
Kirill Terekhov's avatar
Kirill Terekhov committed
32
#define ESTIMATOR_REFINE
Kirill Terekhov's avatar
Kirill Terekhov committed
33
#define RESCALE_B
Kirill Terekhov's avatar
Kirill Terekhov committed
34

35
//#define PREMATURE_DROPPING
Kirill Terekhov's avatar
Kirill Terekhov committed
36

37
//#define EQUALIZE_1NORM
Kirill Terekhov's avatar
Kirill Terekhov committed
38
39
#define EQUALIZE_2NORM
//#define EQUALIZE_IDOMINANCE
Kirill Terekhov's avatar
Kirill Terekhov committed
40

Kirill Terekhov's avatar
Kirill Terekhov committed
41
#define PIVOT_THRESHOLD
42
43
#define PIVOT_THRESHOLD_VALUE 1.0e-9
//#define DIAGONAL_PERTURBATION
Kirill Terekhov's avatar
Kirill Terekhov committed
44
#define DIAGONAL_PERTURBATION_REL 1.0e-7
45
#define DIAGONAL_PERTURBATION_ABS 1.0e-10
Kirill Terekhov's avatar
Kirill Terekhov committed
46
//#define DIAGONAL_PIVOT //probably there is some bug
Kirill Terekhov's avatar
Kirill Terekhov committed
47
#define DIAGONAL_PIVOT_TAU 0.01
Kirill Terekhov's avatar
Kirill Terekhov committed
48
//#define DIAGONAL_PIVOT_COND 100
Kirill Terekhov's avatar
Kirill Terekhov committed
49
#define ILUC2
50
//#define TRACK_DIAGONAL
Kirill Terekhov's avatar
Kirill Terekhov committed
51
52
53
54
55

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

Kirill Terekhov's avatar
Kirill Terekhov committed
56
57

#if defined(REORDER_METIS_ND)
Kirill Terekhov's avatar
Kirill Terekhov committed
58
#define METIS_EXPORT
Kirill Terekhov's avatar
Kirill Terekhov committed
59
60
#include "metis.h"
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
61
62
63
64
65
66
67
#if defined(REORDER_ZOLTAN_HUND)
#include <zoltan.h>
#endif
#if defined(REORDER_MONDRIAAN)
#include <Mondriaan.h>
#endif

Kirill Terekhov's avatar
Kirill Terekhov committed
68
69


70
71
72
73
74
75
76
77
78
	template<typename T>
	std::ostream & fmt(std::ostream & in, const T & value, int width, int precision)
	{
		std::ios save(NULL);
		save.copyfmt(in);
		in << std::fixed << std::setw(width) << std::setprecision(precision) << value;
		std::cout.copyfmt(save);
		return in;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
79

Kirill Terekhov's avatar
Kirill Terekhov committed
80
	void MTILUC_preconditioner::DumpMatrix(interval<INMOST_DATA_ENUM_TYPE, Interval> & Address, 
81
									std::vector<Sparse::Row::entry> & Entries,
Kirill Terekhov's avatar
Kirill Terekhov committed
82
83
84
									INMOST_DATA_ENUM_TYPE wmbeg, INMOST_DATA_ENUM_TYPE wmend,
									std::string file_name)
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
85
86
87
88
		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
89
90
91
		for (INMOST_DATA_ENUM_TYPE k = wmbeg; k < wmend; ++k) 
		{
			nnz += Address[k].Size();
Kirill Terekhov's avatar
Kirill Terekhov committed
92
			vrowmax = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
93

Kirill Terekhov's avatar
Kirill Terekhov committed
94
95
			bool diag_found = false;
			diag = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
96
97
98
99
			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
100
101
102
103
104
105
106
107
108
109
110
				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
111
112
				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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
				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
128
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
129
130
131
			else nodiag++;

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

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

Kirill Terekhov's avatar
Kirill Terekhov committed
136
137
138
139
140
141
		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
142
143
144
145
146
147
148
149
150
151
152
153
		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
154
155
156
157
158
159
160
161
162
163
		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();
	}
164
	void MTILUC_preconditioner::CheckOrder(interval<INMOST_DATA_ENUM_TYPE, Interval> & Address, 
165
					 std::vector<Sparse::Row::entry> & Entries, 
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
					 INMOST_DATA_ENUM_TYPE rbeg, INMOST_DATA_ENUM_TYPE rend)
	{
		INMOST_DATA_ENUM_TYPE i,r;
		for (i = rbeg; i < rend; i++) if( Address[i].first != Address[i].last )
		{
			//check ordered on entry
			bool ordered = true;
			for (r = Address[i].first; r < Address[i].last-1; r++)
			{
				if( !(Entries[r].first < Entries[r+1].first) )
				{
					ordered = false;
					break;
				}
			}
			if( !ordered )
			{
				std::cout << "Row " << i << " not ordered: " << std::endl;
				std::cout << "Interval: " << Address[i].first << ":" << Address[i].last << std::endl;
				for (r = Address[i].first; r < Address[i].last; r++)
				{
					std::cout << "(" << Entries[r].first << "," << Entries[r].second << ") ";
				}
				std::cout << std::endl;
				throw -1;
			}
			bool nan = false;
			for (r = Address[i].first; r < Address[i].last; r++)
			{
				if( Entries[r].second != Entries[r].second )
				{
					nan = true;
					break;
				}
			}
			if( nan )
			{
				std::cout << "Row " << i << " contains nan: " << std::endl;
				std::cout << "Interval: " << Address[i].first << ":" << Address[i].last << std::endl;
				for (r = Address[i].first; r < Address[i].last; r++)
				{
					std::cout << "(" << Entries[r].first << "," << Entries[r].second << ") ";
				}
				std::cout << std::endl;
				throw -1;
			}
		}
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
214
	void MTILUC_preconditioner::SwapEntries(interval<INMOST_DATA_ENUM_TYPE, Interval> & Address, 
215
					 std::vector<Sparse::Row::entry> & Entries, 
Kirill Terekhov's avatar
Kirill Terekhov committed
216
217
218
219
220
					 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;
221
222
		assert(k < j);
		for (i = rbeg; i < rend; i++) if( Address[i].last != Address[i].last )
Kirill Terekhov's avatar
Kirill Terekhov committed
223
224
		{
			INMOST_DATA_ENUM_TYPE pk = Address[i].last, pj = Address[i].last, r;
225
226
227
228
229
230
			//check ordered on entry
			for (r = Address[i].first; r < Address[i].last-1; r++)
			{
				assert(Entries[r].first < Entries[r+1].first);
			}
			
Kirill Terekhov's avatar
Kirill Terekhov committed
231
232
233
234
			for (r = Address[i].first; r < Address[i].last; r++)
			{
				if (Entries[r].first >= k) { pk = r; break; }
			}
235
			for (r = Address[i].first; r < Address[i].last; r++)
Kirill Terekhov's avatar
Kirill Terekhov committed
236
237
238
			{
				if (Entries[r].first >= j) { pj = r; break; }
			}
239
240
241
242
243
244
245
246
247
248
249
			bool found_k = pk < Address[i].last && Entries[pk].first == k;
			bool found_j = pj < Address[i].last && Entries[pj].first == j;
			
			if( found_k && found_j )
			{
				//just swap values, indices are intact
				u = Entries[pj].second;
				Entries[pj].second = Entries[pk].second;
				Entries[pk].second = u;
			}
			else if( found_k )
Kirill Terekhov's avatar
Kirill Terekhov committed
250
			{
251
252
253
254
255
256
257
258
259
				//we have to place value of k into position of j and give it an index of j
				//don't move at pj, since it's index is greater then j
				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;
				
				//check ordered
				for (r = Address[i].first; r < Address[i].last-1; r++)
Kirill Terekhov's avatar
Kirill Terekhov committed
260
				{
261
					assert(Entries[r].first < Entries[r+1].first);
Kirill Terekhov's avatar
Kirill Terekhov committed
262
				}
263
264
265
266
267
268
269
270
271
272
273
274
275
				
			}
			else if( found_j )
			{
				//we have to place value of j into position of k and give it an index of k
				// move at pk, since it's index is greater then k
				u = Entries[pj].second;
				for (r = pj; r > pk; r--) Entries[r] = Entries[r - 1];
				Entries[pk].first = k;
				Entries[pk].second = u;
				
				//check ordered
				for (r = Address[i].first; r < Address[i].last-1; r++)
Kirill Terekhov's avatar
Kirill Terekhov committed
276
				{
277
					assert(Entries[r].first < Entries[r+1].first);
Kirill Terekhov's avatar
Kirill Terekhov committed
278
279
				}
			}
280
281
282
283
284
285
			//check nan
			for (r = Address[i].first; r < Address[i].last; r++)
			{
				assert(Entries[r].second == Entries[r].second);
			}
			
Kirill Terekhov's avatar
Kirill Terekhov committed
286
287
		}
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
288
	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
289
290
291
292
293
294
295
296
297
298
	{
		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
299
	void MTILUC_preconditioner::inversePQ(INMOST_DATA_ENUM_TYPE wbeg,
Kirill Terekhov's avatar
Kirill Terekhov committed
300
301
302
303
304
305
306
307
308
309
310
				   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
311
	void MTILUC_preconditioner::applyPQ(INMOST_DATA_ENUM_TYPE wbeg,
Kirill Terekhov's avatar
Kirill Terekhov committed
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
				 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
332
	INMOST_DATA_ENUM_TYPE & MTILUC_preconditioner::EnumParameter(std::string name)
Kirill Terekhov's avatar
Kirill Terekhov committed
333
334
335
	{
		if (name == "scale_iters") return sciters;
		else if( name == "estimator" ) return estimator;
336
		else if( name == "verbosity" ) return verbosity;
Kirill Terekhov's avatar
Kirill Terekhov committed
337
338
		throw - 1;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
339
	INMOST_DATA_REAL_TYPE & MTILUC_preconditioner::RealParameter(std::string name)
Kirill Terekhov's avatar
Kirill Terekhov committed
340
341
342
	{
		if (name == "tau") return tau;
		else if( name == "tau2" ) return iluc2_tau;
Kirill Terekhov's avatar
Kirill Terekhov committed
343
344
    else if( name == "condition_number_L" ) return condestL;
    else if( name == "condition_number_U" ) return condestU;
Kirill Terekhov's avatar
Kirill Terekhov committed
345
346
		throw - 1;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
347
	void MTILUC_preconditioner::Copy(const Method * other)
Kirill Terekhov's avatar
Kirill Terekhov committed
348
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
349
		const MTILUC_preconditioner * b = dynamic_cast<const MTILUC_preconditioner *>(other);
Kirill Terekhov's avatar
Kirill Terekhov committed
350
351
352
353
354
355
		assert(b != NULL);
		tau = b->tau;
		Alink = b->Alink;
		info = b->info;
		sciters = b->sciters;
		eps = b->eps;
356
		verbosity = b->verbosity;
Kirill Terekhov's avatar
Kirill Terekhov committed
357
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
358
	MTILUC_preconditioner::MTILUC_preconditioner(const MTILUC_preconditioner & other) :Method(other)
Kirill Terekhov's avatar
Kirill Terekhov committed
359
360
361
	{
		Copy(&other);
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
362
	MTILUC_preconditioner & MTILUC_preconditioner::operator =(MTILUC_preconditioner const & other)
Kirill Terekhov's avatar
Kirill Terekhov committed
363
364
365
366
	{
		Copy(&other);
		return *this;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
367
	MTILUC_preconditioner::MTILUC_preconditioner(Solver::OrderInfo & info)
Kirill Terekhov's avatar
Kirill Terekhov committed
368
369
370
371
372
373
374
375
376
377
378
		:tau(DEFAULT_TAU),info(&info)
	{
		Alink = NULL;
		init = false;
		sciters = 8;
		eps = 1e-54;
#if defined(ESTIMATOR)
		estimator = 1;
#else
		estimator = 0;
#endif
379
		verbosity = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
380
381
382
		tau = 1.0e-3;
		iluc2_tau = tau*tau;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
383
384
385
	bool MTILUC_preconditioner::isInitialized() { return init; }
	bool MTILUC_preconditioner::isFinalized() { return !init; }
	bool MTILUC_preconditioner::Initialize()
Kirill Terekhov's avatar
Kirill Terekhov committed
386
387
388
	{

		const INMOST_DATA_REAL_TYPE subst = 1.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
389
		const INMOST_DATA_REAL_TYPE tol_modif = PIVOT_THRESHOLD_VALUE;
Kirill Terekhov's avatar
Kirill Terekhov committed
390
391
392
		const INMOST_DATA_ENUM_TYPE UNDEF = ENUMUNDEF, EOL = ENUMUNDEF - 1;

		
Kirill Terekhov's avatar
Kirill Terekhov committed
393
394
395
		INMOST_DATA_ENUM_TYPE wbeg = 0, wend = 0; //working interval
		INMOST_DATA_ENUM_TYPE mobeg = 0, moend = 0; // total interval
		INMOST_DATA_ENUM_TYPE vbeg = 0, vend = 0; // vector interval
Kirill Terekhov's avatar
Kirill Terekhov committed
396
		
Kirill Terekhov's avatar
Kirill Terekhov committed
397
398
399
		INMOST_DATA_ENUM_TYPE k = 0, i = 0, j = 0, Li = 0, Ui = 0, curr = 0, next = 0;
		INMOST_DATA_REAL_TYPE l = 0, u = 0,udiag = 0, abs_udiag = 0, max_diag = -1.0e+20, min_diag = 1.0e+20, mean_diag = 0;
		INMOST_DATA_ENUM_TYPE nzA = 0, nzLU = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
400
		Sparse::Vector DL, DR;
Kirill Terekhov's avatar
Kirill Terekhov committed
401
		info->GetOverlapRegion(info->GetRank(), mobeg, moend);
Kirill Terekhov's avatar
Kirill Terekhov committed
402
		info->GetVectorRegion(vbeg,vend);
Kirill Terekhov's avatar
Kirill Terekhov committed
403
404
		
		//prepare temporal array
Kirill Terekhov's avatar
Kirill Terekhov committed
405
406
		temp.set_interval_beg(vbeg);
		temp.set_interval_end(vend);
Kirill Terekhov's avatar
Kirill Terekhov committed
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
		//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);
423
424
		for(Sparse::Vector::iterator ri = DL.Begin(); ri != DL.End(); ++ri) *ri = 1.0;
		for(Sparse::Vector::iterator ri = DR.Begin(); ri != DR.End(); ++ri) *ri = 1.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
425
426
427
428
429

		

		for (k = mobeg; k != moend; k++) ddP[k] = ddQ[k] = k;
		// supplementary data for ddPQ reordering
430
		
Kirill Terekhov's avatar
Kirill Terekhov committed
431
		interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> invP(mobeg, moend), invQ(mobeg,moend), localP(mobeg, moend,ENUMUNDEF), localQ(mobeg,moend,ENUMUNDEF);		
432
		interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> Bstart(mobeg,moend);
Kirill Terekhov's avatar
Kirill Terekhov committed
433
434

		//supplimentary data structures for ILUC
Kirill Terekhov's avatar
Kirill Terekhov committed
435
		INMOST_DATA_ENUM_TYPE LU_Beg = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
436
437
438
439
440
441
442
443
		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
444
		std::vector<Sparse::Row::entry> A_Entries;
Kirill Terekhov's avatar
Kirill Terekhov committed
445
		//std::vector<INMOST_DATA_ENUM_TYPE> sort_indeces;
Kirill Terekhov's avatar
Kirill Terekhov committed
446
447
448
		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
449

Kirill Terekhov's avatar
Kirill Terekhov committed
450
		INMOST_DATA_REAL_TYPE NuU = 1, NuL = 1, NuD, NuU_max = 1.0, NuL_max = 1.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
451
#if defined(ESTIMATOR)
Kirill Terekhov's avatar
Kirill Terekhov committed
452
		//supplimentary data structures for condition estimates of L^{-1}, U^{-1}
Kirill Terekhov's avatar
Kirill Terekhov committed
453
		INMOST_DATA_REAL_TYPE mup = 0, mum = 0, smup = 0, smum = 0, NuL1 = 1, NuL2 = 1, NuU1 = 1, NuU2 = 1;
Kirill Terekhov's avatar
Kirill Terekhov committed
454
		INMOST_DATA_REAL_TYPE NuU1_old = 1, NuL1_old = 1, NuU2_old = 1, NuL2_old = 1;
Kirill Terekhov's avatar
Kirill Terekhov committed
455
		INMOST_DATA_REAL_TYPE NuU1_new = 1, NuL1_new = 1, vp = 0, vm = 0, v = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
456
#if defined(ESTIMATOR_REFINE)
Kirill Terekhov's avatar
Kirill Terekhov committed
457
458
		INMOST_DATA_ENUM_TYPE np = 0, nm = 0;
		INMOST_DATA_REAL_TYPE NuU2_new = 0, NuL2_new = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
459
460
461
462
463
#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)
Kirill Terekhov's avatar
Kirill Terekhov committed
464
		INMOST_DATA_REAL_TYPE NuU_tmp = 0, NuL_tmp = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
465
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
466
467
468
469
470
		//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
471
472
473
		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
474
		
Kirill Terekhov's avatar
Kirill Terekhov committed
475
476
		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);
Kirill Terekhov's avatar
Kirill Terekhov committed
477
		double tfactor = 0.0, tswap = 0.0, trescale = 0.0, treorder = 0.0, ttransversal = 0.0, treassamble = 0.0, ttotal, tt = 0.0, testimator = 0.0, tlocal = 0.0;
478
        (void) tswap; //can be unused for some defines
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
479
#if defined(REORDER_METIS_ND)
Kirill Terekhov's avatar
Kirill Terekhov committed
480
		double tmetisgraph = 0, tmetisnd = 0;
481
482
483
#endif
#if defined(REORDER_RCM)
		double trcmgraph = 0, trcmorder = 0;
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
484
485
#endif
        double tlfactor, tlrescale, tlreorder, tlreassamble;
Kirill Terekhov's avatar
Kirill Terekhov committed
486
487
488
489
490
491
492
		ttotal = Timer();

		
		//calculate number of nonzeros
		nzA = 0;
		for (k = mobeg; k < moend; ++k)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
493
			for (Sparse::Row::iterator r = (*Alink)[k].Begin(); r != (*Alink)[k].End(); ++r)
Kirill Terekhov's avatar
Kirill Terekhov committed
494
495
496
497
498
499
				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
500
		//LU_Entries.reserve(nzA*4);
Kirill Terekhov's avatar
Kirill Terekhov committed
501
502
503
504

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

Kirill Terekhov's avatar
Kirill Terekhov committed
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
#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
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
555
			//DumpMatrix(A_Address,A_Entries,mobeg,moend,"mondriaan.mtx");
Kirill Terekhov's avatar
Kirill Terekhov committed
556
557
558
559
560
561
562
563
564
565
566
567
568
			/*
			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
569
570
571
572
		j = 0;
		for (k = mobeg; k < moend; ++k)
		{
			A_Address[k].first = j;
Kirill Terekhov's avatar
Kirill Terekhov committed
573
			for (Sparse::Row::iterator r = (*Alink)[k].Begin(); r != (*Alink)[k].End(); ++r)
Kirill Terekhov's avatar
Kirill Terekhov committed
574
575
			{
				if (r->first >= mobeg && r->first < moend && fabs(r->second) > 0.0)
Kirill Terekhov's avatar
Kirill Terekhov committed
576
					A_Entries[j++] = Sparse::Row::make_entry(r->first, r->second);
Kirill Terekhov's avatar
Kirill Terekhov committed
577
578
			}
			A_Address[k].last = j;
Kirill Terekhov's avatar
Kirill Terekhov committed
579
			//assert(A_Address[k].Size() != 0); //singular matrix
Kirill Terekhov's avatar
Kirill Terekhov committed
580
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
581
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
582
		//DumpMatrix(A_Address, A_Entries, mobeg, moend, "A.mtx");
Kirill Terekhov's avatar
Kirill Terekhov committed
583
584
585

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

586
		//~ INMOST_DATA_ENUM_TYPE Sbeg;
587
		std::vector<INMOST_DATA_ENUM_TYPE> indices;
Kirill Terekhov's avatar
Kirill Terekhov committed
588
589
590
#if defined(ILUC2)
		INMOST_DATA_ENUM_TYPE nzLU2 = 0;
		INMOST_DATA_REAL_TYPE tau2 = iluc2_tau;
591
		std::vector<Sparse::Row::entry> LU2_Entries;
Kirill Terekhov's avatar
Kirill Terekhov committed
592
593
594
		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
595
		//LU2_Entries.reserve(nzA*4);
Kirill Terekhov's avatar
Kirill Terekhov committed
596
597
598
599
#else
		INMOST_DATA_REAL_TYPE tau2 = tau;
#endif

600
601
602
603
		for(k = mobeg; k < moend; ++k)
		{
			U_Address[k].first = U_Address[k].last = ENUMUNDEF;
			L_Address[k].first = L_Address[k].last = ENUMUNDEF;
604
#if defined(ILUC2)
605
606
			U2_Address[k].first = U2_Address[k].last = ENUMUNDEF;
			L2_Address[k].first = L2_Address[k].last = ENUMUNDEF;
607
#endif
608
609
		}

610
611
612

		if( verbosity > 1 ) 
			std::cout << "nonzeros in A " << nzA << std::endl;
613
		int swaps = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
614
615
616
617
618
		//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
619
620
			//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
621
622
623
			//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
624
/////////// MAXIMUM TRANSVERSE REORDERING /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Kirill Terekhov's avatar
Kirill Terekhov committed
625
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Kirill Terekhov's avatar
Kirill Terekhov committed
626
			{
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
627
				ttransversal = Timer();
Kirill Terekhov's avatar
Kirill Terekhov committed
628
629
630
631
632
633
634
635
636
				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
637
				//interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> & UpdateStack = Ulist;
Kirill Terekhov's avatar
Kirill Terekhov committed
638
639
640
641
642
				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);
				
643
				BinaryHeap Heap(&Dist[wbeg],wend-wbeg);
Kirill Terekhov's avatar
Kirill Terekhov committed
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
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////// 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;
Kirill Terekhov's avatar
Kirill Terekhov committed
676
						if( Cmax[i] == 0.0 || C_Entries[it] == 0.0 )
Kirill Terekhov's avatar
Kirill Terekhov committed
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
							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
747

Kirill Terekhov's avatar
Kirill Terekhov committed
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
779
780
781
782
783
784
785
				
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////// 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] )
								{
									Parent[Perm[Ui]] = Li;
									AugmentPosition[Ui] = Lit;
786
787
									if( Heap.Contains(Ui-wbeg) )
										Heap.DecreaseKey(Ui-wbeg,l);
Kirill Terekhov's avatar
Kirill Terekhov committed
788
									else 
789
										Heap.PushHeap(Ui-wbeg,l);
Kirill Terekhov's avatar
Kirill Terekhov committed
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
								}
							}
						}

						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];

						}
						Heap.Clear();
					}
				}
843
				
Kirill Terekhov's avatar
Kirill Terekhov committed
844
845
846
				//T = Timer();
				for (k = cbeg; k < cend; ++k)
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
847
848
849
850
851
852
853
854
					if( V[k] == std::numeric_limits<INMOST_DATA_REAL_TYPE>::max() ) l = 1;
					else l = exp(V[k]);
					if( U[k] == std::numeric_limits<INMOST_DATA_REAL_TYPE>::max() || Cmax[k] == 0 ) u = 1;
					else u = exp(U[k])/Cmax[k];
					//if( l != l || fabs(l) < 1.0e-12 || isnan(l) || isinf(l) ) std::cout << "k " << k << " l is " << l << " V " << V[k] << std::endl;
					//if( u != u || fabs(u) < 1.0e-12 || isnan(u) || isinf(u) ) std::cout << "k " << k << " u is " << u << " U " << U[k] << " Cmax " << Cmax[k] << std::endl;
					DL[k] = l;
					DR[k] = u;
Kirill Terekhov's avatar
Kirill Terekhov committed
855
					
Kirill Terekhov's avatar
Kirill Terekhov committed
856
857
					
					bool flip_sign = false;
Kirill Terekhov's avatar
Kirill Terekhov committed
858
859
860
861
					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];
Kirill Terekhov's avatar
Kirill Terekhov committed
862
863
						if( U[i] == std::numeric_limits<INMOST_DATA_REAL_TYPE>::max() || Cmax[i] == 0 ) u = 1;
						else u = exp(U[i])/Cmax[i];
Kirill Terekhov's avatar
Kirill Terekhov committed
864
865
						if( j == k && (l*A_Entries[jt].second*u) < 0.0 ) flip_sign = true;
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
866
867
					
					if( flip_sign ) DL[k] *= -1;
Kirill Terekhov's avatar
Kirill Terekhov committed
868
				}
869
				
Kirill Terekhov's avatar
Kirill Terekhov committed
870
871
872
873
				//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);
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
                
                { //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
894
895
896
				
				//exit(-1);

Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
897
				ttransversal = Timer() - ttransversal;
Kirill Terekhov's avatar
Kirill Terekhov committed
898

Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
899
				treorder += ttransversal;
Kirill Terekhov's avatar
Kirill Terekhov committed
900
901
902
903
			}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////// END MAXIMUM TRANSVERSE REORDERING /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
904
			tt = Timer();
Kirill Terekhov's avatar
Kirill Terekhov committed
905
906
#if defined(REORDER_DDPQ)

907
908
			if( verbosity > 1 )
				std::cout << level_size.size() << " calculate weights" << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
			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
952
953
954
			
			//first select those elements that are largest over row and column simultaneously
			sort_wgts.clear();
Kirill Terekhov's avatar
Kirill Terekhov committed
955
956
957
958
959
960
			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
961
					//sort_wgts.push_back(wgt_coord(temp[k], coord(k, Blist[k])));
Kirill Terekhov's avatar
Kirill Terekhov committed
962
963
					//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
964
					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
965
966
967
968
				}
			}
			//if(reorder_nnz) 
				std::sort(sort_wgts.rbegin(), sort_wgts.rend());
969
970
971
			if( verbosity > 1 )
				std::cout << level_size.size() << " fill reordering" << std::endl;

Kirill Terekhov's avatar
Kirill Terekhov committed
972
973
974
975
976
977
978
979
980
981
982
			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
983
				if( it-sort_wgts.begin() > 0.01*(moend-mobeg) ) break; //5% of all values
Kirill Terekhov's avatar
Kirill Terekhov committed
984
985
986
			}
			cend = i;
#elif defined(REORDER_METIS_ND)
Kirill Terekhov's avatar
Kirill Terekhov committed
987
			tt = Timer();
Kirill Terekhov's avatar
Kirill Terekhov committed
988
			idx_t nvtxs = wend-wbeg;
Kirill Terekhov's avatar
Kirill Terekhov committed
989
990
991
			std::vector<idx_t> xadj(nvtxs+1), adjncy, perm(nvtxs),iperm(nvtxs);
			adjncy.reserve(nzA*2);
			
992
			for (k = cbeg; k < cend; ++k) if( localQ[k] == ENUMUNDEF ) std::cout << __FILE__ << ":" << __LINE__ << " No column permutation for row " << k << ". Matrix is structurally singular\n";
Kirill Terekhov's avatar
Kirill Terekhov committed
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014

			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
1015
1016
1017
1018
1019

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

Kirill Terekhov's avatar
Kirill Terekhov committed
1022
1023
			for (k = wend; k > wbeg; --k)
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1024
				//vwgt[k-1] = 1;
Kirill Terekhov's avatar
Kirill Terekhov committed
1025
1026
1027
				if (A_Address[k-1].Size() > 0)
				{
					i = A_Entries[A_Address[k-1].first].first;
Kirill Terekhov's avatar
Kirill Terekhov committed
1028
1029
					Blist[k-1] = Bbeg[i];
					Bbeg[i] = k-1;
Kirill Terekhov's avatar
Kirill Terekhov committed
1030
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
1031
				Ulist[k-1] = A_Address[k-1].first;
Kirill Terekhov's avatar
Kirill Terekhov committed
1032
1033
			}
			
Kirill Terekhov's avatar
Kirill Terekhov committed
1034
1035

			xadj[0] = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
1036
1037
1038
1039
			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
1040
1041
					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
1042
1043
				}
				
Kirill Terekhov's avatar
Kirill Terekhov committed
1044
				Li = Bbeg[i];
Kirill Terekhov's avatar
Kirill Terekhov committed
1045
1046
				while (Li != EOL)
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
1047
1048
1049
					if( Li != i )
						adjncy.push_back(static_cast<idx_t>(Li-wbeg));
					Li = Blist[Li];
Kirill Terekhov's avatar
Kirill Terekhov committed
1050
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
1051
1052
				
				Li = Bbeg[i];
Kirill Terekhov's avatar
Kirill Terekhov committed
1053
1054
				while (Li != EOL)
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
1055
1056
1057
					Ui = Blist[Li];
					Ulist[Li]++;
					if (A_Address[Li].last - Ulist[Li] > 0)
Kirill Terekhov's avatar
Kirill Terekhov committed
1058
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1059
1060
1061
						k = A_Entries[Ulist[Li]].first;
						Blist[Li] = Bbeg[k];
						Bbeg[k] = Li;
Kirill Terekhov's avatar
Kirill Terekhov committed
1062
1063
1064
1065
1066
					}

					Li = Ui;
				}
				
Kirill Terekhov's avatar
Kirill Terekhov committed
1067
1068
1069
1070
1071
1072
1073
1074
1075
				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;
1076
			
Kirill Terekhov's avatar
Kirill Terekhov committed
1077
1078
1079
1080
1081
1082
			//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
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
			
			//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;
			//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;
Kirill Terekhov's avatar
Kirill Terekhov committed
1096
1097
			for(k = wbeg; k < wend; ++k)
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1098
1099
1100
1101
				//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
1102
1103
1104
			}
			cend = wend;
			i = wend;
Kirill Terekhov's avatar
Kirill Terekhov committed
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

#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
1139
                sort_wgts.push_back(wgt_coord(Ulist[k]+Llist[Blist[k]]-1, coord(k, Blist[k])));
Kirill Terekhov's avatar
Kirill Terekhov committed
1140
1141
1142
				//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
1143
				//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
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
			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
1154
#elif defined(REORDER_RCM)
1155
1156
1157
1158
1159
			{
				//create a symmetric graph of the matrix A + A^T
				std::vector<INMOST_DATA_ENUM_TYPE> xadj(wend-wbeg+1), adjncy;
				adjncy.reserve(nzA*2);

1160
				for (k = cbeg; k < cend; ++k) if( localQ[k] == ENUMUNDEF ) std::cout << __FILE__ << ":" << __LINE__ << " No column permutation for row " << k << ". Matrix is structurally singular\n";
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
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236

				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);

				trcmgraph = Timer();

				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;
						Blist[k-1] = Bbeg[i];
						Bbeg[i] = k-1;
					}
					Ulist[k-1] = A_Address[k-1].first;
				}
				xadj[0] = 0;
				for(i = wbeg; i < wend; ++i)
				{
					for (INMOST_DATA_ENUM_TYPE jt = A_Address[i].first; jt != A_Address[i].last; ++jt)
					{
						if( A_Entries[jt].first != i )
							adjncy.push_back(static_cast<INMOST_DATA_ENUM_TYPE>(A_Entries[jt].first));
					}

					Li = Bbeg[i];
					while (Li != EOL)
					{
						if( Li != i )
							adjncy.push_back(static_cast<INMOST_DATA_ENUM_TYPE>(Li));
						Li = Blist[Li];
					}

					Li = Bbeg[i];
					while (Li != EOL)
					{
						Ui = Blist[Li];
						Ulist[Li]++;
						if (A_Address[Li].last - Ulist[Li] > 0)
						{
							k = A_Entries[Ulist[Li]].first;
							Blist[Li] = Bbeg[k];
							Bbeg[k] = Li;
						}

						Li = Ui;
					}

					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<INMOST_DATA_ENUM_TYPE>(adjncy.size());
				}

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

				trcmgraph = Timer()-trcmgraph;

				trcmorder = Timer();
				std::fill(Ulist.begin() + wbeg - mobeg, Ulist.begin() + wend - mobeg, ENUMUNDEF);
				//find node with the lowest order
1237
				//INMOST_DATA_ENUM_TYPE start = wbeg;
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
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
				INMOST_DATA_ENUM_TYPE index = wbeg;
				INMOST_DATA_ENUM_TYPE cur = ENUMUNDEF;
				std::deque<INMOST_DATA_ENUM_TYPE> q;
				std::vector<INMOST_DATA_ENUM_TYPE> conns;
				//for(k = wbeg+1; k < wend; ++k)
				//    if( RCM_Comparator(wbeg,xadj)(k,start) )
				//        start = k;
				//Ulist[start] = index++;
				do
				{
					cur = ENUMUNDEF;
					for(k = wbeg; k < wend && cur == ENUMUNDEF; ++k)
					{
						if( Ulist[k] == ENUMUNDEF )
							cur = k;
					}
					assert(cur != ENUMUNDEF);
					for(k = cur+1; k < wend; ++k) if( Ulist[k] == ENUMUNDEF )
					{
						if( RCM_Comparator(wbeg,xadj)(k,cur) )
							cur = k;
					}
					q.push_back(cur);
					Ulist[cur] = index++;
					while(!q.empty())
					{
						cur = q.front();
						q.pop_front();
						for (INMOST_DATA_ENUM_TYPE it = xadj[cur-wbeg]; it < xadj[cur-wbeg+1]; ++it)
							if( Ulist[adjncy[it]] == ENUMUNDEF )
								conns.push_back(adjncy[it]);
						std::sort(conns.begin(),conns.end(),RCM_Comparator(wbeg,xadj));
						for (k = 0; k < static_cast<INMOST_DATA_ENUM_TYPE>(conns.size()); ++k)
						{
							Ulist[conns[k]] = index++;
							q.push_back(conns[k]);
						}
						conns.clear();
					}
				}
				while( index < wend );

				for(k = wbeg; k < wend; ++k)
					Ulist[k] = wend-(Ulist[k]-wbeg)-1;

				for(k = wbeg; k < wend; ++k)
				{
					localP[k] = Ulist[k];//wend - Ulist[k] + 1;
					localQ[k] = Ulist[k];//wend - Ulist[k] + 1;
					//localP[Ulist[k]] = k;
					//localQ[Ulist[k]] = k;
				}
				cend = wend;
				i = wend;


				trcmorder = Timer() - trcmorder;
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1296
1297
1298
1299
#else
			cend = wend;
			i = wbeg;
#endif
1300
1301
			tt = Timer() - tt;
			treorder += tt;
Kirill Terekhov's avatar
Kirill Terekhov committed
1302
1303
1304
1305
1306
1307
1308
1309
			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)
1310
1311
1312
1313
1314
1315
1316
			if( verbosity > 1 )
			{
				//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;
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1317
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
1318
			tt = Timer();
Kirill Terekhov's avatar
Kirill Terekhov committed
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
			//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
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343

			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
1344
1345
1346
1347
1348
1349
			tlreorder = Timer() - tt;
			treorder += tlreorder;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////// REASSAMBLE             ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			tt = Timer();
Kirill Terekhov's avatar
Kirill Terekhov committed
1350
			double tt1, tt2,ttt;
Kirill Terekhov's avatar
Kirill Terekhov committed
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
			//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
1368
					B_Entries.push_back(Sparse::Row::make_entry(i, u));
Kirill Terekhov's avatar
Kirill Terekhov committed
1369
1370
1371
1372
1373
				}
				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());