MPI: Difference between revisions

Jump to navigation Jump to search
236 bytes removed ,  7 years ago
no edit summary
No edit summary
No edit summary
Line 158: Line 158:
We could now run this program under control of MPI, but each process would only output the original string which isn't very interesting. Let's instead have each process output its rank and how many processes are running in total. This information is obtained at run-time by the use of the following functions:
We could now run this program under control of MPI, but each process would only output the original string which isn't very interesting. Let's instead have each process output its rank and how many processes are running in total. This information is obtained at run-time by the use of the following functions:


{| border="0" cellpadding="5" cellspacing="0" align="center"
<tabs>
! style="background:#8AA8E5;" | C API
<tab name="C">
! style="background:#ECCF98;" | FORTRAN API
<source lang="c">
|-valign="top"
|<source lang="c">
  int MPI_Comm_size(MPI_Comm comm, int *nproc);
  int MPI_Comm_size(MPI_Comm comm, int *nproc);
  int MPI_Comm_rank(MPI_Comm comm, int *myrank);
  int MPI_Comm_rank(MPI_Comm comm, int *myrank);
</source>
</source>
|<source lang="fortran">
</tab>
<tab>
<source lang="fortran">
  MPI_COMM_SIZE(COMM, NPROC, IERR)
  MPI_COMM_SIZE(COMM, NPROC, IERR)
  INTEGER :: COMM, NPROC, IERR
  INTEGER :: COMM, NPROC, IERR
Line 173: Line 173:
  INTEGER :: COMM, RANK, IERR
  INTEGER :: COMM, RANK, IERR
</source>
</source>
|}
</tab>
</tabs>
 


<tt>MPI_Comm_size</tt> reports the number of processes running as part of this job by assigning it to the result parameter <tt>nproc</tt>.  Similarly, <tt>MPI_Comm_rank</tt> reports the rank of the calling process to the result parameter <tt>myrank</tt>. Ranks in MPI start from 0 rather than 1, so given N processes we expect the ranks to be 0..(N-1). The <tt>comm</tt> argument is a ''communicator'', which is a set of processes capable of sending messages to one another. For the purpose of this tutorial we will always pass in the predefined value <tt>MPI_COMM_WORLD</tt>, which is simply all the MPI processes started with the job. It is possible to define and use your own communicators, but that is beyond the scope of this tutorial and the reader is referred to the provided references for additional detail.
<tt>MPI_Comm_size</tt> reports the number of processes running as part of this job by assigning it to the result parameter <tt>nproc</tt>.  Similarly, <tt>MPI_Comm_rank</tt> reports the rank of the calling process to the result parameter <tt>myrank</tt>. Ranks in MPI start from 0 rather than 1, so given N processes we expect the ranks to be 0..(N-1). The <tt>comm</tt> argument is a ''communicator'', which is a set of processes capable of sending messages to one another. For the purpose of this tutorial we will always pass in the predefined value <tt>MPI_COMM_WORLD</tt>, which is simply all the MPI processes started with the job. It is possible to define and use your own communicators, but that is beyond the scope of this tutorial and the reader is referred to the provided references for additional detail.
Line 179: Line 181:
Let us incorporate these functions into our program, and have each process output its rank and size information. Note that since all processes are still performing identical operations, there are no conditional blocks required in the code.
Let us incorporate these functions into our program, and have each process output its rank and size information. Note that since all processes are still performing identical operations, there are no conditional blocks required in the code.


{| border="0" cellpadding="5" cellspacing="0" align="center"
<tabs>
! style="background:#8AA8E5;" | '''C CODE''': <tt>phello1.c</tt>
<tab name="C">
! style="background:#ECCF98;" | '''FORTRAN CODE''': <tt>phello1.f</tt>
|-valign="top"
|
{{File
{{File
   |name=example1.c
   |name=example1.c
Line 206: Line 205:
  }
  }
}}
}}
|<source lang="fortran">
</tab>
<tab name="Fortran">
{{File
  |name=example1.f90
  |lang="fortran"
  |contents=
  program phello1
  program phello1
   
   
Line 222: Line 226:
   
   
  end program phello1
  end program phello1
</source>
}}
|}
</tab>
</tabs>
 


Compile and run this program using 2, 4 and 8 processes. Note that each running process produces output based on the values of its local variables. The stdout of all running processes is simply concatenated together. As you run the program using more processes, you may see that the output from the different processes does not appear in order or rank: You should make no assumptions about the order of output from different processes.
Compile and run this program using 2, 4 and 8 processes. Note that each running process produces output based on the values of its local variables. The stdout of all running processes is simply concatenated together. As you run the program using more processes, you may see that the output from the different processes does not appear in order or rank: You should make no assumptions about the order of output from different processes.
Bureaucrats, cc_docs_admin, cc_staff, rsnt_translations
2,837

edits

Navigation menu