1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "simpleGeomDecomp.H"
27 #include "addToRunTimeSelectionTable.H"
28 #include "SortableList.H"
29 #include "globalIndex.H"
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 defineTypeNameAndDebug(simpleGeomDecomp, 0);
38 addToRunTimeSelectionTable
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 // assignToProcessorGroup : given nCells cells and nProcGroup processor
49 // groups to share them, how do we share them out? Answer : each group
50 // gets nCells/nProcGroup cells, and the first few get one
51 // extra to make up the numbers. This should produce almost
52 // perfect load balancing
54 void Foam::simpleGeomDecomp::assignToProcessorGroup
56 labelList& processorGroup,
57 const label nProcGroup
60 label jump = processorGroup.size()/nProcGroup;
61 label jumpb = jump + 1;
62 label fstProcessorGroup = processorGroup.size() - jump*nProcGroup;
67 // assign cells to the first few processor groups (those with
68 // one extra cell each
69 for (j=0; j<fstProcessorGroup; j++)
71 for (register label k=0; k<jumpb; k++)
73 processorGroup[ind++] = j;
77 // and now to the `normal' processor groups
78 for (; j<nProcGroup; j++)
80 for (register label k=0; k<jump; k++)
82 processorGroup[ind++] = j;
88 void Foam::simpleGeomDecomp::assignToProcessorGroup
90 labelList& processorGroup,
91 const label nProcGroup,
92 const labelList& indices,
93 const scalarField& weights,
94 const scalar summedWeights
97 // This routine gets the sorted points.
98 // Easiest to explain with an example.
99 // E.g. 400 points, sum of weights : 513.
100 // Now with number of divisions in this direction (nProcGroup) : 4
101 // gives the split at 513/4 = 128
102 // So summed weight from 0..128 goes into bin 0,
103 // ,, 128..256 goes into bin 1
105 // Finally any remaining ones go into the last bin (3).
107 const scalar jump = summedWeights/nProcGroup;
108 const label nProcGroupM1 = nProcGroup - 1;
109 scalar sumWeights = 0;
113 // assign cells to all except last group.
114 for (j=0; j<nProcGroupM1; j++)
116 const scalar limit = jump*scalar(j + 1);
117 while (sumWeights < limit)
119 sumWeights += weights[indices[ind]];
120 processorGroup[ind++] = j;
123 // Ensure last included.
124 while (ind < processorGroup.size())
126 processorGroup[ind++] = nProcGroupM1;
131 Foam::labelList Foam::simpleGeomDecomp::decomposeOneProc
133 const pointField& points
136 // construct a list for the final result
137 labelList finalDecomp(points.size());
139 labelList processorGroups(points.size());
141 labelList pointIndices(points.size());
142 forAll(pointIndices, i)
147 const pointField rotatedPoints(rotDelta_ & points);
149 // and one to take the processor group id's. For each direction.
150 // we assign the processors to groups of processors labelled
151 // 0..nX to give a banded structure on the mesh. Then we
152 // construct the actual processor number by treating this as
153 // the units part of the processor number.
157 UList<scalar>::less(rotatedPoints.component(vector::X))
160 assignToProcessorGroup(processorGroups, n_.x());
164 finalDecomp[pointIndices[i]] = processorGroups[i];
168 // now do the same thing in the Y direction. These processor group
169 // numbers add multiples of nX to the proc. number (columns)
173 UList<scalar>::less(rotatedPoints.component(vector::Y))
176 assignToProcessorGroup(processorGroups, n_.y());
180 finalDecomp[pointIndices[i]] += n_.x()*processorGroups[i];
184 // finally in the Z direction. Now we add multiples of nX*nY to give
189 UList<scalar>::less(rotatedPoints.component(vector::Z))
192 assignToProcessorGroup(processorGroups, n_.z());
196 finalDecomp[pointIndices[i]] += n_.x()*n_.y()*processorGroups[i];
203 Foam::labelList Foam::simpleGeomDecomp::decomposeOneProc
205 const pointField& points,
206 const scalarField& weights
209 // construct a list for the final result
210 labelList finalDecomp(points.size());
212 labelList processorGroups(points.size());
214 labelList pointIndices(points.size());
215 forAll(pointIndices, i)
220 const pointField rotatedPoints(rotDelta_ & points);
222 // and one to take the processor group id's. For each direction.
223 // we assign the processors to groups of processors labelled
224 // 0..nX to give a banded structure on the mesh. Then we
225 // construct the actual processor number by treating this as
226 // the units part of the processor number.
230 UList<scalar>::less(rotatedPoints.component(vector::X))
233 const scalar summedWeights = sum(weights);
234 assignToProcessorGroup
245 finalDecomp[pointIndices[i]] = processorGroups[i];
249 // now do the same thing in the Y direction. These processor group
250 // numbers add multiples of nX to the proc. number (columns)
254 UList<scalar>::less(rotatedPoints.component(vector::Y))
257 assignToProcessorGroup
268 finalDecomp[pointIndices[i]] += n_.x()*processorGroups[i];
272 // finally in the Z direction. Now we add multiples of nX*nY to give
277 UList<scalar>::less(rotatedPoints.component(vector::Z))
280 assignToProcessorGroup
291 finalDecomp[pointIndices[i]] += n_.x()*n_.y()*processorGroups[i];
298 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
300 Foam::simpleGeomDecomp::simpleGeomDecomp(const dictionary& decompositionDict)
302 geomDecomp(decompositionDict, typeName)
306 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
308 Foam::labelList Foam::simpleGeomDecomp::decompose
310 const pointField& points
313 if (!Pstream::parRun())
315 return decomposeOneProc(points);
319 globalIndex globalNumbers(points.size());
321 // Collect all points on master
322 if (Pstream::master())
324 pointField allPoints(globalNumbers.size());
326 label nTotalPoints = 0;
328 SubField<point>(allPoints, points.size()).assign(points);
329 nTotalPoints += points.size();
332 for (int slave=1; slave<Pstream::nProcs(); slave++)
334 IPstream fromSlave(Pstream::scheduled, slave);
335 pointField nbrPoints(fromSlave);
342 nTotalPoints += nbrPoints.size();
346 labelList finalDecomp(decomposeOneProc(allPoints));
349 for (int slave=1; slave<Pstream::nProcs(); slave++)
351 OPstream toSlave(Pstream::scheduled, slave);
352 toSlave << SubField<label>
355 globalNumbers.localSize(slave),
356 globalNumbers.offset(slave)
360 finalDecomp.setSize(points.size());
368 OPstream toMaster(Pstream::scheduled, Pstream::masterNo());
372 // Receive back decomposition
373 IPstream fromMaster(Pstream::scheduled, Pstream::masterNo());
374 labelList finalDecomp(fromMaster);
382 Foam::labelList Foam::simpleGeomDecomp::decompose
384 const pointField& points,
385 const scalarField& weights
388 if (!Pstream::parRun())
390 return decomposeOneProc(points, weights);
394 globalIndex globalNumbers(points.size());
396 // Collect all points on master
397 if (Pstream::master())
399 pointField allPoints(globalNumbers.size());
400 scalarField allWeights(allPoints.size());
402 label nTotalPoints = 0;
404 SubField<point>(allPoints, points.size()).assign(points);
405 SubField<scalar>(allWeights, points.size()).assign(weights);
406 nTotalPoints += points.size();
409 for (int slave=1; slave<Pstream::nProcs(); slave++)
411 IPstream fromSlave(Pstream::scheduled, slave);
412 pointField nbrPoints(fromSlave);
413 scalarField nbrWeights(fromSlave);
425 ).assign(nbrWeights);
426 nTotalPoints += nbrPoints.size();
430 labelList finalDecomp(decomposeOneProc(allPoints, allWeights));
433 for (int slave=1; slave<Pstream::nProcs(); slave++)
435 OPstream toSlave(Pstream::scheduled, slave);
436 toSlave << SubField<label>
439 globalNumbers.localSize(slave),
440 globalNumbers.offset(slave)
444 finalDecomp.setSize(points.size());
452 OPstream toMaster(Pstream::scheduled, Pstream::masterNo());
453 toMaster<< points << weights;
456 // Receive back decomposition
457 IPstream fromMaster(Pstream::scheduled, Pstream::masterNo());
458 labelList finalDecomp(fromMaster);
466 // ************************************************************************* //