Skip to content

Commit

Permalink
static local client for configuration and proper memory management
Browse files Browse the repository at this point in the history
  • Loading branch information
FoamScience committed Nov 2, 2023
1 parent 4dfd763 commit 9443dc3
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 8 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,55 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / 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,
bool clusterMode
)
:
regIOobject(io),
client_(clusterMode, io.name()) // deprecated constructor though
{
}



// ************************************************************************* //
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / 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
{
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,
bool clusterMode = false
);

//- 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 Down Expand Up @@ -66,8 +67,26 @@ Foam::functionObjects::smartSimFunctionObject::smartSimFunctionObject
redisDBPtr_(nullptr)
//client_(clusterMode_)
{
static SmartRedis::Client client(clusterMode_, clientName_.c_str());
redisDBPtr_.reset(&client);
// Only look at top runTime level for a RedisAI client,
// if you want more clients, register them to lower levels of the registry (eg. mesh)
if (runTime.foundObject<smartRedisAdapter>(clientName_)) {
redisDBPtr_ = autoPtr<smartRedisAdapter>(&runTime.lookupObjectRef<smartRedisAdapter>(clientName_));
} else {
static smartRedisAdapter client // no client was registered yet? construct and register one now
(
IOobject
(
clientName_,
runTime.system()/"smartRedisDict",
runTime,
IOobject::NO_READ,
IOobject::NO_WRITE,
true
),
clusterMode_
);
redisDBPtr_.reset(&client);
}
read(dict);
}

Expand Down Expand Up @@ -121,7 +140,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,
redisDB().client().put_tensor(sField.name(), (void*)sField.internalField().cdata(), dims,
SRTensorTypeDouble, SRMemLayoutContiguous);

}
Expand All @@ -131,7 +150,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,
redisDB().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 Down Expand Up @@ -181,8 +181,8 @@ class smartSimFunctionObject
// dimension and 6 is a symmetric tensor field dimension.
labelList fieldDimensions_;

//- Pointer to the client
autoPtr<SmartRedis::Client> redisDBPtr_;
//- Pointer to the client wrapper
autoPtr<smartRedisAdapter> redisDBPtr_;

public:

Expand Down Expand Up @@ -220,7 +220,7 @@ public:

virtual bool write();

SmartRedis::Client& redisDB() {return *redisDBPtr_;}
smartRedisAdapter& redisDB() {return *redisDBPtr_;}
};


Expand Down

0 comments on commit 9443dc3

Please sign in to comment.