main.cpp 13.7 KB
Newer Older
Kirill Terekhov's avatar
Kirill Terekhov committed
1
2
3
//g++ main.cpp rotate.cpp -L/usr/X11R6/lib -lX11 -lXi -lXmu -lGL -lglut -lGLU ../../inmost.a -O5
// press space - explode mesh to see connection 

Kirill Terekhov's avatar
Kirill Terekhov committed
4
#define _CRT_SECURE_NO_WARNINGS
Kirill Terekhov's avatar
Kirill Terekhov committed
5
#include "inmost.h"
Kirill Terekhov's avatar
Kirill Terekhov committed
6
7
8
9
10
11
12
13
14
15
#include "my_glut.h"
#include <iostream>
#include <sstream>
#include <algorithm>
#include <deque>
#include <stdarg.h>


using namespace INMOST;
int width = 1000, height = 1000;
16
Sparse::Matrix * m = NULL;
Kirill Terekhov's avatar
Kirill Terekhov committed
17
18
19
20
21
22

int zoom = 200;
int block_size = 0;

//~ Storage::real min = 1e20,max = -1e20;

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
void printtext(const char * fmt, ...)
{

	unsigned int i;
	char stext[1024];
	va_list ap;
	if (fmt == NULL) return;
	va_start(ap, fmt);
	vsprintf(stext, fmt, ap);
	va_end(ap);
	for (i = 0; i<strlen(stext); i++)
	{
		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10,
			stext[i]);
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
38
39
}

