Skip to content

Commit

Permalink
shared DB client between multiple function objects
Browse files Browse the repository at this point in the history
  • Loading branch information
FoamScience committed Nov 2, 2023
1 parent e3c6e84 commit 4dcc52f
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
smartRedisAdapter.C
smartSimFunctionObject.C

LIB = $(FOAM_USER_LIBBIN)/libsmartSimFunctionObject
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2023 AUTHOR,AFFILIATION
-------------------------------------------------------------------------------
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 <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/

#include "smartRedisAdapter.H"
#include "Time.H"


// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

namespace Foam
{
defineTypeNameAndDebug(smartRedisAdapter, 0);
}


// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //

Foam::smartRedisAdapter::smartRedisAdapter
(
const IOobject& io,
const dictionary& dict
)
:
regIOobject(io),
refCount(),
clusterMode_(dict.getOrDefault<Switch>("clusterMode", true)),
client_(clusterMode_, io.name()) // deprecated constructor though
{
}



// ************************************************************************* //
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2023 AUTHOR, AFFILIATION
-------------------------------------------------------------------------------
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 <http://www.gnu.org/licenses/>.
Class
Foam::smartRedisAdapter
Description
\*---------------------------------------------------------------------------*/

#ifndef smartRedisAdapter_H
#define smartRedisAdapter_H

#include "regIOobject.H"
#include "client.h"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

namespace Foam
{

/*---------------------------------------------------------------------------*\
Class smartRedisAdapter Declaration
\*---------------------------------------------------------------------------*/

class smartRedisAdapter
:
public regIOobject,
public refCount
{
protected:

// Protected Data

//- cluster mode
bool clusterMode_;

//- SmartRedis Database Client
SmartRedis::Client client_;

public:

//- Runtime type information
TypeName("smartRedisAdapter");


// Constructors

//- Construct from Time and dictionary
explicit smartRedisAdapter
(
const IOobject& io,
const dictionary& dict
);

//- No copy construct
smartRedisAdapter(const smartRedisAdapter&) = delete;

//- No copy assignment
void operator=(const smartRedisAdapter&) = delete;


//- Destructor
virtual ~smartRedisAdapter() = default;


// Member Functions

//- return the client's instance
SmartRedis::Client& client() { return client_; };

//- Implement writing to ostream from regIOobject
virtual bool writeData(Ostream&) const { return true; }
};


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

} // End namespace Foam

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

#endif

// ************************************************************************* //
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ License

#include "IOdictionary.H"
#include "objectRegistry.H"
#include "smartRedisAdapter.H"
#include "smartSimFunctionObject.H"
#include "Time.H"
#include "fvMesh.H"
Expand All @@ -45,7 +46,6 @@ namespace functionObjects
{
defineTypeNameAndDebug(smartSimFunctionObject, 0);
addToRunTimeSelectionTable(functionObject, smartSimFunctionObject, dictionary);
SmartRedis::Client smartSimFunctionObject::redisDB(false, "default");
}
}

Expand All @@ -60,10 +60,25 @@ Foam::functionObjects::smartSimFunctionObject::smartSimFunctionObject
)
:
fvMeshFunctionObject(name, runTime, dict),
//clusterMode_(dict.getOrDefault<bool>("clusterMode", true)),
clientName_(dict.getOrDefault<word>("clientName", "default")),
fieldNames_(dict.get<wordList>("fieldNames")),
fieldDimensions_(dict.get<labelList>("fieldDimensions"))//,
//client_(clusterMode_)
fieldDimensions_(dict.get<labelList>("fieldDimensions")),
redisDB_(
runTime.foundObject<smartRedisAdapter>(clientName_)
? &runTime.lookupObjectRef<smartRedisAdapter>(clientName_)
: new smartRedisAdapter
(
IOobject
(
clientName_,
runTime.timeName(),
runTime,
IOobject::NO_READ,
IOobject::AUTO_WRITE
),
dict
)
)
{
read(dict);
}
Expand Down Expand Up @@ -118,7 +133,7 @@ bool Foam::functionObjects::smartSimFunctionObject::end()
const volScalarField& sField = mesh_.lookupObject<volScalarField>(fieldNames_[fieldI]);

// Send the cell-centered scalar field to SmartRedis
redisDB.put_tensor(sField.name(), (void*)sField.internalField().cdata(), dims,
client().put_tensor(sField.name(), (void*)sField.internalField().cdata(), dims,
SRTensorTypeDouble, SRMemLayoutContiguous);

}
Expand All @@ -128,7 +143,7 @@ bool Foam::functionObjects::smartSimFunctionObject::end()
const volVectorField& vField = mesh_.lookupObject<volVectorField>(fieldNames_[fieldI]);

// Send the cell-centered scalar field to SmartRedis
redisDB.put_tensor(vField.name(), (void*)vField.internalField().cdata(), dims,
client().put_tensor(vField.name(), (void*)vField.internalField().cdata(), dims,
SRTensorTypeDouble, SRMemLayoutContiguous);
}
else if (fieldDimensions_[fieldI] == 6) // TODO(TM): symmTensor field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ SourceFiles
#define smartSimFunctionObject_H

#include "fvMeshFunctionObject.H"
#include "client.h"
#include "smartRedisAdapter.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

Expand All @@ -167,9 +167,9 @@ class smartSimFunctionObject
{
// Private Data

//- Set to false if not using a clustered database
//bool clusterMode_;
//- Client name (for debug output)
word clientName_;

//- List of field names to send-receive from SmartRedis
wordList fieldNames_;

Expand All @@ -178,15 +178,15 @@ class smartSimFunctionObject
// dimension and 6 is a symmetric tensor field dimension.
labelList fieldDimensions_;

//- The RedisAI database client
tmp<smartRedisAdapter> redisDB_;

public:

//- Runtime type information
TypeName("smartSimFunctionObject");


//- SmartRedis Database Client
static SmartRedis::Client redisDB;

// Constructors

//- Construct from Time and dictionary
Expand Down Expand Up @@ -217,6 +217,8 @@ public:
virtual bool end();

virtual bool write();

SmartRedis::Client& client() { return redisDB_->client(); }
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ TEST_CASE("Shared SmartRedis client", "[cavity][serial][parallel]")
dict0.set("type", "smartSimFunctionObject");
dict0.set("fieldNames", wordList());
dict0.set("fieldDimensions", labelList());
dict0.set("cluserMode", false);
dict0.set("clientName", "default");
dictionary dict1;
dict1.set("region", polyMesh::defaultRegion);
dict1.set("type", "smartSimFunctionObject");
dict1.set("fieldNames", wordList());
dict1.set("fieldDimensions", labelList());
dict1.set("cluserMode", false);
dict1.set("clientName", "default");
functionObjects::smartSimFunctionObject o0("smartSim0", runTime, dict0);
functionObjects::smartSimFunctionObject o1("smartSim1", runTime, dict1);
REQUIRE(&(o0.redisDB) == &(o1.redisDB));
REQUIRE(&(o0.client()) == &(o1.client()));
}

0 comments on commit 4dcc52f

Please sign in to comment.