XdmfPartitioner.cpp 17.4 KB
Newer Older
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
27
28
29
/*******************************************************************/
/*                               XDMF                              */
/*                   eXtensible Data Model and Format              */
/*                                                                 */
/*  Id : Id  */
/*  Date : $Date$ */
/*  Version : $Revision$ */
/*                                                                 */
/*  Author:                                                        */
/*     Kenneth Leiter                                              */
/*     kenneth.leiter@arl.army.mil                                 */
/*     US Army Research Laboratory                                 */
/*     Aberdeen Proving Ground, MD                                 */
/*                                                                 */
/*     Copyright @ 2010 US Army Research Laboratory                */
/*     All Rights Reserved                                         */
/*     See Copyright.txt or http://www.arl.hpc.mil/ice for details */
/*                                                                 */
/*     This software is distributed WITHOUT ANY WARRANTY; without  */
/*     even the implied warranty of MERCHANTABILITY or FITNESS     */
/*     FOR A PARTICULAR PURPOSE.  See the above copyright notice   */
/*     for more information.                                       */
/*                                                                 */
/*******************************************************************/

#ifndef BUILD_EXE

extern "C"
{
30
	#include <metis.h>
31
32
}

33
#include <iostream>
34
35
#include <sstream>
#include "XdmfArray.hpp"
36
#include "XdmfAttribute.hpp"
37
38
#include "XdmfAttributeCenter.hpp"
#include "XdmfAttributeType.hpp"
39
#include "XdmfGeometry.hpp"
40
#include "XdmfGeometryType.hpp"
41
#include "XdmfGrid.hpp"
42
#include "XdmfGridCollection.hpp"
43
#include "XdmfGridCollectionType.hpp"
44
#include "XdmfHDF5Writer.hpp"
45
#include "XdmfMap.hpp"
46
#include "XdmfPartitioner.hpp"
47
#include "XdmfSet.hpp"
48
#include "XdmfSetType.hpp"
49
#include "XdmfTopology.hpp"
50
#include "XdmfTopologyType.hpp"
51
52
53
54
55
56
57
58
59

XdmfPartitioner::XdmfPartitioner()
{
}

XdmfPartitioner::~XdmfPartitioner()
{
}

60
boost::shared_ptr<XdmfGridCollection> XdmfPartitioner::partition(const boost::shared_ptr<XdmfGrid> gridToPartition, const unsigned int numberOfPartitions,
61
		boost::shared_ptr<XdmfHDF5Writer> heavyDataWriter) const
