📄 35.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <style type="text/css"> body { font-family: Verdana, Arial, Helvetica, sans-serif;} a.at-term { font-style: italic; } </style> <title>The Atomic Update</title> <meta name="Generator" content="ATutor"> <meta name="Keywords" content=""></head><body> <p>The <em><a href="../glossary.html#atomic+update" target="body" class="at-term">atomic update</a></em> applies to a single statement in which a scalar value is accessed and updated. It prevents a thread executing this statement from being interrupted by another thread. This is similar to the critical directive just discussed, but atomic is more restrictive, allowing the directive to be
implemented using special hardware features common to modern shared memory computers.
</p>
<p class="codelang">Fortran syntax:</p>
<pre><code>!$omp atomic
<em>statement</em></code></pre>
<p> where <var>statement</var> has one of the following forms: </p>
<pre><code>x = x operator expr
x = intrinsic(x, expr)
</pre></code>
<p>and </p>
<blockquote>
<p><var>x</var> is a scalar variable of intrinsic type </p>
<p><var>operator</var> is one of the operators <code>+, *, -, /, .AND., .OR., .EQV., .NEQV.</code> </p>
<p><em>intrinsic</em> is one of the intrinsics <code>MAX, MIN, IAND, IOR, IEOR</code> </p>
<p><var>expr</var> is a scalar expression that does not reference <code>x</code>. </p>
</blockquote>
<p class="codelang">C/C++ syntax: </p>
<pre><code>#pragma omp atomic
<em>statement</em></pre></code>
<p> where <var>statement</var> has one of the following forms: </p>
<pre><code>x < binop >= expr
x++
++x
x--
--x</code></pre>
<p> and </p>
<blockquote>
<p><var>x</var> is a scalar variable of intrinsic type </p>
<p><var>binop</var> is one of the operators <code>+, *, -, /, &, ^, |, <<,
>></code> </p>
<p><var>expr</var> is a scalar expression that does not reference <code>x</code>.
</p>
</blockquote>
<p>Remember that the atomic directive is in effect only for the statement that immediately follows it! </p>
<p>The following example illustrates the use of atomic updates: </p>
<pre><code>integer, dimension(8) :: a,index
data index/1,1,2,3,1,4,1,5/
c$omp parallel private(i), shared(a, index)
c$omp do
do i = 1, 8
c$omp atomic
a(index(i)) = a(index(i)) + index(i)
enddo
c$omp end parallel</code></pre>
<p>Note that <var>index(i)</var> has the same value for several different values of <var>i</var>. The atomic directive guarantees that different threads do not interfere with each other when updating the same value. </p></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -