Skip to content

Commit c087f1e

Browse files
committed
Refactor Framework
1 parent d3657cd commit c087f1e

3 files changed

Lines changed: 52 additions & 6 deletions

File tree

taskflow/graph/basic_taskflow.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,15 @@ std::shared_future<void> BasicTaskflow<E>::run_n(Framework& f, size_t repeat, C&
393393
for(auto& n: f._graph) {
394394

395395
// TODO: swap this with the last and then pop_back
396-
// Here we use "front" because users may add nodes to the graph
397-
if(!n._successors.empty() && n._successors.front() == f._last_target) {
398-
n._successors.erase(n._successors.begin());
396+
if(!n._successors.empty()) {
397+
for(size_t i=0; i<n._successors.size(); i++) {
398+
if(n._successors[i] == f._last_target) {
399+
std::swap(n._successors[i], n._successors.back());
400+
n._successors.pop_back();
401+
break;
402+
}
403+
}
399404
}
400-
// remove_if std::vector
401405

402406
// reset the dynamic tasking
403407
if(n._subgraph.has_value()) {

taskflow/graph/framework.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@ namespace tf {
66

77
// TODO: document the class
88

9-
// Class: Framework
9+
/**
10+
@class Framework
11+
12+
@brief A reusable task dependency graph.
13+
14+
A framework can be executed by a taskflow object repetitively and thus
15+
avoids the graph reconstruction overhead.
16+
17+
*/
1018
class Framework : public FlowBuilder {
1119

1220
friend class Topology;

unittest/taskflow.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,41 @@ TEST_CASE("Framework" * doctest::timeout(300)) {
956956
}
957957

958958

959-
// TODO: test correctness when framework got changed between runs
959+
// TODO: test correctness when framework got changed between runs
960+
for(unsigned W=0; W<=4; ++W) {
961+
962+
std::atomic<size_t> count {0};
963+
tf::Framework f;
964+
auto A = f.silent_emplace([&](){ count ++; });
965+
auto B = f.silent_emplace([&](auto& subflow){
966+
count ++;
967+
auto B1 = subflow.silent_emplace([&](){ count++; });
968+
auto B2 = subflow.silent_emplace([&](){ count++; });
969+
auto B3 = subflow.silent_emplace([&](){ count++; });
970+
B1.precede(B3); B2.precede(B3);
971+
});
972+
auto C = f.silent_emplace([&](){ count ++; });
973+
auto D = f.silent_emplace([&](){ count ++; });
974+
975+
A.precede(B, C);
976+
B.precede(D);
977+
C.precede(D);
978+
979+
tf::Taskflow tf(W);
980+
tf.run_n(f, 10).get();
981+
REQUIRE(count == 70);
982+
983+
auto E = f.silent_emplace([](){});
984+
D.precede(E);
985+
tf.run_n(f, 10).get();
986+
REQUIRE(count == 140);
987+
988+
auto F = f.silent_emplace([](){});
989+
E.precede(F);
990+
tf.silent_run_n(f, 10);
991+
tf.wait_for_all();
992+
REQUIRE(count == 210);
993+
}
960994

961995
}
962996

0 commit comments

Comments
 (0)