[Santa Clara University]
Department of Mathematics
and Computer Science

Math 169

MPI Information

D. C. Smolarski, S.J.

[Return to Math 169 Homepage | Return to Supplement Listing Page]

Contents


MPI on the HP Unix Server

Before creating and executing an MPI program, you must modify your Unix environment carefully to allow the use of MPI on a sequential machine.
  1. First, in your root directory, you need to create a file named .rhosts (note the initial dot as part of the filename). The file should have one line in it with the node name of math followed by your username, e.g.,
              math.scu.edu    dsmolars
    
    This file should have access mode 600. To make sure it has the correct permissions, after saving the file, enter the system command:
              chmod 600 .rhosts
    
  2. Next, edit your .cshrc startup file in your root directory if your shell is csh or tcsh. Comment the line
              set path=( $path . )
    
    out by putting a # as the first character in the line and add the following line
              set path=( $path /usr/local/lib/mpich/bin . )
    
    If, however, your shell is bash or sh (i.e., Posix), edit your .profile startup file in your root directory. Comment the line
              PATH=$PATH:.
    
    out by putting a # as the first character in the line and add the following line
              PATH=$PATH:/usr/local/lib/mpich/bin:.
    
For each program, one does the following:
  1. Create a source code (program) file using an editor of your choice (e.g. Emacs or vi) in the language of your choice, C, C++, or F90. The extension should be appropriate to the language.
  2. After the source code file is created, execution of the code is via a modified Unix sequence of steps that appears to be versions of standard compilers. In reality, these are script files that invoke standard compilers.

    The options are

    mpif90 for Fortran-90 source files
    mpif77 for Fortran-77 source files
    mpicc for C source files
    mpiCC for C++ source files
    Using these compiler scripts will produce an a.out as is usual by default. Usually, a user will use the standard -o filename switch to produce a default executable with another filename. This executable file is not used by itself (although it will produce output). Instead, one uses a execution driver script, mpirun with an option -np indicating the number of "processors" one wishes to emulate. E.g.,
                   mpicc mpitest.c
    	       mpirun -np 4 a.out
    
    or
                   mpicc -o mpitest mpitest.c
    	       mpirun -np 4 mpitest
    

Sample C/MPI Source Code

/* synch_ex.c
 *
 * Illustrates the fact that sequences of broadcasts on different 
 *     processes (that use the same communicator) are matched by 
 *     their order.
 *
 * Input: None
 * Output: Values of x and y on each process
 *
 * Note:  Should be run with at least three processes.
 *
 * See pp. 72-73 of PPMPI.
 */
#include <stdio.h>
#include <unistd.h> /* needed for using the "sleep" function */
#include "mpi.h"  /* needed to call MPI functions */

main(int argc, char* argv[]) {
    int x = 0;
    int y = 0;
    int my_rank;

    MPI_Init(&argc, &argv); /* this must be the first MPI function called */
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
               /* this tells MPI all the processes running and
	          returns the number of the local process */
    if (my_rank == 0) {
        x = 5;
        y = 10;
        MPI_Bcast(&x, 1, MPI_INT, 0, MPI_COMM_WORLD);
        MPI_Bcast(&y, 1, MPI_INT, 0, MPI_COMM_WORLD); 

        /* Local work (call to sleep isn't really needed) */
        sleep(2);
    } else if (my_rank == 1) {
        /* Local work (call to sleep isn't really needed) */
        sleep(2);

        MPI_Bcast(&y, 1, MPI_INT, 0, MPI_COMM_WORLD);
        MPI_Bcast(&x, 1, MPI_INT, 0, MPI_COMM_WORLD);
    } else if (my_rank == 2) {
        /* Local work (call to sleep isn't really needed) */
        sleep(2); 

        MPI_Bcast(&x, 1, MPI_INT, 0, MPI_COMM_WORLD);
        MPI_Bcast(&y, 1, MPI_INT, 0, MPI_COMM_WORLD); 
    }

    printf("Process %d > x = %d, y = %d\n", my_rank, x, y);
    
    MPI_Finalize(); /* this must be the LAST MPI function called */
}

Sample Execution

math 21: script
Script started on Fri Apr 28 21:34:37 2000
math 22: mpicc mpitest.c
/usr/ccs/bin/ld: (Warning) At least one PA 2.0 object file (/usr/local/lib/mpich-1.2.0/lib/libmpich.a(initfcmn.o)) was detected. The linked output may not run on a PA 1.x system.
math 23: mpirun -np 4 a.out
Process 0 > x = 5, y = 10
Process 2 > x = 5, y = 10
Process 3 > x = 0, y = 0
Process 1 > x = 10, y = 5
math 24: exit
exit

script done on Fri Apr 28 21:35:37 2000


My email address is dsmolarski@math.scu.edu

This page last updated 6 May 2000.