UnitTemplate.h 11 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//================================================================================
// Name        : UnitTemplate.h
// Author      : Alessio Netti
// Copyright   : Leibniz Supercomputing Centre
// Description : Template implementing features to use Units in Analyzers.
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
// Copyright (C) 2018-2019 Leibniz Supercomputing Centre
//
// This program 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 2
// of the License, or (at your option) any later version.
Alessio Netti's avatar
Alessio Netti committed
16
//
17
18
19
20
// This program 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.
Alessio Netti's avatar
Alessio Netti committed
21
//
22
23
24
25
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//================================================================================
Alessio Netti's avatar
Alessio Netti committed
26
27
28
29
30
31
32
33
34
35
36
37
38

#ifndef PROJECT_UNITTEMPLATE_H
#define PROJECT_UNITTEMPLATE_H

#include "UnitInterface.h"

/**
 * Template that implements features needed to use Units in Analyzers.
 *
 * The template accepts any class derived from SensorBase, allowing users to add their own attributes to input and output
 * sensors. Users should employ this template in their plugins if possible, and change it only if absolutely necessary.
 *
 */
Alessio Netti's avatar
Alessio Netti committed
39
template <class S=SensorBase>
Alessio Netti's avatar
Alessio Netti committed
40
41
42
43
44
45
46
47
48
49
class UnitTemplate : public UnitInterface {
    // The template shall only be instantiated for classes which derive from SensorBase
    static_assert(std::is_base_of<SensorBase, S>::value, "S must derive from SensorBase!");

protected:

    // For readability
    using S_Ptr = std::shared_ptr<S>;

public:
50
51
52
53
54
55
56
57
58
59
    
    /**
     * @brief           Class constructor
     * 
     * @param name      Name of this unit 
     */
    UnitTemplate(const std::string& name) :
            UnitInterface(),
            _name(name),
            _inputMode(SELECTIVE) {
Alessio Netti's avatar
Alessio Netti committed
60

61
62
63
64
65
        // base inputs and outputs vectors are constructed using iterators
        _baseInputs = std::vector<SBasePtr>(_inputs.begin(), _inputs.end());
        _baseOutputs = std::vector<SBasePtr>(_outputs.begin(), _outputs.end());
    }
    
Alessio Netti's avatar
Alessio Netti committed
66
67
68
69
70
71
72
73
    /**
    * @brief            Class constructor
    *
    * @param name       The name of this unit
    * @param inputs     The vector of input sensors associated to this unit
    * @param outputs    The vector of outputs sensors associated to this unit
    */
    UnitTemplate(const std::string& name, const std::vector<S_Ptr>& inputs, const std::vector<S_Ptr>& outputs) :
Alessio Netti's avatar
Alessio Netti committed
74
            UnitInterface(),
75
            _name(name),
Alessio Netti's avatar
Alessio Netti committed
76
            _inputMode(SELECTIVE),
77
78
            _inputs(inputs),
            _outputs(outputs) {
Alessio Netti's avatar
Alessio Netti committed
79
80
81
82
83
84
85
86
87
88

        // base inputs and outputs vectors are constructed using iterators
        _baseInputs = std::vector<SBasePtr>(_inputs.begin(), _inputs.end());
        _baseOutputs = std::vector<SBasePtr>(_outputs.begin(), _outputs.end());
    }

    /**
    * @brief            Copy constructor
    */
    UnitTemplate(const UnitTemplate& other) :
Alessio Netti's avatar
Alessio Netti committed
89
90
            _name(other._name),
            _inputMode(other._inputMode) {
Alessio Netti's avatar
Alessio Netti committed
91
92
93
94
95
96
97
98
99
100

        for(auto s : other._inputs) {
            _inputs.push_back(s);
            _baseInputs.push_back(s);
        }

        for(auto s : other._outputs) {
            _outputs.push_back(s);
            _baseOutputs.push_back(s);
        }
101
102
103
104
        
        for(auto u : other._subUnits) {
            _subUnits.push_back(u);
        }
Alessio Netti's avatar
Alessio Netti committed
105
106
107
108
109
110
111
    }

