<spanclass="m-breadcrumb"><ahref="cudaStandardAlgorithms.html">CUDA Standard Algorithms</a> »</span>
Single Task
</h1>
<navclass="m-block m-default">
<h3>Contents</h3>
<ul>
<li><ahref="#CUDASTDSingleTaskIncludeTheHeader">Include the Header</a></li>
<li><ahref="#CUDASTDSingleTaskRunATaskWithASingleThread">Run a Task with a Single Thread</a></li>
</ul>
</nav>
<p>Taskflow provides a standard template method for running a callable using a single GPU thread.</p><sectionid="CUDASTDSingleTaskIncludeTheHeader"><h2><ahref="#CUDASTDSingleTaskIncludeTheHeader">Include the Header</a></h2><p>You need to include the header file, <code>taskflow/cuda/algorithm/for_each.hpp</code>, for creating a single-threaded task.</p><preclass="m-code"><spanclass="cp">#include</span><spanclass="w"></span><spanclass="cpf"><taskflow/cuda/algorithm/for_each.hpp></span></pre></section><sectionid="CUDASTDSingleTaskRunATaskWithASingleThread"><h2><ahref="#CUDASTDSingleTaskRunATaskWithASingleThread">Run a Task with a Single Thread</a></h2><p>You can launch a kernel with only one GPU thread running it, which is handy when you want to set up a single or a few variables that do not need multiple threads. The following example creates a single-task kernel that sets a device variable to <code>1</code>.</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">// wait for the kernel completes</span>
<spanclass="n">stream</span><spanclass="p">.</span><spanclass="n">synchronize</span><spanclass="p">();</span></pre><p>Since the callable runs on GPU, it must be declared with a <code>__device__</code> specifier.</p></section>
</div>
</div>
</div>
</article></main>
<divclass="m-doc-search" id="search">
<ahref="#!" onclick="return hideSearch()"></a>
<divclass="m-container">
<divclass="m-row">
<divclass="m-col-m-8 m-push-m-2">
<divclass="m-doc-search-header m-text m-small">
<div><spanclass="m-label m-default">Tab</span> / <spanclass="m-label m-default">T</span> to search, <spanclass="m-label m-default">Esc</span> to close</div>