UnitTemplate.h 12.9 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
// Copyright   : Leibniz Supercomputing Centre
6
// Description : Template implementing features to use Units in Operators.
7
8
9
10
11
12
13
14
15
16
//================================================================================

//================================================================================
// 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 Operators.
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 operator
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
    
    /**
     * @brief           Class constructor
     * 
     * @param name      Name of this unit 
     */
    UnitTemplate(const std::string& name) :
            UnitInterface(),
            _name(name),
63
64
            _inputMode(SELECTIVE),
            _parent(nullptr) {}
65
    
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
            _inputs(inputs),
78
79
            _outputs(outputs),
            _parent(nullptr) {
Alessio Netti's avatar
Alessio Netti committed
80
81
82
83
84
85
86
87
88
89

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

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

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

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

        return *this;
    }

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

156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
    /**
    * @brief            Tells whether this is a sub-unit in a hierarchical unit with no further sub-units
    * 
    * @return           True if this is a terminal sub-unit, false otherwise
    */
    bool isLeafUnit()    { return _parent && _subUnits.empty(); }
    
    /**
    * @brief            Tells whether this is a sub-unit in a hierarchical unit 
    * 
    * @return           True if this is a sub-unit, false otherwise
    */
    bool isSubUnit()    { return _parent && !_subUnits.empty(); }

    /**
    * @brief            Tells whether this is the top entity of a hierarchical unit 
    * 
    * @return           True if this is a "top" unit, false otherwise
    */
    bool isTopUnit()    { return !_parent && !_subUnits.empty(); }

177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
    /**
    * @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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
    /**
    * @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; }

227
228
229
230
231
232
233
234
235
236
237
238
239
240
    /**
    * @brief            Sets the parent of this unit
    *
    * @param p          Pointer to a UnitTemplate object
    */
    void setParent(UnitTemplate<S>* p)                { _parent = p; }

    /**
    * @brief            Get the pointer to this unit's parent
    *
    * @return           The unit's parent
    */
    UnitTemplate<S> *getParent()                      { return _parent; }

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
267
268
    /**
    * @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; }

269
270
271
272
273
274
275
    /**
     * @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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
    /**
    * @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());
    }

302
303
304
305
306
307
308
309
310
    /**
    * @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;
311
312
        for(const auto& subUnit : _subUnits)
            subUnit->setParent(this);
313
314
    }

Alessio Netti's avatar
Alessio Netti committed
315
316
317
318
319
320
321
322
323
324
325
326
327
328
    /**
    * @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); }

329

330
331
332
333
334
    /**
    * @brief            Adds a sub-unit to this unit
    *
    * @param sUnit     A shared pointer to a UnitTemplate object
    */
335
    void addSubUnit(const std::shared_ptr<UnitTemplate<S>> sUnit) { _subUnits.push_back(sUnit); sUnit->setParent(this); }
336
    
337
338
339
340
341
342
343
344
345
346
    /**
    * @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)
347
            LOG_VAR(ll) << "                    " << i->getName();
348
349
350
        LOG_VAR(ll) << "                Outputs: ";
        for (const auto &o : _outputs)
            o->printConfig(ll, lg, 20);
351
352
353
354
355
        if(_subUnits.size()>0) {
            LOG_VAR(ll) << "                Sub-units: ";
            for (const auto &u : _subUnits)
                LOG_VAR(ll) << "                    " << u->getName();
        }
356
357
    }

Alessio Netti's avatar
Alessio Netti committed
358
359
360
361
protected:

    // Name corresponds to the output unit we are addressing
    std::string _name;
Alessio Netti's avatar
Alessio Netti committed
362
363
    // Input mode associated to this unit
    inputMode_t _inputMode;
Alessio Netti's avatar
Alessio Netti committed
364
365
366
367
368
369
    // 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;
370
371
    // Vector of sub-units that are associated to this unit
    std::vector<std::shared_ptr<UnitTemplate<S>>> _subUnits;
372
373
    // Pointer to the unit's parent (if it is a sub-unit)
    UnitTemplate<S>* _parent;
Alessio Netti's avatar
Alessio Netti committed
374
375
376
377
378
    // Same as baseInputs
    std::vector<SBasePtr> _baseOutputs;
};

#endif //PROJECT_UNITTEMPLATE_H