Skip to content

Commit 569ebf1

Browse files
committed
shared DB client between multiple function objects
1 parent e3c6e84 commit 569ebf1

File tree

6 files changed

+237
-14
lines changed

6 files changed

+237
-14
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
smartRedisAdapter.C
12
smartSimFunctionObject.C
23

34
LIB = $(FOAM_USER_LIBBIN)/libsmartSimFunctionObject
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*---------------------------------------------------------------------------*\
2+
========= |
3+
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4+
\\ / O peration |
5+
\\ / A nd | www.openfoam.com
6+
\\/ M anipulation |
7+
-------------------------------------------------------------------------------
8+
Copyright (C) 2023 AUTHOR,AFFILIATION
9+
-------------------------------------------------------------------------------
10+
License
11+
This file is part of OpenFOAM.
12+
13+
OpenFOAM is free software: you can redistribute it and/or modify it
14+
under the terms of the GNU General Public License as published by
15+
the Free Software Foundation, either version 3 of the License, or
16+
(at your option) any later version.
17+
18+
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21+
for more details.
22+
23+
You should have received a copy of the GNU General Public License
24+
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25+
26+
\*---------------------------------------------------------------------------*/
27+
28+
#include "smartRedisAdapter.H"
29+
#include "Time.H"
30+
31+
32+
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33+
34+
namespace Foam
35+
{
36+
defineTypeNameAndDebug(smartRedisAdapter, 0);
37+
}
38+
39+
40+
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
41+
42+
Foam::smartRedisAdapter::smartRedisAdapter
43+
(
44+
const IOobject& io,
45+
const dictionary& dict
46+
)
47+
:
48+
regIOobject(io),
49+
refCount(),
50+
clusterMode_(dict.getOrDefault<Switch>("clusterMode", true)),
51+
client_(clusterMode_, io.name()) // deprecated constructor though
52+
{
53+
}
54+
55+
Foam::smartRedisAdapter::smartRedisAdapter
56+
(
57+
smartRedisAdapter* ptr
58+
)
59+
:
60+
regIOobject(*ptr),
61+
refCount(),
62+
clusterMode_(ptr->clusterMode_),
63+
client_(std::move(ptr->client_)) // no copy of the client
64+
{
65+
}
66+
67+
68+
// ************************************************************************* //
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*---------------------------------------------------------------------------*\
2+
========= |
3+
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4+
\\ / O peration |
5+
\\ / A nd | www.openfoam.com
6+
\\/ M anipulation |
7+
-------------------------------------------------------------------------------
8+
Copyright (C) 2023 AUTHOR, AFFILIATION
9+
-------------------------------------------------------------------------------
10+
License
11+
This file is part of OpenFOAM.
12+
13+
OpenFOAM is free software: you can redistribute it and/or modify it
14+
under the terms of the GNU General Public License as published by
15+
the Free Software Foundation, either version 3 of the License, or
16+
(at your option) any later version.
17+
18+
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21+
for more details.
22+
23+
You should have received a copy of the GNU General Public License
24+
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25+
26+
Class
27+
Foam::smartRedisAdapter
28+
29+
Description
30+
31+
\*---------------------------------------------------------------------------*/
32+
33+
#ifndef smartRedisAdapter_H
34+
#define smartRedisAdapter_H
35+
36+
#include "regIOobject.H"
37+
#include "client.h"
38+
39+
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
40+
41+
namespace Foam
42+
{
43+
44+
/*---------------------------------------------------------------------------*\
45+
Class smartRedisAdapter Declaration
46+
\*---------------------------------------------------------------------------*/
47+
48+
class smartRedisAdapter
49+
:
50+
public regIOobject,
51+
public refCount
52+
{
53+
protected:
54+
55+
// Protected Data
56+
57+
//- cluster mode
58+
bool clusterMode_;
59+
60+
//- SmartRedis Database Client
61+
SmartRedis::Client client_;
62+
63+
public:
64+
65+
//- Runtime type information
66+
TypeName("smartRedisAdapter");
67+
68+
69+
// Constructors
70+
71+
//- Construct from Time and dictionary
72+
smartRedisAdapter
73+
(
74+
const IOobject& io,
75+
const dictionary& dict
76+
);
77+
78+
//- Construct from a pointer by moving the client
79+
explicit smartRedisAdapter
80+
(
81+
smartRedisAdapter* ptr
82+
);
83+
84+
//- No copy construct
85+
smartRedisAdapter(const smartRedisAdapter&) = delete;
86+
87+
//- No copy assignment
88+
void operator=(const smartRedisAdapter&) = delete;
89+
90+
91+
//- Destructor
92+
virtual ~smartRedisAdapter() = default;
93+
94+
95+
// Member Functions
96+
97+
//- return the client's instance
98+
SmartRedis::Client& client() { return client_; };
99+
100+
//- Implement writing to ostream from regIOobject
101+
virtual bool writeData(Ostream&) const { return true; }
102+
};
103+
104+
105+
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
106+
107+
} // End namespace Foam
108+
109+
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
110+
111+
#endif
112+
113+
// ************************************************************************* //

2023-01/smartsim/smartsim_function_object/smartSimFunctionObject/smartSimFunctionObject.C

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ License
2727

2828
#include "IOdictionary.H"
2929
#include "objectRegistry.H"
30+
#include "smartRedisAdapter.H"
3031
#include "smartSimFunctionObject.H"
3132
#include "Time.H"
3233
#include "fvMesh.H"
@@ -45,7 +46,6 @@ namespace functionObjects
4546
{
4647
defineTypeNameAndDebug(smartSimFunctionObject, 0);
4748
addToRunTimeSelectionTable(functionObject, smartSimFunctionObject, dictionary);
48-
SmartRedis::Client smartSimFunctionObject::redisDB(false, "default");
4949
}
5050
}
5151