    /**
    * @brief            Assignment operator
    */
    UnitTemplate& operator=(const UnitTemplate& other) {
        _name = other._name;
Alessio Netti's avatar
Alessio Netti committed
112
        _inputMode = other._inputMode;
Alessio Netti's avatar
Alessio Netti committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126

        _inputs.clear();
        _baseInputs.clear();
        for(auto s : other._inputs) {
            _inputs.push_back(s);
            _baseInputs.push_back(s);
        }

        _outputs.clear();
        _baseOutputs.clear();
        for(auto s : other._outputs) {
            _outputs.push_back(s);
            _baseOutputs.push_back(s);
        }
127
128
129
130
131
        
        _subUnits.clear();
        for(auto u : other._subUnits) {
            _subUnits.push_back(u);
        }
Alessio Netti's avatar
Alessio Netti committed
132
133
134
135

        return *this;
    }

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
    /**
     * @brief           Class destructor
     */
    virtual ~UnitTemplate() { clear(); }
    
    /**
     * @brief           Clears this unit
     */
    void clear() {
        _baseInputs.clear();
        _inputs.clear();
        _baseOutputs.clear();
        _outputs.clear();
        _subUnits.clear();
    }

Alessio Netti's avatar
Alessio Netti committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
    /**
    * @brief            Sets the name of this unit
    *
    * @param name       The name of this unit
    */
    void setName(const std::string& name) override { _name = name; }

    /**
    * @brief            Get the name of this unit
    *
    *                   A unit's name points to the logical entity that it represents; for example, it could be
    *                   "hpcsystem1.node44", or "node44.cpu10". All the outputs of the unit are then associated to its
    *                   entity, and all of the input are related to it as well.
    *
    * @return           The unit's name
    */
    std::string& getName() override                     { return _name; }

    /**
    * @brief            Sets the input mode of this unit
    *
    * @param iMode      The input mode that was used for this unit
    */
    void setInputMode(const inputMode_t iMode) override { _inputMode=iMode; }

    /**
    * @brief            Get the input mode of this unit
    *
    * @return           The unit's input mode
    */
    inputMode_t getInputMode()  override              { return _inputMode; }

Alessio Netti's avatar
Alessio Netti committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
    /**
    * @brief            Get the (base) input sensors of this unit
    *
    * @return           A vector of pointers to SensorBase objects that constitute this unit's input
    */
    std::vector<SBasePtr>& getBaseInputs() override  { return _baseInputs; }

    /**
    * @brief            Get the derived input sensors of this unit
    *
    * @return           A vector of pointers to objects derived from SensorBase that constitute this unit's input
    */
    std::vector<S_Ptr>& getInputs()                  { return _inputs; }

    /**
    * @brief            Get the (base) output sensors of this unit
    *
    * @return           A vector of pointers to SensorBase objects that constitute this unit's output
    */
    std::vector<SBasePtr>& getBaseOutputs() override { return _baseOutputs; }

    /**
    * @brief            Get the derived output sensors of this unit
    *
    * @return           A vector of pointers to objects derived from SensorBase that constitute this unit's output
    */
    std::vector<S_Ptr>& getOutputs()                 { return _outputs; }

212
213
214
215
216
217
218
    /**
     * @brief           Get the internal vector of sub-units of this unit
     * 
     * @return          A reference to the internal vector of sub-unit pointers 
     */
    std::vector<std::shared_ptr<UnitTemplate<S>>>& getSubUnits()  { return _subUnits; }

Alessio Netti's avatar
Alessio Netti committed
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
    /**
    * @brief            Set the inputs of this unit
    *
    *                   Note that these setter methods will replicate the vectors given as input, but the copies
    *                   are shallow (i.e., vectors of pointers will refer to the same objects). That is intended, in
    *                   order to save memory space, and does not lead to race conditions.
    *
    * @param inputs     A vector of pointers to objects derived from SensorBase that will be this unit's input
    */
    void setInputs(const std::vector<S_Ptr>& inputs) {
        _inputs = inputs;
        _baseInputs = std::vector<SBasePtr>(_inputs.begin(), _inputs.end());
    }

