00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "metisDecomp.H"
00028 #include "addToRunTimeSelectionTable.H"
00029 #include "floatScalar.H"
00030 #include "IFstream.H"
00031 #include "Time.H"
00032 #include "cyclicPolyPatch.H"
00033
00034 extern "C"
00035 {
00036 #define OMPI_SKIP_MPICXX
00037 # include "metis.h"
00038 }
00039
00040
00041
00042
00043 namespace Foam
00044 {
00045 defineTypeNameAndDebug(metisDecomp, 0);
00046
00047 addToRunTimeSelectionTable
00048 (
00049 decompositionMethod,
00050 metisDecomp,
00051 dictionaryMesh
00052 );
00053 }
00054
00055
00056
00057
00058
00059 Foam::label Foam::metisDecomp::decompose
00060 (
00061 const List<int>& adjncy,
00062 const List<int>& xadj,
00063
00064 List<int>& finalDecomp
00065 )
00066 {
00067
00068 int numFlag = 0;
00069
00070
00071
00072
00073 word method("k-way");
00074
00075
00076 List<int> options(5, 0);
00077
00078
00079
00080 Field<floatScalar> processorWeights;
00081
00082
00083 List<int> cellWeights;
00084
00085
00086 List<int> faceWeights;
00087
00088
00089 if (decompositionDict_.found("metisCoeffs"))
00090 {
00091 const dictionary& metisCoeffs =
00092 decompositionDict_.subDict("metisCoeffs");
00093 word weightsFile;
00094
00095 if (metisCoeffs.readIfPresent("method", method))
00096 {
00097 if (method != "recursive" && method != "k-way")
00098 {
00099 FatalErrorIn("metisDecomp::decompose()")
00100 << "Method " << method << " in metisCoeffs in dictionary : "
00101 << decompositionDict_.name()
00102 << " should be 'recursive' or 'k-way'"
00103 << exit(FatalError);
00104 }
00105
00106 Info<< "metisDecomp : Using Metis method " << method
00107 << nl << endl;
00108 }
00109
00110 if (metisCoeffs.readIfPresent("options", options))
00111 {
00112 if (options.size() != 5)
00113 {
00114 FatalErrorIn("metisDecomp::decompose()")
00115 << "Number of options in metisCoeffs in dictionary : "
00116 << decompositionDict_.name()
00117 << " should be 5"
00118 << exit(FatalError);
00119 }
00120
00121 Info<< "metisDecomp : Using Metis options " << options
00122 << nl << endl;
00123 }
00124
00125 if (metisCoeffs.readIfPresent("processorWeights", processorWeights))
00126 {
00127 processorWeights /= sum(processorWeights);
00128
00129 if (processorWeights.size() != nProcessors_)
00130 {
00131 FatalErrorIn("metisDecomp::decompose(const pointField&)")
00132 << "Number of processor weights "
00133 << processorWeights.size()
00134 << " does not equal number of domains " << nProcessors_
00135 << exit(FatalError);
00136 }
00137 }
00138
00139 if (metisCoeffs.readIfPresent("cellWeightsFile", weightsFile))
00140 {
00141 Info<< "metisDecomp : Using cell-based weights." << endl;
00142
00143 IOList<int> cellIOWeights
00144 (
00145 IOobject
00146 (
00147 weightsFile,
00148 mesh_.time().timeName(),
00149 mesh_,
00150 IOobject::MUST_READ,
00151 IOobject::AUTO_WRITE
00152 )
00153 );
00154 cellWeights.transfer(cellIOWeights);
00155
00156 if (cellWeights.size() != xadj.size()-1)
00157 {
00158 FatalErrorIn("metisDecomp::decompose(const pointField&)")
00159 << "Number of cell weights " << cellWeights.size()
00160 << " does not equal number of cells " << xadj.size()-1
00161 << exit(FatalError);
00162 }
00163 }
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207 }
00208
00209 int numCells = xadj.size()-1;
00210 int nProcs = nProcessors_;
00211
00212
00213 finalDecomp.setSize(numCells);
00214
00215
00216 int edgeCut = 0;
00217
00218
00219 int wgtFlag = 0;
00220 int* vwgtPtr = NULL;
00221 int* adjwgtPtr = NULL;
00222
00223 if (cellWeights.size())
00224 {
00225 vwgtPtr = cellWeights.begin();
00226 wgtFlag += 2;
00227 }
00228 if (faceWeights.size())
00229 {
00230 adjwgtPtr = faceWeights.begin();
00231 wgtFlag += 1;
00232 }
00233
00234 if (method == "recursive")
00235 {
00236 if (processorWeights.size())
00237 {
00238 METIS_WPartGraphRecursive
00239 (
00240 &numCells,
00241 const_cast<List<int>&>(xadj).begin(),
00242 const_cast<List<int>&>(adjncy).begin(),
00243 vwgtPtr,
00244 adjwgtPtr,
00245 &wgtFlag,
00246 &numFlag,
00247 &nProcs,
00248 processorWeights.begin(),
00249 options.begin(),
00250 &edgeCut,
00251 finalDecomp.begin()
00252 );
00253 }
00254 else
00255 {
00256 METIS_PartGraphRecursive
00257 (
00258 &numCells,
00259 const_cast<List<int>&>(xadj).begin(),
00260 const_cast<List<int>&>(adjncy).begin(),
00261 vwgtPtr,
00262 adjwgtPtr,
00263 &wgtFlag,
00264 &numFlag,
00265 &nProcs,
00266 options.begin(),
00267 &edgeCut,
00268 finalDecomp.begin()
00269 );
00270 }
00271 }
00272 else
00273 {
00274 if (processorWeights.size())
00275 {
00276 METIS_WPartGraphKway
00277 (
00278 &numCells,
00279 const_cast<List<int>&>(xadj).begin(),
00280 const_cast<List<int>&>(adjncy).begin(),
00281 vwgtPtr,
00282 adjwgtPtr,
00283 &wgtFlag,
00284 &numFlag,
00285 &nProcs,
00286 processorWeights.begin(),
00287 options.begin(),
00288 &edgeCut,
00289 finalDecomp.begin()
00290 );
00291 }
00292 else
00293 {
00294 METIS_PartGraphKway
00295 (
00296 &numCells,
00297 const_cast<List<int>&>(xadj).begin(),
00298 const_cast<List<int>&>(adjncy).begin(),
00299 vwgtPtr,
00300 adjwgtPtr,
00301 &wgtFlag,
00302 &numFlag,
00303 &nProcs,
00304 options.begin(),
00305 &edgeCut,
00306 finalDecomp.begin()
00307 );
00308 }
00309 }
00310
00311 return edgeCut;
00312 }
00313
00314
00315
00316
00317 Foam::metisDecomp::metisDecomp
00318 (
00319 const dictionary& decompositionDict,
00320 const polyMesh& mesh
00321 )
00322 :
00323 decompositionMethod(decompositionDict),
00324 mesh_(mesh)
00325 {}
00326
00327
00328
00329
00330 Foam::labelList Foam::metisDecomp::decompose(const pointField& points)
00331 {
00332 if (points.size() != mesh_.nCells())
00333 {
00334 FatalErrorIn("metisDecomp::decompose(const pointField&)")
00335 << "Can use this decomposition method only for the whole mesh"
00336 << endl
00337 << "and supply one coordinate (cellCentre) for every cell." << endl
00338 << "The number of coordinates " << points.size() << endl
00339 << "The number of cells in the mesh " << mesh_.nCells()
00340 << exit(FatalError);
00341 }
00342
00343
00344
00345
00346
00347 List<int> xadj(mesh_.nCells()+1);
00348
00349
00350
00351 label nInternalFaces = 2*mesh_.nInternalFaces();
00352
00353
00354
00355 const polyBoundaryMesh& pbm = mesh_.boundaryMesh();
00356
00357 forAll(pbm, patchi)
00358 {
00359 if (isA<cyclicPolyPatch>(pbm[patchi]))
00360 {
00361 nInternalFaces += pbm[patchi].size();
00362 }
00363 }
00364
00365
00366
00367 List<int> adjncy(nInternalFaces);
00368
00369
00370
00371 label freeAdj = 0;
00372
00373 for (label cellI = 0; cellI < mesh_.nCells(); cellI++)
00374 {
00375 xadj[cellI] = freeAdj;
00376
00377 const labelList& cFaces = mesh_.cells()[cellI];
00378
00379 forAll(cFaces, i)
00380 {
00381 label faceI = cFaces[i];
00382
00383 if
00384 (
00385 mesh_.isInternalFace(faceI)
00386 || isA<cyclicPolyPatch>(pbm[pbm.whichPatch(faceI)])
00387 )
00388 {
00389 freeAdj++;
00390 }
00391 }
00392 }
00393 xadj[mesh_.nCells()] = freeAdj;
00394
00395
00396
00397
00398
00399 labelList nFacesPerCell(mesh_.nCells(), 0);
00400
00401
00402 for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
00403 {
00404 label own = mesh_.faceOwner()[faceI];
00405 label nei = mesh_.faceNeighbour()[faceI];
00406
00407 adjncy[xadj[own] + nFacesPerCell[own]++] = nei;
00408 adjncy[xadj[nei] + nFacesPerCell[nei]++] = own;
00409 }
00410
00411
00412 forAll(pbm, patchi)
00413 {
00414 if (isA<cyclicPolyPatch>(pbm[patchi]))
00415 {
00416 const unallocLabelList& faceCells = pbm[patchi].faceCells();
00417
00418 label sizeby2 = faceCells.size()/2;
00419
00420 for (label facei=0; facei<sizeby2; facei++)
00421 {
00422 label own = faceCells[facei];
00423 label nei = faceCells[facei + sizeby2];
00424
00425 adjncy[xadj[own] + nFacesPerCell[own]++] = nei;
00426 adjncy[xadj[nei] + nFacesPerCell[nei]++] = own;
00427 }
00428 }
00429 }
00430
00431
00432 List<int> finalDecomp;
00433 decompose(adjncy, xadj, finalDecomp);
00434
00435
00436 labelList decomp(finalDecomp.size());
00437 forAll(decomp, i)
00438 {
00439 decomp[i] = finalDecomp[i];
00440 }
00441 return decomp;
00442 }
00443
00444
00445
00446 void Foam::metisDecomp::calcMetisCSR
00447 (
00448 const labelListList& cellCells,
00449 List<int>& adjncy,
00450 List<int>& xadj
00451 )
00452 {
00453
00454 label nConnections = 0;
00455
00456 forAll(cellCells, coarseI)
00457 {
00458 nConnections += cellCells[coarseI].size();
00459 }
00460
00461
00462
00463 adjncy.setSize(nConnections);
00464
00465 xadj.setSize(cellCells.size()+1);
00466
00467
00468
00469
00470 label freeAdj = 0;
00471
00472 forAll(cellCells, coarseI)
00473 {
00474 xadj[coarseI] = freeAdj;
00475
00476 const labelList& cCells = cellCells[coarseI];
00477
00478 forAll(cCells, i)
00479 {
00480 adjncy[freeAdj++] = cCells[i];
00481 }
00482 }
00483 xadj[cellCells.size()] = freeAdj;
00484 }
00485
00486
00487 Foam::labelList Foam::metisDecomp::decompose
00488 (
00489 const labelList& agglom,
00490 const pointField& agglomPoints
00491 )
00492 {
00493 if (agglom.size() != mesh_.nCells())
00494 {
00495 FatalErrorIn
00496 (
00497 "parMetisDecomp::decompose(const labelList&, const pointField&)"
00498 ) << "Size of cell-to-coarse map " << agglom.size()
00499 << " differs from number of cells in mesh " << mesh_.nCells()
00500 << exit(FatalError);
00501 }
00502
00503
00504
00505
00506 List<int> adjncy;
00507 List<int> xadj;
00508 {
00509
00510 labelListList cellCells;
00511 calcCellCells
00512 (
00513 mesh_,
00514 agglom,
00515 agglomPoints.size(),
00516 cellCells
00517 );
00518
00519 calcMetisCSR(cellCells, adjncy, xadj);
00520 }
00521
00522
00523 List<int> finalDecomp;
00524 decompose(adjncy, xadj, finalDecomp);
00525
00526
00527
00528 labelList fineDistribution(agglom.size());
00529
00530 forAll(fineDistribution, i)
00531 {
00532 fineDistribution[i] = finalDecomp[agglom[i]];
00533 }
00534
00535 return fineDistribution;
00536 }
00537
00538
00539 Foam::labelList Foam::metisDecomp::decompose
00540 (
00541 const labelListList& globalCellCells,
00542 const pointField& cellCentres
00543 )
00544 {
00545 if (cellCentres.size() != globalCellCells.size())
00546 {
00547 FatalErrorIn
00548 (
00549 "metisDecomp::decompose(const pointField&, const labelListList&)"
00550 ) << "Inconsistent number of cells (" << globalCellCells.size()
00551 << ") and number of cell centres (" << cellCentres.size()
00552 << ")." << exit(FatalError);
00553 }
00554
00555
00556
00557
00558
00559
00560 List<int> adjncy;
00561 List<int> xadj;
00562 calcMetisCSR(globalCellCells, adjncy, xadj);
00563
00564
00565
00566 List<int> finalDecomp;
00567 decompose(adjncy, xadj, finalDecomp);
00568
00569
00570 labelList decomp(finalDecomp.size());
00571 forAll(decomp, i)
00572 {
00573 decomp[i] = finalDecomp[i];
00574 }
00575 return decomp;
00576 }
00577
00578
00579