UnitTemplate.h 11.7 KB
Newer Older
1
2
3
//================================================================================
// Name        : UnitTemplate.h
// Author      : Alessio Netti
Micha Müller's avatar
Micha Müller committed
4
// Contact     : info@dcdb.it
5
6
7
8
9
10
11
12
13
14
15
16
// 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
17
//
18
19
20
21
// 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
22
//
23
24
25
26
// 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
27
28
29
30
31
32
33

#ifndef PROJECT_UNITTEMPLATE_H
#define PROJECT_UNITTEMPLATE_H

#include "UnitInterface.h"

/**
34
 * @brief Template that implements features needed to use Units in Analyzers.
Alessio Netti's avatar
Alessio Netti committed
35
 *
36
37
38
39
 * @details 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
40
 *
41
 * @ingroup analyzer
Alessio Netti's avatar
Alessio Netti committed
42
 */
Alessio Netti's avatar
Alessio Netti committed
43
template <class S=SensorBase>
Alessio Netti's avatar
Alessio Netti committed
44
45
46
47
48
49
50
51
52
53
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:
54
55
56
57
58
59
60
61
62
63
    
    /**
     * @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
64

65
66
67
68
69
        // 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
70
71
72
73
74
75
76
77
    /**
    * @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
78
            UnitInterface(),
79
            _name(name),
Alessio Netti's avatar
Alessio Netti committed
80
            _inputMode(SELECTIVE),
81
82
            _inputs(inputs),
            _outputs(outputs) {
Alessio Netti's avatar
Alessio Netti committed
83
84
85
86
87
88
89
90
91
92

        // 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
93
94
            _name(other._name),
            _inputMode(other._inputMode) {
Alessio Netti's avatar
Alessio Netti committed
95
96
97
98
99
100
101
102
103
104

        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);
        }
105
106
107
108
        
        for(auto u : other._subUnits) {
            _subUnits.push_back(u);
        }
Alessio Netti's avatar
Alessio Netti committed
109
110
111
112
113
114
115
    }

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

        _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);
        }
131
132
133
134
135
        
        _subUnits.clear();
        for(auto u : other._subUnits) {
            _subUnits.push_back(u);
        }
Alessio Netti's avatar
Alessio Netti committed
136
137
138
139

        return *this;
    }

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

156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
    /**
    * @brief            Initializes the sensors in the unit
    *
    * @param cacheSize  size of the sensor cache 
    * @param flatten    if true, sensors in sub-units are also initialized
    */
    void init(unsigned int cacheSize, bool flatten=false) override {
        for(const auto s : _outputs)
            if (!s->isInit())
                s->initSensor(cacheSize);
        if(flatten) {
            for (const auto &su : _subUnits)
                for (const auto s : su->getOutputs())
                    if (!s->isInit())
                        s->initSensor(cacheSize);
        }
    }

Alessio Netti's avatar
Alessio Netti committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
    /**
    * @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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
    /**
    * @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; }

234
235
236
237
238
239
240
    /**
     * @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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
    /**
    * @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());
    }

267
268
269
270
271
272
273
274
275
276
277
    /**
    * @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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
    /**
    * @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); }

292

293
294
295
296
297
298
    /**
    * @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); }
299
    
300
301
302
303
304
305
306
307
308
309
    /**
    * @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)
310
            LOG_VAR(ll) << "                    " << i->getName();
311
312
313
        LOG_VAR(ll) << "                Outputs: ";
        for (const auto &o : _outputs)
            o->printConfig(ll, lg, 20);
314
315
316
317
318
        if(_subUnits.size()>0) {
            LOG_VAR(ll) << "                Sub-units: ";
            for (const auto &u : _subUnits)
                LOG_VAR(ll) << "                    " << u->getName();
        }
319
320
    }

Alessio Netti's avatar
Alessio Netti committed
321
322
323
324
protected:

    // Name corresponds to the output unit we are addressing
    std::string _name;
Alessio Netti's avatar
Alessio Netti committed
325
326
    // Input mode associated to this unit
    inputMode_t _inputMode;
Alessio Netti's avatar
Alessio Netti committed
327
328
329
330
331
332
    // 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;
333
334
    // 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
335
336
337
338
339
    // Same as baseInputs
    std::vector<SBasePtr> _baseOutputs;
};

#endif //PROJECT_UNITTEMPLATE_H