⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 27.html

📁 国外MPI教材
💻 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>OpenMP Directive Clauses</title>	<meta name="Generator" content="ATutor">	<meta name="Keywords" content=""></head><body> <p>The OPENMP directive clauses - <em>Private</em>, <em>Shared</em> and <em>Default</em> - control whether the listed variables are shared among different threads, or are private (local) to each thread. </p>
<ul>
  <li> <em><a href="../glossary.html#private+variable" target="body" class="at-term">Private variable</a>s</em> mask the variables with the same names in the serial part of the code; they are undefined upon entering the parallel region. </p>
  </li>
  <li> 
    <p>For <em><a href="../glossary.html#shared+variable" target="body" class="at-term">shared variable</a>s</em>, all threads access the same memory locations. Each thread is given a separate copy of any <em>private</em> variables for the duration of the parallel code. </p>
  </li>
  <li> The <em>default</em> clause sets the default scoping for variables not explicitly listed. </li>
</ul>

<p>Example: </p>

<code><pre>c$omp  parallel shared(a)
c$omp& private(myid,x)
     myid=omp_get_thread_num()
     x = work(myid)
     if (x < 1.0) then
       a(myid) = x
     end if
c$omp end parallel</pre></code>


<p>In this example, each thread has its own copy of <code>x</code> and <code>myid</code>. Unless <code>x</code> is made private, its value is indeterminate during parallel execution. </p>

<p>The following is equivalent to the above: </p>

<code><pre>c$omp parallel do default(private)
c$omp& shared(a)
      ...</pre></code>

<p>Sometimes a private variable in a loop needs to be initialized to the value from a serial part of the code prior to the loop. Since the private clause does not initialize variables, a different clause, <code>firstprivate</code>, can be used for this purpose. The firstprivate clause has the following syntax:</p>

  <pre><code>firstprivate(var1, var2, ...)</code></pre>

<p>The listed variables are made private <em>and</em> are initialized to the value in the preceding serial code. </p>

<p>Example: </p>

<pre><code>program first
      integer myid,c
      c=98
c$omp parallel private(myid)
c$omp& firstprivate(c)
      myid=omp_get_thread_num()
      print *,'T:', myid, 'c = ', c
c$omp end parallel
      end</code></pre>

<p>In this example, each thread has a private copy of <var>c</var>, initialized to the value 98. The output of the code is something like: </p>

<pre><code>T:1 c=98
T:3 c=98
T:2 c=98
T:0 c=98</code></pre>

      
<p>Note that the order of the output statements may change from run to run, as there is no synchronization between threads.</p></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -