diff --git a/src/finiteVolume/fvMesh/fvMeshDistributors/cpuLoad/cpuLoad.C b/src/finiteVolume/fvMesh/fvMeshDistributors/cpuLoad/cpuLoad.C index 249ddc5fe3..3c761d37d4 100644 --- a/src/finiteVolume/fvMesh/fvMeshDistributors/cpuLoad/cpuLoad.C +++ b/src/finiteVolume/fvMesh/fvMeshDistributors/cpuLoad/cpuLoad.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2022 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2022-2024 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -107,6 +107,24 @@ Foam::optionalCpuLoad& Foam::optionalCpuLoad::New } +Foam::optionalCpuLoad& Foam::optionalCpuLoad::New +( + const polyMesh& mesh, + const word& name, + const bool loadBalancing +) +{ + if (loadBalancing && isA(mesh)) + { + return New(refCast(mesh), name, loadBalancing); + } + else + { + return optionalCpuLoad::optionalCpuLoad_; + } +} + + // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // void Foam::cpuLoad::reset() diff --git a/src/finiteVolume/fvMesh/fvMeshDistributors/cpuLoad/cpuLoad.H b/src/finiteVolume/fvMesh/fvMeshDistributors/cpuLoad/cpuLoad.H index fcc815db46..140e5791a0 100644 --- a/src/finiteVolume/fvMesh/fvMeshDistributors/cpuLoad/cpuLoad.H +++ b/src/finiteVolume/fvMesh/fvMeshDistributors/cpuLoad/cpuLoad.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2022 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2022-2024 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -77,6 +77,8 @@ public: // Selectors + //- Construct from fvMesh if loadBalancing is true + // otherwise return the dummy optionalCpuLoad static optionalCpuLoad& New ( const fvMesh& mesh, @@ -84,6 +86,15 @@ public: const bool loadBalancing ); + //- Construct from polyMesh if it is an fvMesh and loadBalancing is true + // otherwise return the dummy optionalCpuLoad + static optionalCpuLoad& New + ( + const polyMesh& mesh, + const word& name, + const bool loadBalancing + ); + //- Destructor virtual ~optionalCpuLoad() diff --git a/src/fvMeshDistributors/loadBalancer/fvMeshDistributorsLoadBalancer.C b/src/fvMeshDistributors/loadBalancer/fvMeshDistributorsLoadBalancer.C index 0d07d6ce87..01680982a8 100644 --- a/src/fvMeshDistributors/loadBalancer/fvMeshDistributorsLoadBalancer.C +++ b/src/fvMeshDistributors/loadBalancer/fvMeshDistributorsLoadBalancer.C @@ -85,12 +85,13 @@ bool Foam::fvMeshDistributors::loadBalancer::update() if ( Pstream::nProcs() > 1 - && mesh.time().timeIndex() > 1 + && mesh.time().timeIndex() - mesh.time().startTimeIndex() > 1 && timeIndex_ != mesh.time().timeIndex() ) { timeIndex_ = mesh.time().timeIndex(); + // Get the CPU time fer this processor which includes waiting time const scalar timeStepCpuTime = cpuTime_.cpuTimeIncrement(); // CPU loads per cell @@ -107,80 +108,125 @@ bool Foam::fvMeshDistributors::loadBalancer::update() { timeIndex_ = mesh.time().timeIndex(); - scalar sumCpuLoad = 0; + scalarList procCpuLoads(cpuLoads.size()); + label l = 0; forAllConstIter(HashTable, cpuLoads, iter) { - sumCpuLoad += sum(iter()->primitiveField()); + procCpuLoads[l++] = sum(iter()->primitiveField()); } - const scalar cellCFDCpuTime = returnReduce - ( - (timeStepCpuTime - sumCpuLoad)/mesh.nCells(), - minOp() - ); + List allProcCpuLoads(Pstream::nProcs()); + allProcCpuLoads[Pstream::myProcNo()] = procCpuLoads; + Pstream::gatherList(allProcCpuLoads); + Pstream::scatterList(allProcCpuLoads); - // Total CPU time for this processor - const scalar processorCpuTime = - mesh.nCells()*cellCFDCpuTime + sumCpuLoad; + scalarList sumProcCpuLoads(procCpuLoads.size(), scalar(0)); + scalarList maxProcCpuLoads(procCpuLoads.size(), scalar(0)); + forAll(maxProcCpuLoads, l) + { + forAll(allProcCpuLoads, proci) + { + sumProcCpuLoads[l] += allProcCpuLoads[proci][l]; + + maxProcCpuLoads[l] = + max(maxProcCpuLoads[l], allProcCpuLoads[proci][l]); + } + } + + // Sum over loads of the maximum load CPU time per processor + const scalar sumMaxProcCpuLoad(sum(maxProcCpuLoads)); + + // Maximum number of cells per processor + const label maxNcells = returnReduce(mesh.nCells(), maxOp