| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c65b0c5 commit 808956a
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,7 +6,7 @@ | |||
| 6 | 6 | #include <taskflow/taskflow.hpp> // the only include you need | |
| 7 | 7 | ||
| 8 | 8 | int main(){ | |
| 9 | - | ||
| 9 | + | ||
| 10 | 10 | tf::Executor executor; | |
| 11 | 11 | tf::Taskflow taskflow; | |
| 12 | 12 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -616,9 +616,9 @@ inline void Executor::_invoke(unsigned me, Node* node) { | |||
| 616 | 616 | // access caused by topology clear. | |
| 617 | 617 | const auto num_successors = node->num_successors(); | |
| 618 | 618 | ||
| 619 | - // regular node type | ||
| 619 | + // static task | ||
| 620 | 620 | // The default node work type. We only need to execute the callback if any. | |
| 621 | - if(auto index=node->_work.index(); index == 0) { | ||
| 621 | + if(auto index=node->_work.index(); index == 1) { | ||
| 622 | 622 | if(node->_module != nullptr) { | |
| 623 | 623 | bool first_time = !node->is_spawned(); | |
| 624 | 624 | _invoke_static_work(me, node); | |
@@ -632,8 +632,8 @@ inline void Executor::_invoke(unsigned me, Node* node) { | |||
| 632 | 632 | } | |
| 633 | 633 | } | |
| 634 | 634 | } | |
| 635 | - // subflow node type | ||
| 636 | - else { | ||
| 635 | + // dynamic task | ||
| 636 | + else if (index == 2){ | ||
| 637 | 637 | ||
| 638 | 638 | // Clear the subgraph before the task execution | |
| 639 | 639 | if(!node->is_spawned()) { | |
@@ -687,7 +687,7 @@ inline void Executor::_invoke(unsigned me, Node* node) { | |||
| 687 | 687 | if(!node->is_subtask()) { | |
| 688 | 688 | // Only dynamic tasking needs to restore _dependents | |
| 689 | 689 | // TODO: | |
| 690 | - if(node->_work.index() == 1 && !node->_subgraph->empty()) { | ||
| 690 | + if(node->_work.index() == 2 && !node->_subgraph->empty()) { | ||
| 691 | 691 | while(!node->_dependents.empty() && node->_dependents.back()->is_subtask()) { | |
| 692 | 692 | node->_dependents.pop_back(); | |
| 693 | 693 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,6 @@ | |||
| 1 | 1 | #pragma once | |
| 2 | 2 | ||
| 3 | 3 | #include "../error/error.hpp" | |
| 4 | - #include "../utility/object_pool.hpp" | ||
| 5 | 4 | #include "../utility/traits.hpp" | |
| 6 | 5 | #include "../utility/passive_vector.hpp" | |
| 7 | 6 | ||
@@ -15,19 +14,16 @@ class FlowBuilder; | |||
| 15 | 14 | class Subflow; | |
| 16 | 15 | class Taskflow; | |
| 17 | 16 | ||
| 18 | - // TLS node pool | ||
| 19 | - inline thread_local ObjectPool<Node> NodePool; | ||
| 20 | - | ||
| 21 | 17 | // ---------------------------------------------------------------------------- | |
| 22 | 18 | ||
| 23 | 19 | // Class: Graph | |
| 24 | 20 | class Graph { | |
| 21 | + | ||
| 22 | + friend class Node; | ||
| 25 | 23 | ||
| 26 | 24 | public: | |
| 27 | 25 | ||
| 28 | 26 | Graph() = default; | |
| 29 | - ~Graph(); | ||
| 30 | - | ||
| 31 | 27 | Graph(const Graph&) = delete; | |
| 32 | 28 | Graph(Graph&&); | |
| 33 | 29 | ||
@@ -40,15 +36,17 @@ class Graph { | |||
| 40 | 36 | ||
| 41 | 37 | size_t size() const; | |
| 42 | 38 | ||
| 43 | - template <typename... ArgsT> | ||
| 44 | - Node& emplace_back(ArgsT&&...); | ||
| 39 | + template <typename C> | ||
| 40 | + Node& emplace_back(C&&); | ||
| 41 | + | ||
| 42 | + Node& emplace_back(); | ||
| 45 | 43 | ||
| 46 | 44 | std::vector<std::unique_ptr<Node>>& nodes(); | |
| 47 | 45 | ||
| 48 | 46 | const std::vector<std::unique_ptr<Node>>& nodes() const; | |
| 49 | 47 | ||
| 50 | 48 | private: | |
| 51 | - | ||
| 49 | + | ||
| 52 | 50 | std::vector<std::unique_ptr<Node>> _nodes; | |
| 53 | 51 | }; | |
| 54 | 52 | ||
@@ -78,15 +76,9 @@ class Node { | |||
| 78 | 76 | ||
| 79 | 77 | ~Node(); | |
| 80 | 78 | ||
| 81 | - template <typename C> | ||
| 82 | - void animate(C&&); | ||
| 83 | - | ||
| 84 | - void animate(); | ||
| 85 | - void recycle(); | ||
| 86 | 79 | void precede(Node&); | |
| 87 | 80 | void dump(std::ostream&) const; | |
| 88 | - void clear_subgraph(); | ||
| 89 | - | ||
| 81 | + | ||
| 90 | 82 | size_t num_successors() const; | |
| 91 | 83 | size_t num_dependents() const; | |
| 92 | 84 | ||
@@ -107,11 +99,11 @@ class Node { | |||
| 107 | 99 | private: | |
| 108 | 100 | ||
| 109 | 101 | std::string _name; | |
| 110 | - std::variant<StaticWork, DynamicWork> _work; | ||
| 102 | + std::variant<std::monostate, StaticWork, DynamicWork> _work; | ||
| 111 | 103 | ||
| 112 | 104 | tf::PassiveVector<Node*> _successors; | |
| 113 | 105 | tf::PassiveVector<Node*> _dependents; | |
| 114 | - | ||
| 106 | + | ||
| 115 | 107 | std::optional<Graph> _subgraph; | |
| 116 | 108 | ||
| 117 | 109 | Topology* _topology {nullptr}; | |
@@ -129,50 +121,25 @@ Node::Node(C&& c) : _work {std::forward<C>(c)} { | |||
| 129 | 121 | ||
| 130 | 122 | // Destructor | |
| 131 | 123 | inline Node::~Node() { | |
| 132 | - clear_subgraph(); | ||
| 133 | - } | ||
| 134 | - | ||
| 135 | - // Constructor-like call | ||
| 136 | - inline void Node::animate() { | ||
| 137 | - } | ||
| 138 | - | ||
| 139 | - // Constructor-like call | ||
| 140 | - template <typename C> | ||
| 141 | - void Node::animate(C&& c) { | ||
| 142 | - _work = std::forward<C>(c); | ||
| 143 | - } | ||
| 144 | - | ||
| 145 | - // Destructor-like call | ||
| 146 | - inline void Node::recycle() { | ||
| 147 | - _name.clear(); | ||
| 148 | - _work.emplace<StaticWork>(); | ||
| 149 | - _successors.clear(); | ||
| 150 | - _dependents.clear(); | ||
| 151 | - _topology = nullptr; | ||
| 152 | - _module = nullptr; | ||
| 153 | - _status = 0; | ||
| 154 | - _num_dependents.store(0, std::memory_order_relaxed); | ||
| 155 | - clear_subgraph(); | ||
| 156 | - } | ||
| 157 | - | ||
| 158 | - inline void Node::clear_subgraph() { | ||
| 159 | 124 | // this is to avoid stack overflow | |
| 160 | 125 | if(_subgraph.has_value()) { | |
| 161 | - std::list<Graph> gs; | ||
| 162 | - gs.push_back(std::move(*_subgraph)); | ||
| 126 | + std::vector<std::unique_ptr<Node>> nodes; | ||
| 127 | + std::move( | ||
| 128 | + _subgraph->_nodes.begin(), _subgraph->_nodes.end(), std::back_inserter(nodes) | ||
| 129 | + ); | ||
| 130 | + _subgraph->_nodes.clear(); | ||
| 163 | 131 | _subgraph.reset(); | |
| 164 | - auto i=gs.begin(); | ||
| 165 | - while(i!=gs.end()) { | ||
| 166 | - auto n = i->nodes().begin(); | ||
| 167 | - while(n != i->nodes().end()) { | ||
| 168 | - if((*n)->_subgraph.has_value()) { | ||
| 169 | - gs.push_back(std::move(*((*n)->_subgraph))); | ||
| 170 | - (*n)->_subgraph.reset(); | ||
| 171 | - } | ||
| 172 | - ++n; | ||
| 173 | - } | ||
| 174 | - ++i; | ||
| 175 | - } | ||
| 132 | + size_t i = 0; | ||
| 133 | + while(i < nodes.size()) { | ||
| 134 | + if(auto& sbg = nodes[i]->_subgraph; sbg) { | ||
| 135 | + std::move( | ||
| 136 | + sbg->_nodes.begin(), sbg->_nodes.end(), std::back_inserter(nodes) | ||
| 137 | + ); | ||
| 138 | + sbg->_nodes.clear(); | ||
| 139 | + sbg.reset(); | ||
| 140 | + } | ||
| 141 | + ++i; | ||
| 142 | + } | ||
| 176 | 143 | } | |
| 177 | 144 | } | |
| 178 | 145 | ||
@@ -239,16 +206,101 @@ inline void Node::dump(std::ostream& os) const { | |||
| 239 | 206 | ||
| 240 | 207 | // ---------------------------------------------------------------------------- | |
| 241 | 208 | ||
| 209 | + /*// Class: NodePool | ||
| 210 | + class NodePool { | ||
| 211 | + | ||
| 212 | + public: | ||
| 213 | + | ||
| 214 | + template <typename C> | ||
| 215 | + std::unique_ptr<Node> acquire(C&&); | ||
| 216 | + | ||
| 217 | + std::unique_ptr<Node> acquire(); | ||
| 218 | + | ||
| 219 | + void release(std::unique_ptr<Node>); | ||
| 220 | + | ||
| 221 | + private: | ||
| 222 | + | ||
| 223 | + //std::mutex _mutex; | ||
| 224 | + | ||
| 225 | + std::vector<std::unique_ptr<Node>> _nodes; | ||
| 226 | + | ||
| 227 | + void _recycle(Node&); | ||
| 228 | + }; | ||
| 229 | + | ||
| 230 | + // Function: acquire | ||
| 231 | + template <typename C> | ||
| 232 | + inline std::unique_ptr<Node> NodePool::acquire(C&& c) { | ||
| 233 | + if(_nodes.empty()) { | ||
| 234 | + return std::make_unique<Node>(std::forward<C>(c)); | ||
| 235 | + } | ||
| 236 | + else { | ||
| 237 | + auto node = std::move(_nodes.back()); | ||
| 238 | + node->_work = std::forward<C>(c); | ||
| 239 | + _nodes.pop_back(); | ||
| 240 | + return node; | ||
| 241 | + } | ||
| 242 | + } | ||
| 243 | + | ||
| 244 | + // Function: acquire | ||
| 245 | + inline std::unique_ptr<Node> NodePool::acquire() { | ||
| 246 | + if(_nodes.empty()) { | ||
| 247 | + return std::make_unique<Node>(); | ||
| 248 | + } | ||
| 249 | + else { | ||
| 250 | + auto node = std::move(_nodes.back()); | ||
| 251 | + _nodes.pop_back(); | ||
| 252 | + return node; | ||
| 253 | + } | ||
| 254 | + } | ||
| 255 | + | ||
| 256 | + // Procedure: release | ||
| 257 | + inline void NodePool::release(std::unique_ptr<Node> node) { | ||
| 258 | + | ||
| 259 | + return; | ||
| 260 | + | ||
| 261 | + //assert(node); | ||
| 262 | + if(_nodes.size() >= 65536) { | ||
| 263 | + return; | ||
| 264 | + } | ||
| 265 | + | ||
| 266 | + auto children = node->_extract_children(); | ||
| 267 | + | ||
| 268 | + for(auto& child : children) { | ||
| 269 | + _recycle(*child); | ||
| 270 | + } | ||
| 271 | + _recycle(*node); | ||
| 272 | + | ||
| 273 | + std::move(children.begin(), children.end(), std::back_inserter(_nodes)); | ||
| 274 | + _nodes.push_back(std::move(node)); | ||
| 275 | + } | ||
| 276 | + | ||
| 277 | + // Procedure: _recycle | ||
| 278 | + inline void NodePool::_recycle(Node& node) { | ||
| 279 | + node._name.clear(); | ||
| 280 | + node._work = {}; | ||
| 281 | + node._successors.clear(); | ||
| 282 | + node._dependents.clear(); | ||
| 283 | + node._topology = nullptr; | ||
| 284 | + node._module = nullptr; | ||
| 285 | + node._status = 0; | ||
| 286 | + node._num_dependents.store(0, std::memory_order_relaxed); | ||
| 287 | + //assert(!node._subgraph); | ||
| 288 | + } | ||
| 289 | + | ||
| 290 | + // ---------------------------------------------------------------------------- | ||
| 291 | + | ||
| 292 | + namespace this_thread { | ||
| 293 | + inline thread_local NodePool nodepool; | ||
| 294 | + } | ||
| 295 | + */ | ||
| 296 | + | ||
| 297 | + // ---------------------------------------------------------------------------- | ||
| 298 | + | ||
| 242 | 299 | // Move constructor | |
| 243 | 300 | inline Graph::Graph(Graph&& other) : | |
| 244 | 301 | _nodes {std::move(other._nodes)} { | |
| 245 | 302 | } | |
| 246 | 303 | ||
| 247 | - // Destructor | ||
| 248 | - inline Graph::~Graph() { | ||
| 249 | - clear(); | ||
| 250 | - } | ||
| 251 | - | ||
| 252 | 304 | // Move assignment | |
| 253 | 305 | inline Graph& Graph::operator = (Graph&& other) { | |
| 254 | 306 | _nodes = std::move(other._nodes); | |
@@ -258,9 +310,6 @@ inline Graph& Graph::operator = (Graph&& other) { | |||
| 258 | 310 | // Procedure: clear | |
| 259 | 311 | // clear and recycle the nodes | |
| 260 | 312 | inline void Graph::clear() { | |
| 261 | - for(auto& node : _nodes) { | ||
| 262 | - NodePool.release(std::move(node)); | ||
| 263 | - } | ||
| 264 | 313 | _nodes.clear(); | |
| 265 | 314 | } | |
| 266 | 315 | ||
@@ -292,9 +341,16 @@ inline const std::vector<std::unique_ptr<Node>>& Graph::nodes() const { | |||
| 292 | 341 | ||
| 293 | 342 | // Function: emplace_back | |
| 294 | 343 | // create a node from a give argument; constructor is called if necessary | |
| 295 | - template <typename... ArgsT> | ||
| 296 | - Node& Graph::emplace_back(ArgsT&&... args) { | ||
| 297 | - _nodes.push_back(NodePool.acquire(std::forward<ArgsT>(args)...)); | ||
| 344 | + template <typename C> | ||
| 345 | + Node& Graph::emplace_back(C&& c) { | ||
| 346 | + _nodes.push_back(std::make_unique<Node>(std::forward<C>(c))); | ||
| 347 | + return *(_nodes.back()); | ||
| 348 | + } | ||
| 349 | + | ||
| 350 | + // Function: emplace_back | ||
| 351 | + // create a node from a give argument; constructor is called if necessary | ||
| 352 | + inline Node& Graph::emplace_back() { | ||
| 353 | + _nodes.push_back(std::make_unique<Node>()); | ||
| 298 | 354 | return *(_nodes.back()); | |
| 299 | 355 | } | |
| 300 | 356 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments