diff --git a/src/finiteVolume/Make/files b/src/finiteVolume/Make/files
index 4376b30319..1c1d209d18 100644
--- a/src/finiteVolume/Make/files
+++ b/src/finiteVolume/Make/files
@@ -104,6 +104,7 @@ fvMeshDistributors = fvMesh/fvMeshDistributors
$(fvMeshDistributors)/fvMeshDistributor/fvMeshDistributor.C
$(fvMeshDistributors)/fvMeshDistributor/fvMeshDistributorNew.C
$(fvMeshDistributors)/none/fvMeshDistributorsNone.C
+$(fvMeshDistributors)/cpuLoad/cpuLoad.C
fvMeshMovers = fvMesh/fvMeshMovers
diff --git a/src/finiteVolume/fvMesh/fvMeshDistributors/cpuLoad/cpuLoad.C b/src/finiteVolume/fvMesh/fvMeshDistributors/cpuLoad/cpuLoad.C
new file mode 100644
index 0000000000..b3e246704b
--- /dev/null
+++ b/src/finiteVolume/fvMesh/fvMeshDistributors/cpuLoad/cpuLoad.C
@@ -0,0 +1,124 @@
+/*---------------------------------------------------------------------------*\
+ ========= |
+ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
+ \\ / O peration | Website: https://openfoam.org
+ \\ / A nd | Copyright (C) 2022 OpenFOAM Foundation
+ \\/ M anipulation |
+-------------------------------------------------------------------------------
+License
+ This file is part of OpenFOAM.
+
+ OpenFOAM is free software: you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with OpenFOAM. If not, see .
+
+\*---------------------------------------------------------------------------*/
+
+#include "cpuLoad.H"
+
+// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
+
+namespace Foam
+{
+ defineTypeNameAndDebug(cpuLoad, 0);
+ optionalCpuLoad optionalCpuLoad::optionalCpuLoad_;
+}
+
+
+// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
+
+Foam::cpuLoad::cpuLoad(const fvMesh& mesh, const word& name)
+:
+ volScalarField::Internal
+ (
+ IOobject
+ (
+ name,
+ mesh.time().timeName(),
+ mesh
+ ),
+ mesh,
+ dimensionedScalar(dimTime, 0)
+ )
+{}
+
+
+// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
+
+Foam::cpuLoad::~cpuLoad()
+{}
+
+
+// * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
+
+Foam::optionalCpuLoad& Foam::optionalCpuLoad::New
+(
+ const fvMesh& mesh,
+ const word& name,
+ const bool loadBalancing
+)
+{
+ if (loadBalancing)
+ {
+ if
+ (
+ mesh.thisDb().objectRegistry::template
+ foundObject
+ (
+ name
+ )
+ )
+ {
+ return mesh.thisDb().objectRegistry::template
+ lookupObjectRef
+ (
+ name
+ );
+ }
+ else
+ {
+ if (cpuLoad::debug)
+ {
+ InfoInFunction
+ << "constructing " << name
+ << " for region " << mesh.name() << endl;
+ }
+
+ cpuLoad* cpuLoadPtr(new cpuLoad(mesh, name));
+
+ regIOobject::store(cpuLoadPtr);
+
+ return *cpuLoadPtr;
+ }
+ }
+ else
+ {
+ return optionalCpuLoad::optionalCpuLoad_;
+ }
+}
+
+
+// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
+
+void Foam::cpuLoad::reset()
+{
+ cpuTime_.cpuTimeIncrement();
+}
+
+
+void Foam::cpuLoad::cpuTimeIncrement(const label celli)
+{
+ operator[](celli) += cpuTime_.cpuTimeIncrement();
+}
+
+
+// ************************************************************************* //
diff --git a/src/finiteVolume/fvMesh/fvMeshDistributors/cpuLoad/cpuLoad.H b/src/finiteVolume/fvMesh/fvMeshDistributors/cpuLoad/cpuLoad.H
new file mode 100644
index 0000000000..fcc815db46
--- /dev/null
+++ b/src/finiteVolume/fvMesh/fvMeshDistributors/cpuLoad/cpuLoad.H
@@ -0,0 +1,172 @@
+/*---------------------------------------------------------------------------*\
+ ========= |
+ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
+ \\ / O peration | Website: https://openfoam.org
+ \\ / A nd | Copyright (C) 2022 OpenFOAM Foundation
+ \\/ M anipulation |
+-------------------------------------------------------------------------------
+License
+ This file is part of OpenFOAM.
+
+ OpenFOAM is free software: you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with OpenFOAM. If not, see .
+
+Class
+ Foam::cpuLoad
+
+Description
+ Class to maintain a field of the CPU load per cell.
+
+ The optionalCpuLoad is a base-class for cpuLoad to simplify the
+ implementation of optional CPU time caching. This is achieved via the
+ optionalCpuLoad::New function which returns a optionalCpuLoad with dummy
+ functions if loadBalancing is false otherwise it creates or looks-up and
+ returns a cpuLoad with the given name.
+
+ Used for loadBalancing.
+
+SourceFiles
+ cpuLoad.C
+
+\*---------------------------------------------------------------------------*/
+
+#ifndef cpuLoad_H
+#define cpuLoad_H
+
+#include "volFields.H"
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+namespace Foam
+{
+
+/*---------------------------------------------------------------------------*\
+ Class optionalCpuLoad Declaration
+\*---------------------------------------------------------------------------*/
+
+class optionalCpuLoad
+{
+ // Private Member Data
+
+ //- Dummy optionalCpuLoad
+ // returned by optionalCpuLoad::New if loadBalancing = false
+ static optionalCpuLoad optionalCpuLoad_;
+
+
+public:
+
+ // Constructors
+
+ //- Construct from mesh, name and switch
+ optionalCpuLoad()
+ {}
+
+ //- Disallow default bitwise copy construction
+ optionalCpuLoad(const optionalCpuLoad&) = delete;
+
+
+ // Selectors
+
+ static optionalCpuLoad& New
+ (
+ const fvMesh& mesh,
+ const word& name,
+ const bool loadBalancing
+ );
+
+
+ //- Destructor
+ virtual ~optionalCpuLoad()
+ {}
+
+
+ // Member Functions
+
+ //- Dummy reset function
+ virtual void reset()
+ {}
+
+ //- Dummy cpuTimeIncrement function
+ virtual void cpuTimeIncrement(const label celli)
+ {}
+
+
+ // Member Operators
+
+ //- Disallow default bitwise assignment
+ void operator=(const optionalCpuLoad&) = delete;
+};
+
+
+/*---------------------------------------------------------------------------*\
+ Class cpuLoad Declaration
+\*---------------------------------------------------------------------------*/
+
+class cpuLoad
+:
+ public optionalCpuLoad,
+ public volScalarField::Internal
+{
+ // Private Data
+
+ cpuTime cpuTime_;
+
+
+public:
+
+ // Declare name of the class and its debug switch
+ TypeName("cpuLoad");
+
+
+ // Constructors
+
+ //- Construct from mesh, name and switch
+ cpuLoad
+ (
+ const fvMesh& mesh,
+ const word& name
+ );
+
+ //- Disallow default bitwise copy construction
+ cpuLoad(const cpuLoad&) = delete;
+
+
+ //- Destructor
+ virtual ~cpuLoad();
+
+
+ // Member Functions
+
+ //- Reset the CPU time
+ virtual void reset();
+
+ //- Cache the CPU time increment for celli
+ virtual void cpuTimeIncrement(const label celli);
+
+
+ // Member Operators
+
+ //- Disallow default bitwise assignment
+ void operator=(const cpuLoad&) = delete;
+};
+
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+} // End namespace Foam
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+#endif
+
+// ************************************************************************* //
diff --git a/src/fvMeshDistributors/loadBalancer/fvMeshDistributorsLoadBalancer.C b/src/fvMeshDistributors/loadBalancer/fvMeshDistributorsLoadBalancer.C
index fe82081c60..dd15368edf 100644
--- a/src/fvMeshDistributors/loadBalancer/fvMeshDistributorsLoadBalancer.C
+++ b/src/fvMeshDistributors/loadBalancer/fvMeshDistributorsLoadBalancer.C
@@ -25,7 +25,7 @@ License
#include "fvMeshDistributorsLoadBalancer.H"
#include "decompositionMethod.H"
-#include "volFields.H"
+#include "cpuLoad.H"
#include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@@ -93,20 +93,26 @@ bool Foam::fvMeshDistributors::loadBalancer::update()
const scalar timeStepCpuTime = cpuTime_.cpuTimeIncrement();
- // Chemistry CPU load per cell
- volScalarField::Internal& chemistryCpuTimeReg =
- mesh.lookupObjectRef
- (
- "chemistryCpuTime"
- );
+ // CPU loads per cell
+ HashTable cpuLoads(this->mesh().lookupClass());
- const scalarField& chemistryCpuTime = chemistryCpuTimeReg.field();
+ if (!cpuLoads.size())
+ {
+ FatalErrorInFunction
+ << "No CPU loads have been allocated"
+ << exit(FatalError);
+ }
if (mesh.time().timeIndex() % redistributionInterval_ == 0)
{
timeIndex_ = mesh.time().timeIndex();
- const scalar sumCpuLoad(sum(chemistryCpuTime));
+ scalar sumCpuLoad = 0;
+
+ forAllConstIter(HashTable, cpuLoads, iter)
+ {
+ sumCpuLoad += sum(iter()->field());
+ }
const scalar cellCFDCpuTime = returnReduce
(
@@ -139,19 +145,36 @@ bool Foam::fvMeshDistributors::loadBalancer::update()
if (multiConstraint_)
{
- const int nWeights = 2;
+ const int nWeights = cpuLoads.size() + 1;
weights.setSize(nWeights*mesh.nCells());
- forAll(chemistryCpuTime, i)
+ for (label i=0; i, cpuLoads, iter)
+ {
+ const scalarField& cpuLoadField = iter()->field();
+
+ forAll(cpuLoadField, i)
+ {
+ weights[nWeights*i + loadi] = cpuLoadField[i];
+ }
+
+ loadi++;
}
}
else
{
- weights = chemistryCpuTime + cellCFDCpuTime;
+ weights.setSize(mesh.nCells(), cellCFDCpuTime);
+
+ forAllConstIter(HashTable, cpuLoads, iter)
+ {
+ weights += iter()->field();
+ }
}
if (imbalance > maxImbalance_)
@@ -171,7 +194,10 @@ bool Foam::fvMeshDistributors::loadBalancer::update()
}
}
- chemistryCpuTimeReg.checkOut();
+ forAllIter(HashTable, cpuLoads, iter)
+ {
+ iter()->checkOut();
+ }
}
return redistributed;
diff --git a/src/thermophysicalModels/chemistryModel/chemistryModel/chemistryModel.C b/src/thermophysicalModels/chemistryModel/chemistryModel/chemistryModel.C
index 0eac704cf2..ae637ef88f 100644
--- a/src/thermophysicalModels/chemistryModel/chemistryModel/chemistryModel.C
+++ b/src/thermophysicalModels/chemistryModel/chemistryModel/chemistryModel.C
@@ -26,7 +26,7 @@ License
#include "chemistryModel.H"
#include "UniformField.H"
#include "localEulerDdtScheme.H"
-#include "cpuTime.H"
+#include "cpuLoad.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
@@ -710,41 +710,16 @@ Foam::scalar Foam::chemistryModel::solve
const DeltaTType& deltaT
)
{
- if (loadBalancing_)
- {
- if
- (
- !this->mesh().objectRegistry::template
- foundObject("chemistryCpuTime")
- )
- {
- regIOobject::store
- (
- volScalarField::Internal::New
- (
- "chemistryCpuTime",
- this->mesh(),
- dimensionedScalar(dimTime, 0)
- ).ptr()
- );
- }
- }
-
- volScalarField::Internal& chemistryCpuTime =
- loadBalancing_
- ? this->mesh().objectRegistry::template
- lookupObjectRef
- (
- "chemistryCpuTime"
- )
- : const_cast(volScalarField::Internal::null());
-
tabulation_.reset();
const basicSpecieMixture& composition = this->thermo().composition();
+ optionalCpuLoad& chemistryCpuTime
+ (
+ optionalCpuLoad::New(this->mesh(), "chemistryCpuTime", loadBalancing_)
+ );
+
// CPU time analysis
- cpuTime cpuTime_;
cpuTime solveCpuTime_;
scalar totalSolveCpuTime_ = 0;
@@ -773,6 +748,8 @@ Foam::scalar Foam::chemistryModel::solve
scalarField phiq(nEqns() + 1);
scalarField Rphiq(nEqns() + 1);
+ chemistryCpuTime.reset();
+
forAll(rho0vf, celli)
{
const scalar rho = rhovf[celli];
@@ -913,7 +890,7 @@ Foam::scalar Foam::chemistryModel::solve
if (loadBalancing_)
{
- chemistryCpuTime[celli] += cpuTime_.cpuTimeIncrement();
+ chemistryCpuTime.cpuTimeIncrement(celli);
}
}