FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add test for run_until and store cahced_num_sinks in tpg · gicho/cpp-taskflow@e1b6233 · GitHub

Repository navigation

Commit e1b6233

Browse files
committed
Add test for run_until and store cahced_num_sinks in tpg
1 parent 676398c commit e1b6233

3 files changed

Lines changed: 53 additions & 5 deletions

File tree

‎taskflow/graph/basic_taskflow.hpp‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,10 @@ std::shared_future<void> BasicTaskflow<E>::run_until(Framework& f, P&& predicate
303303

304304
// Clear last execution data & Build precedence between nodes and target
305305
tpg._bind(f._graph);
306-
int num_sinks = tpg._num_sinks;
307306

308307
do {
309308
_schedule(tpg._sources);
310-
tpg._num_sinks = num_sinks;
309+
tpg._num_sinks = tpg._cached_num_sinks;
311310
} while(!std::invoke(tpg._predicate));
312311

313312
std::invoke(c);
@@ -327,11 +326,11 @@ std::shared_future<void> BasicTaskflow<E>::run_until(Framework& f, P&& predicate
327326
tpg._bind(f._graph);
328327
}
329328

330-
tpg._work = [&f, c=std::forward<C>(c), num_sinks=tpg._num_sinks.load(), this] () mutable {
329+
tpg._work = [&f, c=std::forward<C>(c), this] () mutable {
331330

332331
// case 1: we still need to run the topology again
333332
if(!std::invoke(f._topologies.front()->_predicate)) {
334-
f._topologies.front()->_num_sinks = num_sinks;
333+
f._topologies.front()->_num_sinks = f._topologies.front()->_cached_num_sinks;
335334
_schedule(f._topologies.front()->_sources);
336335
}
337336
// case 2: the final run of this topology
@@ -348,7 +347,6 @@ std::shared_future<void> BasicTaskflow<E>::run_until(Framework& f, P&& predicate
348347
f._topologies.front()->_promise.set_value();
349348
f._topologies.pop_front();
350349
f._topologies.front()->_bind(f._graph);
351-
num_sinks = f._topologies.front()->_num_sinks;
352350
f._mtx.unlock();
353351
_schedule(f._topologies.front()->_sources);
354352
}

‎taskflow/graph/topology.hpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Topology {
4545

4646
PassiveVector<Node*> _sources;
4747
std::atomic<int> _num_sinks {0};
48+
int _cached_num_sinks;
4849

4950
std::function<bool()> _predicate {nullptr};
5051
std::function<void()> _work {nullptr};
@@ -126,6 +127,7 @@ inline void Topology::_bind(Graph& g) {
126127
_num_sinks++;
127128
}
128129
}
130+
_cached_num_sinks = _num_sinks;
129131

130132
}
131133

‎unittest/taskflow.cpp‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,54 @@ TEST_CASE("Framework" * doctest::timeout(300)) {
992992
REQUIRE(count == 210);
993993
}
994994

995+
996+
// Test run_until
997+
for(unsigned W=0; W<=4; ++W) {
998+
999+
std::atomic<size_t> count {0};
1000+
tf::Framework f;
1001+
auto A = f.silent_emplace([&](){ count ++; });
1002+
auto B = f.silent_emplace([&](auto& subflow){
1003+
count ++;
1004+
auto B1 = subflow.silent_emplace([&](){ count++; });
1005+
auto B2 = subflow.silent_emplace([&](){ count++; });
1006+
auto B3 = subflow.silent_emplace([&](){ count++; });
1007+
B1.precede(B3); B2.precede(B3);
1008+
});
1009+
auto C = f.silent_emplace([&](){ count ++; });
1010+
auto D = f.silent_emplace([&](){ count ++; });
1011+
1012+
A.precede(B, C);
1013+
B.precede(D);
1014+
C.precede(D);
1015+
1016+
tf::Taskflow tf(W);
1017+
tf.run_until(f, [run=10]() mutable { return run-- == 0; },
1018+
[&](){
1019+
REQUIRE(count == 70);
1020+
count = 0;
1021+
}
1022+
).get();
1023+
1024+
1025+
tf.run_until(f, [run=10]() mutable { return run-- == 0; },
1026+
[&](){
1027+
REQUIRE(count == 70);
1028+
count = 0;
1029+
auto E = f.silent_emplace([&](){ count ++; });
1030+
auto F = f.silent_emplace([&](){ count ++; });
1031+
A.precede(E).precede(F);
1032+
});
1033+
1034+
tf.run_until(f, [run=10]() mutable { return run-- == 0; },
1035+
[&](){
1036+
REQUIRE(count == 90);
1037+
count = 0;
1038+
}
1039+
).get();
1040+
1041+
}
1042+
9951043
}
9961044

9971045

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL