60 lines
7.4 KiB
XML
60 lines
7.4 KiB
XML
<?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.9.1" xml:lang="en-US">
|
|
<compounddef id="PrioritizedTasking" kind="page">
|
|
<compoundname>PrioritizedTasking</compoundname>
|
|
<title>Prioritized Tasking</title>
|
|
<tableofcontents>
|
|
<tocsect>
|
|
<name>Assign a Priority to a Task</name>
|
|
<reference>PrioritizedTasking_1AssignAPriorityToATask</reference>
|
|
</tocsect>
|
|
</tableofcontents>
|
|
<briefdescription>
|
|
</briefdescription>
|
|
<detaileddescription>
|
|
<para>This chapter demonstrates how to assigns a task a priority to <emphasis>hint</emphasis> the scheduler about one task of a higher priority should start earlier than another task of a lower priority. Task priorities are useful in many cases. For instance, we may prioritize some tasks over others to improve responsiveness or data locality of parallel tasks.</para>
|
|
<sect1 id="PrioritizedTasking_1AssignAPriorityToATask">
|
|
<title>Assign a Priority to a Task</title>
|
|
<para>Taskflow supports three different priority levels, tf::TaskPriority::HIGH, tf::TaskPriority::NORMAL, and tf::TaskPriority::LOW, as defined in tf::TaskPriority. When there are parallel tasks (i.e., no dependencies), Taskflow will <computeroutput>try</computeroutput> to execute tasks of higher priorities before tasks of lower priorities. By default, all tasks have the highest priorities (<computeroutput>tf::TaskPriority::HIGH</computeroutput>) unless otherwise assigned.</para>
|
|
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="classtf_1_1Executor" kindref="compound">tf::Executor</ref><sp/>executor(1);</highlight></codeline>
|
|
<codeline><highlight class="normal"><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
|
|
<codeline><highlight class="normal"></highlight></codeline>
|
|
<codeline><highlight class="normal"></highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>counter<sp/>=<sp/>0;</highlight></codeline>
|
|
<codeline><highlight class="normal"></highlight></codeline>
|
|
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>[A,<sp/>B,<sp/>C,<sp/>D,<sp/>E]<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></codeline>
|
|
<codeline><highlight class="normal"><sp/><sp/>[&]<sp/>()<sp/>{<sp/></highlight></codeline>
|
|
<codeline><highlight class="normal"><sp/><sp/><sp/><sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"Task<sp/>B:<sp/>"</highlight><highlight class="normal"><sp/><<<sp/>counter++<sp/><<<sp/></highlight><highlight class="charliteral">'\n'</highlight><highlight class="normal">;<sp/><sp/></highlight><highlight class="comment">//<sp/>0</highlight><highlight class="normal"></highlight></codeline>
|
|
<codeline><highlight class="normal"><sp/><sp/>},</highlight></codeline>
|
|
<codeline><highlight class="normal"><sp/><sp/>[&]<sp/>()<sp/>{<sp/></highlight></codeline>
|
|
<codeline><highlight class="normal"><sp/><sp/><sp/><sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"Task<sp/>C:<sp/>"</highlight><highlight class="normal"><sp/><<<sp/>counter++<sp/><<<sp/></highlight><highlight class="charliteral">'\n'</highlight><highlight class="normal">;<sp/><sp/></highlight><highlight class="comment">//<sp/>2</highlight><highlight class="normal"></highlight></codeline>
|
|
<codeline><highlight class="normal"><sp/><sp/>},</highlight></codeline>
|
|
<codeline><highlight class="normal"><sp/><sp/>[&]<sp/>()<sp/>{<sp/></highlight></codeline>
|
|
<codeline><highlight class="normal"><sp/><sp/><sp/><sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"Task<sp/>D:<sp/>"</highlight><highlight class="normal"><sp/><<<sp/>counter++<sp/><<<sp/></highlight><highlight class="charliteral">'\n'</highlight><highlight class="normal">;<sp/><sp/></highlight><highlight class="comment">//<sp/>1</highlight><highlight class="normal"></highlight></codeline>
|
|
<codeline><highlight class="normal"><sp/><sp/>},</highlight></codeline>
|
|
<codeline><highlight class="normal"><sp/><sp/>[]<sp/>()<sp/>{<sp/>}</highlight></codeline>
|
|
<codeline><highlight class="normal">);</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);<sp/></highlight></codeline>
|
|
<codeline><highlight class="normal">E.<ref refid="classtf_1_1Task_1a331b1b726555072e7c7d10941257f664" kindref="member">succeed</ref>(B,<sp/>C,<sp/>D);</highlight></codeline>
|
|
<codeline><highlight class="normal"></highlight></codeline>
|
|
<codeline><highlight class="normal">B.priority(tf::TaskPriority::HIGH);</highlight></codeline>
|
|
<codeline><highlight class="normal">C.priority(tf::TaskPriority::LOW);</highlight></codeline>
|
|
<codeline><highlight class="normal">D.priority(tf::TaskPriority::NORMAL);</highlight></codeline>
|
|
<codeline><highlight class="normal"></highlight></codeline>
|
|
<codeline><highlight class="normal">executor.run(taskflow).wait();</highlight></codeline>
|
|
</programlisting></para>
|
|
<para>In the above code, we have a task graph of five tasks, <computeroutput>A</computeroutput>, <computeroutput>B</computeroutput>, <computeroutput>C</computeroutput>, <computeroutput>D</computeroutput>, and <computeroutput>E</computeroutput>, in which <computeroutput>B</computeroutput>, <computeroutput>C</computeroutput>, and <computeroutput>D</computeroutput> can run in simultaneously when <computeroutput>A</computeroutput> finishes. Since we only uses one worker thread in the executor, we can deterministically run <computeroutput>B</computeroutput> first, then <computeroutput>D</computeroutput>, and <computeroutput>C</computeroutput> in order of their priority values. The output of the above code is as follows:</para>
|
|
<para><programlisting filename=".shell-session"><codeline><highlight class="normal">Task<sp/>B:<sp/>0</highlight></codeline>
|
|
<codeline><highlight class="normal">Task<sp/>D:<sp/>1</highlight></codeline>
|
|
<codeline><highlight class="normal">Task<sp/>C:<sp/>2</highlight></codeline>
|
|
</programlisting></para>
|
|
<para>Task priorities are just <emphasis>hints</emphasis> to Taskflow's work-stealing scheduler about which task should run before another. Due to the randomness nature of work stealing, there is no guarantee that the scheduler will always follow these hints to run tasks when multiple workers exist.</para>
|
|
<para><simplesect kind="note"><para>Currently, Taskflow does not have any high-level abstraction for assigning priorities to threads but tasks. </para>
|
|
</simplesect>
|
|
</para>
|
|
</sect1>
|
|
</detaileddescription>
|
|
<location file="doxygen/cookbook/prioritized_tasking.dox"/>
|
|
</compounddef>
|
|
</doxygen>
|