diff --git a/2023-01/smartsim/smartsim_function_object/smartSimFunctionObject/Make/files b/2023-01/smartsim/smartsim_function_object/smartSimFunctionObject/Make/files index 316ea6da..d7075d2f 100644 --- a/2023-01/smartsim/smartsim_function_object/smartSimFunctionObject/Make/files +++ b/2023-01/smartsim/smartsim_function_object/smartSimFunctionObject/Make/files @@ -1,3 +1,4 @@ +smartRedisAdapter.C smartSimFunctionObject.C LIB = $(FOAM_USER_LIBBIN)/libsmartSimFunctionObject diff --git a/2023-01/smartsim/smartsim_function_object/smartSimFunctionObject/smartRedisAdapter.C b/2023-01/smartsim/smartsim_function_object/smartSimFunctionObject/smartRedisAdapter.C new file mode 100644 index 00000000..7854bfab --- /dev/null +++ b/2023-01/smartsim/smartsim_function_object/smartSimFunctionObject/smartRedisAdapter.C @@ -0,0 +1,68 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / 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 . + +\*---------------------------------------------------------------------------*/ + +#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("clusterMode", true)), + client_(clusterMode_, io.name()) // deprecated constructor though +{ +} + +Foam::smartRedisAdapter::smartRedisAdapter +( + smartRedisAdapter* ptr +) +: + regIOobject(*ptr), + refCount(), + clusterMode_(ptr->clusterMode_), + client_(std::move(ptr->client_)) // no copy of the client +{ +} + + +// ************************************************************************* // diff --git a/2023-01/smartsim/smartsim_function_object/smartSimFunctionObject/smartRedisAdapter.H b/2023-01/smartsim/smartsim_function_object/smartSimFunctionObject/smartRedisAdapter.H new file mode 100644 index 00000000..857ca8a8 --- /dev/null +++ b/2023-01/smartsim/smartsim_function_object/smartSimFunctionObject/smartRedisAdapter.H @@ -0,0 +1,113 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / 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 . + +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 + smartRedisAdapter + ( + const IOobject& io, + const dictionary& dict + ); + + //- Construct from a pointer by moving the client + explicit smartRedisAdapter + ( + smartRedisAdapter* ptr + ); + + //- 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 + +// ************************************************************************* // diff --git a/2023-01/smartsim/smartsim_function_object/smartSimFunctionObject/smartSimFunctionObject.C b/2023-01/smartsim/smartsim_function_object/smartSimFunctionObject/smartSimFunctionObject.C index 912e43b5..eb6c28d5 100644 --- a/2023-01/smartsim/smartsim_function_object/smartSimFunctionObject/smartSimFunctionObject.C +++ b/2023-01/smartsim/smartsim_function_object/smartSimFunctionObject/smartSimFunctionObject.C @@ -27,6 +27,7 @@ License #include "IOdictionary.H" #include "objectRegistry.H" +#include "smartRedisAdapter.H" #include "smartSimFunctionObject.H" #include "Time.H" #include "fvMesh.H" @@ -45,7 +46,6 @@ namespace functionObjects { defineTypeNameAndDebug(smartSimFunctionObject, 0); addToRunTimeSelectionTable(functionObject, smartSimFunctionObject, dictionary); - SmartRedis::Client smartSimFunctionObject::redisDB(false, "default"); } } @@ -60,10 +60,44 @@ Foam::functionObjects::smartSimFunctionObject::smartSimFunctionObject ) : fvMeshFunctionObject(name, runTime, dict), - //clusterMode_(dict.getOrDefault("clusterMode", true)), + clientName_(dict.getOrDefault("clientName", "default")), fieldNames_(dict.get("fieldNames")), - fieldDimensions_(dict.get("fieldDimensions"))//, - //client_(clusterMode_) + fieldDimensions_(dict.get("fieldDimensions")), + redisDB_( + runTime.foundObject(clientName_) + ? &runTime.lookupObjectRef(clientName_) + : new smartRedisAdapter + ( + IOobject + ( + clientName_, + runTime.timeName(), + runTime, + IOobject::NO_READ, + IOobject::AUTO_WRITE + ), + dict + ) + ) + //redisDB_( + // runTime.foundObject(clientName_) + // ? std::make_shared(&runTime.lookupObjectRef(clientName_)) + // : std::make_shared + // ( + // new smartRedisAdapter + // ( + // IOobject + // ( + // clientName_, + // runTime.timeName(), + // runTime, + // IOobject::NO_READ, + // IOobject::AUTO_WRITE + // ), + // dict + // ) + // ) + //) { read(dict); } @@ -118,7 +152,7 @@ bool Foam::functionObjects::smartSimFunctionObject::end() const volScalarField& sField = mesh_.lookupObject(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); } @@ -128,7 +162,7 @@ bool Foam::functionObjects::smartSimFunctionObject::end() const volVectorField& vField = mesh_.lookupObject(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 diff --git a/2023-01/smartsim/smartsim_function_object/smartSimFunctionObject/smartSimFunctionObject.H b/2023-01/smartsim/smartsim_function_object/smartSimFunctionObject/smartSimFunctionObject.H index 20de6964..8676125f 100644 --- a/2023-01/smartsim/smartsim_function_object/smartSimFunctionObject/smartSimFunctionObject.H +++ b/2023-01/smartsim/smartsim_function_object/smartSimFunctionObject/smartSimFunctionObject.H @@ -148,7 +148,7 @@ SourceFiles #define smartSimFunctionObject_H #include "fvMeshFunctionObject.H" -#include "client.h" +#include "smartRedisAdapter.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -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_; @@ -178,15 +178,16 @@ class smartSimFunctionObject // dimension and 6 is a symmetric tensor field dimension. labelList fieldDimensions_; + //- The RedisAI database client + tmp redisDB_; + //std::shared_ptr redisDB_; + public: //- Runtime type information TypeName("smartSimFunctionObject"); - //- SmartRedis Database Client - static SmartRedis::Client redisDB; - // Constructors //- Construct from Time and dictionary @@ -217,6 +218,8 @@ public: virtual bool end(); virtual bool write(); + + SmartRedis::Client& client() { return redisDB_->client(); } }; diff --git a/2023-01/smartsim/smartsim_function_object/tests/smartSimFOTest.C b/2023-01/smartsim/smartsim_function_object/tests/smartSimFOTest.C index 054e63dc..18a9d9c3 100644 --- a/2023-01/smartsim/smartsim_function_object/tests/smartSimFOTest.C +++ b/2023-01/smartsim/smartsim_function_object/tests/smartSimFOTest.C @@ -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("clusterMode", 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("clusterMode", 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()); }