Essentia  2.1-beta5-dev
network.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2016 Music Technology Group - Universitat Pompeu Fabra
3  *
4  * This file is part of Essentia
5  *
6  * Essentia is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License as published by the Free
8  * Software Foundation (FSF), either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the Affero GNU General Public License
17  * version 3 along with this program. If not, see http://www.gnu.org/licenses/
18  */
19 
20 #ifndef ESSENTIA_SCHEDULER_NETWORK_H
21 #define ESSENTIA_SCHEDULER_NETWORK_H
22 
23 #include <vector>
24 #include <set>
25 #include <stack>
26 #include "../streaming/streamingalgorithm.h"
27 #include "../essentiautil.h"
28 
29 namespace essentia {
30 namespace streaming {
31 
32 class AlgorithmComposite;
33 class SourceBase;
34 
35 } // namespace streaming
36 } // namespace essentia
37 
38 
39 
40 
41 namespace essentia {
42 namespace scheduler {
43 
44 typedef std::vector<streaming::Algorithm*> AlgoVector;
45 typedef std::set<streaming::Algorithm*> AlgoSet;
46 
47 
48 
56 class NetworkNode {
57  public:
58  NetworkNode(streaming::Algorithm* algo) : _algo(algo) {}
59 
60  const std::vector<NetworkNode*>& children() const { return _children; }
61  void setChildren(const std::vector<NetworkNode*>& children) { _children = children; }
62  void addChild(NetworkNode* child) { if (!contains(_children, child)) _children.push_back(child); }
63 
64  const streaming::Algorithm* algorithm() const { return _algo; }
65  streaming::Algorithm* algorithm() { return _algo; }
66 
67  std::vector<NetworkNode*> addVisibleDependencies(std::map<streaming::Algorithm*, NetworkNode*>& algoNodeMap);
68 
69  protected:
74  std::vector<NetworkNode*> _children;
75 };
76 
77 typedef std::vector<NetworkNode*> NodeVector;
78 typedef std::set<NetworkNode*> NodeSet;
79 typedef std::stack<NetworkNode*> NodeStack;
80 
81 
82 
98 class Network {
99 
100  public:
109  Network(streaming::Algorithm* generator, bool takeOwnership = true);
110 
111  ~Network();
112 
119  void run();
120 
124  void runPrepare();
125 
132  bool runStep();
133 
137  void update() {
138  buildVisibleNetwork();
139  buildExecutionNetwork();
140  topologicalSortExecutionNetwork();
141  }
142 
147  void reset();
148 
155  void clear();
156 
162  void deleteAlgorithms();
163 
164 
169  streaming::Algorithm* findAlgorithm(const std::string& name);
170 
171  NetworkNode* visibleNetworkRoot() { return _visibleNetworkRoot; }
173  if (_visibleNetworkRoot && !_executionNetworkRoot) {
174  buildExecutionNetwork();
175  }
176  return _executionNetworkRoot;
177  }
178 
183  const std::vector<streaming::Algorithm*>& linearExecutionOrder() const { return _toposortedNetwork; }
184 
185 
192  static std::vector<streaming::Algorithm*> innerVisibleAlgorithms(streaming::Algorithm* algo);
193 
197  void printBufferFillState();
198 
204 
205  protected:
210  std::vector<streaming::Algorithm*> _toposortedNetwork;
211 
216  void buildVisibleNetwork();
217 
224  void buildExecutionNetwork();
225 
229  void topologicalSortExecutionNetwork();
230 
237  // TODO: rename AlgoSet visibleAlgos() const;
238  AlgoSet _algos;
239 
247  void checkConnections();
248 
254  void checkBufferSizes();
255 
260  void clearVisibleNetwork();
261 
266  void clearExecutionNetwork();
267 };
268 
269 
274 void deleteNetwork(const streaming::Algorithm* algo);
275 
280 
281 AlgoVector computeDependencies(const streaming::Algorithm* algo);
282 AlgoVector computeNormalDependencies(const streaming::Algorithm* algo);
283 AlgoVector computeCompositeDependencies(const streaming::Algorithm* algo);
284 
292 
298 
305  streaming::Algorithm* algo);
306 
307 } // namespace scheduler
308 } // namespace essentia
309 
310 
311 #endif // ESSENTIA_SCHEDULER_NETWORK_H
const std::vector< streaming::Algorithm * > & linearExecutionOrder() const
Definition: network.h:183
streaming::Algorithm * algorithm()
Definition: network.h:65
Definition: network.h:56
streaming::Algorithm * _generator
Definition: network.h:207
void setChildren(const std::vector< NetworkNode *> &children)
Definition: network.h:61
const streaming::Algorithm * algorithm() const
Definition: network.h:64
AlgoVector computeNormalDependencies(const streaming::Algorithm *algo)
AlgoVector computeDependencies(const streaming::Algorithm *algo)
NetworkNode * visibleNetworkRoot()
Definition: network.h:171
std::vector< NetworkNode * > _children
Definition: network.h:74
std::vector< streaming::Algorithm * > _toposortedNetwork
Definition: network.h:210
std::set< NetworkNode * > NodeSet
Definition: network.h:78
NetworkNode * _executionNetworkRoot
Definition: network.h:209
std::vector< streaming::Algorithm * > AlgoVector
Definition: network.h:44
AlgoSet parentBranchInsideComposite(streaming::AlgorithmComposite *composite, streaming::Algorithm *algo)
void cacheDependencies(streaming::Algorithm *algo)
Definition: streamingalgorithmcomposite.h:52
void addChild(NetworkNode *child)
Definition: network.h:62
AlgoSet _algos
Definition: network.h:238
const std::vector< NetworkNode * > & children() const
Definition: network.h:60
bool contains(const std::vector< T > &v, const T &elem)
Definition: essentiautil.h:81
void printNetworkBufferFillState()
NetworkNode * executionNetworkRoot()
Definition: network.h:172
AlgoVector computeCompositeDependencies(const streaming::Algorithm *algo)
NetworkNode(streaming::Algorithm *algo)
Definition: network.h:58
Definition: network.h:98
void deleteNetwork(const streaming::Algorithm *algo)
NetworkNode * _visibleNetworkRoot
Definition: network.h:208
std::vector< NetworkNode * > NodeVector
Definition: network.h:77
static Network * lastCreated
Definition: network.h:203
Definition: algorithm.h:28
std::stack< NetworkNode * > NodeStack
Definition: network.h:79
streaming::Algorithm * _algo
Definition: network.h:73
bool _takeOwnership
Definition: network.h:206
Definition: streamingalgorithm.h:140
void update()
Definition: network.h:137
AlgoSet compositeInnerAlgos(streaming::Algorithm *algo)
std::set< streaming::Algorithm * > AlgoSet
Definition: network.h:45