@@ -60,10 +60,44 @@ Foam::functionObjects::smartSimFunctionObject::smartSimFunctionObject
6060
)
6161
:
6262
fvMeshFunctionObject(name, runTime, dict),
63-
//clusterMode_(dict.getOrDefault<bool>("clusterMode", true)),
63+
clientName_(dict.getOrDefault<word>("clientName", "default")),
6464
fieldNames_(dict.get<wordList>("fieldNames")),
65-
fieldDimensions_(dict.get<labelList>("fieldDimensions"))//,
66-
//client_(clusterMode_)
65+
fieldDimensions_(dict.get<labelList>("fieldDimensions")),
66+
redisDB_(
67+
runTime.foundObject<smartRedisAdapter>(clientName_)
68+
? &runTime.lookupObjectRef<smartRedisAdapter>(clientName_)
69+
: new smartRedisAdapter
70+
(
71+
IOobject
72+
(
73+
clientName_,
74+
runTime.timeName(),
75+
runTime,
76+
IOobject::NO_READ,
77+
IOobject::AUTO_WRITE
78+
),
79+
dict
80+
)
81+
)
82+
//redisDB_(
83+
// runTime.foundObject<smartRedisAdapter>(clientName_)
84+
// ? std::make_shared<smartRedisAdapter>(&runTime.lookupObjectRef<smartRedisAdapter>(clientName_))
85+
// : std::make_shared<smartRedisAdapter>
86+
// (
87+
// new smartRedisAdapter
88+
// (
89+
// IOobject
90+
// (
91+
// clientName_,
92+
// runTime.timeName(),
93+
// runTime,
94+
// IOobject::NO_READ,
95+
// IOobject::AUTO_WRITE
96+
// ),
97+
// dict
98+
// )
99+
// )
100+
//)
67101
{
68102
read(dict);
69103
}
@@ -118,7 +152,7 @@ bool Foam::functionObjects::smartSimFunctionObject::end()
118152
const volScalarField& sField = mesh_.lookupObject<volScalarField>(fieldNames_[fieldI]);
119153

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

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

130164
// Send the cell-centered scalar field to SmartRedis
131-
redisDB.put_tensor(vField.name(), (void*)vField.internalField().cdata(), dims,
165+
client().put_tensor(vField.name(), (void*)vField.internalField().cdata(), dims,
132166
SRTensorTypeDouble, SRMemLayoutContiguous);
133167
}
134168
else if (fieldDimensions_[fieldI] == 6) // TODO(TM): symmTensor field

2023-01/smartsim/smartsim_function_object/smartSimFunctionObject/smartSimFunctionObject.H

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ SourceFiles
148148
#define smartSimFunctionObject_H
149149

150150
#include "fvMeshFunctionObject.H"
151-
#include "client.h"
151+
#include "smartRedisAdapter.H"
152152

153153
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
154154

@@ -167,9 +167,9 @@ class smartSimFunctionObject
167167
{
168168
// Private Data
169169

170-
//- Set to false if not using a clustered database
171-
//bool clusterMode_;
172-
170+
//- Client name (for debug output)
171+
word clientName_;
172+
173173
//- List of field names to send-receive from SmartRedis
174174
wordList fieldNames_;
175175

@@ -178,15 +178,16 @@ class smartSimFunctionObject
178178
// dimension and 6 is a symmetric tensor field dimension.
179179
labelList fieldDimensions_;
180180

181+
//- The RedisAI database client
182+
tmp<smartRedisAdapter> redisDB_;
183+
//std::shared_ptr<smartRedisAdapter> redisDB_;
184+
181185
public:
182186

183187
//- Runtime type information
184188
TypeName("smartSimFunctionObject");
185189

186190

187-
//- SmartRedis Database Client
188-
static SmartRedis::Client redisDB;
189-
190191
// Constructors
191192

192193
//- Construct from Time and dictionary
@@ -217,6 +218,8 @@ public:
217218
virtual bool end();
218219

219220
virtual bool write();
221+
222+
SmartRedis::Client& client() { return redisDB_->client(); }
220223
};
221224

222225

2023-01/smartsim/smartsim_function_object/tests/smartSimFOTest.C

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,16 @@ TEST_CASE("Shared SmartRedis client", "[cavity][serial][parallel]")
3232
dict0.set("type", "smartSimFunctionObject");
3333
dict0.set("fieldNames", wordList());
3434
dict0.set("fieldDimensions", labelList());
35+
dict0.set("clusterMode", false);
36+
dict0.set("clientName", "default");
3537
dictionary dict1;
3638
dict1.set("region", polyMesh::defaultRegion);
3739
dict1.set("type", "smartSimFunctionObject");
3840
dict1.set("fieldNames", wordList());
3941
dict1.set("fieldDimensions", labelList());
42+
dict1.set("clusterMode", false);
43+
dict1.set("clientName", "default");
4044
functionObjects::smartSimFunctionObject o0("smartSim0", runTime, dict0);
4145
functionObjects::smartSimFunctionObject o1("smartSim1", runTime, dict1);
42-
REQUIRE(&(o0.redisDB) == &(o1.redisDB));
46+
REQUIRE(&o0.client() == &o1.client());
4347
}

0 commit comments

Comments
 (0)