XdmfPartitioner.cpp 17.2 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
34
#include <sstream>
#include "XdmfArray.hpp"
35
#include "XdmfAttribute.hpp"
36
37
#include "XdmfAttributeCenter.hpp"
#include "XdmfAttributeType.hpp"
38
#include "XdmfGeometry.hpp"
39
#include "XdmfGeometryType.hpp"
40
#include "XdmfGrid.hpp"
41
#include "XdmfGridCollection.hpp"
42
#include "XdmfGridCollectionType.hpp"
43
#include "XdmfHDF5Writer.hpp"
44
#include "XdmfPartitioner.hpp"
45
#include "XdmfSet.hpp"
46
#include "XdmfSetType.hpp"
47
#include "XdmfTopology.hpp"
48
#include "XdmfTopologyType.hpp"
49
50
51
52
53
54
55
56
57

XdmfPartitioner::XdmfPartitioner()
{
}

XdmfPartitioner::~XdmfPartitioner()
{
}

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

64
	boost::shared_ptr<const XdmfTopologyType> topologyType = gridToPartition->getTopology()->getType();
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
	if(topologyType == XdmfTopologyType::Triangle() || topologyType == XdmfTopologyType::Triangle_6())
	{
		metisElementType = 1;
		numNodesPerElement = 3;
	}
	else if(topologyType == XdmfTopologyType::Quadrilateral() || topologyType == XdmfTopologyType::Quadrilateral_8())
	{
		metisElementType = 4;
		numNodesPerElement = 4;
	}
	else if(topologyType == XdmfTopologyType::Tetrahedron() || topologyType == XdmfTopologyType::Tetrahedron_10())
	{
		metisElementType = 2;
		numNodesPerElement = 4;
	}
	else if(topologyType == XdmfTopologyType::Hexahedron() || topologyType == XdmfTopologyType::Hexahedron_20() ||
			topologyType == XdmfTopologyType::Hexahedron_24() || topologyType == XdmfTopologyType::Hexahedron_27() ||
			topologyType == XdmfTopologyType::Hexahedron_64())
	{
		metisElementType = 3;
		numNodesPerElement = 8;
	}
	else
	{
		assert(false);
	}

	if(!gridToPartition->getTopology()->getArray()->isInitialized())
	{
		gridToPartition->getTopology()->getArray()->read();
	}

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

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

	// Need to remap connectivity for nonlinear elements so that metis handles it properly.
	std::map<idxtype, idxtype> xdmfIdToMetisId;
108
	if(numNodesPerElement != topologyType->getNodesPerElement())
109
	{
110
		unsigned int index = 0;
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
		for (unsigned int i=0; i<numElements * numNodesPerElement; ++i)
		{
			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];
160
		for (unsigned int j=0; j<topologyType->getNodesPerElement(); ++j)
161
162
163
164
165
		{
			unsigned int globalNodeId = gridToPartition->getTopology()->getArray()->getValueCopy<unsigned int>(totalIndex);
			if (globalToLocalNodeIdMap[partitionId].count(globalNodeId) == 0)
			{
				// Have not seen this node, need to add to map
166
				unsigned int size = globalToLocalNodeIdMap[partitionId].size();
167
168
169
170
171
172
				globalToLocalNodeIdMap[partitionId][globalNodeId] = size;
			}
			totalIndex++;
		}
		globalElementIds[partitionId].push_back(i);
	}
173
	delete [] elementsPartition;
174

175
	boost::shared_ptr<XdmfAttribute> globalNodeIds = gridToPartition->getAttribute("GlobalNodeId");
176
	bool generateGlobalNodeIds = !globalNodeIds;
177
178

	boost::shared_ptr<XdmfGridCollection> partitionedGrids = XdmfGridCollection::New();
179
	partitionedGrids->setType(XdmfGridCollectionType::Spatial());
180
181
182
183
184
185

	if(!gridToPartition->getGeometry()->getArray()->isInitialized())
	{
		gridToPartition->getGeometry()->getArray()->read();
	}

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

		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
202
			partitioned->getGeometry()->setType(gridToPartition->getGeometry()->getType());
203
			boost::shared_ptr<XdmfArray> geometryVals = partitioned->getGeometry()->getArray();
