partitioner.cpp 34.3 KB
Newer Older
Kirill Terekhov's avatar
Kirill Terekhov committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "inmost.h"



#if defined(USE_PARTITIONER)

#if defined(USE_PARTITIONER_ZOLTAN)
#include <zoltan.h>
#endif
#if defined(USE_PARTITIONER_PARMETIS)
#include <metis.h>
#include <parmetis.h>
#endif
#include <sstream>
#include <deque>
#define ZOLTAN_CHKERR(x) if( x != ZOLTAN_OK )  {throw ErrorInPartitioner;}


#if defined(USE_PARALLEL_WRITE_TIME)
#define REPORT_MPI(x) {m->WriteTab(m->GetStream()) << "<MPI><![CDATA[" << #x << "]]></MPI>" << std::endl; x;}
#define REPORT_STR(x) {m->WriteTab(m->GetStream()) << "<TEXT><![CDATA[" << x << "]]></TEXT>" << std::endl;}
#define REPORT_VAL(str,x) {m->WriteTab(m->GetStream()) << "<VALUE name=\"" << str << "\"> <CONTENT><![CDATA[" << x << "]]> </CONTENT><CODE><![CDATA[" << #x << "]]></CODE></VALUE>" << std::endl;}
#define ENTER_FUNC() long double all_time = Timer(); m->WriteTab(m->GetStream()) << "<FUNCTION name=\"" << __FUNCTION__ << "\" id=\"func" << m->GetFuncID()++ << "\">" << std::endl; m->Enter();
#define EXIT_FUNC() m->WriteTab(m->GetStream()) << "<TIME>" << Timer() - all_time << "</TIME>" << std::endl; m->Exit(); m->WriteTab(m->GetStream()) << "</FUNCTION>" << std::endl;
#else
#define REPORT_MPI(x) x
Kirill Terekhov's avatar
Kirill Terekhov committed
27
28
29
30
#define REPORT_STR(x) {}
#define REPORT_VAL(str,x) {}
#define ENTER_FUNC() {}
#define EXIT_FUNC() {}
Kirill Terekhov's avatar
Kirill Terekhov committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#endif

namespace INMOST
{
#if defined(USE_PARTITIONER_ZOLTAN)
	static int get_number_of_objects(void *data, int *ierr)
	{
		Partitioner * p = static_cast<Partitioner *>(data);
		Mesh * m = p->GetMesh();
		*ierr = ZOLTAN_OK;
		int num = 0;
		for(Mesh::iteratorCell it = m->BeginCell(); it != m->EndCell(); it++)
			if( it->GetStatus() != Element::Ghost ) num++;
		return num;
	}
	
	static void get_object_list(void *data, int sizeGID, int sizeLID,
								ZOLTAN_ID_PTR globalID, ZOLTAN_ID_PTR localID,
								int wgt_dim, float *obj_wgts, int *ierr)
	{
		Partitioner * p = static_cast<Partitioner *>(data);
		Mesh * m = p->GetMesh();
		*ierr = ZOLTAN_OK;
		int i = 0;	 
		for(Mesh::iteratorCell it = m->BeginCell(); it != m->EndCell(); it++)
			if( it->GetStatus() != Element::Ghost )
			{
				globalID[i] = it->GlobalID();
				localID[i] = i;
				for(int j = 0; j < wgt_dim; j++)
61
					 obj_wgts[i] = static_cast<float>(it->RealArray(p->GetWeight())[j]);
Kirill Terekhov's avatar
Kirill Terekhov committed
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
110
111
112
113
114
115
116
117
118
119
120
121
122
				i++;
			}
	}
	
	static int get_num_geometry(void *data, int *ierr)
	{
		Partitioner * p = static_cast<Partitioner *>(data);
		Mesh * m = p->GetMesh();
		*ierr = ZOLTAN_OK;
		return m->GetDimensions();
	}
	
	static void get_geometry_list(void *data, int sizeGID, int sizeLID,
								  int num_obj,
								  ZOLTAN_ID_PTR globalID, ZOLTAN_ID_PTR localID,
								  int num_dim, double *geom_vec, int *ierr)
	{
		Partitioner * p = static_cast<Partitioner *>(data);
		Mesh * m = p->GetMesh();
		int dim = m->GetDimensions();
		if ( (sizeGID != 1) || (sizeLID != 1) || (num_dim != dim))
		{
			*ierr = ZOLTAN_FATAL;
			return;
		}
		
		*ierr = ZOLTAN_OK;
		
		
		int i = 0;
		std::vector<Storage::real> coords(dim);
		for(Mesh::iteratorCell it = m->BeginCell(); it != m->EndCell(); it++)
			if( it->GetStatus() != Element::Ghost )
			{
				it->Centroid(&coords[0]);
				for(int j = 0; j < dim; j++)
					geom_vec[dim*i + j] = coords[j];
				i++;
			}
		return;
	}
	
	static void get_num_edges_list(void *data, int sizeGID, int sizeLID,
								   int num_obj,
								   ZOLTAN_ID_PTR globalID, ZOLTAN_ID_PTR localID,
								   int *numEdges, int *ierr)
	{
		Partitioner * p = static_cast<Partitioner *>(data);
		Mesh * m = p->GetMesh();
		
		if ( (sizeGID != 1) || (sizeLID != 1))
		{
			*ierr = ZOLTAN_FATAL;
			return;
		}
		
		int i = 0;	
		for(Mesh::iteratorCell it = m->BeginCell(); it != m->EndCell(); it++)
			if( it->GetStatus() != Element::Ghost )
			{
				numEdges[i] = 0;
123
124
				ElementArray<Face> faces = it->getFaces();
				for(ElementArray<Face>::iterator jt = faces.begin(); jt != faces.end(); jt++)
Kirill Terekhov's avatar
Kirill Terekhov committed
125
				{
126
127
					Cell n = it->Neighbour(jt->self());
					if( n.isValid() ) numEdges[i]++;
Kirill Terekhov's avatar
Kirill Terekhov committed
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
				}		
				i++;
			}
		
		*ierr = ZOLTAN_OK;
		return;
	}
	