40
41
class Input
{
Kirill Terekhov's avatar
Kirill Terekhov committed
42
public:
43
	enum InputType { Double, Integer };
Kirill Terekhov's avatar
Kirill Terekhov committed
44
private:
45
46
47
48
	std::string str;
	void * input_link;
	InputType type;
	bool done;
Kirill Terekhov's avatar
Kirill Terekhov committed
49
50
51


public:
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
	Input(int * val) { input_link = val; type = Integer; done = false; str = ""; }
	Input(double * val) { input_link = val; type = Double; done = false; str = ""; }
	Input(void * link, InputType type) : input_link(link), type(type) { done = false; str = ""; }
	Input(const Input & other) :input_link(other.input_link), str(other.str), type(other.type), done(other.done) {}
	Input & operator =(Input const & other) { input_link = other.input_link; str = other.str; type = other.type; done = other.done; return *this; }
	~Input() {}
	void KeyPress(char c)
	{
		if (c == 13)
		{

			done = true;
			if (type == Double) *((double *)input_link) = atof(str.c_str());
			else if (type == Integer) *((int *)input_link) = atoi(str.c_str());
			glutPostRedisplay();
		}
		else if (c == 8)
		{
			if (!str.empty()) str.erase(str.size() - 1);
			glutPostRedisplay();
		}
		else if ((c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.' || c == 'e' || c == 'E')
		{
			str += c;
			glutPostRedisplay();
		}
		else if (c == 27)
		{

			done = true;
			glutPostRedisplay();
		}
	}
	bool Done() { return done; }
	void Draw()
	{
		float h = 24.0f / (float)height;

		glColor3f(1, 1, 1);
		glBegin(GL_QUADS);
		glVertex3f(-0.99, -0.99, 1);
		glVertex3f(-0.99, -0.99 + h, 1);
		glVertex3f(0.99, -0.99 + h, 1);
		glVertex3f(0.99, -0.99, 1);
		glEnd();
		glColor3f(0, 0, 0);
		glBegin(GL_LINE_LOOP);
		glVertex3f(-0.99, -0.99, 1);
		glVertex3f(-0.99, -0.99 + h, 1);
		glVertex3f(0.99, -0.99 + h, 1);
		glVertex3f(0.99, -0.99, 1);
		glEnd();

		glColor4f(0, 0, 0, 1);
		glRasterPos2d(-0.985, -0.985);
		//printtext(str.c_str());
		printtext("input number (%s): %s", type == Integer ? "integer" : "double", str.c_str());
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
110
111
112
} *CommonInput = NULL;


113
114
class Reorder_ddPQ
{
Kirill Terekhov's avatar
Kirill Terekhov committed
115
116
117
118

public:
};

119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
class Reorder_ARMS
{
  interval<INMOST_DATA_ENUM_TYPE, bool> markers;
	interval<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> reorder;
	interval<INMOST_DATA_ENUM_TYPE, std::vector< INMOST_DATA_ENUM_TYPE> > neighbours;
	std::vector< std::pair<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE> > node_neighbours;
	std::vector< INMOST_DATA_ENUM_TYPE > block_entries;
	std::vector< INMOST_DATA_ENUM_TYPE > block_ends;
	std::deque<INMOST_DATA_ENUM_TYPE> stack;
	int nblocks, maxblock, sblock;
	INMOST_DATA_ENUM_TYPE mbeg, mend, visited, select;
	Sparse::Matrix & A;
	void add_block_entry(INMOST_DATA_ENUM_TYPE ind)
	{
		markers[ind] = true;
		block_entries.push_back(ind);
		reorder[ind] = visited++;
		stack.push_back(ind);
	}
	void force_finalize_block()
	{
		for (INMOST_DATA_ENUM_TYPE j = 0; j < block_entries.size(); j++)
		{
			for (INMOST_DATA_ENUM_TYPE k = 0; k < neighbours[block_entries[j]].size(); ++k)
				markers[neighbours[block_entries[j]][k]] = true;
		}
		sblock += static_cast<int>(block_entries.size());
		block_ends.push_back(sblock);
		nblocks++;
		stack.clear();
		block_entries.clear();
	}
	bool try_finalize_block()
	{
		if (block_entries.size() >= maxblock)
		{

			/*INMOST_DATA_ENUM_TYPE hide = 0;
			for (INMOST_DATA_ENUM_TYPE j = 0; j < block_entries.size(); j++)
			{
				for (INMOST_DATA_ENUM_TYPE k = 0; k < neighbours[block_entries[j]].size(); ++k)
					if (!A[neighbours[block_entries[j]][k]].GetMarker())
						hide++;
			}
			if (hide < block_entries.size() / 2)*/
			{
				force_finalize_block();
				return true;
			}
		}
		return false;
	}
	INMOST_DATA_ENUM_TYPE GetOrder(INMOST_DATA_ENUM_TYPE ind)
	{
		return static_cast<INMOST_DATA_ENUM_TYPE>(neighbours[ind].size());
		INMOST_DATA_ENUM_TYPE ret = 0, k;
		for (k = 0; k < neighbours[ind].size(); k++)
		if (!markers[neighbours[ind][k]])
			ret++;
		return ret;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
180
public:
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
	INMOST_DATA_ENUM_TYPE GetBlockNumber()
	{
		return nblocks;
	}
	INMOST_DATA_ENUM_TYPE GetMatrixPartSize()
	{
		return sblock;
	}
	void clear()
	{
		for (INMOST_DATA_ENUM_TYPE k = mbeg; k < mend; k++)
			reorder[k] = k;
		sblock = 0;
		nblocks = 0;
	}
	Reorder_ARMS(Sparse::Matrix * m, INMOST_DATA_ENUM_TYPE _mbeg, INMOST_DATA_ENUM_TYPE _mend) : A(*m)
	{
		nblocks = 0;
		maxblock = 120;
		sblock = 0;
		mbeg = _mbeg;
		mend = _mend;
		//m->GetInterval(mbeg, mend);
		reorder.set_interval_beg(mbeg);
		reorder.set_interval_end(mend);
		neighbours.set_interval_beg(mbeg);
		neighbours.set_interval_end(mend);
    markers.set_interval_beg(mbeg);
    markers.set_interval_end(mend);

		for (INMOST_DATA_ENUM_TYPE k = mbeg; k < mend; ++k)
		{
			for (Sparse::Row::iterator it = A[k].Begin(); it != A[k].End(); ++it)
			if ( it->first >= mbeg && it->first < mend)
				neighbours[it->first].push_back(k);
		}
		for (INMOST_DATA_ENUM_TYPE k = mbeg; k < mend; ++k)
		{
			for (Sparse::Row::iterator it = A[k].Begin(); it != A[k].End(); ++it)
			if (it->first >= mbeg && it->first < mend)
				neighbours[k].push_back(it->first);

			std::sort(neighbours[k].begin(), neighbours[k].end());
			neighbours[k].resize(std::unique(neighbours[k].begin(), neighbours[k].end()) - neighbours[k].begin());
		}
		clear();
	}
	INMOST_DATA_ENUM_TYPE position(INMOST_DATA_ENUM_TYPE index) { return reorder[index]; }
	void compute(int _maxblock)
	{
		//maxblock = (mend - mbeg) * 0.85 / 4;//_maxblock;

		maxblock = _maxblock;
		std::cout << "Block size: " << maxblock << std::endl;
		sblock = 0;
		nblocks = 0;
		std::fill(reorder.begin(), reorder.end(), ENUMUNDEF);
		visited = mbeg;
		node_neighbours.clear();
		stack.clear();
		block_ends.clear();
		block_entries.clear();
		block_ends.push_back(0);
		while (true)
		{
			select = ENUMUNDEF;
			for (INMOST_DATA_ENUM_TYPE k = mbeg; k < mend; ++k)
			{
				if (!markers[k])
				{
					if (select == ENUMUNDEF || GetOrder(k) < GetOrder(select))
						select = k;
				}
			}
			if (select == ENUMUNDEF) break;

			add_block_entry(select);
			try_finalize_block();

			while (!stack.empty())
			{
				if (visited % 50 == 0)
				{
					std::cout << visited * 100.0 / (mend - mbeg) << "% " << nblocks << " blocks\r";
					std::cout.flush();
				}
				select = stack.front();
				stack.pop_front();
				for (INMOST_DATA_ENUM_TYPE k = 0; k != neighbours[select].size(); ++k)
				{
					INMOST_DATA_ENUM_TYPE j = neighbours[select][k];
					if (!markers[j])
					{
						node_neighbours.push_back(std::pair<INMOST_DATA_ENUM_TYPE, INMOST_DATA_ENUM_TYPE>(GetOrder(j), j));
						//A[j].SetMarker();
					}
				}
				//std::sort(node_neighbours.rbegin(), node_neighbours.rend());
				std::sort(node_neighbours.begin(), node_neighbours.end());
				for (INMOST_DATA_ENUM_TYPE k = 0; k < node_neighbours.size(); ++k)
				{
					add_block_entry(node_neighbours[k].second);
					if (try_finalize_block()) break;
				}
				node_neighbours.clear();
			}
		}
		if (block_entries.size() > maxblock * 0.5)
			force_finalize_block();
		std::cout << "Number of blocks: " << nblocks << std::endl;
		std::cout << "Restructured matrix size: " << sblock << std::endl;
		std::cout << "Left untouched: " << mend - mbeg - visited << std::endl;
		for (INMOST_DATA_ENUM_TYPE k = mbeg; k < mend; ++k)
		{
			markers[k] = false;
			if (reorder[k] == ENUMUNDEF)
				reorder[k] = visited++;
		}
		std::cout << "Completed ordering: " << visited << std::endl;
		//calculate average density
		{
			INMOST_DATA_REAL_TYPE nnzall = 0.0;
			std::vector<INMOST_DATA_REAL_TYPE> nnz(nblocks,0.0);
			for (INMOST_DATA_ENUM_TYPE i = mbeg; i < mend; i++)
			for (Sparse::Row::iterator r = A[i].Begin(); r != A[i].End(); ++r)
			{
				INMOST_DATA_ENUM_TYPE ki = position(i), kj = position(r->first), p;
				std::vector<INMOST_DATA_ENUM_TYPE>::iterator qi = std::lower_bound(block_ends.begin(), block_ends.end(), ki);
				std::vector<INMOST_DATA_ENUM_TYPE>::iterator qj = std::lower_bound(block_ends.begin(), block_ends.end(), kj);
				if (qi == qj && qi != block_ends.end())
				{
					p = qi - block_ends.begin();
					if (p < static_cast<unsigned>(nblocks))
						nnz[p] += 1.0;
				}
			}
			for (INMOST_DATA_ENUM_TYPE k = 0; k < static_cast<unsigned>(nblocks); ++k)
			{
				nnz[k] /= static_cast<INMOST_DATA_REAL_TYPE>((block_ends[k + 1] - block_ends[k])*(block_ends[k + 1] - block_ends[k]));
				nnzall += nnz[k];
			}
			std::cout << "average block density: " << nnzall / static_cast<INMOST_DATA_REAL_TYPE>(nblocks) << std::endl;
		}
	}
} * ord;


void set_output_matrix()
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0, m->Size(),
		0, m->Size(),
		-1, 1);
	glMatrixMode(GL_MODELVIEW);
Kirill Terekhov's avatar
Kirill Terekhov committed
336
337
}

338
339
340
341
342
343
void set_output_uid()
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-1, 1, -1, 1, -1, 1);
	glMatrixMode(GL_MODELVIEW);
Kirill Terekhov's avatar
Kirill Terekhov committed
344
345
}

