program main !*****************************************************************************80 ! !! MAIN is the main program for TYPE. ! ! Discussion: ! ! TYPE demonstrates user-defined datatypes in MPI. ! ! The datatype defined will be a structure that contains three integers. ! ! Process 0 will set up an example of this structure, and send it ! to Proces 1, which will alter it and send it back. ! ! Modified: ! ! 18 October 2005 ! ! Reference: ! ! William Gropp, Ewing Lusk, Anthony Skjellum, ! Using MPI: Portable Parallel Programming with the ! Message-Passing Interface, ! Second Edition, ! MIT Press, 1999, ! ISBN: 0262571323. ! ! ! Fortran77 include file: ! include 'mpif.h' ! ! Fortran90 module: ! ! use mpi ! ! implicit none ! type point integer :: x integer :: y integer :: z end type integer dest integer i integer ierr integer master integer my_id integer num_procs type ( point ) :: my_point integer point_type integer source integer status(MPI_STATUS_SIZE) integer tag master = 0 ! ! Initialize MPI. ! call MPI_Init ( ierr ) ! ! Get the number of processes. ! call MPI_Comm_size ( MPI_COMM_WORLD, num_procs, ierr ) ! ! Get the individual process ID. ! call MPI_Comm_rank ( MPI_COMM_WORLD, my_id, ierr ) ! ! Print a message. ! if ( my_id == master ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TYPE - Master process:' write ( *, '(a)' ) ' FORTRAN90 version' write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' An MPI example program to set up and ' write ( *, '(a)' ) ' use an MPI datatype.' write ( *, '(a)' ) ' ' write ( *, '(a,i8)' ) ' The number of processes is ', num_procs end if write ( *, '(a)' ) ' ' write ( *, '(a,i8,a)' ) ' Process ', my_id, ' is active.' ! ! Define and commit the new datatype. ! call MPI_Type_contiguous ( 3, MPI_INTEGER, point_type, ierr ) call MPI_Type_commit ( point_type, ierr ) if ( my_id == master ) then my_point%x = 1 my_point%y = 2 my_point%z = 4 dest = 1 tag = 1 call MPI_Send ( my_point, 1, point_type, dest, tag, MPI_COMM_WORLD, ierr ) write ( *, '(a)' ) ' ' write ( *, '(a,i8,a,3i8)' ) ' Process ', my_id, & ' sent an item of type POINT_TYPE, with value ', & my_point%x, my_point%y, my_point%z source = 1 tag = 2 call MPI_Recv ( my_point, 1, point_type, source, tag, MPI_COMM_WORLD, & status, ierr ) write ( *, '(a,i8,a,3i8)' ) ' Process ', my_id, & ' received a modified item of type POINT_TYPE, with value ', & my_point%x, my_point%y, my_point%z else if ( my_id == 1 ) then source = 0 tag = 1 write ( *, '(a)' ) ' ' write ( *, '(a,i8,a,3i8)' ) ' Process ', my_id, & ' expecting an item of type POINT_TYPE.' call MPI_Recv ( my_point, 1, point_type, source, tag, MPI_COMM_WORLD, & status, ierr ) write ( *, '(a)' ) ' ' write ( *, '(a,i8,a,3i8)' ) ' Process ', my_id, & ' received an item of type POINT_TYPE, with value ', & my_point%x, my_point%y, my_point%z i = my_point%x my_point%x = my_point%z * 100 my_point%y = my_point%y * 10 my_point%z = i dest = 0 tag = 2 call MPI_Send ( my_point, 1, point_type, dest, tag, MPI_COMM_WORLD, ierr ) write ( *, '(a,i8,a,3i8)' ) ' Process ', my_id, & ' sent a modified item of type POINT_TYPE, with value ', & my_point%x, my_point%y, my_point%z else write ( *, '(a)' ) ' ' write ( *, '(a,i8,a)' ) ' Process ', my_id, & ': MPI has nothing for me to do!' end if ! ! Shut down MPI. ! call MPI_Finalize ( ierr ) if ( my_id == master ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TYPE - Master process:' write ( *, '(a)' ) ' Normal end of execution.' end if stop end