	static void get_edge_list(void *data, int sizeGID, int sizeLID,
							  int num_obj, ZOLTAN_ID_PTR globalID, ZOLTAN_ID_PTR localID,
							  int *num_edges,
							  ZOLTAN_ID_PTR nborGID, int *nborProc,
							  int wgt_dim, float *ewgts, int *ierr)
	{
		int *nextProc;
		float * nextWgt;
		ZOLTAN_ID_TYPE *nextNbor;
		
		Partitioner * p = static_cast<Partitioner *>(data);
		Mesh * m = p->GetMesh();
		*ierr = ZOLTAN_OK;
		
		if ( (sizeGID != 1) || (sizeLID != 1))
		{
			*ierr = ZOLTAN_FATAL;
			return;
		}
		
		nextNbor = nborGID;
		nextProc = nborProc;
		nextWgt = ewgts;
		
		int i = 0;
		for(Mesh::iteratorCell it = m->BeginCell(); it != m->EndCell(); it++)
			if( it->GetStatus() != Element::Ghost )
			{
164
165
				ElementArray<Face> faces = it->getFaces();
				for(ElementArray<Face>::iterator jt = faces.begin(); jt != faces.end(); jt++)
Kirill Terekhov's avatar
Kirill Terekhov committed
166
				{
167
168
					Cell n = it->Neighbour(jt->self());
					if( n.isValid() )
Kirill Terekhov's avatar
Kirill Terekhov committed
169
170
171
172
					{
						*nextNbor++ = n->GlobalID();
						*nextProc++ = n->Integer(m->OwnerTag());
						for(int j = 0; j < wgt_dim; j++)
173
							*nextWgt++ = static_cast<float>(jt->RealArray(p->GetWeight())[j]);
Kirill Terekhov's avatar
Kirill Terekhov committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
					}
				}		
				i++;
			}
		return;
	}
#endif
	
	Partitioner::Partitioner(Mesh * _m)
	{
		m = _m;
		if( m->GetMeshState() == Mesh::Serial ) m->ResolveShared();
		pzz = NULL;
	}
	
	Partitioner::Partitioner(const Partitioner & other)
	{
#if defined(USE_PARTITIONER_ZOLTAN)
		if( pt == Zoltan_Parmetis || pt == Zoltan_Scotch || pt == Zoltan_PHG || pt == Zoltan_RCB || pt == Zoltan_RIB || pt == Zoltan_HSFC )
		{
			if( other.pzz != NULL ) pzz = static_cast<void *>(Zoltan_Copy(static_cast<struct Zoltan_Struct *>(other.pzz)));
			else pzz = NULL;
		}
#endif
		m = other.m;
		weight_tag = other.weight_tag;
	}
	
	Partitioner & Partitioner::operator = (Partitioner const & other)
	{
#if defined(USE_PARTITIONER_ZOLTAN)
		if( pt == Zoltan_Parmetis || pt == Zoltan_Scotch || pt == Zoltan_PHG || pt == Zoltan_RCB || pt == Zoltan_RIB || pt == Zoltan_HSFC )
		{
			if( other.pzz != NULL ) 
			{
				if( pzz == NULL ) pzz = static_cast<void *>(Zoltan_Copy(static_cast<struct Zoltan_Struct *>(other.pzz)));
				else Zoltan_Copy_To(static_cast<struct Zoltan_Struct *>(pzz),static_cast<struct Zoltan_Struct *>(other.pzz));
			}
			else pzz = NULL;
		}
#endif
		m = other.m;
		weight_tag = other.weight_tag;
		return *this;
	}
	
	Partitioner::~Partitioner()
	{
#if defined(USE_PARTITIONER_ZOLTAN)
		if( pt == Zoltan_Parmetis || pt == Zoltan_Scotch || pt == Zoltan_PHG || pt == Zoltan_RCB || pt == Zoltan_RIB || pt == Zoltan_HSFC )
		{
			if( pzz != NULL )
			{
				struct Zoltan_Struct * zz = static_cast<struct Zoltan_Struct *>(pzz);
				Zoltan_Destroy(&zz);
				pzz = static_cast<void *>(zz);
			}
		}
#endif
	}
	
	void Partitioner::Initialize(int * argc, char *** argv)
	{
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
237
238
		(void) argc;
		(void) argv;
Kirill Terekhov's avatar
Kirill Terekhov committed
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
#if defined(USE_PARTITIONER_ZOLTAN)
		float ver;
		Zoltan_Initialize(*argc,*argv,&ver);
#endif
	}
	