346
347
348
349
350
351
352
void reshape(int w, int h)
{
	if( m != NULL )
	{
		set_output_matrix();
		glViewport(0, 0, w, h);
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
353
354
355
}


356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387

void keyboard(unsigned char key, int x, int y)
{
	if (CommonInput != NULL)
	{
		CommonInput->KeyPress(key);
		return;
	}
	if( key == 27 )
	{
		delete m;
		exit(-1);
	}
	if( key == '+' || key == '=' )
	{
		zoom++;

	}
	if( key == '-' )
	{
		if( zoom > 1 )
		{
			zoom--;

		}
	}
	if (key == 'c') ord->clear();
	if (key == 'r')
	{
		CommonInput = new Input(&block_size);
	}
	glutPostRedisplay();
Kirill Terekhov's avatar
Kirill Terekhov committed
388
389
390
391
}

void DrawEntry(int i, int j)//, Storage::real r)
{
392
393
394
395
396
	//~ glColor3f(r,0.0,1.0-r);
	glVertex2i(i-(zoom-1),j-(zoom-1)-1);
	glVertex2i(i+zoom,j-(zoom-1)-1);
	glVertex2i(i+zoom,j+zoom-1);
	glVertex2i(i-(zoom-1),j+zoom-1);
Kirill Terekhov's avatar
Kirill Terekhov committed
397
398
399
}


400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
interval<int,double> row_sum;

void draw()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	set_output_matrix();

	/*
	glColor3f(0, 1, 1);
	glBegin(GL_LINES);
	glVertex2i(ord->GetMatrixPartSize(), 0);
	glVertex2i(ord->GetMatrixPartSize(), m->Size());

	glVertex2i(0, m->Size() - ord->GetMatrixPartSize());
	glVertex2i(m->Size(), m->Size() - ord->GetMatrixPartSize());
	glEnd();
	*/

	glColor3f(0,0,0);
	glBegin(GL_QUADS);
	for(Sparse::Matrix::iterator it = m->Begin(); it != m->End(); ++it)
		for(Sparse::Row::iterator jt = it->Begin(); jt != it->End(); ++jt)
			if( jt->first != it - m->Begin() )
			//DrawEntry(ord->position((it - m->Begin())), m->Size() - ord->position(jt->first));//, sqrt((jt->second-min)/(max-min)));
			DrawEntry((it - m->Begin()), m->Size() - jt->first);//, sqrt((jt->second-min)/(max-min)));
	glEnd();


	/*
	glColor3f(0.0, 1.0, 0);
	glBegin(GL_QUADS);
	for (Sparse::Matrix::iterator it = m->Begin(); it != m->End(); ++it)
	{
		int ind = it - m->Begin();
		if (fabs((*it)[ind]) > row_sum[it-m->Begin()]) //DrawEntry(ord->position((it - m->Begin())), m->Size() - ord->position(ind));
			DrawEntry((it - m->Begin()), m->Size() - ind);
	}
	glEnd();
	*/


	glBegin(GL_QUADS);
	for (Sparse::Matrix::iterator it = m->Begin(); it != m->End(); ++it)
	{

		int ind = it - m->Begin();
		double t = fabs((*it)[ind]) / row_sum[ind];
		//if (fabs((*it)[ind]) < row_sum[ind]) //DrawEntry(ord->position((it - m->Begin())), m->Size() - ord->position(ind));
		glColor3f(0.0, t, 1.0-t);
			DrawEntry(ind, m->Size() - ind);
	}
	glEnd();

	zoom += 1;
	glColor3f(1.0, 0, 0);
	glBegin(GL_QUADS);
	for (Sparse::Matrix::iterator it = m->Begin(); it != m->End(); ++it)
	{
		int ind = it - m->Begin();
		if (fabs((*it)[ind]) < 1e-9) //DrawEntry(ord->position((it - m->Begin())), m->Size() - ord->position(ind));
			DrawEntry((it - m->Begin()), m->Size() - ind);
	}
	glEnd();
	zoom -= 1;

	if (CommonInput != NULL)
	{
		glDisable(GL_DEPTH_TEST);
		glLoadIdentity();
		set_output_uid();
		CommonInput->Draw();
		if (CommonInput->Done())
		{
			delete CommonInput;
			CommonInput = NULL;
			ord->compute(block_size);
			glutPostRedisplay();
		}
		glEnable(GL_DEPTH_TEST);
	}

	glutSwapBuffers();
Kirill Terekhov's avatar
Kirill Terekhov committed
483
484
485
}


