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
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
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
00079
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
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
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
00133
00134
00135
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
00154
00155
00156 }
00157 }
00158
00159
00160
00161
00162
00163 SCOTCH_Graph grafdat;
00164 check(SCOTCH_graphInit(&grafdat), "SCOTCH_graphInit");
00165 check
00166 (
00167 SCOTCH_graphBuild
00168 (
00169 &grafdat,
00170 0,
00171 xadj.size()-1,
00172 xadj.begin(),
00173 &xadj[1],
00174 NULL,
00175 NULL,
00176 adjncy.size(),
00177 adjncy.begin(),
00178 NULL
00179 ),
00180 "SCOTCH_graphBuild"
00181 );
00182 check(SCOTCH_graphCheck(&grafdat), "SCOTCH_graphCheck");
00183
00184
00185
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
00205 str << xadj.size()-1 << ' ' << adjncy.size() << nl;
00206
00207 label baseval = 0;
00208
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
00231
00232
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
00269
00270
00271
00272
00273
00274
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,
00293 finalDecomp.begin()
00294 ),
00295 "SCOTCH_graphMap"
00296 );
00297
00298 # ifdef LINUX_GNUC
00299 feenableexcept(oldExcepts);
00300 # endif
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318 SCOTCH_graphExit(&grafdat);
00319
00320 SCOTCH_stratExit(&stradat);
00321
00322 SCOTCH_archExit(&archdat);
00323
00324 return 0;
00325 }
00326
00327
00328
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
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
00357
00358
00359
00360 List<int> xadj(mesh_.nCells()+1);
00361
00362
00363
00364 label nInternalFaces = 2*mesh_.nInternalFaces();
00365
00366
00367
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
00379
00380 List<int> adjncy(nInternalFaces);
00381
00382
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
00410
00411
00412 labelList nFacesPerCell(mesh_.nCells(), 0);
00413
00414
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
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
00445 List<int> finalDecomp;
00446 decompose(adjncy, xadj, finalDecomp);
00447
00448
00449 labelList decomp(finalDecomp.size());
00450 forAll(decomp, i)
00451 {
00452 decomp[i] = finalDecomp[i];
00453 }
00454 return decomp;
00455 }
00456
00457
00458
00459 void Foam::scotchDecomp::calcMetisCSR
00460 (
00461 const labelListList& cellCells,
00462 List<int>& adjncy,
00463 List<int>& xadj
00464 )
00465 {
00466
00467 label nConnections = 0;
00468
00469 forAll(cellCells, coarseI)
00470 {
00471 nConnections += cellCells[coarseI].size();
00472 }
00473
00474
00475
00476 adjncy.setSize(nConnections);
00477
00478 xadj.setSize(cellCells.size()+1);
00479
00480
00481
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
00517
00518
00519 List<int> adjncy;
00520 List<int> xadj;
00521 {
00522
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
00536 List<int> finalDecomp;
00537 decompose(adjncy, xadj, finalDecomp);
00538
00539
00540
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
00570
00571
00572
00573 List<int> adjncy;
00574 List<int> xadj;
00575 calcMetisCSR(globalCellCells, adjncy, xadj);
00576
00577
00578
00579 List<int> finalDecomp;
00580 decompose(adjncy, xadj, finalDecomp);
00581
00582
00583 labelList decomp(finalDecomp.size());
00584 forAll(decomp, i)
00585 {
00586 decomp[i] = finalDecomp[i];
00587 }
00588 return decomp;
00589 }
00590
00591
00592