同步操作将从 Gitee 极速下载/cpp-taskflow 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
<?xml version='1.0' encoding='UTF-8' standalone='no'?><doxygen xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="compound.xsd" version="1.8.14"><compounddef id="RuntimeTasking" kind="page"><compoundname>RuntimeTasking</compoundname><title>Runtime Tasking</title><tableofcontents/><briefdescription></briefdescription><detaileddescription><para>Taskflow allows you to interact with the scheduling runtime from the execution context of a runtime task. Runtime tasking is mostly used for designing specialized parallel algorithms that go beyond the default scheduling rules of taskflows.</para><sect1 id="RuntimeTasking_1CreateARuntimeTask"><title>Create a Runtime Task</title><para>A runtime task is a callable that takes a reference to a <ref refid="classtf_1_1Runtime" kindref="compound">tf::Runtime</ref> object in its argument. A <ref refid="classtf_1_1Runtime" kindref="compound">tf::Runtime</ref> object is created by the running executor and contains several methods for users to interact with the scheduling runtime. For instance, the following code creates a runtime task to <emphasis>forcefully</emphasis> schedule a conditioned task that would never happens:</para><para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>A,<sp/>B,<sp/>C,<sp/>D;</highlight></codeline><codeline><highlight class="normal"><ref refid="cpp/utility/tuple/tie" kindref="compound" external="/home/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::tie</ref>(A,<sp/>B,<sp/>C,<sp/>D)<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>(</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>[]<sp/>()<sp/>{<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>0;<sp/>},</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>[&C]<sp/>(<ref refid="classtf_1_1Runtime" kindref="compound">tf::Runtime</ref>&<sp/>rt)<sp/>{<sp/><sp/></highlight><highlight class="comment">//<sp/>C<sp/>must<sp/>be<sp/>captured<sp/>by<sp/>reference</highlight><highlight class="normal"></highlight></codeline><codeline><highlight class="normal"><sp/><sp/><sp/><sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"B\n"</highlight><highlight class="normal">;<sp/></highlight></codeline><codeline><highlight class="normal"><sp/><sp/><sp/><sp/>rt.<ref refid="classtf_1_1Runtime_1aa7e72cc0f298475195b252c8f1793343" kindref="member">schedule</ref>(C);</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>},</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>[]<sp/>()<sp/>{<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"C\n"</highlight><highlight class="normal">;<sp/>},</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>[]<sp/>()<sp/>{<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"D\n"</highlight><highlight class="normal">;<sp/>}</highlight></codeline><codeline><highlight class="normal">);</highlight></codeline><codeline><highlight class="normal">A.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(B,<sp/>C,<sp/>D);</highlight></codeline><codeline><highlight class="normal">executor.<ref refid="classtf_1_1Executor_1a519777f5783981d534e9e53b99712069" kindref="member">run</ref>(taskflow).wait();</highlight></codeline></programlisting></para><para><dotfile name="/home/twhuang/Code/taskflow/doxygen/images/runtime_task_1.dot"></dotfile></para><para>When the condition task <computeroutput>A</computeroutput> completes and returns <computeroutput>0</computeroutput>, the scheduler moves on to the runtime task <computeroutput>B</computeroutput>. Under the normal circumstance, tasks <computeroutput>C</computeroutput> and <computeroutput>D</computeroutput> will not run because their conditional dependencies never happen. This can be broken by forcefully scheduling <computeroutput>C</computeroutput> or/and <computeroutput>D</computeroutput> via a runtime task that resides in the same graph. Here, the runtime task <computeroutput>B</computeroutput> call <ref refid="classtf_1_1Runtime_1aa7e72cc0f298475195b252c8f1793343" kindref="member">tf::Runtime::schedule(tf::Task)</ref> to run task <computeroutput>C</computeroutput> even though the weak dependency between <computeroutput>A</computeroutput> and <computeroutput>C</computeroutput> will never happen based on the graph structure itself. As a result, we will see both <computeroutput>B</computeroutput> and <computeroutput>C</computeroutput> in the output:</para><para><programlisting filename=".shell-session"><codeline><highlight class="normal">B<sp/><sp/><sp/><sp/>#<sp/>B<sp/>is<sp/>a<sp/>runtime<sp/>task<sp/>to<sp/>schedule<sp/>C<sp/>out<sp/>of<sp/>its<sp/>dependency<sp/>constraint</highlight></codeline><codeline><highlight class="normal">C</highlight></codeline></programlisting></para><para><simplesect kind="attention"><para>You should only schedule an <emphasis>active</emphasis> task from a runtime task. An active task is a task in a running taskflow. The task may or may not be running, and scheduling that task will immediately put the task into the task queue of the worker that is running the runtime task.</para></simplesect></para></sect1><sect1 id="RuntimeTasking_1AcquireTheRunningExecutor"><title>Acquire the Running Executor</title><para>You can acquire the reference to the running executor using <ref refid="classtf_1_1Runtime_1a4ee48a82df1f9758a999d18e6015cec4" kindref="member">tf::Runtime::executor()</ref>. The running executor of a runtime task is the executor that runs the parent taskflow of that runtime task.</para><para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="classtf_1_1Executor" kindref="compound">tf::Executor</ref><sp/>executor;</highlight></codeline><codeline><highlight class="normal"><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline><codeline><highlight class="normal">taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&](<ref refid="classtf_1_1Runtime" kindref="compound">tf::Runtime</ref>&<sp/>rt){</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>assert(&(rt.<ref refid="classtf_1_1Runtime_1a4ee48a82df1f9758a999d18e6015cec4" kindref="member">executor</ref>())<sp/>==<sp/>&executor);</highlight></codeline><codeline><highlight class="normal">});</highlight></codeline><codeline><highlight class="normal">executor.<ref refid="classtf_1_1Executor_1a519777f5783981d534e9e53b99712069" kindref="member">run</ref>(taskflow).wait();</highlight></codeline></programlisting></para></sect1><sect1 id="RuntimeTasking_1RuntimeTaskingRunATaskGraphSynchronously"><title>Run a Task Graph Synchronously</title><para>A runtime task can spawn and run a task graph synchronously using <ref refid="classtf_1_1Runtime_1a479dc5dbf2b988bd166a2b6da089bb17" kindref="member">tf::Runtime::run_and_wait</ref>. This model allows you to leverage dynamic tasking to execute a parallel workload within a runtime task. The following code creates a subflow of two independent tasks and executes it synchronously via the given runtime task:</para><para><programlisting filename=".cpp"><codeline><highlight class="normal">taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](<ref refid="classtf_1_1Runtime" kindref="compound">tf::Runtime</ref>&<sp/>rt){</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>rt.<ref refid="classtf_1_1Runtime_1a479dc5dbf2b988bd166a2b6da089bb17" kindref="member">run_and_wait</ref>([](<ref refid="classtf_1_1Subflow" kindref="compound">tf::Subflow</ref>&<sp/>sf){</highlight></codeline><codeline><highlight class="normal"><sp/><sp/><sp/><sp/>sf.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"independent<sp/>task<sp/>1\n"</highlight><highlight class="normal">;<sp/>});</highlight></codeline><codeline><highlight class="normal"><sp/><sp/><sp/><sp/>sf.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"independent<sp/>task<sp/>2\n"</highlight><highlight class="normal">;<sp/>});</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>});</highlight></codeline><codeline><highlight class="normal"><sp/><sp/></highlight><highlight class="comment">//<sp/>subflow<sp/>joins<sp/>upon<sp/>run_and_wait<sp/>returns</highlight><highlight class="normal"></highlight></codeline><codeline><highlight class="normal">});</highlight></codeline></programlisting></para><para>You can also create a task graph yourself and execute it through a runtime task. This organization avoids repetitive creation of a subflow with the same topology, such as running a runtime task repetitively. The following code performs the same execution logic as the above example but using the given task graph to avoid repetitive creations of a subflow:</para><para><programlisting filename=".cpp"><codeline><highlight class="comment">//<sp/>create<sp/>a<sp/>custom<sp/>graph</highlight><highlight class="normal"></highlight></codeline><codeline><highlight class="normal"><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>graph;</highlight></codeline><codeline><highlight class="normal">graph.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"independent<sp/>task<sp/>1\n"</highlight><highlight class="normal">;<sp/>});</highlight></codeline><codeline><highlight class="normal">graph.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"independent<sp/>task<sp/>2\n"</highlight><highlight class="normal">;<sp/>});</highlight></codeline><codeline><highlight class="normal"></highlight></codeline><codeline><highlight class="normal">taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&](<ref refid="classtf_1_1Runtime" kindref="compound">tf::Runtime</ref>&<sp/>rt){<sp/></highlight></codeline><codeline><highlight class="normal"><sp/><sp/>rt.<ref refid="classtf_1_1Runtime_1a479dc5dbf2b988bd166a2b6da089bb17" kindref="member">run_and_wait</ref>(graph);<sp/><sp/></highlight><highlight class="comment">//<sp/>this<sp/>worker<sp/>thread<sp/>continues<sp/>the<sp/>work-stealing<sp/>loop</highlight><highlight class="normal"></highlight></codeline><codeline><highlight class="normal">});</highlight></codeline><codeline><highlight class="normal">executor.<ref refid="classtf_1_1Executor_1a6d0617eebc9421f1ba1f82ce6dd02c00" kindref="member">run_n</ref>(taskflow,<sp/>10000);</highlight></codeline></programlisting></para><para>Although <ref refid="classtf_1_1Runtime_1a479dc5dbf2b988bd166a2b6da089bb17" kindref="member">tf::Runtime::run_and_wait</ref> blocks until the operation completes, the caller thread (worker) is not blocked (e.g., sleep or holding any lock). Instead, the caller thread joins the work-stealing loop of the executor and returns whenever the spawned task graph completes. This is different from waiting for a submitted taskflow to finish (<computeroutput>executor.run(taskflow).wait()</computeroutput>) which blocks the caller thread until the submitted taskflow completes. When multiple submitted taskflows are being waited, their executions can potentially lead to deadlock. Using <ref refid="classtf_1_1Runtime_1a479dc5dbf2b988bd166a2b6da089bb17" kindref="member">tf::Runtime::run_and_wait</ref> avoids the deadlock problem. </para></sect1></detaileddescription></compounddef></doxygen>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。