<p>To compile Taskflow with CUDA code, you need a <code>nvcc</code> compiler. Please visit the official page of <ahref="https://developer.nvidia.com/cuda-downloads">Downloading CUDA Toolkit</a>.</p>
<p>Taskflow's GPU programming interface for CUDA is tf::cudaFlow. Consider the following <code>simple.cu</code> program that launches a single kernel function to output a message:</p>
<divclass="ttc" id="aclasstf_1_1cudaGraphBase_html"><divclass="ttname"><ahref="classtf_1_1cudaGraphBase.html">tf::cudaGraphBase</a></div><divclass="ttdoc">class to create a CUDA graph with uunique ownership</div><divclass="ttdef"><b>Definition</b> cuda_graph.hpp:531</div></div>
<divclass="ttc" id="aclasstf_1_1cudaGraphBase_html_abb33299f42206f30f1d0f35c7c6fe6de"><divclass="ttname"><ahref="classtf_1_1cudaGraphBase.html#abb33299f42206f30f1d0f35c7c6fe6de">tf::cudaGraphBase::single_task</a></div><divclass="ttdeci">cudaTask single_task(C c)</div><divclass="ttdoc">runs a callable with only a single kernel thread</div></div>
<divclass="ttc" id="aclasstf_1_1cudaGraphExecBase_html"><divclass="ttname"><ahref="classtf_1_1cudaGraphExecBase.html">tf::cudaGraphExecBase</a></div><divclass="ttdoc">class to create an executable CUDA graph with unique ownership</div><divclass="ttdef"><b>Definition</b> cuda_graph_exec.hpp:93</div></div>
<divclass="ttc" id="aclasstf_1_1cudaStreamBase_html"><divclass="ttname"><ahref="classtf_1_1cudaStreamBase.html">tf::cudaStreamBase</a></div><divclass="ttdoc">class to create a CUDA stream with unique ownership</div><divclass="ttdef"><b>Definition</b> cuda_stream.hpp:189</div></div>
<divclass="ttc" id="aclasstf_1_1cudaStreamBase_html_a1e5140505629afd4b3422399f8080cb0"><divclass="ttname"><ahref="classtf_1_1cudaStreamBase.html#a1e5140505629afd4b3422399f8080cb0">tf::cudaStreamBase::synchronize</a></div><divclass="ttdeci">cudaStreamBase & synchronize()</div><divclass="ttdoc">synchronizes the associated stream</div><divclass="ttdef"><b>Definition</b> cuda_stream.hpp:232</div></div>
<divclass="ttc" id="aclasstf_1_1cudaStreamBase_html_a7dcdfb79385a57c4c59b7c9f21e8beb9"><divclass="ttname"><ahref="classtf_1_1cudaStreamBase.html#a7dcdfb79385a57c4c59b7c9f21e8beb9">tf::cudaStreamBase::run</a></div><divclass="ttdeci">cudaStreamBase & run(const cudaGraphExecBase< C, D > &exec)</div><divclass="ttdoc">runs the given executable CUDA graph</div></div>
</div><!-- fragment --><p>The easiest way to compile Taskflow with CUDA code (e.g., cudaFlow, kernels) is to use <ahref="https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html">nvcc</a>:</p>
</div><!-- fragment --><h1><aclass="anchor" id="CompileTaskflowWithCUDASeparately"></a>
Compile Source Code Separately</h1>
<p>Large GPU applications often compile a program into separate objects and link them together to form an executable or a library. You can compile your CPU code and GPU code separately with Taskflow using <code>nvcc</code> and other compilers (such as <code>g++</code> and <code>clang++</code>). Consider the following example that defines two tasks on two different pieces (<code>main.cpp</code> and <code>cudaflow.cpp</code>) of source code:</p>
<divclass="ttc" id="aclasstf_1_1Executor_html"><divclass="ttname"><ahref="classtf_1_1Executor.html">tf::Executor</a></div><divclass="ttdoc">class to create an executor</div><divclass="ttdef"><b>Definition</b> executor.hpp:62</div></div>
<divclass="ttc" id="aclasstf_1_1Executor_html_a519777f5783981d534e9e53b99712069"><divclass="ttname"><ahref="classtf_1_1Executor.html#a519777f5783981d534e9e53b99712069">tf::Executor::run</a></div><divclass="ttdeci">tf::Future< void > run(Taskflow &taskflow)</div><divclass="ttdoc">runs a taskflow once</div></div>
<divclass="ttc" id="aclasstf_1_1FlowBuilder_html_a4d52a7fe2814b264846a2085e931652c"><divclass="ttname"><ahref="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">tf::FlowBuilder::emplace</a></div><divclass="ttdeci">Task emplace(C &&callable)</div><divclass="ttdoc">creates a static task</div><divclass="ttdef"><b>Definition</b> flow_builder.hpp:1562</div></div>
<divclass="ttc" id="aclasstf_1_1Task_html"><divclass="ttname"><ahref="classtf_1_1Task.html">tf::Task</a></div><divclass="ttdoc">class to create a task handle over a taskflow node</div><divclass="ttdef"><b>Definition</b> task.hpp:263</div></div>
<divclass="ttc" id="aclasstf_1_1Task_html_a8c78c453295a553c1c016e4062da8588"><divclass="ttname"><ahref="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">tf::Task::precede</a></div><divclass="ttdeci">Task & precede(Ts &&... tasks)</div><divclass="ttdoc">adds precedence links from this to other tasks</div><divclass="ttdef"><b>Definition</b> task.hpp:952</div></div>
<divclass="ttc" id="aclasstf_1_1Taskflow_html"><divclass="ttname"><ahref="classtf_1_1Taskflow.html">tf::Taskflow</a></div><divclass="ttdoc">class to create a taskflow object</div><divclass="ttdef"><b>Definition</b> taskflow.hpp:64</div></div>
</div><!-- fragment --><divclass="fragment"><divclass="line"><spanclass="comment">// cudaflow.cpp</span></div>
<divclass="line"># now we have the two compiled .o objects, main.o and cudaflow.o</div>
<divclass="line">main.o cudaflow.o </div>
</div><!-- fragment --><p>The <code>--extended-lambda</code> option tells <code>nvcc</code> to generate GPU code for the lambda defined with <code><b>device</b></code>. The <code>-x cu</code> tells <code>nvcc</code> to treat the input files as <code></code>.cu files containing both CPU and GPU code. By default, <code>nvcc</code> treats <code></code>.cpp files as CPU-only code. This option is required to have <code>nvcc</code> generate device code here, but it is also a handy way to avoid renaming source files in larger projects. The <code>–dc</code> option tells <code>nvcc</code> to generate device code for later linking.</p>
<p>You may also need to specify the target architecture to tell <code>nvcc</code> to target on a compatible SM architecture using the option -arch. For instance, the following command requires device code linking to have compute capability 7.5 or later:</p>
<divclass="fragment"><divclass="line">~$ nvcc -std=c++17 --extended-lambda -x cu -arch=sm_75 -I path/to/taskflow \</div>
</div><!-- fragment --><h2><aclass="anchor" id="CompileTaskflowWithCUDANaiveLinking"></a>
Link Objects Using nvcc</h2>
<p>Using <code>nvcc</code> to link compiled object code is nothing special but replacing the normal compiler with <code>nvcc</code> and it takes care of all the necessary steps:</p>
</div><!-- fragment --><h2><aclass="anchor" id="CompileTaskflowWithCUDADifferentLinkers"></a>
Link Objects Using Different Linkers</h2>
<p>You can choose to use a compiler other than <code>nvcc</code> for the final link step. Since your CPU compiler does not know how to link CUDA device code, you have to add a step in your build to have <code>nvcc</code> link the CUDA device code, using the option <code>-dlink:</code></p>
</div><!-- fragment --><p>This step links all the <em>device object code</em> and places it into <code>gpuCode.o</code>.</p>
<dlclass="section note"><dt>Note</dt><dd>Note that this step does not link the CPU object code and discards the CPU object code in <code>main.o</code> and <code>cudaflow.o</code>.</dd></dl>
<p>To complete the link to an executable, you can use, for example, <code>ld</code> or <code>g++</code>.</p>
<divclass="fragment"><divclass="line"># replace /usr/local/cuda/lib64 with your own CUDA library installation path</div>
</div><!-- fragment --><p>We give <code>g++</code> all of the objects again because it needs the CPU object code, which is not in <code>gpuCode.o</code>. The device code stored in the original objects, <code>main.o</code> and <code>cudaflow.o</code>, does not conflict with the code in <code>gpuCode.o</code>. <code>g++</code> ignores device code because it does not know how to link it, and the device code in <code>gpuCode.o</code> is already linked and ready to go.</p>
<dlclass="section note"><dt>Note</dt><dd>This intentional ignorance is extremely useful in large builds where intermediate objects may have both CPU and GPU code. In this case, we just let the GPU and CPU linkers each do its own job, noting that the CPU linker is always the last one we run. The CUDA <aclass="el" href="classtf_1_1Runtime.html" title="class to create a runtime task">Runtime</a> API library is automatically linked when we use <code>nvcc</code> for linking, but we must explicitly link it (<code>-lcudart</code>) when using another linker. </dd></dl>
</div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.13.1-->
<!-- start footer part -->
<divid="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<liclass="navelem"><aclass="el" href="install.html">Building and Installing</a></li>
<liclass="footer">
Maintained by <ahref="https://tsung-wei-huang.github.io/">Dr. Tsung-Wei Huang</a>
—
Generated by <ahref="https://www.doxygen.org/index.html"><imgclass="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.12.0