204
			unsigned int numDimensions = partitioned->getGeometry()->getType()->getDimensions();
205
206
			geometryVals->initialize(gridToPartition->getGeometry()->getArray()->getType());
			geometryVals->resize<unsigned int>(currNodeMap.size() * numDimensions);
207
208
209
210
211
212
213
214
215
216
217
218
219

			for(std::map<unsigned int, unsigned int>::const_iterator iter = currNodeMap.begin(); iter != currNodeMap.end(); ++iter)
			{
				geometryVals->copyValues(iter->second * numDimensions, gridToPartition->getGeometry()->getArray(), iter->first * numDimensions, numDimensions);
			}

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

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

			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
229
				for(unsigned int j=0; j<topologyType->getNodesPerElement(); ++j)
230
				{
231
					unsigned int globalNodeId = currNodeMap[gridToPartition->getTopology()->getArray()->getValueCopy<unsigned int>(*iter * topologyType->getNodesPerElement() + j)];
232
233
234
235
236
237
238
239
240
241
					topologyVals->copyValues(index, &globalNodeId, 1);
					index++;
				}
			}

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

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

				boost::shared_ptr<XdmfArray> globalNodeIdsVals = globalNodeIds->getArray();
251
				globalNodeIdsVals->resize<unsigned int>(currNodeMap.size());
252
253
254
255
256
				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);
257

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

267
268
	gridToPartition->getGeometry()->getArray()->release();
	gridToPartition->getTopology()->getArray()->release();
269

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

340
341
342
343
344
345
346
347
348
349
350
351
352
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
388
389
390
391
392
393
394
395
396
397
	// Split sets into proper partitions
	for(unsigned int i=0; i<gridToPartition->getNumberOfSets(); ++i)
	{
		boost::shared_ptr<XdmfSet> currSet = gridToPartition->getSet(i);
		if(!currSet->getArray()->isInitialized())
		{
			currSet->getArray()->read();
		}
		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<XdmfArray> setVals = XdmfArray::New();
				if(currSet->getSetType() == XdmfSetType::Cell() || currSet->getSetType() == XdmfSetType::Face() || currSet->getSetType() == XdmfSetType::Edge())
				{
					for(unsigned int k=0; k<currSet->getArray()->getSize(); ++k)
					{
						std::vector<unsigned int>::const_iterator val = std::find(currElemIds.begin(), currElemIds.end(), currSet->getArray()->getValueCopy<unsigned int>(k));
						if(val != currElemIds.end())
						{
							unsigned int valToPush = val - currElemIds.begin();
							setVals->pushBack(valToPush);
						}
					}
				}
				else if(currSet->getSetType() == XdmfSetType::Node())
				{
					for(unsigned int k=0; k<currSet->getArray()->getSize(); ++k)
					{
						std::map<unsigned int, unsigned int>::const_iterator val = currNodeMap.find(currSet->getArray()->getValueCopy<unsigned int>(k));
						if(val != currNodeMap.end())
						{
							setVals->pushBack(val->second);
						}
					}
				}
				if(setVals->getSize() > 0)
				{
					boost::shared_ptr<XdmfSet> createdSet = XdmfSet::New();
					createdSet->setName(currSet->getName());
					createdSet->setSetType(currSet->getSetType());
					createdSet->setArray(setVals);
					partitioned->insert(createdSet);
					if(heavyDataWriter)
					{
						createdSet->getArray()->accept(heavyDataWriter);
						createdSet->getArray()->release();
					}
				}
			}
		}
		currSet->getArray()->release();
	}
398
	return partitionedGrids;
399
400
}

401
402
403
404
405
406
#else

#include <sstream>
#include "XdmfDomain.hpp"
#include "XdmfGridCollection.hpp"
#include "XdmfHDF5Writer.hpp"
407
#include "XdmfPartitioner.hpp"
408
409
#include "XdmfReader.hpp"
#include "XdmfWriter.hpp"
410
411
412
413
414
415
416
417
418
419

/**
 * 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>)
 *
 */
420
int main(int argc, char* argv[])
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
	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]));

469
	if(domain->getNumberGrids() <= 0)
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
	{
		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);

490
	std::cout << "Wrote: " << xmlFileName.str() << std::endl;
491
}
492
493

#endif //BUILD_EXE