	void Partitioner::Finalize()
	{
	}
	
	
	void Partitioner::Evaluate()
	{
		ENTER_FUNC();
		int package = 0;
		switch(pt)
		{
			case Inner_RCM:
				package = 0;
				break;
			case Zoltan_Parmetis:
			case Zoltan_PHG:
			case Zoltan_RIB:
			case Zoltan_RCB:
			case Zoltan_HSFC:
			case Zoltan_Scotch:
				package = 1;
				break;
			case Parmetis:
				package = 2;
				break;
		}
		if( package == 0 )
		{
#if defined(USE_MPI)
			if( m->GetMeshState() == Mesh::Serial ) m->ResolveShared();
			
			unsigned mpisize = m->GetProcessorsNumber();
			
//~ #if defined(NDEBUG)
			//Check!!
			unsigned ncell = m->NumberOfCells();
			std::vector<unsigned> ncells(mpisize);
			REPORT_MPI(MPI_Allgather(&ncell,1,MPI_UNSIGNED,&ncells[0],1,MPI_UNSIGNED,m->GetCommunicator()));
			unsigned nhave = 0;
			for(unsigned k = 0; k < mpisize; k++) if( ncells[k] ) nhave++;
			if( nhave > 1 )
			{
				std::cout << "currently only non-distributed meshes are supported by reverse cuthill-mckee algorithm" << std::endl;
				throw NotImplemented;
			}
//~ #endf
			
			
			Tag index = m->CreateTag("CM_INDEX",DATA_INTEGER,CELL,NONE,1);
			Tag order = m->CreateTag("ORDER",DATA_INTEGER,CELL,NONE,1);
			
			
			for(Mesh::iteratorCell it = m->BeginCell(); it != m->EndCell(); ++it)
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
299
300
				ElementArray<Face> f = it->getFaces();
				for(ElementArray<Face>::iterator jt = f.begin(); jt != f.end(); ++jt)
Kirill Terekhov's avatar
Kirill Terekhov committed
301
302
303
304
305
					if( !jt->Boundary() ) it->Integer(order)++;
			}
			
			Storage::integer visited = 1;
			Storage::integer chunk = m->NumberOfCells() / mpisize + 1;
Kirill Terekhov's avatar
Kirill Terekhov committed
306
307
308
			std::deque<HandleType> queue;
			ElementArray<Element> around(m);
			HandleType select;
Kirill Terekhov's avatar
Kirill Terekhov committed
309
310
			while(visited != static_cast<Storage::integer>(m->NumberOfCells()+1) )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
311
312
				select = InvalidHandle();
				Mesh::iteratorCell halt_it;
Kirill Terekhov's avatar
Kirill Terekhov committed
313
314
315
316
				for(Mesh::iteratorCell it = m->BeginCell(); it != m->EndCell(); ++it)
				{
					if( it->Integer(index) == 0 )
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
						select = *it;
						halt_it = it;
						break;
					}
				}
				if( select == InvalidHandle() ) break;
				Storage::integer sorder = m->Integer(select,order);
				for(Mesh::iteratorCell it = halt_it; it != m->EndCell(); ++it)
				{
					if( it->Integer(index) == 0 )
					{
						Storage::integer norder = it->Integer(order);
						if( norder < sorder ) 
						{
							select = *it;
							sorder = norder;
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
334
335
336
					}
				}
				queue.push_back(select);
Kirill Terekhov's avatar
Kirill Terekhov committed
337
				m->Integer(queue.back(),index) = visited++;
Kirill Terekhov's avatar
Kirill Terekhov committed
338
339
				while(!queue.empty())
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
340
					around = Cell(m,queue.front())->BridgeAdjacencies(FACE,CELL);
Kirill Terekhov's avatar
Kirill Terekhov committed
341
					queue.pop_front();
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
342
					std::sort(around.data(),around.data()+around.size(),Mesh::IntegerComparator(m,order));
Kirill Terekhov's avatar
Kirill Terekhov committed
343
					for(ElementArray<Element>::iterator it = around.begin(); it != around.end(); ++it)
Kirill Terekhov's avatar
Kirill Terekhov committed
344
345
346
					{
						if( it->Integer(index) == 0 )
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
347
348
							queue.push_back(*it);
							m->Integer(queue.back(),index) = visited++;
Kirill Terekhov's avatar
Kirill Terekhov committed
349
350
351
352
353
354
355

							if( visited % chunk == 0 ) 
							{
								queue.clear();
								break;
							}

Kirill Terekhov's avatar
Kirill Terekhov committed
356
357
						}
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
358
359
					//this may not fire properly
					//if( visited % chunk == 0 ) queue.clear();
Kirill Terekhov's avatar
Kirill Terekhov committed
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
388
389
390
				}
			}
			m->DeleteTag(order);
			//index may be used
			
			//reverse
			//for(Mesh::iteratorCell it = m->BeginCell(); it != m->EndCell(); ++it)
			//	it->Integer(index) = m->NumberOfCells() - it->Integer(index);
				
			
			
			Tag redist = m->RedistributeTag();
			
			for(Mesh::iteratorCell it = m->BeginCell(); it != m->EndCell(); ++it)
			{
				it->Integer(redist) = it->Integer(index)/chunk;
				//~ std::cout << it->Integer(index) << " -> " <<  it->Integer(redist) << std::endl;
			}

			m->DeleteTag(index);
#else
			throw NotImplemented;
