next up previous index
Next: Dividing the Pie Up: Fortran Interface Previous: Hello World

Greetings, Master

gustav@sp21:../MPI 18:33:06 !625 $ cat hello-2.f
PROGRAM hello_master

  IMPLICIT NONE

  INCLUDE "mpif.h"

  INTEGER :: ierror, result_len, pool_size, count, my_rank
  INTEGER, PARAMETER :: master_rank = 0, bufsiz = 512
  INTEGER, DIMENSION(MPI_STATUS_SIZE) :: status
  CHARACTER(len=bufsiz) :: my_name = " ", master_name = " ", &
       send_buffer = " ", my_rank_string = " ", recv_buffer = " "
  LOGICAL :: i_am_the_master = .FALSE.

  CALL mpi_init(ierror)
  CALL mpi_comm_size(MPI_COMM_WORLD, pool_size, ierror)
  CALL mpi_comm_rank(MPI_COMM_WORLD, my_rank, ierror)
  CALL mpi_get_processor_name(my_name, result_len, ierror)

  IF (my_rank .EQ. master_rank) THEN
     i_am_the_master = .TRUE.
     master_name = my_name
  END IF

  CALL mpi_bcast(master_name, bufsiz, MPI_CHARACTER, master_rank, &
       MPI_COMM_WORLD, ierror)

  WRITE(my_rank_string, '(1i3)') my_rank
  send_buffer = 'hello ' // TRIM(master_name) &
       // ', greetings from ' // my_name // ', rank = ' &
       // my_rank_string
  CALL mpi_send(send_buffer, bufsiz, MPI_CHARACTER, master_rank, 0, &
       MPI_COMM_WORLD, ierror)

  IF (i_am_the_master) THEN
     DO count = 1, pool_size
        CALL mpi_recv (recv_buffer, bufsiz, MPI_CHARACTER, MPI_ANY_SOURCE, &
             MPI_ANY_TAG, MPI_COMM_WORLD, status, ierror)
        WRITE(*,*) TRIM(recv_buffer)
     END DO
  END IF

  CALL mpi_finalize(ierror)

END PROGRAM hello_master
gustav@sp21:../MPI 18:34:34 !626 $ mpxlf90 -o hello-2 hello-2.f
** hello_master   === End of Compilation 1 ===
1501-510  Compilation successful for file hello-2.f.
gustav@sp21:../MPI 18:34:46 !627 $ poe hello-2 -procs 8
 hello sp17.ucs.indiana.edu, greetings from sp17.ucs.indiana.edu
 hello sp17.ucs.indiana.edu, greetings from sp22.ucs.indiana.edu
 hello sp17.ucs.indiana.edu, greetings from sp20.ucs.indiana.edu
 hello sp17.ucs.indiana.edu, greetings from sp19.ucs.indiana.edu
 hello sp17.ucs.indiana.edu, greetings from sp24.ucs.indiana.edu
 hello sp17.ucs.indiana.edu, greetings from sp21.ucs.indiana.edu
 hello sp17.ucs.indiana.edu, greetings from sp23.ucs.indiana.edu
 hello sp17.ucs.indiana.edu, greetings from sp40.ucs.indiana.edu
gustav@sp21:../MPI 18:35:02 !628 $



Zdzislaw Meglicki
2001-02-26