62
{
63
	int metisElementType;
64
	int nodesPerElement;
65

66
	boost::shared_ptr<const XdmfTopologyType> topologyType = gridToPartition->getTopology()->getType();
67
68
69
	if(topologyType == XdmfTopologyType::Triangle() || topologyType == XdmfTopologyType::Triangle_6())
	{
		metisElementType = 1;
70
		nodesPerElement = 3;
71
72
73
74
	}
	else if(topologyType == XdmfTopologyType::Quadrilateral() || topologyType == XdmfTopologyType::Quadrilateral_8())
	{
		metisElementType = 4;
75
		nodesPerElement = 4;
76
77
78
79
	}
	else if(topologyType == XdmfTopologyType::Tetrahedron() || topologyType == XdmfTopologyType::Tetrahedron_10())
	{
		metisElementType = 2;
80
		nodesPerElement = 4;
81
82
83
84
85
86
	}
	else if(topologyType == XdmfTopologyType::Hexahedron() || topologyType == XdmfTopologyType::Hexahedron_20() ||
			topologyType == XdmfTopologyType::Hexahedron_24() || topologyType == XdmfTopologyType::Hexahedron_27() ||
			topologyType == XdmfTopologyType::Hexahedron_64())
	{
		metisElementType = 3;
87
		nodesPerElement = 8;
88
89
90
91
92
93
	}
	else
	{
		assert(false);
	}

94
	if(!gridToPartition->getTopology()->isInitialized())
95
	{
96
		gridToPartition->getTopology()->read();
97
98
99
	}

	int numElements = gridToPartition->getTopology()->getNumberElements();
100
	idxtype * metisConnectivity = new idxtype[nodesPerElement * numElements];
101
102
	for(unsigned int i=0; i<numElements; ++i)
	{
103
		gridToPartition->getTopology()->getValuesCopy(i*topologyType->getNodesPerElement(), &metisConnectivity[i*nodesPerElement], nodesPerElement);
104
105
106
107
108
109
	}

	int numNodes = gridToPartition->getGeometry()->getNumberPoints();

	// Need to remap connectivity for nonlinear elements so that metis handles it properly.
	std::map<idxtype, idxtype> xdmfIdToMetisId;
110
	if(nodesPerElement != topologyType->getNodesPerElement())
111
	{
112
		unsigned int index = 0;
113
		for (unsigned int i=0; i<numElements * nodesPerElement; ++i)
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
		{
			std::map<idxtype, idxtype>::const_iterator val = xdmfIdToMetisId.find(metisConnectivity[i]);
			if (val != xdmfIdToMetisId.end())
			{
				metisConnectivity[i] = val->second;
			}
			else
			{
				// Haven't seen this id before, map to index and set to new id
				xdmfIdToMetisId[metisConnectivity[i]] = index;
				metisConnectivity[i] = index;
				index++;
			}
		}
		numNodes = index;
	}

	int startIndex = 0;
	int numCutEdges = 0;

	idxtype * elementsPartition = new idxtype[numElements];
	idxtype * nodesPartition = new idxtype[numNodes];

	std::cout << "Entered METIS" << std::endl;
	METIS_PartMeshDual(&numElements, &numNodes, metisConnectivity, &metisElementType, &startIndex, (int*)&numberOfPartitions, &numCutEdges, elementsPartition, nodesPartition);
	//METIS_PartMeshNodal(&numElements, &numNodes, metisConnectivity, &metisElementType, &startIndex, &numPartitions, &numCutEdges, elementsPartition, nodesPartition);
	std::cout << "Exited METIS" << std::endl;

	delete [] metisConnectivity;
	delete [] nodesPartition;

	// For each partition, map global to local node id
	std::vector<std::map<unsigned int, unsigned int> > globalToLocalNodeIdMap;
	// For each partition, list global element id.
	std::vector<std::vector<unsigned int> > globalElementIds;
	for(unsigned int i=0; i<numberOfPartitions; ++i)
	{
		std::map<unsigned int, unsigned int> nodeMap;
		globalToLocalNodeIdMap.push_back(nodeMap);
		std::vector<unsigned int> elementIds;
		globalElementIds.push_back(elementIds);
	}

	// Fill in globalNodeId for each partition
	unsigned int totalIndex = 0;
	for (unsigned int i=0; i<numElements; ++i)
	{
		unsigned int partitionId = elementsPartition[i];
162
		for (unsigned int j=0; j<topologyType->getNodesPerElement(); ++j)
163
		{
164
			unsigned int globalNodeId = gridToPartition->getTopology()->getValueCopy<unsigned int>(totalIndex);
165
166
167
			if (globalToLocalNodeIdMap[partitionId].count(globalNodeId) == 0)
			{
				// Have not seen this node, need to add to map
168
				unsigned int size = globalToLocalNodeIdMap[partitionId].size();
169
170
171
172
173
174
				globalToLocalNodeIdMap[partitionId][globalNodeId] = size;
			}
			totalIndex++;
		}
		globalElementIds[partitionId].push_back(i);
	}
175
	delete [] elementsPartition;
176

177
	bool generateGlobalNodeIds = !gridToPartition->getAttribute("GlobalNodeId");
178
179

	boost::shared_ptr<XdmfGridCollection> partitionedGrids = XdmfGridCollection::New();
180
	partitionedGrids->setType(XdmfGridCollectionType::Spatial());
181

182
	if(!gridToPartition->getGeometry()->isInitialized())
183
	{
184
		gridToPartition->getGeometry()->read();
185
186
	}

187
	// Split geometry and topology into proper partitions
188
189
	for(unsigned int i=0; i<numberOfPartitions; ++i)
	{
190
191
		std::map<unsigned int, unsigned int> & currNodeMap = globalToLocalNodeIdMap[i];
		std::vector<unsigned int> & currElemIds = globalElementIds[i];
192
193
194
195
196
197
198
199
200
201
202

		if(currElemIds.size() > 0)
		{
			std::stringstream name;
			name << gridToPartition->getName() << "_" << i;

			boost::shared_ptr<XdmfGrid> partitioned = XdmfGrid::New();
			partitioned->setName(name.str());
			partitionedGrids->insert(partitioned);

			// Fill in geometry for this partition
203
			partitioned->getGeometry()->setType(gridToPartition->getGeometry()->getType());
204
			boost::shared_ptr<XdmfArray> geometryVals = partitioned->getGeometry();
205
			unsigned int numDimensions = partitioned->getGeometry()->getType()->getDimensions();
206
			geometryVals->initialize(gridToPartition->getGeometry()->getArrayType());
207
			geometryVals->resize<unsigned int>(currNodeMap.size() * numDimensions);
208
209
210

			for(std::map<unsigned int, unsigned int>::const_iterator iter = currNodeMap.begin(); iter != currNodeMap.end(); ++iter)
			{
211
				geometryVals->copyValues(iter->second * numDimensions, gridToPartition->getGeometry(), iter->first * numDimensions, numDimensions);
212
213
214
215
216
217
218
219
220
			}

			if(heavyDataWriter)
			{
				geometryVals->accept(heavyDataWriter);
				geometryVals->release();
			}

			// Fill in topology for this partition
221
			partitioned->getTopology()->setType(gridToPartition->getTopology()->getType());
222
223
			boost::shared_ptr<XdmfArray> topologyVals = partitioned->getTopology();
			topologyVals->initialize(gridToPartition->getTopology()->getArrayType());
224
			topologyVals->resize<unsigned int>(currElemIds.size() * topologyType->getNodesPerElement());
225
226
227
228
229

			unsigned int index = 0;
			for(std::vector<unsigned int>::const_iterator iter = currElemIds.begin(); iter != currElemIds.end(); ++iter)
			{
				// Translate these global node ids to local node ids
230
				for(unsigned int j=0; j<topologyType->getNodesPerElement(); ++j)
231
				{
232
					unsigned int globalNodeId = currNodeMap[gridToPartition->getTopology()->getValueCopy<unsigned int>(*iter * topologyType->getNodesPerElement() + j)];
233
234
235
236
237
238
239
240
241
242
					topologyVals->copyValues(index, &globalNodeId, 1);
					index++;
				}
			}

			if(heavyDataWriter)
			{
				topologyVals->accept(heavyDataWriter);
				topologyVals->release();
			}
243

244
245
			if (generateGlobalNodeIds)
			{
246
				boost::shared_ptr<XdmfAttribute> globalNodeIds = XdmfAttribute::New();
247
				globalNodeIds->setName("GlobalNodeId");
248
249
				globalNodeIds->setType(XdmfAttributeType::GlobalId());
				globalNodeIds->setCenter(XdmfAttributeCenter::Node());
250

251
				boost::shared_ptr<XdmfArray> globalNodeIdsVals = globalNodeIds;
252
				globalNodeIdsVals->resize<unsigned int>(currNodeMap.size());
253
254
255
256
257
				for (std::map<unsigned int, unsigned int>::const_iterator iter = currNodeMap.begin(); iter != currNodeMap.end(); ++iter)
				{
					globalNodeIdsVals->copyValues(iter->second, &iter->first, 1);
				}
				partitioned->insert(globalNodeIds);
258

259
260
261
262
263
264
265
266
				if(heavyDataWriter)
				{
					globalNodeIdsVals->accept(heavyDataWriter);
					globalNodeIdsVals->release();
				}
			}
		}
	}
267

268
269
	gridToPartition->getGeometry()->release();
	gridToPartition->getTopology()->release();
270

271
	// Split attributes into proper partitions
272
	for(unsigned int i=0; i<gridToPartition->getNumberAttributes(); ++i)
273
274
	{
		boost::shared_ptr<XdmfAttribute> currAttribute = gridToPartition->getAttribute(i);
275
		if(!currAttribute->isInitialized())
276
		{
277
			currAttribute->read();
278
279
280
281
282
283
284
285
286
287
288
		}
		unsigned int partitionId = 0;
		for(unsigned int j=0; j<numberOfPartitions; ++j)
		{
			std::map<unsigned int, unsigned int> & currNodeMap = globalToLocalNodeIdMap[j];
			std::vector<unsigned int> & currElemIds = globalElementIds[j];
			if(currElemIds.size() > 0)
			{
				boost::shared_ptr<XdmfGrid> partitioned = partitionedGrids->getGrid(partitionId);
				partitionId++;
				boost::shared_ptr<XdmfAttribute> createdAttribute;
289
				if(currAttribute->getCenter() == XdmfAttributeCenter::Grid())
290
291
292
293
294
295
				{
					if(partitionId == 0)
					{
						createdAttribute = currAttribute;
					}
				}
296
297
298
				else if(currAttribute->getCenter() == XdmfAttributeCenter::Cell() ||
						currAttribute->getCenter() == XdmfAttributeCenter::Face() ||
						currAttribute->getCenter() == XdmfAttributeCenter::Edge())
299
300
301
				{
					createdAttribute = XdmfAttribute::New();
					createdAttribute->setName(currAttribute->getName());
302
303
					createdAttribute->setCenter(currAttribute->getCenter());
					createdAttribute->setType(currAttribute->getType());
304
					unsigned int index = 0;
305
306
307
					unsigned int numValsPerComponent = currAttribute->size() / gridToPartition->getTopology()->getNumberElements();
					createdAttribute->initialize(currAttribute->getArrayType());
					createdAttribute->resize<unsigned int>(currElemIds.size() * numValsPerComponent);
308
309
					for(std::vector<unsigned int>::const_iterator iter = currElemIds.begin(); iter != currElemIds.end(); ++iter)
					{
310
						createdAttribute->copyValues(index, currAttribute, *iter * numValsPerComponent, numValsPerComponent);
311
312
313
						index += numValsPerComponent;
					}
				}
314
				else if(currAttribute->getCenter() == XdmfAttributeCenter::Node())
315
316
317
				{
					createdAttribute = XdmfAttribute::New();
					createdAttribute->setName(currAttribute->getName());
318
319
					createdAttribute->setCenter(currAttribute->getCenter());
					createdAttribute->setType(currAttribute->getType());
320
321
					createdAttribute->initialize(currAttribute->getArrayType());
					createdAttribute->resize<unsigned int>(currNodeMap.size());
322
323
					for(std::map<unsigned int, unsigned int>::const_iterator iter = currNodeMap.begin(); iter != currNodeMap.end(); ++iter)
					{
324
						createdAttribute->copyValues(iter->second, currAttribute, iter->first, 1);
325
326
327
328
329
330
331
					}
				}
				if(createdAttribute != NULL)
				{
					partitioned->insert(createdAttribute);
					if(heavyDataWriter)
					{
332
333
						createdAttribute->accept(heavyDataWriter);
						createdAttribute->release();
334
335
336
337
					}
				}
			}
		}
338
		currAttribute->release();
339
	}
340

341
	// Split sets into proper partitions
342
	for(unsigned int i=0; i<gridToPartition->getNumberSets(); ++i)
343
344
	{
		boost::shared_ptr<XdmfSet> currSet = gridToPartition->getSet(i);
345
		if(!currSet->isInitialized())
346
		{
347
			currSet->read();
348
349
350
351
352
353
354
355
356
357
		}
		unsigned int partitionId = 0;
		for(unsigned int j=0; j<numberOfPartitions; ++j)
		{
			std::map<unsigned int, unsigned int> & currNodeMap = globalToLocalNodeIdMap[j];
			std::vector<unsigned int> & currElemIds = globalElementIds[j];
			if(currElemIds.size() > 0)
			{
				boost::shared_ptr<XdmfGrid> partitioned = partitionedGrids->getGrid(partitionId);
				partitionId++;
358
359
360
361

				boost::shared_ptr<XdmfSet> partitionedSet = XdmfSet::New();

				if(currSet->getType() == XdmfSetType::Cell() || currSet->getType() == XdmfSetType::Face() || currSet->getType() == XdmfSetType::Edge())
362
				{
363
					for(XdmfSet::const_iterator iter = currSet->begin(); iter != currSet->end(); ++iter)
364
					{
365
						std::vector<unsigned int>::const_iterator val = std::find(currElemIds.begin(), currElemIds.end(), *iter);
366
367
368
						if(val != currElemIds.end())
						{
							unsigned int valToPush = val - currElemIds.begin();
369
							partitionedSet->insert(valToPush);
370
371
372
						}
					}
				}
373
				else if(currSet->getType() == XdmfSetType::Node())
374
				{
375
					for(XdmfSet::const_iterator iter = currSet->begin(); iter != currSet->end(); ++iter)
376
					{
377
						std::map<unsigned int, unsigned int>::const_iterator val = currNodeMap.find(*iter);
378
379
						if(val != currNodeMap.end())
						{
380
							partitionedSet->insert(val->second);
381
382
383
						}
					}
				}
384
				if(partitionedSet->size() > 0)
385
				{
386
					partitioned->insert(partitionedSet);
387
388
					partitionedSet->setName(currSet->getName());
					partitionedSet->setType(currSet->getType());
389
390
					if(heavyDataWriter)
					{
391
392
						partitionedSet->accept(heavyDataWriter);
						partitionedSet->release();
393
394
395
396
					}
				}
			}
		}
397
		currSet->release();
398
	}
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421

	// Add XdmfMap to map boundary nodes between partitions
	std::vector<boost::shared_ptr<XdmfAttribute> > globalNodeIds;
	for(unsigned int i=0; i<partitionedGrids->getNumberGrids(); ++i)
	{
		boost::shared_ptr<XdmfAttribute> globalNodeId = partitionedGrids->getGrid(i)->getAttribute("GlobalNodeId");
		globalNodeId->read();
		globalNodeIds.push_back(globalNodeId);
	}

	std::vector<boost::shared_ptr<XdmfMap> > maps = XdmfMap::New(globalNodeIds);
	for(unsigned int i=0; i<partitionedGrids->getNumberGrids(); ++i)
	{
		boost::shared_ptr<XdmfMap> map = maps[i];
		partitionedGrids->getGrid(i)->setMap(map);
		if(heavyDataWriter)
		{
			globalNodeIds[i]->release();
			map->accept(heavyDataWriter);
			map->release();
		}
	}

422
	return partitionedGrids;
423
424
}