#endif
		}
		if( package == 1 )
		{
#if defined(USE_PARTITIONER_ZOLTAN)
			int changes, numGidEntries, numLidEntries, numImport, numExport;
			ZOLTAN_ID_PTR importGlobalGids, importLocalGids, exportGlobalGids, exportLocalGids;
			int *importProcs, *importToPart, *exportProcs, *exportToPart;
			int rc;
391
			if( m->Integer(m->GetHandle(),m->LayersTag()) == 0 ) m->ExchangeGhost(1,FACE);
Kirill Terekhov's avatar
Kirill Terekhov committed
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
			
			rc = Zoltan_LB_Partition(static_cast<struct Zoltan_Struct *>(pzz), /* input (all remaining fields are output) */
									 &changes,        /* 1 if partitioning was changed, 0 otherwise */ 
									 &numGidEntries,  /* Number of integers used for a global ID */
									 &numLidEntries,  /* Number of integers used for a local ID */
									 &numImport,      /* Number of vertices to be sent to me */
									 &importGlobalGids,  /* Global IDs of vertices to be sent to me */
									 &importLocalGids,   /* Local IDs of vertices to be sent to me */
									 &importProcs,    /* Process rank for source of each incoming vertex */
									 &importToPart,   /* New partition for each incoming vertex */
									 &numExport,      /* Number of vertices I must send to other processes*/
									 &exportGlobalGids,  /* Global IDs of the vertices I must send */
									 &exportLocalGids,   /* Local IDs of the vertices I must send */
									 &exportProcs,    /* Process to which I send each of the vertices */
									 &exportToPart);  /* Partition to which each vertex will belong */
			ZOLTAN_CHKERR(rc);		
			
			if( changes )
			{
				std::vector<int> new_distribution;
				for(Mesh::iteratorCell it = m->BeginCell(); it != m->EndCell(); it++)
					if( it->GetStatus() != Element::Ghost )
						new_distribution.push_back(it->Integer(m->OwnerTag()));	
				for(int i = 0; i < numExport; i++)
					new_distribution[exportLocalGids[i]] = exportToPart[i];
				
				Tag redist = m->RedistributeTag();
				int i = 0;
				for(Mesh::iteratorCell it = m->BeginCell(); it != m->EndCell(); it++)
					if( it->GetStatus() != Element::Ghost )
						it->Integer(redist) = new_distribution[i++];
			}
			Zoltan_LB_Free_Part(&importGlobalGids, &importLocalGids, 
								&importProcs, &importToPart);
			Zoltan_LB_Free_Part(&exportGlobalGids, &exportLocalGids, 
								&exportProcs, &exportToPart);
#endif
		}
		if( package == 2 )
		{
#if defined(USE_PARTITIONER_PARMETIS)
			INMOST_DATA_ENUM_TYPE dim = m->GetDimensions();
			double time;
			bool debug_output = false;
			bool time_output = false;
			int result;
			int rank = m->GetProcessorRank();
			int size = m->GetProcessorsNumber();
			int have_vwgt = 0, have_adjwgt = 0, have_mwgt = 0;
			int nreserve = 32;
			idx_t nparts;
			//Array storing parts indexes
			std::vector<unsigned int> xparts;
			//These arrays determine local sparse matrix
			std::vector<idx_t> vtxdist;
			std::vector<idx_t> xadj;
			std::vector<idx_t> adjncy;
			//These arrays determine weights defined for vertices
			std::vector<idx_t> vwgt; //unused yet
			std::vector<idx_t> adjwgt; //unused yet
			idx_t wgtflag = 0;
			idx_t numflag = 0;
			idx_t ndims = m->GetDimensions();
			std::vector<real_t> xyz;
			//This sets weight for every part set in respect to according weight and tolerance
			idx_t ncon = 1;
			std::vector<real_t> tpwgts;
			std::vector<real_t> ubvec;
			//set options
			std::vector<idx_t> options(3,0);
			//number of edgecuts
			idx_t edgecut;
			//output data - which entity should belong to which part
			std::vector<idx_t> part;
			real_t itr = 1000.0;
			//send data to processors with zero nodes
			std::vector<unsigned int> send;
			idx_t mysize;
			//to estimate memory used by entities
			std::vector<idx_t> vsize; //unused yet
			
			//compute part ranges for every processor
			//and total number of parts
			xparts.resize(size+1);
			xparts[0] = 0;
			
			for(unsigned int i = 1; i < xparts.size(); i++)
				xparts[i] = i;
				
			
			nparts = xparts[xparts.size()-1];
			
			
			//set graph size
			vtxdist.resize(size+1);
			
			


			//set options
			if( debug_output )
			{	
				options[0] = 1;
				options[1] = 1;
				options[2] = 15;
			}
			
			
			if( time_output ) time = Timer();
			//set ghost boundaries so we certanly know we can access adjacencies
			
			if( m->GetMeshState() != Mesh::Parallel )  
				m->ResolveShared();
Kirill Terekhov's avatar
Kirill Terekhov committed
505
			if( m->Integer(m->GetHandle(),m->LayersTag()) == 0 ) 
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
555
556
				m->ExchangeGhost(1,FACE);
			//~ if( !m->GlobalIDTag().isValid() || !m->GlobalIDTag().isDefined(CELL) )
				m->AssignGlobalID(CELL);
			

			
			
			if( time_output ) time = Timer();
			
			
			mysize = 0;
			for(Mesh::iteratorCell it = m->BeginCell(); it != m->EndCell(); ++it) if( it->GetStatus() != Element::Ghost ) mysize++;
			vtxdist[0] = 0;
			REPORT_MPI(result = MPI_Allgather(&mysize,1,IDX_T,&vtxdist[1],1,IDX_T,m->GetCommunicator()));
			if( result != MPI_SUCCESS ) throw Impossible;
			for(unsigned int i = 2; i < vtxdist.size(); i++)
				vtxdist[i] += vtxdist[i-1];
				
				
			if( time_output )
			{
				time = Timer()-time;
				REPORT_STR("time to compute graph information");
				REPORT_VAL("time",time);
			}


			{	
				xadj.resize(mysize+1);
				adjncy.reserve(mysize*nreserve); //preallocate array
				xadj[0] = 0;
			}
			
			xyz.resize(mysize*dim);
			part.resize(mysize);
									
			
			if( time_output ) time = Timer();
			
			//compute itr parameter
			{
				itr = 1000; //communication time to distribution time
			}


			
			
			//estimate storage space
			if( pa == Repartition ) 
			{
				vsize.resize(mysize,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
557
558
559
560
561
				int k = 0;
				for(Mesh::iteratorCell it = m->BeginCell(); it != m->EndCell(); ++it) if( it->GetStatus() != Element::Ghost )
				{
					vsize[k++] = m->MemoryUsage(*it);
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
			}
			
			
			
			
			{
				if( GetWeight().isValid() && GetWeight().GetSize() != ENUMUNDEF )
				{
					if( GetWeight().isDefined(MESH) ) have_mwgt = 1;
					if( GetWeight().isDefined(CELL) && !GetWeight().isSparse(CELL) ) have_vwgt = 1;
					if( GetWeight().isDefined(FACE) && !GetWeight().isSparse(FACE) ) have_adjwgt = 1;
					if( have_vwgt + have_adjwgt ) ncon = GetWeight().GetSize();
				}
				
				
				if( have_vwgt ) vwgt.resize(mysize*ncon);
				if( have_adjwgt) adjwgt.reserve(mysize*ncon*nreserve);
				if( !have_adjwgt && !have_vwgt ) ncon = 1;
				//set constraint tolerances
Kirill Terekhov's avatar
Kirill Terekhov committed
581
582
				const real_t v = static_cast<real_t>(1.05);
				ubvec.resize(ncon,v);
Kirill Terekhov's avatar
Kirill Terekhov committed
583
584
585
				if( have_mwgt )
				{
					std::vector<real_t> l_tpwgts(ncon);
Kirill Terekhov's avatar
Kirill Terekhov committed
586
					for(idx_t q = 0; q < ncon; q++) l_tpwgts[q] = static_cast<real_t>(m->RealArray(m->GetHandle(),GetWeight())[q]);
Kirill Terekhov's avatar
Kirill Terekhov committed
587
588
589
590
591
592
593
594
595
596
597
					REPORT_MPI(MPI_Allgather(&l_tpwgts[0],ncon,IDX_T,&tpwgts[0],ncon,IDX_T,m->GetCommunicator()));
					
					for(idx_t j = 0; j < ncon; j++)
					{
						real_t sum_tpwgts = 0;
						for(unsigned i = 0; i < tpwgts.size()/ncon; i++) sum_tpwgts += tpwgts[i*ncon+j];
						for(unsigned i = 0; i < tpwgts.size()/ncon; i++) tpwgts[i*ncon+j] /= sum_tpwgts;
					}
				}
				else
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
598
					real_t c = static_cast<real_t>(1.0/static_cast<real_t>(nparts));
Kirill Terekhov's avatar
Kirill Terekhov committed
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
					tpwgts.resize(nparts*ncon,c);
				}
			}			
			
			if( time_output ) time = Timer();
			
			//get part tag
			
			if( pa == Repartition )
			{
				part.resize(mysize,rank);
			}
			
			if( time_output )
			{
				time = Timer()-time;
				REPORT_STR("time to compute weights");
				REPORT_VAL("time",time);
			}
			
			REPORT_STR("start making graph, number of local elements:");
			REPORT_VAL("local_size",mysize);
			
			
			if( time_output ) time = Timer();
			
			
			//make graph
			int k = 0;
			for(Mesh::iteratorCell it = m->BeginCell(); it != m->EndCell(); ++it)
			if( it->GetStatus() != Element::Ghost )
			{
				idx_t sum = 0;
				if( pa == Partition )
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
634
635
636
637
638
639
640
641
					Storage::real xyz_temp[3] = {0.0,0.0,0.0};
					it->Centroid(xyz_temp);
					for(int q = 0; q < m->GetDimensions(); ++q)
						xyz[k*dim+q] = static_cast<real_t>(xyz_temp[q]);
				}
				{
					ElementArray<Face> faces = it->getFaces();
					for(ElementArray<Face>::iterator jt = faces.begin(); jt != faces.end(); ++jt)
Kirill Terekhov's avatar
Kirill Terekhov committed
642
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
643
644
						Cell n = it->Neighbour(jt->self());
						if( n.isValid() )
Kirill Terekhov's avatar
Kirill Terekhov committed
645
646
647
648
649
						{
							adjncy.push_back(n->GlobalID());
							if( have_adjwgt ) 
							{
								for(idx_t q = 0; q < ncon; q++) 
Kirill Terekhov's avatar
Kirill Terekhov committed
650
									adjwgt.push_back(jt->RealArray(GetWeight())[q]); //why adjwgt is not real???
Kirill Terekhov's avatar
Kirill Terekhov committed
651
652
653
654
655
656
657
658
659
660
661
							}
							sum++;
						}
					}
					
					xadj[k+1] = xadj[k] + sum;
					
					//Fill weight arrays here
					if( have_vwgt )
					{
						for(idx_t q = 0; q < ncon; q++)
Kirill Terekhov's avatar
Kirill Terekhov committed
662
							vwgt[k*ncon+q] = it->RealArrayDF(GetWeight())[q]; //why vwgt is not real???
Kirill Terekhov's avatar
Kirill Terekhov committed
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
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
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
909
					}
				}
				k++;
			}
			
			REPORT_STR("graph made, number of adjacencies: ");
			REPORT_VAL("adjacencies_size",adjncy.size());
			
			
			if( time_output )
			{
				time = Timer()-time;
				REPORT_STR("time to compute graph");
				REPORT_VAL("time",time);
			}
			
			
			wgtflag = have_vwgt + have_adjwgt*2;
			
			if( debug_output )
			{
				m->BeginSequentialCode();
				std::cout << "on processor " << rank << std::endl;
				std::cout << "vtxdist: " << std::endl;
				for(unsigned int i = 0; i < vtxdist.size(); i++)
				{
					std::cout << vtxdist[i] << " ";
					if( (i+1)%30 == 0 ) std::cout << std::endl;
				}
				std::cout << std::endl;
				
				{
					std::cout << "xadj size " << xadj.size() << ":" << std::endl;
					for(unsigned int i = 0; i < xadj.size(); i++)
					{
						std::cout << xadj[i] << " ";
						if( (i+1)%30 == 0 ) std::cout << std::endl;
					}
					std::cout << std::endl;
					std::cout << "adjncy size " << adjncy.size() << ":" << std::endl;
					for(unsigned int i = 0; i < adjncy.size(); i++)
					{
						std::cout << adjncy[i] << " ";
						if( (i+1)%30 == 0 ) std::cout << std::endl;
					}
					std::cout << std::endl;
					if( have_vwgt )
					{
						std::cout << "vwgt: " << std::endl;
						for(unsigned int i = 0; i < vwgt.size(); i++)
						{
							std::cout << vwgt[i] << " ";
							if( (i+1)%30 == 0 ) std::cout << std::endl;
						}
						std::cout << std::endl;
					}
					if( have_adjwgt )
					{
						std::cout << "adjwgt: " << std::endl;
						for(unsigned int i = 0; i < adjwgt.size(); i++)
						{
							std::cout << adjwgt[i] << " ";
							if( (i+1)%30 == 0 ) std::cout << std::endl;
						}
						std::cout << std::endl;
					}
					if( pa == Repartition )
					{
						std::cout << "vsize: " << std::endl;
						for(unsigned int i = 0; i < vsize.size(); i++)
						{
							std::cout << vsize[i] << " ";
							if( (i+1)%30 == 0 ) std::cout << std::endl;
						}
						std::cout << std::endl;
					}			
					std::cout << "wgtflag = " << wgtflag << std::endl;
					std::cout << "numflag = " << numflag << std::endl;
				}
				if( pa == Partition )
				{
					std::cout << "ndims = " << ndims << std::endl;
					std::cout << "xyz: " << std::endl;
					for(unsigned int i = 0; i < xyz.size(); i++)
					{
						std::cout << xyz[i] << " ";
						if( (i+1)%30 == 0 ) std::cout << std::endl;
					}
					std::cout << std::endl;
				}
				std::cout << "ncon = " << ncon << std::endl;
				std::cout << "nparts = " << nparts << std::endl;
				std::cout << "tpwgts: " << std::endl;
				for(unsigned int i = 0; i < tpwgts.size(); i++)
				{
					std::cout << tpwgts[i] << " ";
					if( (i+1)%30 == 0 ) std::cout << std::endl;
				}
				std::cout << std::endl;
				std::cout << "ubvec: " << std::endl;
				for(unsigned int i = 0; i < ubvec.size(); i++)
				{
					std::cout << ubvec[i] << " ";
					if( (i+1)%30 == 0 ) std::cout << std::endl;
				}
				std::cout << std::endl;
				std::cout << "options: " << std::endl;
				for(unsigned int i = 0; i < options.size(); i++)
				{
					std::cout << options[i] << " ";
					if( (i+1)%30 == 0 ) std::cout << std::endl;
				}
				std::cout << std::endl;
				m->EndSequentialCode();
			}
			
			//redistribute entities;
			if( time_output ) time = Timer();
			
			
			
			
			{
				int it,jt,kt,flag;
				unsigned int fill[3];
				
				send.reserve(3*nparts*2);
				for(it = size; it >= 1; it--)
				{
					if( vtxdist[it]-vtxdist[it-1] == 0 )
					{
						fill[0] = it - 1; //to
						flag = 0;
						for(jt = it-1; jt >= 1; jt--)
						{
							if( vtxdist[jt]-vtxdist[jt-1] > 0 )
							{
								fill[1] = jt - 1; //from
								fill[2] = vtxdist[jt]-1-vtxdist[rank]; //id
								send.insert(send.end(),fill,fill+3);
								vtxdist[jt]--;
								for(kt = jt+1; kt < it; kt++)
									vtxdist[kt] = vtxdist[jt];
								flag = 1;
								break;
							}
						}
						if( !flag ) throw Impossible;
					}
				}
				
				REPORT_STR("redistribute graph:");
				REPORT_VAL("send_size",send.size()/3);
				
				
				for(unsigned int i = 0; i < send.size()/3; i++)
				{
					idx_t trans_adjncy_size;
					real_t trans_xyz[3];
					std::vector<idx_t> trans_adjncy;
					if( send[i*3+0] == send[i*3+1] ) throw Impossible;
					if( send[i*3+1] == static_cast<unsigned int>(rank) )
					{	
						{
							trans_adjncy_size = xadj[send[i*3+2]+1]-xadj[send[i*3+2]];
							for(jt = xadj[send[i*3+2]]; jt < xadj[send[i*3+2]+1]; jt++)
								trans_adjncy.push_back(adjncy[jt]);
							if( have_adjwgt )
								for(jt = xadj[send[i*3+2]]; jt < xadj[send[i*3+2]+1]; jt++)
									trans_adjncy.push_back(adjwgt[jt]);
							if( have_vwgt )
								trans_adjncy.push_back(vwgt[send[i*3+2]]);
							if( pa == Repartition )
								trans_adjncy.push_back(part[send[i*3+2]]);
							REPORT_VAL("to",send[i*3+0]);
							REPORT_VAL("send_size",(trans_adjncy_size*(1+have_adjwgt)));
							REPORT_MPI(result = MPI_Send(&trans_adjncy[0],trans_adjncy_size*(1+have_adjwgt),IDX_T,send[i*3+0],2,m->GetCommunicator()));
							if( result != MPI_SUCCESS ) 
								throw Impossible;
						}
						if( pa == Partition )
						{
							for(jt = 0; jt < static_cast<int>(dim); jt++)
								trans_xyz[jt] = xyz[send[i*3+2]*dim+jt];
							REPORT_VAL("to",send[i*3+0]);
							REPORT_MPI(result = MPI_Send(trans_xyz,dim,REAL_T,send[i*3+0],3,m->GetCommunicator()));
							if( result != MPI_SUCCESS ) throw Impossible;
						}
					}
					else if( send[i*3+0] == static_cast<unsigned int>(rank) )
					{
						MPI_Status stat;
						int msgsize;
						
						{
							REPORT_VAL("from",send[i*3+1]);
							REPORT_MPI(result = MPI_Probe(send[i*3+1],2,m->GetCommunicator(),&stat));
							if( result != MPI_SUCCESS ) throw Impossible;
							result = MPI_Get_count(&stat,IDX_T,&msgsize);
							if( result != MPI_SUCCESS ) throw Impossible;
							trans_adjncy_size = (msgsize-have_vwgt-((pa == Repartition)?1:0))/(1+have_adjwgt);
							trans_adjncy.resize(msgsize);
							REPORT_VAL("from",send[i*3+1]);
							REPORT_MPI(result = MPI_Recv(&trans_adjncy[0],msgsize,IDX_T,send[i*3+1],2,m->GetCommunicator(),&stat));
							if( result != MPI_SUCCESS ) throw Impossible;
							xadj.resize(2);
							xadj[0] = 0;
							xadj[1] = trans_adjncy_size;
							for(jt = 0; jt < trans_adjncy_size; jt++)
								adjncy.push_back(trans_adjncy[jt]);
							if( have_adjwgt )
								for(jt = 0; jt < trans_adjncy_size; jt++)
									adjwgt.push_back(trans_adjncy[jt+trans_adjncy_size]);
							if( have_vwgt )
								vwgt.push_back(trans_adjncy[trans_adjncy_size*2]);
							if( pa == Repartition )
								part.push_back(trans_adjncy[trans_adjncy_size*2+1]);
						}
						if( pa == Partition )
						{
							REPORT_VAL("from",send[i*3+1]);
							REPORT_MPI(result = MPI_Recv(&trans_xyz,dim,REAL_T,send[i*3+1],3,m->GetCommunicator(),&stat));
							if( result != MPI_SUCCESS ) throw Impossible;
							xyz.resize(dim);
							for(jt = 0; jt < static_cast<int>(dim); jt++)
								xyz[jt] = trans_xyz[jt];
						}
						part.resize(1);
					}
				}
				
				REPORT_STR("graph redistributed");
			}
			
			if( time_output )
			{
				time = Timer()-time;
				REPORT_STR("time to redistribute graph");
				REPORT_VAL("time",time);
			}
			
			REPORT_STR("BEGIN PARMETIS");
			
			if( time_output ) time = Timer();
			
			MPI_Comm comm = m->GetCommunicator();
			
