Essentia  2.1-beta6-dev
network.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 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:
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; }
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 
112 
119  void run();
120 
124  void runPrepare();
125 
132  bool runStep();
133 
137  void update() {
141  }
142 
147  void reset();
148 
155  void clear();
156 
163 
164 
169  streaming::Algorithm* findAlgorithm(const std::string& name);
170 
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 
198 
204 
205  protected:
210  std::vector<streaming::Algorithm*> _toposortedNetwork;
211 
217 
225 
230 
237  // TODO: rename AlgoSet visibleAlgos() const;
239 
248 
255 
261 
267 };
268 
269 
275 
280 
284 
292 
298 
305  streaming::Algorithm* algo);
306 
307 } // namespace scheduler
308 } // namespace essentia
309 
310 
311 #endif // ESSENTIA_SCHEDULER_NETWORK_H
Definition: network.h:56
const std::vector< NetworkNode * > & children() const
Definition: network.h:60
streaming::Algorithm * algorithm()
Definition: network.h:65
void addChild(NetworkNode *child)
Definition: network.h:62
void setChildren(const std::vector< NetworkNode * > &children)
Definition: network.h:61
streaming::Algorithm * _algo
Definition: network.h:73
const streaming::Algorithm * algorithm() const
Definition: network.h:64
std::vector< NetworkNode * > _children
Definition: network.h:74
std::vector< NetworkNode * > addVisibleDependencies(std::map< streaming::Algorithm *, NetworkNode * > &algoNodeMap)
NetworkNode(streaming::Algorithm *algo)
Definition: network.h:58
Definition: network.h:98
streaming::Algorithm * _generator
Definition: network.h:207
static Network * lastCreated
Definition: network.h:203
static std::vector< streaming::Algorithm * > innerVisibleAlgorithms(streaming::Algorithm *algo)
Network(streaming::Algorithm *generator, bool takeOwnership=true)
bool _takeOwnership
Definition: network.h:206
const std::vector< streaming::Algorithm * > & linearExecutionOrder() const
Definition: network.h:183
AlgoSet _algos
Definition: network.h:238
void update()
Definition: network.h:137
std::vector< streaming::Algorithm * > _toposortedNetwork
Definition: network.h:210
NetworkNode * visibleNetworkRoot()
Definition: network.h:171
NetworkNode * _visibleNetworkRoot
Definition: network.h:208
NetworkNode * _executionNetworkRoot
Definition: network.h:209
NetworkNode * executionNetworkRoot()
Definition: network.h:172
streaming::Algorithm * findAlgorithm(const std::string &name)
Definition: streamingalgorithmcomposite.h:52
Definition: streamingalgorithm.h:140
AlgoVector computeCompositeDependencies(const streaming::Algorithm *algo)
void printNetworkBufferFillState()
AlgoSet parentBranchInsideComposite(streaming::AlgorithmComposite *composite, streaming::Algorithm *algo)
AlgoVector computeDependencies(const streaming::Algorithm *algo)
std::set< NetworkNode * > NodeSet
Definition: network.h:78
std::vector< NetworkNode * > NodeVector
Definition: network.h:77
std::set< streaming::Algorithm * > AlgoSet
Definition: network.h:45
AlgoVector computeNormalDependencies(const streaming::Algorithm *algo)
AlgoSet compositeInnerAlgos(streaming::Algorithm *algo)
void deleteNetwork(const streaming::Algorithm *algo)
void cacheDependencies(streaming::Algorithm *algo)
std::vector< streaming::Algorithm * > AlgoVector
Definition: network.h:44
std::stack< NetworkNode * > NodeStack
Definition: network.h:79
Definition: algorithm.h:28
bool contains(const std::vector< T > &v, const T &elem)
Definition: essentiautil.h:81