12.07.2015 Views

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

an mpi tutorial 605✞/ / MPIhello . c has each processor prints hello to screen#include "mpi .h"#include ☎int main ( int argc , char ∗argv [] ) {int myrank ;MPI_Init ( &argc , &argv ) ; // Initialize MPIMPI_Comm_rank ( MPI_COMM_WORLD, &myrank ) ; / / Get CPU’ s rankprintf ( "Hello World from processor %d\n" , myrank ) ;MPI_Finalize ( ) ; // Finalize MPIreturn 0;}✝Listing D.3 MPIhello.c gets each processor to say hello via MPI.D.3.1 MPIhello.c ExplainedHere is what is contained in MPIhello.c:• The inclusion of MPI headers via the #include "mpi.h" statement on lines2–3. These are short files that assist the C compiler by telling it the type of argumentsthat MPI functions use for input and output without giving any detailsabout the functions. (In Fortran we used include "/usr/local/cluster/mpich-2.1.6/include/mpif.h" after the program line.)• The main method is declared with an int main(int argc, char *argv[]) statement,where argc is a pointer to the number of arguments and argv is apointer to the argument vector passed to main when you run the programfrom a shell. (Pointers are variable types that give the locations in memorywhere the values of the variables reside rather than the variables’ actual values.)These arguments are passed to MPI to tell it how many processors youdesire.• The int myrank statement declares the variable myrank, which stands forthe rank of the computer. Each processor running the program is assigneda unique number called its rank by MPI. This is how you tell the differenceamong identical programs running on different CPUs.• The processor that executes the highest level of the program is called the hostor master, and all other machines are called guests or slaves. The host always hasmyrank =0, while all the other processors, based on who responds first, havetheir processor numbers assigned to myrank. This means that myrank =1forthe first guest to respond, 2 for the second, and so on. Giving each processora unique value for myrank is a critical element in parallel processing.• The MPI_init() and MPI_Finalize() commands in MPIhello.c initialize andterminate MPI, respectively.All MPI programs must have these lines, with theMPI commands always placed between them. The MPI_Init(&argv, &argc)function call takes two arguments, both beginning with a & that indicates apointer. These arguments are used for communication between the operatingsystem and MPI.−101<strong>COPYRIGHT</strong> <strong>2008</strong>, PRINCET O N UNIVE R S I T Y P R E S SEVALUATION COPY ONLY. NOT FOR USE IN COURSES.ALLpup_06.04 — <strong>2008</strong>/2/15 — Page 605

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!