Skip to content

Commit 9443dc3

Browse files
committed
static local client for configuration and proper memory management
1 parent 4dfd763 commit 9443dc3

File tree

5 files changed

+189
-8
lines changed

5 files changed

+189
-8
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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
bool clusterMode
46+
)
47+
:
48+
regIOobject(io),
49+
client_(clusterMode, io.name()) // deprecated constructor though
50+
{
51+
}
52+
53+
54+
55+
// ************************************************************************* //
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
{
52+
protected:
53+
54+
// Protected Data
55+
56+
//- cluster mode
57+
bool clusterMode_;
58+
59+
//- SmartRedis Database Client
60+
SmartRedis::Client client_;
61+
62+
public:
63+
64+
//- Runtime type information
65+
TypeName("smartRedisAdapter");
66+
67+
68+
// Constructors
69+
70+
//- Construct from Time and dictionary
71+
explicit smartRedisAdapter
72+
(
73+
const IOobject& io,
74+
bool clusterMode = false
75+
);
76+
77+
//- No copy construct
78+
smartRedisAdapter(const smartRedisAdapter&) = delete;
79+
80+
//- No copy assignment
81+
void operator=(const smartRedisAdapter&) = delete;
82+
83+
84+
//- Destructor
85+
virtual ~smartRedisAdapter() = default;
86+
87+
88+
// Member Functions
89+
90+
//- return the client's instance
91+
SmartRedis::Client& client() {return client_;};
92+
93+
//- Implement writing to ostream from regIOobject
94+
virtual bool writeData(Ostream&) const { return true; }
95+
};
96+
97+
98+
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
99+
100+
} // End namespace Foam
101+
102+
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
103+
104+
#endif
105+
106+
// ************************************************************************* //

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

Lines changed: 23 additions & 4 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"
@@ -66,8 +67,26 @@ Foam::functionObjects::smartSimFunctionObject::smartSimFunctionObject
6667
redisDBPtr_(nullptr)
6768
//client_(clusterMode_)
6869
{
69-
static SmartRedis::Client client(clusterMode_, clientName_.c_str());
70-
redisDBPtr_.reset(&client);
70+
// Only look at top runTime level for a RedisAI client,
71+
// if you want more clients, register them to lower levels of the registry (eg. mesh)
72+
if (runTime.foundObject<smartRedisAdapter>(clientName_)) {
73+
redisDBPtr_ = autoPtr<smartRedisAdapter>(&runTime.lookupObjectRef<smartRedisAdapter>(clientName_));
74+
} else {
75+
static smartRedisAdapter client // no client was registered yet? construct and register one now
76+
(
77+
IOobject
78+
(
79+
clientName_,
80+
runTime.system()/"smartRedisDict",
81+
runTime,
82+
IOobject::NO_READ,
83+
IOobject::NO_WRITE,
84+
true
85+
),
86+
clusterMode_
87+
);
88+
redisDBPtr_.reset(&client);
89+
}
7190
read(dict);
7291
}
7392

@@ -121,7 +140,7 @@ bool Foam::functionObjects::smartSimFunctionObject::end()
121140
const volScalarField& sField = mesh_.lookupObject<volScalarField>(fieldNames_[fieldI]);
122141

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

127146
}
@@ -131,7 +150,7 @@ bool Foam::functionObjects::smartSimFunctionObject::end()
131150
const volVectorField& vField = mesh_.lookupObject<volVectorField>(fieldNames_[fieldI]);
132151

133152
// Send the cell-centered scalar field to SmartRedis
134-
redisDB().put_tensor(vField.name(), (void*)vField.internalField().cdata(), dims,
153+
redisDB().client().put_tensor(vField.name(), (void*)vField.internalField().cdata(), dims,
135154
SRTensorTypeDouble, SRMemLayoutContiguous);
136155
}
137156
else if (fieldDimensions_[fieldI] == 6) // TODO(TM): symmTensor field

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

Lines changed: 4 additions & 4 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

@@ -181,8 +181,8 @@ class smartSimFunctionObject
181181
// dimension and 6 is a symmetric tensor field dimension.
182182
labelList fieldDimensions_;
183183

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

187187
public:
188188

@@ -220,7 +220,7 @@ public:
220220

221221
virtual bool write();
222222

223-
SmartRedis::Client& redisDB() {return *redisDBPtr_;}
223+
smartRedisAdapter& redisDB() {return *redisDBPtr_;}
224224
};
225225

226226

0 commit comments

Comments
 (0)