    /**
    * @brief            Set the outputs of this unit
    *
    *                   The same considerations done for setInputs apply here as well.
    *
    * @param outputs    A vector of pointers to objects derived from SensorBase that will be this unit's output
    */
    void setOutputs(const std::vector<S_Ptr>& outputs) {
        _outputs = outputs;
        _baseOutputs = std::vector<SBasePtr>(_outputs.begin(), _outputs.end());
    }

245
246
247
248
249
250
251
252
253
254
255
    /**
    * @brief            Set the outputs of this unit
    *
    *                   The same considerations done for setInputs apply here as well.
    *
    * @param outputs    A vector of pointers to objects derived from SensorBase that will be this unit's output
    */
    void setSubUnits(const std::vector<std::shared_ptr<UnitTemplate<S>>>& sUnits) {
        _subUnits = sUnits;
    }

Alessio Netti's avatar
Alessio Netti committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
    /**
    * @brief            Add a single input sensor to this unit
    *
    * @param input      A pointer to an object derived from SensorBase that will be added to this unit's inputs
    */
    void addInput(const S_Ptr input) { _inputs.push_back(input); _baseInputs.push_back(input); }

    /**
    * @brief            Add a single output sensor to this unit
    *
    * @param output     A pointer to an object derived from SensorBase that will be added to this unit's outputs
    */
    void addOutput(const S_Ptr output) { _outputs.push_back(output); _baseOutputs.push_back(output); }

270

271
272
273
274
275
276
    /**
    * @brief            Adds a sub-unit to this unit
    *
    * @param sUnit     A shared pointer to a UnitTemplate object
    */
    void addSubUnit(const std::shared_ptr<UnitTemplate<S>> sUnit) { _subUnits.push_back(sUnit); }
277
    
278
279
280
281
282
283
284
285
286
287
    /**
    * @brief            Prints the current unit configuration
    *
    * @param ll         Logging level at which the configuration is printed
    * @param lg         Logger object to be used
    */
    virtual void printConfig(LOG_LEVEL ll, LOGGER& lg) override {
        LOG_VAR(ll) << "                Unit: " << _name;
        LOG_VAR(ll) << "                Inputs: ";
        for (const auto &i : _inputs)
288
            LOG_VAR(ll) << "                    " << i->getName();
289
290
291
        LOG_VAR(ll) << "                Outputs: ";
        for (const auto &o : _outputs)
            o->printConfig(ll, lg, 20);
292
293
294
295
296
        if(_subUnits.size()>0) {
            LOG_VAR(ll) << "                Sub-units: ";
            for (const auto &u : _subUnits)
                LOG_VAR(ll) << "                    " << u->getName();
        }
297
298
    }

Alessio Netti's avatar
Alessio Netti committed
299
300
301
302
protected:

    // Name corresponds to the output unit we are addressing
    std::string _name;
Alessio Netti's avatar
Alessio Netti committed
303
304
    // Input mode associated to this unit
    inputMode_t _inputMode;
Alessio Netti's avatar
Alessio Netti committed
305
306
307
308
309
310
    // Vector of sensor objects that make up inputs
    std::vector<S_Ptr> _inputs;
    // Base version that uses SensorBase is needed to expose units externally
    std::vector<SBasePtr> _baseInputs;
    // Vector of Sensor objects that make up outputs
    std::vector<S_Ptr> _outputs;
311
312
    // Vector of sub-units that are associated to this unit
    std::vector<std::shared_ptr<UnitTemplate<S>>> _subUnits;
Alessio Netti's avatar
Alessio Netti committed
313
314
315
316
317
    // Same as baseInputs
    std::vector<SBasePtr> _baseOutputs;
};

#endif //PROJECT_UNITTEMPLATE_H