OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

metisDecomp.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00057 
00058 // Call Metis with options from dictionary.
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     // C style numbering
00068     int numFlag = 0;
00069 
00070     // Method of decomposition
00071     // recursive: multi-level recursive bisection (default)
00072     // k-way: multi-level k-way
00073     word method("k-way");
00074 
00075     // decomposition options. 0 = use defaults
00076     List<int> options(5, 0);
00077 
00078     // processor weights initialised with no size, only used if specified in
00079     // a file
00080     Field<floatScalar> processorWeights;
00081 
00082     // cell weights (so on the vertices of the dual)
00083     List<int> cellWeights;
00084 
00085     // face weights (so on the edges of the dual)
00086     List<int> faceWeights;
00087 
00088     // Check for user supplied weights and decomp options
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         //- faceWeights disabled. Only makes sense for cellCells from mesh.
00166         //if (metisCoeffs.readIfPresent("faceWeightsFile", weightsFile))
00167         //{
00168         //    Info<< "metisDecomp : Using face-based weights." << endl;
00169         //
00170         //    IOList<int> weights
00171         //    (
00172         //        IOobject
00173         //        (
00174         //            weightsFile,
00175         //            mesh_.time().timeName(),
00176         //            mesh_,
00177         //            IOobject::MUST_READ,
00178         //            IOobject::AUTO_WRITE
00179         //        )
00180         //    );
00181         //
00182         //    if (weights.size() != adjncy.size()/2)
00183         //    {
00184         //        FatalErrorIn("metisDecomp::decompose(const pointField&)")
00185         //            << "Number of face weights " << weights.size()
00186         //            << " does not equal number of internal faces "
00187         //            << adjncy.size()/2
00188         //            << exit(FatalError);
00189         //    }
00190         //
00191         //    // Assume symmetric weights. Keep same ordering as adjncy.
00192         //    faceWeights.setSize(adjncy.size());
00193         //
00194         //    labelList nFacesPerCell(mesh_.nCells(), 0);
00195         //
00196         //    for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
00197         //    {
00198         //        label w = weights[faceI];
00199         //
00200         //        label own = mesh_.faceOwner()[faceI];
00201         //        label nei = mesh_.faceNeighbour()[faceI];
00202         //
00203         //        faceWeights[xadj[own] + nFacesPerCell[own]++] = w;
00204         //        faceWeights[xadj[nei] + nFacesPerCell[nei]++] = w;
00205         //    }
00206         //}
00207     }
00208 
00209     int numCells = xadj.size()-1;
00210     int nProcs = nProcessors_;
00211 
00212     // output: cell -> processor addressing
00213     finalDecomp.setSize(numCells);
00214 
00215     // output: number of cut edges
00216     int edgeCut = 0;
00217 
00218     // Vertex weight info
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;       // Weights on vertices
00227     }
00228     if (faceWeights.size())
00229     {
00230         adjwgtPtr = faceWeights.begin();
00231         wgtFlag += 1;       // Weights on edges
00232     }
00233 
00234     if (method == "recursive")
00235     {
00236         if (processorWeights.size())
00237         {
00238             METIS_WPartGraphRecursive
00239             (
00240                 &numCells,         // num vertices in graph
00241                 const_cast<List<int>&>(xadj).begin(),   // indexing into adjncy
00242                 const_cast<List<int>&>(adjncy).begin(), // neighbour info
00243                 vwgtPtr,           // vertexweights
00244                 adjwgtPtr,         // no edgeweights
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,         // num vertices in graph
00259                 const_cast<List<int>&>(xadj).begin(),   // indexing into adjncy
00260                 const_cast<List<int>&>(adjncy).begin(), // neighbour info
00261                 vwgtPtr,           // vertexweights
00262                 adjwgtPtr,         // no edgeweights
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,         // num vertices in graph
00279                 const_cast<List<int>&>(xadj).begin(),   // indexing into adjncy
00280                 const_cast<List<int>&>(adjncy).begin(), // neighbour info
00281                 vwgtPtr,           // vertexweights
00282                 adjwgtPtr,         // no edgeweights
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,         // num vertices in graph
00297                 const_cast<List<int>&>(xadj).begin(),   // indexing into adjncy
00298                 const_cast<List<int>&>(adjncy).begin(), // neighbour info
00299                 vwgtPtr,           // vertexweights
00300                 adjwgtPtr,         // no edgeweights
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 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
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 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
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     // Make Metis CSR (Compressed Storage Format) storage
00344     //   adjncy      : contains neighbours (= edges in graph)
00345     //   xadj(celli) : start of information in adjncy for celli
00346 
00347     List<int> xadj(mesh_.nCells()+1);
00348 
00349     // Initialise the number of internal faces of the cells to twice the
00350     // number of internal faces
00351     label nInternalFaces = 2*mesh_.nInternalFaces();
00352 
00353     // Check the boundary for coupled patches and add to the number of
00354     // internal faces
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     // Create the adjncy array the size of the total number of internal and
00366     // coupled faces
00367     List<int> adjncy(nInternalFaces);
00368 
00369     // Fill in xadj
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     // Fill in adjncy
00397     // ~~~~~~~~~~~~~~
00398 
00399     labelList nFacesPerCell(mesh_.nCells(), 0);
00400 
00401     // Internal faces
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     // Coupled faces. Only cyclics done.
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     // Decompose using default weights
00432     List<int> finalDecomp;
00433     decompose(adjncy, xadj, finalDecomp);
00434 
00435     // Copy back to labelList
00436     labelList decomp(finalDecomp.size());
00437     forAll(decomp, i)
00438     {
00439         decomp[i] = finalDecomp[i];
00440     }
00441     return decomp;
00442 }
00443 
00444 
00445 // From cell-cell connections to Metis format (like CompactListList)
00446 void Foam::metisDecomp::calcMetisCSR
00447 (
00448     const labelListList& cellCells,
00449     List<int>& adjncy,
00450     List<int>& xadj
00451 )
00452 {
00453     // Count number of internal faces
00454     label nConnections = 0;
00455 
00456     forAll(cellCells, coarseI)
00457     {
00458         nConnections += cellCells[coarseI].size();
00459     }
00460 
00461     // Create the adjncy array as twice the size of the total number of
00462     // internal faces
00463     adjncy.setSize(nConnections);
00464 
00465     xadj.setSize(cellCells.size()+1);
00466 
00467 
00468     // Fill in xadj
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     // Make Metis CSR (Compressed Storage Format) storage
00504     //   adjncy      : contains neighbours (= edges in graph)
00505     //   xadj(celli) : start of information in adjncy for celli
00506     List<int> adjncy;
00507     List<int> xadj;
00508     {
00509         // Get cellCells on coarse mesh.
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     // Decompose using default weights
00523     List<int> finalDecomp;
00524     decompose(adjncy, xadj, finalDecomp);
00525 
00526 
00527     // Rework back into decomposition for original mesh_
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     // Make Metis CSR (Compressed Storage Format) storage
00557     //   adjncy      : contains neighbours (= edges in graph)
00558     //   xadj(celli) : start of information in adjncy for celli
00559 
00560     List<int> adjncy;
00561     List<int> xadj;
00562     calcMetisCSR(globalCellCells, adjncy, xadj);
00563 
00564 
00565     // Decompose using default weights
00566     List<int> finalDecomp;
00567     decompose(adjncy, xadj, finalDecomp);
00568 
00569     // Copy back to labelList
00570     labelList decomp(finalDecomp.size());
00571     forAll(decomp, i)
00572     {
00573         decomp[i] = finalDecomp[i];
00574     }
00575     return decomp;
00576 }
00577 
00578 
00579 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd