From 95e7822a74f20895addd33b36ba7e62999a3366b Mon Sep 17 00:00:00 2001 From: clin99 Date: Fri, 1 Mar 2019 21:57:15 -0600 Subject: [PATCH 01/12] Add execution model --- example/pipeline.cpp | 60 +++++++ taskflow/graph/basic_taskflow.hpp | 267 +++++++++++++++++++++++++++++- taskflow/graph/graph.hpp | 17 +- taskflow/graph/topology.hpp | 3 + 4 files changed, 343 insertions(+), 4 deletions(-) create mode 100644 example/pipeline.cpp diff --git a/example/pipeline.cpp b/example/pipeline.cpp new file mode 100644 index 000000000..119acdc78 --- /dev/null +++ b/example/pipeline.cpp @@ -0,0 +1,60 @@ +// A simple example to capture the following task dependencies. +// +// TaskA---->TaskB---->TaskD +// TaskA---->TaskC---->TaskD + +#include // the only include you need + +int main(){ + + tf::Taskflow tf; + tf::Framework f; + + std::mutex mtx; + + auto [A, B, C] = f.emplace( + [&] () { + std::scoped_lock lock(mtx); + std::puts("TaskA"); + }, + [&] (auto &subflow) { + std::scoped_lock lock(mtx); + std::puts("TaskB"); + subflow.emplace([&](){ + std::scoped_lock lock(mtx); + std::puts("TaskB1"); + }); + subflow.emplace([&](){ + std::scoped_lock lock(mtx); + std::puts("TaskB2"); + }); + subflow.detach(); + }, + [&] () { + std::scoped_lock lock(mtx); + std::puts("TaskC"); + } + ); + + A.name("A"); + B.name("B"); + C.name("C"); + + // Linear + A.precede(B); + B.precede(C); + + + tf.pipeline_until(f, [iter = 3]() mutable { + std::cout << "iter = " << iter << std::endl; + return iter -- == 0; }, [](){} + ).get(); + + //std::cout << f.dump() << std::endl; + //tf.wait_for_all(); // block until finished + + return 0; +} + + + diff --git a/taskflow/graph/basic_taskflow.hpp b/taskflow/graph/basic_taskflow.hpp index 2be4ad8b3..e46aca0fc 100644 --- a/taskflow/graph/basic_taskflow.hpp +++ b/taskflow/graph/basic_taskflow.hpp @@ -49,7 +49,12 @@ class BasicTaskflow : public FlowBuilder { Closure& operator = (const Closure&) = default; - void operator ()() const; + void operator ()() ; + + void normal_mode() ; + void pipeline_mode() ; + + bool execute_pipeline_task(Graph&); BasicTaskflow* taskflow {nullptr}; Node* node {nullptr}; @@ -237,6 +242,11 @@ class BasicTaskflow : public FlowBuilder { template std::shared_future run_until(Framework& framework, P&& predicate, C&& callable); + + template + std::shared_future pipeline_until(Framework& framework, P&& predicate, C&& callable); + + private: Graph _graph; @@ -247,6 +257,7 @@ class BasicTaskflow : public FlowBuilder { void _schedule(Node&); void _schedule(PassiveVector&); + }; // ============================================================================ @@ -374,6 +385,65 @@ std::shared_future BasicTaskflow::run_until(Framework& f, P&& predicate return tpg._future; } + +// Function: pipeline_until +template