Kirill Terekhov's avatar
Kirill Terekhov committed
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
			idx_t  * link_vtxdist = vtxdist.empty() ? NULL : &vtxdist[0];
			idx_t  * link_xadj    = xadj.empty()    ? NULL : &xadj[0];
			idx_t  * link_adjncy  = adjncy.empty()  ? NULL : &adjncy[0];
			idx_t  * link_vwgt    = vwgt.empty()    ? NULL : &vwgt[0];
			idx_t  * link_adjwgt  = adjwgt.empty()  ? NULL : &adjwgt[0];
			real_t * link_tpwgts  = tpwgts.empty()  ? NULL : &tpwgts[0];
			real_t * link_ubvec   = ubvec.empty()   ? NULL : &ubvec[0];
			idx_t  * link_options = options.empty() ? NULL : &options[0];
			idx_t  * link_part    = part.empty()    ? NULL : &part[0];
			real_t * link_xyz     = xyz.empty()     ? NULL : &xyz[0];
			idx_t  * link_vsize   = vsize.empty()   ? NULL : &vsize[0];
			
			REPORT_VAL("vtxdist",link_vtxdist);
			REPORT_VAL("xadj",link_xadj);
			REPORT_VAL("adjncy",link_adjncy);
			REPORT_VAL("vwgt",link_vwgt);
			REPORT_VAL("adjwgt",link_adjwgt);
			REPORT_VAL("tpwgts",link_tpwgts);
			REPORT_VAL("ubvec",link_ubvec);
			REPORT_VAL("options",link_options);
			REPORT_VAL("part",link_part);
			REPORT_VAL("xyz",link_xyz);
			REPORT_VAL("vsize",link_vsize);

