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

📄 45.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>Adding MPI to C and Fortran Programs (cont.)</title>	<meta name="Generator" content="ATutor">	<meta name="Keywords" content=""></head><body> <p>So far we have covered how to initialize MPI in a C or Fortran program and how to call several fundamental routines which return information about the MPI environment.&nbsp; The last function call that is required is MPI_Finalize,
which is called by every process once you are finished using MPI.&nbsp; Once MPI_Finalize is called, no further calls to the MPI library may be made.</p>

<h3>Exiting MPI</h3>

<p>This must be the last MPI call made. It is collective and must be made by "all" processes.</p>

<p class="codelang">
C:</p>

<pre><code>MPI_Finalize()</code></pre>

<p class="codelang">
Fortran:</p>

<pre><code>CALL MPI_FINALIZE(IERROR)</code></pre>

<p>The material that has just been covered will be used in almost every MPI program that you will write.&nbsp; As such, it is convenient to place these statements into a skeleton program that can act as a template when we want to create new MPI programs.</p>

<h4>Bones.c</h4>

<pre><code>#include &lt;mpi.h&gt;

int main(int argc, char *argv[]) {
  int rank, size;
  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, &size);

/* your code here */

  MPI_Finalize();
}
</code></pre>

<h4>Bones.f</h4>

<pre><code>      PROGRAM skeleton
      INCLUDE 'mpif.h'
      INTEGER ierror, rank, size
      CALL MPI_INIT(ierror)
      CALL MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierror)
      CALL MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierror)

C your code here

      CALL MPI_FINALIZE(ierror)
      END
</code></pre>

<h3>A Programming Classic - Hello World</h3>

<p>No programming training material would be complete without the classic <em>Hello World</em> application. Here we can use the "Bones" templates assembled in the previous section as a starting point.</p>

<p>We will design a program that will run with 3 or more processes.&nbsp; The process with identifier, or <em>rank</em>, of 2 will print out the phrase "Hello world!".&nbsp; All processes in the communicator will print out their rank and the size of the default communicator, MPI_COMM_WORLD.</p>

<p class="codelang">C:</p>

<pre><code>#include &lt;mpi.h&gt;
#include &lt;stdio.h&gt;

int main(int argc, char *argv[])
{

  int rank,size;

  MPI_Init(&amp;argc, &amp;argv);
  MPI_Comm_rank(MPI_COMM_WORLD,&amp;rank);

  if(rank==2) printf ("P:%d Hello world!\n",rank);

  MPI_Comm_size(MPI_COMM_WORLD, &amp;size);
  printf("I am %d out of %d.\n", rank, size);

  MPI_Finalize();

}</code></pre>

<p class="codelang">
Fortran:
</p>

<pre><code>      PROGRAM hello
      INCLUDE 'mpif.h'
      INTEGER ierror, rank, size

      CALL MPI_INIT(ierror)
      CALL MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierror)
      CALL MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierror)

      if(rank.eq.2) print *,'P:',rank,' Hello World'
      print *,'I have rank ',rank,' out of ',size

      CALL MPI_FINALIZE(ierror)
      END</code></pre></body></html>

⌨️ 快捷键说明

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