Nektar++
MeshPartitionScotch.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: MeshPartitionScotch.cpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: Scotch partitioner interface
32//
33///////////////////////////////////////////////////////////////////////////////
34
36
38{
39
43 "Partitioning using the Scotch library.");
44
47 "use-scotch", "", "Use Scotch for mesh partitioning.");
48
51 LibUtilities::CommSharedPtr comm, int meshDim,
52 std::map<int, MeshEntity> element, CompositeDescriptor compMap)
53 : MeshPartition(session, comm, meshDim, element, compMap)
54{
55}
56
58{
59}
60
62 int &nVerts, [[maybe_unused]] int &nVertConds,
67 [[maybe_unused]] Nektar::Array<Nektar::OneD, int> &edgeWgt, int &nparts,
68 int &volume, Nektar::Array<Nektar::OneD, int> &part)
69{
70 int wgtflag = 0;
71 int *vwgt = nullptr;
72 int *vsize = nullptr;
73 if (vertWgt.size() > 0)
74 {
75 wgtflag += 1;
76 vwgt = &vertWgt[0];
77 }
78 if (vertSize.size() > 0)
79 {
80 wgtflag += 2;
81 vsize = &vertSize[0];
82 }
83 int numflag = 0;
84
85 PartGraphVKway(&nVerts, &xadj[0], &adjcy[0], vwgt, vsize, &wgtflag,
86 &numflag, &nparts, &volume, &part[0]);
87}
88
90 const SCOTCH_Num *const n, const SCOTCH_Num *const xadj,
91 const SCOTCH_Num *const adjncy, const SCOTCH_Num *const vwgt,
92 const SCOTCH_Num *const vsize, const SCOTCH_Num *const wgtflag,
93 const SCOTCH_Num *const numflag, const SCOTCH_Num *const nparts,
94 SCOTCH_Num *const volume, SCOTCH_Num *const part)
95{
96 SCOTCH_Num baseval;
97 const SCOTCH_Num *vwgt2;
98 const SCOTCH_Num *vsize2;
99 SCOTCH_Num vsizval; /// Communication volume of current vertex
100 SCOTCH_Num vertnbr;
101 SCOTCH_Num vertnum;
102 SCOTCH_Num edgenum;
103 const SCOTCH_Num *edgetax;
104 const SCOTCH_Num *parttax;
105 SCOTCH_Num *nghbtab;
106 SCOTCH_Num commvol;
107
108 vsize2 = ((*wgtflag & 1) != 0) ? vsize : nullptr;
109 vwgt2 = ((*wgtflag & 2) != 0) ? vwgt : nullptr;
110 baseval = *numflag;
111 vertnbr = *n;
112 edgetax = adjncy - baseval;
113
114 // If no communication load data provided
115 if (vsize2 == nullptr)
116 {
117 if (PartGraph2(n, xadj, adjncy, vwgt2, nullptr, numflag, nparts, part,
118 SCOTCH_STRATQUALITY, 0.01) != 0)
119 {
120 return;
121 }
122 }
123
124 // Will have to turn communication volumes into edge loads
125 else
126 {
127 const SCOTCH_Num *vsiztax;
128 SCOTCH_Num edgenbr;
129 SCOTCH_Num *edlotax;
130 int o;
131
132 edgenbr = xadj[vertnbr] - baseval;
133 if ((edlotax = (SCOTCH_Num *)malloc(edgenbr * sizeof(SCOTCH_Num))) ==
134 nullptr)
135 {
136 return;
137 }
138
139 edlotax -= baseval; // Base access to edlotax
140 vsiztax = vsize2 - baseval;
141
142 // Un-based scan of vertex array xadj
143 for (vertnum = 0, edgenum = baseval; vertnum < vertnbr; vertnum++)
144 {
145 SCOTCH_Num vsizval; // Communication size of current vertex
146 SCOTCH_Num edgennd;
147
148 vsizval = vsize2[vertnum];
149 // Based traversal of edge array adjncy
150 for (edgennd = xadj[vertnum + 1]; edgenum < edgennd; edgenum++)
151 {
152
153 SCOTCH_Num vertend; // Based end vertex number
154
155 vertend = edgetax[edgenum];
156 edlotax[edgenum] = vsizval + vsiztax[vertend];
157 }
158 }
159
160 o = PartGraph2(n, xadj, adjncy, vwgt2, edlotax + baseval, numflag,
161 nparts, part, SCOTCH_STRATQUALITY, 0.01);
162
163 free(edlotax + baseval);
164
165 if (o != 0)
166 {
167 return;
168 }
169 }
170
171 if ((nghbtab = (SCOTCH_Num *)malloc(*nparts * sizeof(SCOTCH_Num))) ==
172 nullptr)
173 {
174 return;
175 }
176
177 memset(nghbtab, ~0, *nparts * sizeof(SCOTCH_Num));
178
179 parttax = part - baseval;
180 vsizval = 1; // Assume no vertex communication sizes
181
182 // Un-based scan of vertex array xadj
183 for (vertnum = 0, edgenum = baseval, commvol = 0; vertnum < vertnbr;
184 vertnum++)
185 {
186 SCOTCH_Num partval;
187 SCOTCH_Num edgennd;
188
189 partval = part[vertnum];
190 nghbtab[partval] = vertnum; // Do not count local neighbors in
191 // communication volume
192 if (vsize2 != nullptr)
193 {
194 vsizval = vsize2[vertnum];
195 }
196
197 // Based traversal of edge array adjncy
198 for (edgennd = xadj[vertnum + 1]; edgenum < edgennd; edgenum++)
199 {
200 SCOTCH_Num vertend; // Based end vertex number
201 SCOTCH_Num partend;
202
203 vertend = edgetax[edgenum];
204 partend = parttax[vertend];
205
206 // If first neighbor in this part set part as accounted for
207 if (nghbtab[partend] != vertnum)
208 {
209 nghbtab[partend] = vertnum;
210 commvol += vsizval;
211 }
212 }
213 }
214 *volume = commvol;
215
216 free(nghbtab);
217}
218
220 const SCOTCH_Num *const n, const SCOTCH_Num *const xadj,
221 const SCOTCH_Num *const adjncy, const SCOTCH_Num *const vwgt,
222 const SCOTCH_Num *const adjwgt, const SCOTCH_Num *const numflag,
223 const SCOTCH_Num *const nparts, SCOTCH_Num *const part, SCOTCH_Num flagval,
224 double kbalval)
225{
226 // Scotch graph object to interface with libScotch
227 SCOTCH_Graph *grafdat = SCOTCH_graphAlloc();
228 ASSERTL0(grafdat != nullptr,
229 "Failed to allocate Scotch graph for partitioning.");
230
231 SCOTCH_Strat stradat;
232 SCOTCH_Num baseval;
233 SCOTCH_Num vertnbr;
234 int o;
235
236 ASSERTL0(SCOTCH_graphInit(grafdat) == 0,
237 "Failed to initialise Scotch graph for partitioning.");
238
239 baseval = *numflag;
240 vertnbr = *n;
241
242 o = 1; // Assume something will go wrong
243 if (SCOTCH_graphBuild(grafdat, baseval, vertnbr, xadj, xadj + 1, vwgt,
244 nullptr, xadj[vertnbr] - baseval, adjncy,
245 adjwgt) == 0)
246 {
247 SCOTCH_stratInit(&stradat);
248 SCOTCH_stratGraphMapBuild(&stradat, flagval, *nparts, kbalval);
249#ifdef SCOTCH_DEBUG_ALL
250 // TRICK: next instruction called only if graph is consistent
251 if (SCOTCH_graphCheck(grafdat) == 0)
252#endif /* SCOTCH_DEBUG_ALL */
253 o = SCOTCH_graphPart(grafdat, *nparts, &stradat, part);
254 SCOTCH_stratExit(&stradat);
255 }
256 SCOTCH_graphExit(grafdat);
257
258 if (o != 0)
259 {
260 return (1);
261 }
262
263 if (baseval != 0)
264 { // MeTiS part array is based, Scotch is not
265 SCOTCH_Num vertnum;
266
267 for (vertnum = 0; vertnum < vertnbr; vertnum++)
268 {
269 part[vertnum] += baseval;
270 }
271 }
272
273 return (0);
274}
275
276} // namespace Nektar::SpatialDomains
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:197
static std::string RegisterCmdLineFlag(const std::string &pName, const std::string &pShortName, const std::string &pDescription)
Registers a command-line flag with the session reader.
static MeshPartitionSharedPtr create(const LibUtilities::SessionReaderSharedPtr session, LibUtilities::CommSharedPtr comm, int meshDim, std::map< int, MeshEntity > element, CompositeDescriptor compMap)
Creates an instance of this class.
void v_PartitionGraphImpl(int &nVerts, int &nVertConds, Nektar::Array< Nektar::OneD, int > &xadj, Nektar::Array< Nektar::OneD, int > &adjcy, Nektar::Array< Nektar::OneD, int > &vertWgt, Nektar::Array< Nektar::OneD, int > &vertSize, Nektar::Array< Nektar::OneD, int > &edgeWgt, int &nparts, int &volume, Nektar::Array< Nektar::OneD, int > &part) final
static std::string className
Name of class.
MeshPartitionScotch(const LibUtilities::SessionReaderSharedPtr session, LibUtilities::CommSharedPtr comm, int meshDim, std::map< int, MeshEntity > element, CompositeDescriptor compMap)
int PartGraph2(const SCOTCH_Num *const n, const SCOTCH_Num *const xadj, const SCOTCH_Num *const adjncy, const SCOTCH_Num *const vwgt, const SCOTCH_Num *const adjwgt, const SCOTCH_Num *const numflag, const SCOTCH_Num *const nparts, SCOTCH_Num *const part, SCOTCH_Num flagval, double kbalval)
void PartGraphVKway(const SCOTCH_Num *const n, const SCOTCH_Num *const xadj, const SCOTCH_Num *const adjncy, const SCOTCH_Num *const vwgt, const SCOTCH_Num *const vsize, const SCOTCH_Num *const wgtflag, const SCOTCH_Num *const numflag, const SCOTCH_Num *const nparts, SCOTCH_Num *const volume, SCOTCH_Num *const part)
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition: Comm.h:55
std::map< int, std::pair< LibUtilities::ShapeType, std::vector< int > > > CompositeDescriptor
Definition: MeshGraph.h:61
MeshPartitionFactory & GetMeshPartitionFactory()