<li><ahref="#OffloadAcudaFlow">Offload a cudaFlow</a></li>
<li><ahref="#UpdateAcudaFlow">Update a cudaFlow</a></li>
<li><ahref="#IntegrateCudaFlowIntoTaskflow">Integrate a cudaFlow into Taskflow</a></li>
</ul>
</nav>
<p>Modern scientific computing typically leverages GPU-powered parallel processing cores to speed up large-scale applications. This chapter discusses how to implement CPU-GPU heterogeneous tasking algorithms with <ahref="https://developer.nvidia.com/cuda-zone">Nvidia CUDA</a>.</p><sectionid="GPUTaskingcudaFlowIncludeTheHeader"><h2><ahref="#GPUTaskingcudaFlowIncludeTheHeader">Include the Header</a></h2><p>You need to include the header file, <code>taskflow/cuda/cudaflow.hpp</code>, for creating a GPU task graph using <ahref="classtf_1_1cudaFlow.html" class="m-doc">tf::<wbr/>cudaFlow</a>.</p><preclass="m-code"><spanclass="cp">#include</span><spanclass="w"></span><spanclass="cpf"><taskflow/cuda/cudaflow.hpp></span></pre></section><sectionid="WhatIsACudaGraph"><h2><ahref="#WhatIsACudaGraph">What is a CUDA Graph?</a></h2><p>CUDA Graph is a new execution model that enables a series of CUDA kernels to be defined and encapsulated as a single unit, i.e., a task graph of operations, rather than a sequence of individually-launched operations. This organization allows launching multiple GPU operations through a single CPU operation and hence reduces the launching overheads, especially for kernels of short running time. The benefit of CUDA Graph can be demonstrated in the figure below:</p><imgclass="m-image" src="cuda_graph_benefit.png" alt="Image" /><p>In this example, a sequence of short kernels is launched one-by-one by the CPU. The CPU launching overhead creates a significant gap in between the kernels. If we replace this sequence of kernels with a CUDA graph, initially we will need to spend a little extra time on building the graph and launching the whole graph in one go on the first occasion, but subsequent executions will be very fast, as there will be very little gap between the kernels. The difference is more pronounced when the same sequence of operations is repeated many times, for example, many training epochs in machine learning workloads. In that case, the initial costs of building and launching the graph will be amortized over the entire training iterations.</p><asideclass="m-note m-warning"><h4>Attention</h4><p>A comprehensive introduction about CUDA Graph can be referred to the <ahref="https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#cuda-graphs">CUDA Graph Programming Guide</a>.</p></aside></section><sectionid="Create_a_cudaFlow"><h2><ahref="#Create_a_cudaFlow">Create a cudaFlow</a></h2><p>Taskflow leverages <ahref="https://developer.nvidia.com/blog/cuda-graphs/">CUDA Graph</a> to enable concurrent CPU-GPU tasking using a task graph model called <ahref="classtf_1_1cudaFlow.html" class="m-doc">tf::<wbr/>cudaFlow</a>. A cudaFlow manages a CUDA graph explicitly to execute dependent GPU operations in a single CPU call. The following example implements a cudaFlow that performs an saxpy (A·X Plus Y) workload:</p><preclass="m-code"><spanclass="cp">#include</span><spanclass="w"></span><spanclass="cpf"><taskflow/cuda/cudaflow.hpp></span>
<spanclass="c1">// saxpy (single-precision A·X Plus Y) kernel</span>
<spanclass="w"></span><spanclass="k">const</span><spanclass="w"></span><spanclass="kt">unsigned</span><spanclass="w"></span><spanclass="n">N</span><spanclass="w"></span><spanclass="o">=</span><spanclass="w"></span><spanclass="mi">1</span><spanclass="o"><<</span><spanclass="mi">20</span><spanclass="p">;</span><spanclass="w"></span><spanclass="c1">// size of the vector</span>
<spanclass="w"></span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">vector</span><spanclass="o"><</span><spanclass="kt">float</span><spanclass="o">></span><spanclass="w"></span><spanclass="n">hx</span><spanclass="p">(</span><spanclass="n">N</span><spanclass="p">,</span><spanclass="w"></span><spanclass="mf">1.0f</span><spanclass="p">);</span><spanclass="w"></span><spanclass="c1">// x vector at host</span>
<spanclass="w"></span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">vector</span><spanclass="o"><</span><spanclass="kt">float</span><spanclass="o">></span><spanclass="w"></span><spanclass="n">hy</span><spanclass="p">(</span><spanclass="n">N</span><spanclass="p">,</span><spanclass="w"></span><spanclass="mf">2.0f</span><spanclass="p">);</span><spanclass="w"></span><spanclass="c1">// y vector at host</span>
<spanclass="w"></span><spanclass="kt">float</span><spanclass="w"></span><spanclass="o">*</span><spanclass="n">dx</span><spanclass="p">{</span><spanclass="k">nullptr</span><spanclass="p">};</span><spanclass="w"></span><spanclass="c1">// x vector at device</span>
<spanclass="w"></span><spanclass="kt">float</span><spanclass="w"></span><spanclass="o">*</span><spanclass="n">dy</span><spanclass="p">{</span><spanclass="k">nullptr</span><spanclass="p">};</span><spanclass="w"></span><spanclass="c1">// y vector at device</span>
<spanclass="p">}</span></pre><p>The cudaFlow graph consists of two CPU-to-GPU data copies (<code>h2d_x</code> and <code>h2d_y</code>), one kernel (<code>saxpy</code>), and two GPU-to-CPU data copies (<code>d2h_x</code> and <code>d2h_y</code>), in this order of their task dependencies.</p><divclass="m-graph"><svgstyle="width: 24.200rem; height: 9.800rem;" viewBox="0.00 0.00 242.00 98.00">
</div><p>We do not expend yet another effort on simplifying kernel programming but focus on tasking CUDA operations and their dependencies. In other words, <ahref="classtf_1_1cudaFlow.html" class="m-doc">tf::<wbr/>cudaFlow</a> is a lightweight C++ abstraction over CUDA Graph. This organization lets users fully take advantage of CUDA features that are commensurate with their domain knowledge, while leaving difficult task parallelism details to Taskflow.</p></section><sectionid="Compile_a_cudaFlow_program"><h2><ahref="#Compile_a_cudaFlow_program">Compile a cudaFlow Program</a></h2><p>Use <ahref="https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html">nvcc</a> to compile a cudaFlow program:</p><preclass="m-console"><spanclass="go">~$ nvcc -std=c++17 my_cudaflow.cu -I path/to/include/taskflow -O2 -o my_cudaflow</span>
<spanclass="go">~$ ./my_cudaflow</span></pre><p>Please visit the page <ahref="CompileTaskflowWithCUDA.html" class="m-doc">Compile Taskflow with CUDA</a> for more details.</p></section><sectionid="run_a_cudaflow_on_a_specific_gpu"><h2><ahref="#run_a_cudaflow_on_a_specific_gpu">Run a cudaFlow on Specific GPU</a></h2><p>By default, a cudaFlow runs on the current GPU context associated with the caller, which is typically GPU <code>0</code>. Each CUDA GPU has an integer identifier in the range of <code>[0, N)</code> to represent the context of that GPU, where <code>N</code> is the number of GPUs in the system. You can run a cudaFlow on a specific GPU by switching the context to a different GPU using <ahref="classtf_1_1cudaScopedDevice.html" class="m-doc">tf::<wbr/>cudaScopedDevice</a>. The code below creates a cudaFlow and runs it on GPU <code>2</code>.</p><preclass="m-code"><spanclass="p">{</span>
<spanclass="w"></span><spanclass="c1">// create an RAII-styled switcher to the context of GPU 2</span>
<spanclass="p">}</span></pre><p><ahref="classtf_1_1cudaScopedDevice.html" class="m-doc">tf::<wbr/>cudaScopedDevice</a> is an RAII-styled wrapper to perform <em>scoped</em> switch to the given GPU context. When the scope is destroyed, it switches back to the original context.</p><asideclass="m-note m-warning"><h4>Attention</h4><p><ahref="classtf_1_1cudaScopedDevice.html" class="m-doc">tf::<wbr/>cudaScopedDevice</a> allows you to place a cudaFlow on a particular GPU device, but it is your responsibility to ensure correct memory access. For example, you may not allocate a memory block on GPU <code>2</code> while accessing it from a kernel on GPU <code>0</code>. An easy practice for multi-GPU programming is to allocate <em>unified shared memory</em> using <code>cudaMallocManaged</code> and let the CUDA runtime perform automatic memory migration between GPUs.</p></aside></section><sectionid="GPUMemoryOperations"><h2><ahref="#GPUMemoryOperations">Create Memory Operation Tasks</a></h2><p>cudaFlow provides a set of methods for users to manipulate device memory. There are two categories, <em>raw</em> data and <em>typed</em> data. Raw data operations are methods with prefix <code>mem</code>, such as <code>memcpy</code> and <code>memset</code>, that operate in <em>bytes</em>. Typed data operations such as <code>copy</code>, <code>fill</code>, and <code>zero</code>, take <em>logical count</em> of elements. For instance, the following three methods have the same result of zeroing <code>sizeof(int)*count</code> bytes of the device memory area pointed to by <code>target</code>.</p><preclass="m-code"><spanclass="kt">int</span><spanclass="o">*</span><spanclass="w"></span><spanclass="n">target</span><spanclass="p">;</span>
<spanclass="n">same_as_above_again</span><spanclass="w"></span><spanclass="o">=</span><spanclass="w"></span><spanclass="n">cudaflow</span><spanclass="p">.</span><spanclass="n">zero</span><spanclass="p">(</span><spanclass="n">target</span><spanclass="p">,</span><spanclass="w"></span><spanclass="n">count</span><spanclass="p">);</span></pre><p>The method <ahref="classtf_1_1cudaGraphBase.html#a32634c5645c14b99ceeaafe77ea5ea62" class="m-doc">tf::<wbr/>cudaFlow::<wbr/>fill</a> is a more powerful variant of <ahref="classtf_1_1cudaGraphBase.html#a10196f49de261a4042de328aab2452c8" class="m-doc">tf::<wbr/>cudaFlow::<wbr/>memset</a>. It can fill a memory area with any value of type <code>T</code>, given that <code>sizeof(T)</code> is 1, 2, or 4 bytes. The following example creates a GPU task to fill <code>count</code> elements in the array <code>target</code> with value <code>1234</code>.</p><preclass="m-code"><spanclass="n">cf</span><spanclass="p">.</span><spanclass="n">fill</span><spanclass="p">(</span><spanclass="n">target</span><spanclass="p">,</span><spanclass="w"></span><spanclass="mi">1234</span><spanclass="p">,</span><spanclass="w"></span><spanclass="n">count</span><spanclass="p">);</span></pre><p>Similar concept applies to <ahref="classtf_1_1cudaGraphBase.html#a5e704c7bb669a82f4fe140ecb4576eb0" class="m-doc">tf::<wbr/>cudaFlow::<wbr/>memcpy</a> and <ahref="classtf_1_1cudaGraphBase.html#a02a041d5dd9e1e8958eb43e09331051e" class="m-doc">tf::<wbr/>cudaFlow::<wbr/>copy</a> as well. The following two methods are equivalent to each other.</p><preclass="m-code"><spanclass="n">cudaflow</span><spanclass="p">.</span><spanclass="n">memcpy</span><spanclass="p">(</span><spanclass="n">target</span><spanclass="p">,</span><spanclass="w"></span><spanclass="n">source</span><spanclass="p">,</span><spanclass="w"></span><spanclass="k">sizeof</span><spanclass="p">(</span><spanclass="kt">int</span><spanclass="p">)</span><spanclass="w"></span><spanclass="o">*</span><spanclass="w"></span><spanclass="n">count</span><spanclass="p">);</span>
<spanclass="n">cudaflow</span><spanclass="p">.</span><spanclass="n">copy</span><spanclass="p">(</span><spanclass="n">target</span><spanclass="p">,</span><spanclass="w"></span><spanclass="n">source</span><spanclass="p">,</span><spanclass="w"></span><spanclass="n">count</span><spanclass="p">);</span></pre></section><sectionid="OffloadAcudaFlow"><h2><ahref="#OffloadAcudaFlow">Offload a cudaFlow</a></h2><p>To offload a cudaFlow to a GPU, you need to use tf::cudaFlow::run and pass a <ahref="namespacetf.html#af19c9b301dc0b0fe2a51a960fa427e83" class="m-doc">tf::<wbr/>cudaStream</a> created on that GPU. The run method is asynchronous and can be explicitly synchronized through the given stream.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">cudaStream</span><spanclass="w"></span><spanclass="n">stream</span><spanclass="p">;</span>
<spanclass="c1">// launch a cudaflow asynchronously through a stream</span>
<spanclass="c1">// wait for the cudaflow to finish</span>
<spanclass="n">stream</span><spanclass="p">.</span><spanclass="n">synchronize</span><spanclass="p">();</span></pre><p>When you offload a cudaFlow using tf::cudaFlow::run, the runtime transforms that cudaFlow (i.e., application GPU task graph) into a native executable instance and submit it to the CUDA runtime for execution. There is always an one-to-one mapping between cudaFlow and its native CUDA graph representation (except those constructed by using <ahref="classtf_1_1cudaFlowCapturer.html" class="m-doc">tf::<wbr/>cudaFlowCapturer</a>).</p></section><sectionid="UpdateAcudaFlow"><h2><ahref="#UpdateAcudaFlow">Update a cudaFlow</a></h2><p>Many GPU applications require you to launch a cudaFlow multiple times and update node parameters (e.g., kernel parameters and memory addresses) between iterations. cudaFlow allows you to update the parameters of created tasks and run the updated cudaFlow with new parameters. Every task-creation method in <ahref="classtf_1_1cudaFlow.html" class="m-doc">tf::<wbr/>cudaFlow</a> has an overload to update the parameters of a created task by that method.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">cudaStream</span><spanclass="w"></span><spanclass="n">stream</span><spanclass="p">;</span>
<spanclass="n">stream</span><spanclass="p">.</span><spanclass="n">synchronize</span><spanclass="p">();</span></pre><p>Between successive offloads (i.e., iterative executions of a cudaFlow), you can <em>ONLY</em> update task parameters, such as changing the kernel execution parameters and memory operation parameters. However, you must <em>NOT</em> change the topology of the cudaFlow, such as adding a new task or adding a new dependency. This is the limitation of CUDA Graph.</p><asideclass="m-note m-warning"><h4>Attention</h4><p>There are a few restrictions on updating task parameters in a cudaFlow. Notably, you must <em>NOT</em> change the topology of an offloaded graph. In addition, update methods have the following limitations:</p><ul><li>kernel task<ul><li>The kernel function is not allowed to change. This restriction applies to all algorithm tasks that are created using lambda.</li></ul></li><li>memset and memcpy tasks:<ul><li>The CUDA device(s) to which the operand(s) was allocated/mapped cannot change</li><li>The source/destination memory must be allocated from the same contexts as the original source/destination memory.</li></ul></li></ul></aside></section><sectionid="IntegrateCudaFlowIntoTaskflow"><h2><ahref="#IntegrateCudaFlowIntoTaskflow">Integrate a cudaFlow into Taskflow</a></h2><p>You can create a task to enclose a cudaFlow and run it from a worker thread. The usage of the cudaFlow remains the same except that the cudaFlow is run by a worker thread from a taskflow task. The following example runs a cudaFlow from a static task:</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Executor</span><spanclass="w"></span><spanclass="n">executor</span><spanclass="p">;</span>