486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554

int main(int argc, char ** argv)
{
	if( argc < 2 )
	{
		std::cout << "usage: " << argv[0] << " matrix.mtx " << std::endl;
		return -1;
	}
	m = new Sparse::Matrix();
	m->Load(argv[1]);
	//ord = new Reorder_ARMS(m,0,m->Size());
	std::cout << "Matrix size: " << m->Size() << std::endl;
	INMOST_DATA_ENUM_TYPE nnz = 0, nnzrow;

	row_sum.set_interval_beg(0);
	row_sum.set_interval_end(m->Size());
	std::fill(row_sum.begin(),row_sum.end(),0.0);

	for (Sparse::Matrix::iterator it = m->Begin(); it != m->End(); ++it)
	{
		nnz += it->Size();

		nnzrow = 0;
		for(Sparse::Row::iterator jt = it->Begin(); jt != it->End(); ++jt)
		{
			nnzrow++;
			row_sum[it-m->Begin()] += fabs(jt->second);
		}
		row_sum[it-m->Begin()] /= (double)nnzrow;
	}



	std::cout << "Nonzeros: " << nnz << std::endl;

	zoom = m->Size() / 1000;

	//~ for(Sparse::Matrix::iterator it = m->Begin(); it != m->End(); ++it)
		//~ for(Sparse::Row::iterator jt = it->Begin(); jt != it->End(); ++jt)
		//~ {
			//~ if(jt->second < min )
			//~ {
				//~ min = jt->second;
				//~ std::cout << (it-m->Begin()) << " " << jt->first << " " << jt->second << std::endl;
			//~ }
			//~ if(jt->second > max )
			//~ {
				//~ max = jt->second;
				//~ std::cout << (it-m->Begin()) << " " << jt->first << " " << jt->second << std::endl;
			//~ }
		//~ }
	//~ std::cout << "max: " << max << " min: " << min << std::endl;
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowSize(width, height);
	glutInitWindowPosition (5, 5);
	glutCreateWindow(argv[1]);



	glClearColor (1.0f, 1.0f, 1.0f, 1.f);
	glutDisplayFunc(draw);
	glutReshapeFunc(reshape);
	glutKeyboardFunc(keyboard);


	glutPostRedisplay();
	glutMainLoop();
}