• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>ROCm/HIP</title>
5<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../../index.html" title="Chapter 1. Fiber">
8<link rel="up" href="../gpu_computing.html" title="GPU computing">
9<link rel="prev" href="cuda.html" title="CUDA">
10<link rel="next" href="../worker.html" title="Running with worker threads">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
15<td align="center"><a href="../../../../../../index.html">Home</a></td>
16<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="cuda.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../gpu_computing.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../worker.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h3 class="title">
27<a name="fiber.gpu_computing.hip"></a><a name="hip"></a><a class="link" href="hip.html" title="ROCm/HIP">ROCm/HIP</a>
28</h3></div></div></div>
29<p>
30        <a href="http://github.com/ROCm-Developer-Tools/HIP/tree/roc-1.6.0/" target="_top">HIP</a>
31        is part of the <a href="http://rocm.github.io/" target="_top">ROC (Radeon Open Compute)</a>
32        platform for parallel computing on AMD and NVIDIA GPUs. The application programming
33        interface of HIP gives access to GPU's instruction set and computation resources
34        (Execution of compute kernels).
35      </p>
36<h5>
37<a name="fiber.gpu_computing.hip.h0"></a>
38        <span class="phrase"><a name="fiber.gpu_computing.hip.synchronization_with_rocm_hip_streams"></a></span><a class="link" href="hip.html#fiber.gpu_computing.hip.synchronization_with_rocm_hip_streams">Synchronization
39        with ROCm/HIP streams</a>
40      </h5>
41<p>
42        HIP operation such as compute kernels or memory transfer (between host and
43        device) can be grouped/queued by HIP streams. are executed on the GPUs. Boost.Fiber
44        enables a fiber to sleep (suspend) till a HIP stream has completed its operations.
45        This enables applications to run other fibers on the CPU without the need
46        to spawn an additional OS-threads. And resume the fiber when the HIP streams
47        has finished.
48      </p>
49<pre class="programlisting"><span class="identifier">__global__</span>
50<span class="keyword">void</span> <span class="identifier">kernel</span><span class="special">(</span> <span class="keyword">int</span> <span class="identifier">size</span><span class="special">,</span> <span class="keyword">int</span> <span class="special">*</span> <span class="identifier">a</span><span class="special">,</span> <span class="keyword">int</span> <span class="special">*</span> <span class="identifier">b</span><span class="special">,</span> <span class="keyword">int</span> <span class="special">*</span> <span class="identifier">c</span><span class="special">)</span> <span class="special">{</span>
51    <span class="keyword">int</span> <span class="identifier">idx</span> <span class="special">=</span> <span class="identifier">threadIdx</span><span class="special">.</span><span class="identifier">x</span> <span class="special">+</span> <span class="identifier">blockIdx</span><span class="special">.</span><span class="identifier">x</span> <span class="special">*</span> <span class="identifier">blockDim</span><span class="special">.</span><span class="identifier">x</span><span class="special">;</span>
52    <span class="keyword">if</span> <span class="special">(</span> <span class="identifier">idx</span> <span class="special">&lt;</span> <span class="identifier">size</span><span class="special">)</span> <span class="special">{</span>
53        <span class="keyword">int</span> <span class="identifier">idx1</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">idx</span> <span class="special">+</span> <span class="number">1</span><span class="special">)</span> <span class="special">%</span> <span class="number">256</span><span class="special">;</span>
54        <span class="keyword">int</span> <span class="identifier">idx2</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">idx</span> <span class="special">+</span> <span class="number">2</span><span class="special">)</span> <span class="special">%</span> <span class="number">256</span><span class="special">;</span>
55        <span class="keyword">float</span> <span class="identifier">as</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">a</span><span class="special">[</span><span class="identifier">idx</span><span class="special">]</span> <span class="special">+</span> <span class="identifier">a</span><span class="special">[</span><span class="identifier">idx1</span><span class="special">]</span> <span class="special">+</span> <span class="identifier">a</span><span class="special">[</span><span class="identifier">idx2</span><span class="special">])</span> <span class="special">/</span> <span class="number">3.0f</span><span class="special">;</span>
56        <span class="keyword">float</span> <span class="identifier">bs</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">b</span><span class="special">[</span><span class="identifier">idx</span><span class="special">]</span> <span class="special">+</span> <span class="identifier">b</span><span class="special">[</span><span class="identifier">idx1</span><span class="special">]</span> <span class="special">+</span> <span class="identifier">b</span><span class="special">[</span><span class="identifier">idx2</span><span class="special">])</span> <span class="special">/</span> <span class="number">3.0f</span><span class="special">;</span>
57        <span class="identifier">c</span><span class="special">[</span><span class="identifier">idx</span><span class="special">]</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">as</span> <span class="special">+</span> <span class="identifier">bs</span><span class="special">)</span> <span class="special">/</span> <span class="number">2</span><span class="special">;</span>
58    <span class="special">}</span>
59<span class="special">}</span>
60
61<span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span> <span class="identifier">f</span><span class="special">([&amp;</span><span class="identifier">done</span><span class="special">]{</span>
62    <span class="identifier">hipStream_t</span> <span class="identifier">stream</span><span class="special">;</span>
63    <span class="identifier">hipStreamCreate</span><span class="special">(</span> <span class="special">&amp;</span> <span class="identifier">stream</span><span class="special">);</span>
64    <span class="keyword">int</span> <span class="identifier">size</span> <span class="special">=</span> <span class="number">1024</span> <span class="special">*</span> <span class="number">1024</span><span class="special">;</span>
65    <span class="keyword">int</span> <span class="identifier">full_size</span> <span class="special">=</span> <span class="number">20</span> <span class="special">*</span> <span class="identifier">size</span><span class="special">;</span>
66    <span class="keyword">int</span> <span class="special">*</span> <span class="identifier">host_a</span><span class="special">,</span> <span class="special">*</span> <span class="identifier">host_b</span><span class="special">,</span> <span class="special">*</span> <span class="identifier">host_c</span><span class="special">;</span>
67    <span class="identifier">hipHostMalloc</span><span class="special">(</span> <span class="special">&amp;</span> <span class="identifier">host_a</span><span class="special">,</span> <span class="identifier">full_size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">),</span> <span class="identifier">hipHostMallocDefault</span><span class="special">);</span>
68    <span class="identifier">hipHostMalloc</span><span class="special">(</span> <span class="special">&amp;</span> <span class="identifier">host_b</span><span class="special">,</span> <span class="identifier">full_size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">),</span> <span class="identifier">hipHostMallocDefault</span><span class="special">);</span>
69    <span class="identifier">hipHostMalloc</span><span class="special">(</span> <span class="special">&amp;</span> <span class="identifier">host_c</span><span class="special">,</span> <span class="identifier">full_size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">),</span> <span class="identifier">hipHostMallocDefault</span><span class="special">);</span>
70    <span class="keyword">int</span> <span class="special">*</span> <span class="identifier">dev_a</span><span class="special">,</span> <span class="special">*</span> <span class="identifier">dev_b</span><span class="special">,</span> <span class="special">*</span> <span class="identifier">dev_c</span><span class="special">;</span>
71    <span class="identifier">hipMalloc</span><span class="special">(</span> <span class="special">&amp;</span> <span class="identifier">dev_a</span><span class="special">,</span> <span class="identifier">size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">)</span> <span class="special">);</span>
72    <span class="identifier">hipMalloc</span><span class="special">(</span> <span class="special">&amp;</span> <span class="identifier">dev_b</span><span class="special">,</span> <span class="identifier">size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">)</span> <span class="special">);</span>
73    <span class="identifier">hipMalloc</span><span class="special">(</span> <span class="special">&amp;</span> <span class="identifier">dev_c</span><span class="special">,</span> <span class="identifier">size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">)</span> <span class="special">);</span>
74    <span class="identifier">std</span><span class="special">::</span><span class="identifier">minstd_rand</span> <span class="identifier">generator</span><span class="special">;</span>
75    <span class="identifier">std</span><span class="special">::</span><span class="identifier">uniform_int_distribution</span><span class="special">&lt;&gt;</span> <span class="identifier">distribution</span><span class="special">(</span><span class="number">1</span><span class="special">,</span> <span class="number">6</span><span class="special">);</span>
76    <span class="keyword">for</span> <span class="special">(</span> <span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">full_size</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span>
77        <span class="identifier">host_a</span><span class="special">[</span><span class="identifier">i</span><span class="special">]</span> <span class="special">=</span> <span class="identifier">distribution</span><span class="special">(</span> <span class="identifier">generator</span><span class="special">);</span>
78        <span class="identifier">host_b</span><span class="special">[</span><span class="identifier">i</span><span class="special">]</span> <span class="special">=</span> <span class="identifier">distribution</span><span class="special">(</span> <span class="identifier">generator</span><span class="special">);</span>
79    <span class="special">}</span>
80    <span class="keyword">for</span> <span class="special">(</span> <span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">full_size</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">+=</span> <span class="identifier">size</span><span class="special">)</span> <span class="special">{</span>
81        <span class="identifier">hipMemcpyAsync</span><span class="special">(</span> <span class="identifier">dev_a</span><span class="special">,</span> <span class="identifier">host_a</span> <span class="special">+</span> <span class="identifier">i</span><span class="special">,</span> <span class="identifier">size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">),</span> <span class="identifier">hipMemcpyHostToDevice</span><span class="special">,</span> <span class="identifier">stream</span><span class="special">);</span>
82        <span class="identifier">hipMemcpyAsync</span><span class="special">(</span> <span class="identifier">dev_b</span><span class="special">,</span> <span class="identifier">host_b</span> <span class="special">+</span> <span class="identifier">i</span><span class="special">,</span> <span class="identifier">size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">),</span> <span class="identifier">hipMemcpyHostToDevice</span><span class="special">,</span> <span class="identifier">stream</span><span class="special">);</span>
83        <span class="identifier">hipLaunchKernel</span><span class="special">(</span><span class="identifier">kernel</span><span class="special">,</span> <span class="identifier">dim3</span><span class="special">(</span><span class="identifier">size</span> <span class="special">/</span> <span class="number">256</span><span class="special">),</span> <span class="identifier">dim3</span><span class="special">(</span><span class="number">256</span><span class="special">),</span> <span class="number">0</span><span class="special">,</span> <span class="identifier">stream</span><span class="special">,</span> <span class="identifier">size</span><span class="special">,</span> <span class="identifier">dev_a</span><span class="special">,</span> <span class="identifier">dev_b</span><span class="special">,</span> <span class="identifier">dev_c</span><span class="special">);</span>
84        <span class="identifier">hipMemcpyAsync</span><span class="special">(</span> <span class="identifier">host_c</span> <span class="special">+</span> <span class="identifier">i</span><span class="special">,</span> <span class="identifier">dev_c</span><span class="special">,</span> <span class="identifier">size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">),</span> <span class="identifier">hipMemcpyDeviceToHost</span><span class="special">,</span> <span class="identifier">stream</span><span class="special">);</span>
85    <span class="special">}</span>
86    <span class="keyword">auto</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">hip</span><span class="special">::</span><span class="identifier">waitfor_all</span><span class="special">(</span> <span class="identifier">stream</span><span class="special">);</span> <span class="comment">// suspend fiber till HIP stream has finished</span>
87    <span class="identifier">BOOST_ASSERT</span><span class="special">(</span> <span class="identifier">stream</span> <span class="special">==</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">get</span><span class="special">&lt;</span> <span class="number">0</span> <span class="special">&gt;(</span> <span class="identifier">result</span><span class="special">)</span> <span class="special">);</span>
88    <span class="identifier">BOOST_ASSERT</span><span class="special">(</span> <span class="identifier">hipSuccess</span> <span class="special">==</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">get</span><span class="special">&lt;</span> <span class="number">1</span> <span class="special">&gt;(</span> <span class="identifier">result</span><span class="special">)</span> <span class="special">);</span>
89    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"f1: GPU computation finished"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
90    <span class="identifier">hipHostFree</span><span class="special">(</span> <span class="identifier">host_a</span><span class="special">);</span>
91    <span class="identifier">hipHostFree</span><span class="special">(</span> <span class="identifier">host_b</span><span class="special">);</span>
92    <span class="identifier">hipHostFree</span><span class="special">(</span> <span class="identifier">host_c</span><span class="special">);</span>
93    <span class="identifier">hipFree</span><span class="special">(</span> <span class="identifier">dev_a</span><span class="special">);</span>
94    <span class="identifier">hipFree</span><span class="special">(</span> <span class="identifier">dev_b</span><span class="special">);</span>
95    <span class="identifier">hipFree</span><span class="special">(</span> <span class="identifier">dev_c</span><span class="special">);</span>
96    <span class="identifier">hipStreamDestroy</span><span class="special">(</span> <span class="identifier">stream</span><span class="special">);</span>
97<span class="special">});</span>
98<span class="identifier">f</span><span class="special">.</span><span class="identifier">join</span><span class="special">();</span>
99</pre>
100<h5>
101<a name="fiber.gpu_computing.hip.h1"></a>
102        <span class="phrase"><a name="fiber.gpu_computing.hip.synopsis"></a></span><a class="link" href="hip.html#fiber.gpu_computing.hip.synopsis">Synopsis</a>
103      </h5>
104<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">hip</span><span class="special">/</span><span class="identifier">waitfor</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
105
106<span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span>
107<span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span>
108<span class="keyword">namespace</span> <span class="identifier">hip</span> <span class="special">{</span>
109
110<span class="identifier">std</span><span class="special">::</span><span class="identifier">tuple</span><span class="special">&lt;</span> <span class="identifier">hipStream_t</span><span class="special">,</span> <span class="identifier">hipError_t</span> <span class="special">&gt;</span> <span class="identifier">waitfor_all</span><span class="special">(</span> <span class="identifier">hipStream_t</span> <span class="identifier">st</span><span class="special">);</span>
111<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">tuple</span><span class="special">&lt;</span> <span class="identifier">hipStream_t</span><span class="special">,</span> <span class="identifier">hipError_t</span> <span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">waitfor_all</span><span class="special">(</span> <span class="identifier">hipStream_t</span> <span class="special">...</span> <span class="identifier">st</span><span class="special">);</span>
112
113<span class="special">}}}</span>
114</pre>
115<p>
116        </p>
117<h5>
118<a name="hip_waitfor_bridgehead"></a>
119  <span class="phrase"><a name="hip_waitfor"></a></span>
120  <a class="link" href="hip.html#hip_waitfor">Non-member function <code class="computeroutput">hip::waitfor()</code></a>
121</h5>
122<p>
123      </p>
124<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">hip</span><span class="special">/</span><span class="identifier">waitfor</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
125
126<span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span>
127<span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span>
128<span class="keyword">namespace</span> <span class="identifier">hip</span> <span class="special">{</span>
129
130<span class="identifier">std</span><span class="special">::</span><span class="identifier">tuple</span><span class="special">&lt;</span> <span class="identifier">hipStream_t</span><span class="special">,</span> <span class="identifier">hipError_t</span> <span class="special">&gt;</span> <span class="identifier">waitfor_all</span><span class="special">(</span> <span class="identifier">hipStream_t</span> <span class="identifier">st</span><span class="special">);</span>
131<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">tuple</span><span class="special">&lt;</span> <span class="identifier">hipStream_t</span><span class="special">,</span> <span class="identifier">hipError_t</span> <span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">waitfor_all</span><span class="special">(</span> <span class="identifier">hipStream_t</span> <span class="special">...</span> <span class="identifier">st</span><span class="special">);</span>
132
133<span class="special">}}}</span>
134</pre>
135<div class="variablelist">
136<p class="title"><b></b></p>
137<dl class="variablelist">
138<dt><span class="term">Effects:</span></dt>
139<dd><p>
140              Suspends active fiber till HIP stream has finished its operations.
141            </p></dd>
142<dt><span class="term">Returns:</span></dt>
143<dd><p>
144              tuple of stream reference and the HIP stream status
145            </p></dd>
146</dl>
147</div>
148</div>
149<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
150<td align="left"></td>
151<td align="right"><div class="copyright-footer">Copyright © 2013 Oliver Kowalke<p>
152        Distributed under the Boost Software License, Version 1.0. (See accompanying
153        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
154      </p>
155</div></td>
156</tr></table>
157<hr>
158<div class="spirit-nav">
159<a accesskey="p" href="cuda.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../gpu_computing.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../worker.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
160</div>
161</body>
162</html>
163