Kirill Terekhov's avatar
Kirill Terekhov committed
934
935
936
937
938
939
940
941
			switch(pa)
			{
				//~ case Partition:
				//~ result = ParMETIS_V3_PartKway(&vtxdist[0],&xadj[0],&adjncy[0],&vwgt[0],&adjwgt[0],
											  //~ &wgtflag,&numflag,&ncon,&nparts,&tpwgts[0],
											  //~ &ubvec[0],&options[0],&edgecut,&part[0],&comm);
				//~ break;
				case Refine:
Kirill Terekhov's avatar
Kirill Terekhov committed
942
943
944
945
				REPORT_STR("run refine");
				result = ParMETIS_V3_RefineKway(link_vtxdist,link_xadj,link_adjncy,link_vwgt,link_adjwgt,
												&wgtflag,&numflag,&ncon,&nparts,link_tpwgts,
												link_ubvec,link_options,&edgecut,link_part,&comm);	
Kirill Terekhov's avatar
Kirill Terekhov committed
946
947
				break;
				case Partition:
Kirill Terekhov's avatar
Kirill Terekhov committed
948
949
950
951
				REPORT_STR("run partition");
				result = ParMETIS_V3_PartGeomKway(link_vtxdist,link_xadj,link_adjncy,link_vwgt,link_adjwgt,
												  &wgtflag,&numflag,&ndims,link_xyz,&ncon,&nparts,link_tpwgts,
												  link_ubvec,link_options,&edgecut,link_part,&comm);
Kirill Terekhov's avatar
Kirill Terekhov committed
952
953
954
955
956
				break;
				//~ case P_PartGeom:
				//~ result = ParMETIS_V3_PartGeom(&vtxdist[0],&ndims,&xyz[0],&part[0],&comm);
				//~ break;
				case Repartition:
Kirill Terekhov's avatar
Kirill Terekhov committed
957
958
959
960
				REPORT_STR("run repartition");
				result = ParMETIS_V3_AdaptiveRepart(link_vtxdist,link_xadj,link_adjncy,link_vwgt,link_vsize,
													link_adjwgt,&wgtflag,&numflag,&ncon,&nparts,link_tpwgts,
													link_ubvec,&itr,link_options,&edgecut,link_part,&comm);
Kirill Terekhov's avatar
Kirill Terekhov committed
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
				break;
			}
			
			
			
			if( time_output )
			{
				time = Timer()-time;
				REPORT_STR("time in parmetis");
				REPORT_VAL("time",time);
			}
			
			REPORT_STR("END PARMETIS");

			if( time_output ) time = Timer();

			//distribute computed parts back
			{
				REPORT_VAL("send_size",send.size()/3);
				for(unsigned int i = 0; i < send.size()/3; i++)
				{
					if( send[i*3+0] == static_cast<unsigned int>(rank) )
					{
						REPORT_VAL("send_part",part[0]);
						REPORT_MPI(result = MPI_Send(&part[0],1,IDX_T,send[i*3+1],4,m->GetCommunicator()));
						if( result != MPI_SUCCESS ) throw Impossible;
						part.clear();
					}
					else if( send[i*3+1] == static_cast<unsigned int>(rank) )
					{ 
						REPORT_MPI(result = MPI_Recv(&part[send[i*3+2]],1,IDX_T,send[i*3+0],4,m->GetCommunicator(),MPI_STATUS_IGNORE));
						REPORT_VAL("recv_part",part[send[i*3+2]]);
						REPORT_VAL("from",part[send[i*3+0]]);
						if( result != MPI_SUCCESS ) throw Impossible;
					}
				}
			}

			if( time_output )
			{
				time = Timer()-time;
				REPORT_STR("redistribute graph back");
				REPORT_VAL("time",time);
			}
									 
			//debug
			if( debug_output )
			{
				m->BeginSequentialCode();
				std::cout << "distribution on " << rank << std::endl;
				for(unsigned int i = 0; i < part.size(); i++)
				{
					std::cout << part[i] << " ";
					if( (i+1)%30 == 0 ) std::cout << std::endl;
				}
				std::cout << std::endl;
				m->EndSequentialCode();
			}

			//assign entities to part sets
			{
				
				Tag redist = m->RedistributeTag();
				
				int k = 0;
				for(Mesh::iteratorCell it = m->BeginCell(); it != m->EndCell(); ++it) 
					if( it->GetStatus() != Element::Ghost ) 
						it->Integer(redist) = part[k++];
				
				
			}
