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

scotchDecomp.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     Default strategy:
00026         Strat=b
00027         {
00028             job=t,
00029             map=t,
00030             poli=S,
00031             sep=
00032             (
00033                 m
00034                 {
00035                     asc=b
00036                     {
00037                         bnd=d{pass=40,dif=1,rem=1}f{move=80,pass=-1,bal=0.005},
00038                         org=f{move=80,pass=-1,bal=0.005},width=3
00039                     },
00040                     low=h{pass=10}f{move=80,pass=-1,bal=0.0005},
00041                     type=h,
00042                     vert=80,
00043                     rat=0.8
00044                 }
00045               | m
00046                 {
00047                     asc=b
00048                     {
00049                         bnd=d{pass=40,dif=1,rem=1}f{move=80,pass=-1,bal=0.005},
00050                         org=f{move=80,pass=-1,bal=0.005},
00051                         width=3},
00052                         low=h{pass=10}f{move=80,pass=-1,bal=0.0005},
00053                         type=h,
00054                         vert=80,
00055                         rat=0.8
00056                 }
00057             )
00058         }
00059 \*---------------------------------------------------------------------------*/
00060 
00061 #include "scotchDecomp.H"
00062 #include "addToRunTimeSelectionTable.H"
00063 #include "floatScalar.H"
00064 #include "IFstream.H"
00065 #include "Time.H"
00066 #include "cyclicPolyPatch.H"
00067 #include "OFstream.H"
00068 
00069 extern "C"
00070 {
00071 #define OMPI_SKIP_MPICXX
00072 #include "module.h"
00073 #include "common.h"
00074 #include "scotch.h"
00075 }
00076 
00077 
00078 // Hack: scotch generates floating point errors so need to switch of error
00079 //       trapping!
00080 #if defined(linux) || defined(linuxAMD64) || defined(linuxIA64)
00081 #    define LINUX
00082 #endif
00083 
00084 #if defined(LINUX) && defined(__GNUC__)
00085 #    define LINUX_GNUC
00086 #endif
00087 
00088 #ifdef LINUX_GNUC
00089 #   ifndef __USE_GNU
00090 #       define __USE_GNU
00091 #   endif
00092 #   include <fenv.h>
00093 #endif
00094 
00095 
00096 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00097 
00098 namespace Foam
00099 {
00100     defineTypeNameAndDebug(scotchDecomp, 0);
00101 
00102     addToRunTimeSelectionTable
00103     (
00104         decompositionMethod,
00105         scotchDecomp,
00106         dictionaryMesh
00107     );
00108 }
00109 
00110 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00111 
00112 void Foam::scotchDecomp::check(const int retVal, const char* str)
00113 {
00114     if (retVal)
00115     {
00116         FatalErrorIn("scotchDecomp::decompose(..)")
00117             << "Called to scotch routine " << str << " failed."
00118             << exit(FatalError);
00119     }
00120 }
00121 
00122 
00123 // Call Metis with options from dictionary.
00124 Foam::label Foam::scotchDecomp::decompose
00125 (
00126     const List<int>& adjncy,
00127     const List<int>& xadj,
00128 
00129     List<int>& finalDecomp
00130 )
00131 {
00132     // Strategy
00133     // ~~~~~~~~
00134 
00135     // Default.
00136     SCOTCH_Strat stradat;
00137     check(SCOTCH_stratInit(&stradat), "SCOTCH_stratInit");
00138 
00139     if (decompositionDict_.found("scotchCoeffs"))
00140     {
00141         const dictionary& scotchCoeffs =
00142             decompositionDict_.subDict("scotchCoeffs");
00143 
00144 
00145         string strategy;
00146         if (scotchCoeffs.readIfPresent("strategy", strategy))
00147         {
00148             if (debug)
00149             {
00150                 Info<< "scotchDecomp : Using strategy " << strategy << endl;
00151             }
00152             SCOTCH_stratGraphMap(&stradat, strategy.c_str());
00153             //fprintf(stdout, "S\tStrat=");
00154             //SCOTCH_stratSave(&stradat, stdout);
00155             //fprintf(stdout, "\n");
00156         }
00157     }
00158 
00159 
00160     // Graph
00161     // ~~~~~
00162 
00163     SCOTCH_Graph grafdat;
00164     check(SCOTCH_graphInit(&grafdat), "SCOTCH_graphInit");
00165     check
00166     (
00167         SCOTCH_graphBuild
00168         (
00169             &grafdat,
00170             0,                      // baseval, c-style numbering
00171             xadj.size()-1,          // vertnbr, nCells
00172             xadj.begin(),           // verttab, start index per cell into adjncy
00173             &xadj[1],               // vendtab, end index  ,,
00174             NULL,                   // velotab, vertex weights
00175             NULL,                   // vlbltab
00176             adjncy.size(),          // edgenbr, number of arcs
00177             adjncy.begin(),         // edgetab
00178             NULL                    // edlotab, edge weights
00179         ),
00180         "SCOTCH_graphBuild"
00181     );
00182     check(SCOTCH_graphCheck(&grafdat), "SCOTCH_graphCheck");
00183 
00184 
00185     // Dump graph
00186     if (decompositionDict_.found("scotchCoeffs"))
00187     {
00188         const dictionary& scotchCoeffs =
00189             decompositionDict_.subDict("scotchCoeffs");
00190 
00191         if (scotchCoeffs.found("writeGraph"))
00192         {
00193             Switch writeGraph(scotchCoeffs.lookup("writeGraph"));
00194 
00195             if (writeGraph)
00196             {
00197                 OFstream str(mesh_.time().path() / mesh_.name() + ".grf");
00198 
00199                 Info<< "Dumping Scotch graph file to " << str.name() << endl
00200                     << "Use this in combination with gpart." << endl;
00201 
00202                 label version = 0;
00203                 str << version << nl;
00204                 // Numer of vertices
00205                 str << xadj.size()-1 << ' ' << adjncy.size() << nl;
00206                 // Numbering starts from 0
00207                 label baseval = 0;
00208                 // Has weights?
00209                 label hasEdgeWeights = 0;
00210                 label hasVertexWeights = 0;
00211                 label numericflag = 10*hasEdgeWeights+hasVertexWeights;
00212                 str << baseval << ' ' << numericflag << nl;
00213                 for (label cellI = 0; cellI < xadj.size()-1; cellI++)
00214                 {
00215                     label start = xadj[cellI];
00216                     label end = xadj[cellI+1];
00217                     str << end-start;
00218 
00219                     for (label i = start; i < end; i++)
00220                     {
00221                         str << ' ' << adjncy[i];
00222                     }
00223                     str << nl;
00224                 }
00225             }
00226         }
00227     }
00228 
00229 
00230     // Architecture
00231     // ~~~~~~~~~~~~
00232     // (fully connected network topology since using switch)
00233 
00234     SCOTCH_Arch archdat;
00235     check(SCOTCH_archInit(&archdat), "SCOTCH_archInit");
00236 
00237     List<label> processorWeights;
00238     if (decompositionDict_.found("scotchCoeffs"))
00239     {
00240         const dictionary& scotchCoeffs =
00241             decompositionDict_.subDict("scotchCoeffs");
00242 
00243         scotchCoeffs.readIfPresent("processorWeights", processorWeights);
00244     }
00245     if (processorWeights.size())
00246     {
00247         if (debug)
00248         {
00249             Info<< "scotchDecomp : Using procesor weights " << processorWeights
00250                 << endl;
00251         }
00252         check
00253         (
00254             SCOTCH_archCmpltw(&archdat, nProcessors_, processorWeights.begin()),
00255             "SCOTCH_archCmpltw"
00256         );
00257     }
00258     else
00259     {
00260         check
00261         (
00262             SCOTCH_archCmplt(&archdat, nProcessors_),
00263             "SCOTCH_archCmplt"
00264         );
00265     }
00266 
00267 
00268     //SCOTCH_Mapping mapdat;
00269     //SCOTCH_graphMapInit(&grafdat, &mapdat, &archdat, NULL);
00270     //SCOTCH_graphMapCompute(&grafdat, &mapdat, &stradat); /* Perform mapping */
00271     //SCOTCH_graphMapExit(&grafdat, &mapdat);
00272 
00273 
00274     // Hack:switch off fpu error trapping
00275 #   ifdef LINUX_GNUC
00276     int oldExcepts = fedisableexcept
00277     (
00278         FE_DIVBYZERO
00279       | FE_INVALID
00280       | FE_OVERFLOW
00281     );
00282 #   endif
00283 
00284     finalDecomp.setSize(xadj.size()-1);
00285     finalDecomp = 0;
00286     check
00287     (
00288         SCOTCH_graphMap
00289         (
00290             &grafdat,
00291             &archdat,
00292             &stradat,           // const SCOTCH_Strat *
00293             finalDecomp.begin() // parttab
00294         ),
00295         "SCOTCH_graphMap"
00296     );
00297 
00298 #   ifdef LINUX_GNUC
00299     feenableexcept(oldExcepts);
00300 #   endif
00301 
00302 
00303 
00304     //finalDecomp.setSize(xadj.size()-1);
00305     //check
00306     //(
00307     //    SCOTCH_graphPart
00308     //    (
00309     //        &grafdat,
00310     //        nProcessors_,       // partnbr
00311     //        &stradat,           // const SCOTCH_Strat *
00312     //        finalDecomp.begin() // parttab
00313     //    ),
00314     //    "SCOTCH_graphPart"
00315     //);
00316 
00317     // Release storage for graph
00318     SCOTCH_graphExit(&grafdat);
00319     // Release storage for strategy
00320     SCOTCH_stratExit(&stradat);
00321     // Release storage for network topology
00322     SCOTCH_archExit(&archdat);
00323 
00324     return 0;
00325 }
00326 
00327 
00328 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00329 
00330 Foam::scotchDecomp::scotchDecomp
00331 (
00332     const dictionary& decompositionDict,
00333     const polyMesh& mesh
00334 )
00335 :
00336     decompositionMethod(decompositionDict),
00337     mesh_(mesh)
00338 {}
00339 
00340 
00341 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00342 
00343 Foam::labelList Foam::scotchDecomp::decompose(const pointField& points)
00344 {
00345     if (points.size() != mesh_.nCells())
00346     {
00347         FatalErrorIn("scotchDecomp::decompose(const pointField&)")
00348             << "Can use this decomposition method only for the whole mesh"
00349             << endl
00350             << "and supply one coordinate (cellCentre) for every cell." << endl
00351             << "The number of coordinates " << points.size() << endl
00352             << "The number of cells in the mesh " << mesh_.nCells()
00353             << exit(FatalError);
00354     }
00355 
00356     // Make Metis CSR (Compressed Storage Format) storage
00357     //   adjncy      : contains neighbours (= edges in graph)
00358     //   xadj(celli) : start of information in adjncy for celli
00359 
00360     List<int> xadj(mesh_.nCells()+1);
00361 
00362     // Initialise the number of internal faces of the cells to twice the
00363     // number of internal faces
00364     label nInternalFaces = 2*mesh_.nInternalFaces();
00365 
00366     // Check the boundary for coupled patches and add to the number of
00367     // internal faces
00368     const polyBoundaryMesh& pbm = mesh_.boundaryMesh();
00369 
00370     forAll(pbm, patchi)
00371     {
00372         if (isA<cyclicPolyPatch>(pbm[patchi]))
00373         {
00374             nInternalFaces += pbm[patchi].size();
00375         }
00376     }
00377 
00378     // Create the adjncy array the size of the total number of internal and
00379     // coupled faces
00380     List<int> adjncy(nInternalFaces);
00381 
00382     // Fill in xadj
00383     // ~~~~~~~~~~~~
00384     label freeAdj = 0;
00385 
00386     for (label cellI = 0; cellI < mesh_.nCells(); cellI++)
00387     {
00388         xadj[cellI] = freeAdj;
00389 
00390         const labelList& cFaces = mesh_.cells()[cellI];
00391 
00392         forAll(cFaces, i)
00393         {
00394             label faceI = cFaces[i];
00395 
00396             if
00397             (
00398                 mesh_.isInternalFace(faceI)
00399              || isA<cyclicPolyPatch>(pbm[pbm.whichPatch(faceI)])
00400             )
00401             {
00402                 freeAdj++;
00403             }
00404         }
00405     }
00406     xadj[mesh_.nCells()] = freeAdj;
00407 
00408 
00409     // Fill in adjncy
00410     // ~~~~~~~~~~~~~~
00411 
00412     labelList nFacesPerCell(mesh_.nCells(), 0);
00413 
00414     // Internal faces
00415     for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
00416     {
00417         label own = mesh_.faceOwner()[faceI];
00418         label nei = mesh_.faceNeighbour()[faceI];
00419 
00420         adjncy[xadj[own] + nFacesPerCell[own]++] = nei;
00421         adjncy[xadj[nei] + nFacesPerCell[nei]++] = own;
00422     }
00423 
00424     // Coupled faces. Only cyclics done.
00425     forAll(pbm, patchi)
00426     {
00427         if (isA<cyclicPolyPatch>(pbm[patchi]))
00428         {
00429             const unallocLabelList& faceCells = pbm[patchi].faceCells();
00430 
00431             label sizeby2 = faceCells.size()/2;
00432 
00433             for (label facei=0; facei<sizeby2; facei++)
00434             {
00435                 label own = faceCells[facei];
00436                 label nei = faceCells[facei + sizeby2];
00437 
00438                 adjncy[xadj[own] + nFacesPerCell[own]++] = nei;
00439                 adjncy[xadj[nei] + nFacesPerCell[nei]++] = own;
00440             }
00441         }
00442     }
00443 
00444     // Decompose using default weights
00445     List<int> finalDecomp;
00446     decompose(adjncy, xadj, finalDecomp);
00447 
00448     // Copy back to labelList
00449     labelList decomp(finalDecomp.size());
00450     forAll(decomp, i)
00451     {
00452         decomp[i] = finalDecomp[i];
00453     }
00454     return decomp;
00455 }
00456 
00457 
00458 // From cell-cell connections to Metis format (like CompactListList)
00459 void Foam::scotchDecomp::calcMetisCSR
00460 (
00461     const labelListList& cellCells,
00462     List<int>& adjncy,
00463     List<int>& xadj
00464 )
00465 {
00466     // Count number of internal faces
00467     label nConnections = 0;
00468 
00469     forAll(cellCells, coarseI)
00470     {
00471         nConnections += cellCells[coarseI].size();
00472     }
00473 
00474     // Create the adjncy array as twice the size of the total number of
00475     // internal faces
00476     adjncy.setSize(nConnections);
00477 
00478     xadj.setSize(cellCells.size()+1);
00479 
00480 
00481     // Fill in xadj
00482     // ~~~~~~~~~~~~
00483     label freeAdj = 0;
00484 
00485     forAll(cellCells, coarseI)
00486     {
00487         xadj[coarseI] = freeAdj;
00488 
00489         const labelList& cCells = cellCells[coarseI];
00490 
00491         forAll(cCells, i)
00492         {
00493             adjncy[freeAdj++] = cCells[i];
00494         }
00495     }
00496     xadj[cellCells.size()] = freeAdj;
00497 }
00498 
00499 
00500 Foam::labelList Foam::scotchDecomp::decompose
00501 (
00502     const labelList& agglom,
00503     const pointField& agglomPoints
00504 )
00505 {
00506     if (agglom.size() != mesh_.nCells())
00507     {
00508         FatalErrorIn
00509         (
00510             "parMetisDecomp::decompose(const labelList&, const pointField&)"
00511         )   << "Size of cell-to-coarse map " << agglom.size()
00512             << " differs from number of cells in mesh " << mesh_.nCells()
00513             << exit(FatalError);
00514     }
00515 
00516     // Make Metis CSR (Compressed Storage Format) storage
00517     //   adjncy      : contains neighbours (= edges in graph)
00518     //   xadj(celli) : start of information in adjncy for celli
00519     List<int> adjncy;
00520     List<int> xadj;
00521     {
00522         // Get cellCells on coarse mesh.
00523         labelListList cellCells;
00524         calcCellCells
00525         (
00526             mesh_,
00527             agglom,
00528             agglomPoints.size(),
00529             cellCells
00530         );
00531 
00532         calcMetisCSR(cellCells, adjncy, xadj);
00533     }
00534 
00535     // Decompose using default weights
00536     List<int> finalDecomp;
00537     decompose(adjncy, xadj, finalDecomp);
00538 
00539 
00540     // Rework back into decomposition for original mesh_
00541     labelList fineDistribution(agglom.size());
00542 
00543     forAll(fineDistribution, i)
00544     {
00545         fineDistribution[i] = finalDecomp[agglom[i]];
00546     }
00547 
00548     return fineDistribution;
00549 }
00550 
00551 
00552 Foam::labelList Foam::scotchDecomp::decompose
00553 (
00554     const labelListList& globalCellCells,
00555     const pointField& cellCentres
00556 )
00557 {
00558     if (cellCentres.size() != globalCellCells.size())
00559     {
00560         FatalErrorIn
00561         (
00562             "scotchDecomp::decompose(const pointField&, const labelListList&)"
00563         )   << "Inconsistent number of cells (" << globalCellCells.size()
00564             << ") and number of cell centres (" << cellCentres.size()
00565             << ")." << exit(FatalError);
00566     }
00567 
00568 
00569     // Make Metis CSR (Compressed Storage Format) storage
00570     //   adjncy      : contains neighbours (= edges in graph)
00571     //   xadj(celli) : start of information in adjncy for celli
00572 
00573     List<int> adjncy;
00574     List<int> xadj;
00575     calcMetisCSR(globalCellCells, adjncy, xadj);
00576 
00577 
00578     // Decompose using default weights
00579     List<int> finalDecomp;
00580     decompose(adjncy, xadj, finalDecomp);
00581 
00582     // Copy back to labelList
00583     labelList decomp(finalDecomp.size());
00584     forAll(decomp, i)
00585     {
00586         decomp[i] = finalDecomp[i];
00587     }
00588     return decomp;
00589 }
00590 
00591 
00592 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd