-
Notifications
You must be signed in to change notification settings - Fork 4
/
vtkMetisMeshPartitioner.cxx
177 lines (148 loc) · 5.27 KB
/
vtkMetisMeshPartitioner.cxx
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
30
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
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
110
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCosmoDensityProfile.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm 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.
=========================================================================*/
#include "vtkMetisMeshPartitioner.h"
// VTK includes
#include "vtkAlgorithm.h"
#include "vtkCellData.h"
#include "vtkDataObject.h"
#include "vtkIdList.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPolyData.h"
#include "vtkStructuredGrid.h"
#include "vtkUnstructuredGrid.h"
// C/C++ includes
#include <cassert>
#include <vector>
#include "metis.h"
namespace { // private namespace
template<typename T>
void Partition(T* data, idx_t nparts)
{
// STEP 1: Setup metis data-structures
idx_t ne = data->GetNumberOfCells();
idx_t nn = data->GetNumberOfPoints();
// METIS mesh data-structure (see METIS manual Section 5.6)
std::vector< idx_t > eptr;
eptr.resize( ne+1 );
std::vector< idx_t > eind;
// vectors used for weighted partitioning
idx_t *vwgt = NULL;
idx_t *vsize = NULL;
real_t *tpwgts = NULL;
// Return values
idx_t objval = 0; // stores edgecut or total communication volume upon
// successful completion.
std::vector< idx_t > epart; // the element partition vector
epart.resize( ne );
std::vector< idx_t > npart; // the node partition vector
npart.resize( nn );
// STEP 2: Setup the metis options vector
idx_t options[METIS_NOPTIONS];
METIS_SetDefaultOptions(options);
options[METIS_OPTION_NUMBERING] = 0;
if( nparts > 8 )
{
options[METIS_OPTION_PTYPE]=METIS_PTYPE_KWAY;
}
else
{
options[METIS_OPTION_PTYPE]=METIS_PTYPE_RB;
}
// STEP 3: Convert unstructured grid to METIS mesh
eptr[0] = 0;
vtkIdList *cellIds = vtkIdList::New();
for(vtkIdType cellIdx=0; cellIdx < data->GetNumberOfCells(); ++cellIdx)
{
cellIds->Reset();
data->GetCellPoints(cellIdx,cellIds);
for(vtkIdType nodeIdx=0; nodeIdx < cellIds->GetNumberOfIds(); ++nodeIdx )
{
eind.push_back( cellIds->GetId(nodeIdx) );
} // END for all cell nodes
eptr[cellIdx+1] = eind.size();
} // END for all grid cells
cellIds->Delete();
// STEP 4: Partition METIS mesh
int rc = METIS_PartMeshNodal(
&ne, &nn, &eptr[0],&eind[0],
vwgt,vsize,&nparts,tpwgts,options,
&objval,&epart[0],&npart[0]);
// STEP 5: Add output array
vtkNew<vtkIntArray> partitionIds;
partitionIds->SetName("PartitionID");
partitionIds->SetNumberOfValues(data->GetNumberOfCells());
for(vtkIdType cellIdx=0; cellIdx < data->GetNumberOfCells(); ++cellIdx)
{
partitionIds->SetValue(cellIdx, epart[ cellIdx ]);
}
data->GetCellData()->AddArray( partitionIds.GetPointer() );
}
} // end of private namespace
//------------------------------------------------------------------------------
vtkStandardNewMacro(vtkMetisMeshPartitioner);
//------------------------------------------------------------------------------
vtkMetisMeshPartitioner::vtkMetisMeshPartitioner()
{
this->NumberOfPartitions = 5;
}
//------------------------------------------------------------------------------
vtkMetisMeshPartitioner::~vtkMetisMeshPartitioner()
{
}
//------------------------------------------------------------------------------
void vtkMetisMeshPartitioner::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
//------------------------------------------------------------------------------
int vtkMetisMeshPartitioner::RequestData(
vtkInformation* vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// STEP 0: Get input object
vtkInformation *input = inputVector[0]->GetInformationObject( 0 );
assert("pre: input information object is NULL!" && (input != NULL) );
vtkDataObject *inputData = input->Get( vtkDataObject::DATA_OBJECT() );
assert("pre: input grid is NULL!" && (inputData != NULL) );
// STEP 1: Get output object
vtkInformation *output = outputVector->GetInformationObject( 0 );
assert("pre: output information object is NULL!" && (output != NULL) );
vtkDataObject *outputData = output->Get(vtkDataObject::DATA_OBJECT() );
// STEP 2: Shallow copy input object to output
outputData->ShallowCopy( inputData );
// STEP 3: Short-circuit here if we are not partitioning
if( this->NumberOfPartitions < 2 )
{
return 1;
}
if(vtkUnstructuredGrid::SafeDownCast(outputData))
{
Partition(vtkUnstructuredGrid::SafeDownCast(outputData), this->NumberOfPartitions);
}
else if(vtkPolyData::SafeDownCast(outputData))
{
Partition(vtkPolyData::SafeDownCast(outputData), this->NumberOfPartitions);
}
else if(vtkStructuredGrid::SafeDownCast(outputData))
{
Partition(vtkUnstructuredGrid::SafeDownCast(outputData), this->NumberOfPartitions);
}
else
{
vtkErrorMacro(<<"Unsupported data type");
return 0;
}
return 1;
}