#endif
		}
		EXIT_FUNC();
	}
	
	void Partitioner::SetMethod(enum Type t, enum Action a)
	{
		pt = t;
		pa = a;
		int package = 0;
		switch(pt)
		{
			case Inner_RCM:
				package = 0;
				break;
			case Zoltan_Parmetis:
			case Zoltan_PHG:
			case Zoltan_RIB:
			case Zoltan_RCB:
			case Zoltan_HSFC:
			case Zoltan_Scotch:
				package = 1;
				break;
			case Parmetis:
				package = 2;
				break;
		}
		if( package == 1 )
		{
#if defined(USE_PARTITIONER_ZOLTAN)
			if(pzz == NULL)
			{
				struct Zoltan_Struct * zz = Zoltan_Create(m->GetCommunicator());
				Zoltan_Set_Num_Obj_Fn(zz, get_number_of_objects, this);
				Zoltan_Set_Obj_List_Fn(zz, get_object_list, this);
				Zoltan_Set_Num_Geom_Fn(zz, get_num_geometry, this);
				Zoltan_Set_Geom_Multi_Fn(zz, get_geometry_list, this);
				Zoltan_Set_Num_Edges_Multi_Fn(zz, get_num_edges_list, this);
				Zoltan_Set_Edge_List_Multi_Fn(zz, get_edge_list, this);
				
				Zoltan_Set_Param(zz, "DEBUG_LEVEL", "0");		
				Zoltan_Set_Param(zz, "LB_METHOD", "RIB");
				Zoltan_Set_Param(zz, "NUM_GID_ENTRIES", "1"); 
				Zoltan_Set_Param(zz, "NUM_LID_ENTRIES", "1");
				Zoltan_Set_Param(zz, "OBJ_WEIGHT_DIM", "0");
				Zoltan_Set_Param(zz, "EDGE_WEIGHT_DIM", "0");
				Zoltan_Set_Param(zz, "RETURN_LISTS", "ALL");
				pzz = static_cast<void *>(zz);
			}
			struct Zoltan_Struct * zz = static_cast<struct Zoltan_Struct *>(pzz);
			switch(t)
			{
				case Zoltan_Parmetis:
				{
					Zoltan_Set_Param(zz, "LB_METHOD", "GRAPH");
					Zoltan_Set_Param(zz,"GRAPH_PACKAGE","PARMETIS");
					Zoltan_Set_Param(zz,"PARMETIS_METHOD","AdaptiveRepart");
					Zoltan_Set_Param(zz,"SCATTER_GRAPH","2");
					break;
				}
				case Zoltan_Scotch:
				{
					Zoltan_Set_Param(zz, "LB_METHOD", "GRAPH");
					Zoltan_Set_Param(zz,"GRAPH_PACKAGE","SCOTCH");
					Zoltan_Set_Param(zz,"SCATTER_GRAPH","2");
					break;
				}
				case Zoltan_RCB:
				{
					Zoltan_Set_Param(zz, "LB_METHOD", "RCB");
					break;
				}
				case Zoltan_RIB:
				{
					Zoltan_Set_Param(zz, "LB_METHOD", "RIB");
					break;
				}
				case Zoltan_HSFC:
				{
					Zoltan_Set_Param(zz, "LB_METHOD", "HSFC");
					
					break;
				}
				case Zoltan_PHG:
				{
					Zoltan_Set_Param(zz,"LB_METHOD", "GRAPH");
					Zoltan_Set_Param(zz,"GRAPH_PACKAGE","PHG");
					Zoltan_Set_Param(zz,"SCATTER_GRAPH","0");
					break;
				}
			}
			switch(a)
			{
				case Partition:
					Zoltan_Set_Param(zz, "LB_APPROACH", "PARTITION");
					break;
				case Repartition:
					Zoltan_Set_Param(zz, "LB_APPROACH", "REPARTITION");
					break;
				case Refine:	
					Zoltan_Set_Param(zz, "LB_APPROACH", "REFINE");
					break;
			}
#else
			throw NotImplemented;
#endif
		}
		if( package == 2 )
		{
#if defined(USE_PARTITIONER_PARMETIS)
#endif
		}
	}
	
	void Partitioner::SetWeight(Tag weight)
	{
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1148
		(void) weight;
Kirill Terekhov's avatar
Kirill Terekhov committed
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
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
1237
		int package = 0;
		switch(pt)
		{
			case Inner_RCM:
				package = 0;
				break;
			case Zoltan_Parmetis:
			case Zoltan_PHG:
			case Zoltan_RIB:
			case Zoltan_RCB:
			case Zoltan_HSFC:
			case Zoltan_Scotch:
				package = 1;
				break;
			case Parmetis:
				package = 2;
				break;
		}
		if( package == 1 )
		{
#if defined(USE_PARTITIONER_ZOLTAN)
			struct Zoltan_Struct * zz = static_cast<struct Zoltan_Struct *>(pzz);
			std::stringstream Wobj, Wedge;
			weight_tag = weight;
			if( weight.isValid() )
			{
				if( weight.GetSize() == ENUMUNDEF ) throw UnknownWeightSize;
				if( weight.isDefined(CELL) )
					Wobj << weight.GetSize();
				else
					Wobj << 0;
				if( weight.isDefined(FACE) )
					Wedge << weight.GetSize();
				else
					Wedge << 0;
			}
			else
			{
				Wobj << 0;
				Wedge << 0;
			}
			Zoltan_Set_Param(zz, "OBJ_WEIGHT_DIM", Wobj.str().c_str());
			Zoltan_Set_Param(zz, "EDGE_WEIGHT_DIM", Wedge.str().c_str());
#else
			throw NotImplemented;
#endif
		}
		if( package == 2 )
		{
#if defined(USE_PARTITIONER_PARMETIS)
			weight_tag = weight;
#endif
		}
	}
	void Partitioner::ResetWeight()
	{
		int package = 0;
		switch(pt)
		{
			case Inner_RCM:
				package = 0;
				break;
			case Zoltan_Parmetis:
			case Zoltan_PHG:
			case Zoltan_RIB:
			case Zoltan_RCB:
			case Zoltan_HSFC:
			case Zoltan_Scotch:
				package = 1;
				break;
			case Parmetis:
				package = 2;
				break;
		}
		if( package == 1 )
		{
#if defined(USE_PARTITIONER_ZOLTAN)
			struct Zoltan_Struct * zz = static_cast<struct Zoltan_Struct *>(pzz);
			Zoltan_Set_Param(zz, "OBJ_WEIGHT_DIM", "0");
			Zoltan_Set_Param(zz, "EDGE_WEIGHT_DIM", "0");
#else
			throw NotImplemented;
#endif
		}
		if( package == 2 )
		{
#if defined(USE_PARTITIONER_PARMETIS)
#endif
		}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1238
		weight_tag = Tag();
Kirill Terekhov's avatar
Kirill Terekhov committed
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
	}
	Mesh * Partitioner::GetMesh()
	{
		return m;
	}
	Tag Partitioner::GetWeight()
	{
		return weight_tag;
	}
}
#endif