425
426
#else

427
#include <iostream>
428
429
430
431
#include <sstream>
#include "XdmfDomain.hpp"
#include "XdmfGridCollection.hpp"
#include "XdmfHDF5Writer.hpp"
432
#include "XdmfPartitioner.hpp"
433
434
#include "XdmfReader.hpp"
#include "XdmfWriter.hpp"
435
436
437
438
439
440
441
442
443
444

/**
 * XdmfPartitioner is a command line utility for partitioning Xdmf grids.
 * The XdmfPartitioner uses the metis library to partition Triangular, Quadrilateral, Tetrahedral,
 * and Hexahedral XdmfGrids.
 *
 * Usage:
 *     XdmfPartitioner <path-of-file-to-partition> <num-partitions> (Optional: <path-to-output-file>)
 *
 */
445
int main(int argc, char* argv[])
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
	std::string usage = "Partitions an Xdmf grid using the metis library: \n \n Usage: \n \n   XdmfPartitioner <path-of-file-to-partition> <num-partitions> (Optional: <path-to-output-file>)";
	std::string meshName = "";

	if (argc < 3)
	{
		std::cout << usage << std::endl;
		return 1;
	}

	FILE * refFile = fopen(argv[1], "r");
	if (refFile)
	{
		// Success
		meshName = argv[1];
		fclose(refFile);
	}
	else
	{
		std::cout << "Cannot open file: " << argv[1] << std::endl;
		return 1;
	}

	unsigned int numPartitions = atoi(argv[2]);

	if (argc >= 4)
	{
		meshName = argv[3];
	}

	if(meshName.find_last_of("/\\") != std::string::npos)
	{
		meshName = meshName.substr(meshName.find_last_of("/\\")+1, meshName.length());
	}

	if (meshName.rfind(".") != std::string::npos)
	{
		meshName = meshName.substr(0, meshName.rfind("."));
	}

	if(argc < 4)
	{
		meshName = meshName + "-partitioned";
	}

	boost::shared_ptr<XdmfReader> reader = XdmfReader::New();
	boost::shared_ptr<XdmfDomain> domain = boost::shared_dynamic_cast<XdmfDomain>(reader->read(argv[1]));

494
	if(domain->getNumberGrids() <= 0)
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
	{
		std::cout << "No grids to partition" << std::endl;
		return 1;
	}

	std::stringstream heavyFileName;
	heavyFileName << meshName << ".h5";
	boost::shared_ptr<XdmfHDF5Writer> heavyDataWriter = XdmfHDF5Writer::New(heavyFileName.str());

	boost::shared_ptr<XdmfPartitioner> partitioner = XdmfPartitioner::New();
	boost::shared_ptr<XdmfGridCollection> toWrite = partitioner->partition(domain->getGrid(0), numPartitions, heavyDataWriter);

	boost::shared_ptr<XdmfDomain> newDomain = XdmfDomain::New();
	newDomain->insert(toWrite);

	std::stringstream xmlFileName;
	xmlFileName << meshName << ".xmf";
	boost::shared_ptr<XdmfWriter> writer = XdmfWriter::New(xmlFileName.str(), heavyDataWriter);
	newDomain->accept(writer);

515
	std::cout << "Wrote: " << xmlFileName.str() << std::endl;
516
}
517
518

#endif //BUILD_EXE