program nasa_ex2 !-- Template for any mpi program ! Starting from template.f, write a program that given a !common value of x (e.g. x=5 in all processes), computes: ! * y=x2 in process 0 ! * y=x3 in process 1 ! * y=x4 in process 2 ! and writes a statement from each process that identifies the !process and reports the values of x and y from that process. !--Include the mpi header file use mpi implicit none ! highly recommended. It will make ! debugging infinitely easier. !--Declare all variables and arrays. integer :: ierr,myid,numprocs,itag integer :: irc, comm real :: x,y,z !--Initialize MPI call MPI_INIT( ierr ) ! --> Required statement !--Who am I? --- get my rank=myid call MPI_COMM_RANK( MPI_COMM_WORLD, myid, ierr ) comm = MPI_COMM_WORLD call system( 'sleep 5' ) !--How many processes in the global group? call MPI_COMM_SIZE( MPI_COMM_WORLD, numprocs, ierr ) !set the value of x on root processor, if (myid == 0) x=5 !then broadcast to all procs call MPI_BCAST(x,1,mpi_real, 0,comm,ierr) ! compute based which proc I am if (myid == 0) then y=x**2 ! call MPI_BCAST(x,1,mpi_real, 0,comm,ierr) else if (myid == 1) then ! call MPI_BCAST(z,1,mpi_real, 0,comm,ierr) y=x**3 else if (myid == 2) then ! call MPI_BCAST(z,1,mpi_real, 0,comm,ierr) y=x**4 end if !-- print y write(*,*)'On process ',myid,' x=',x,', y=',y,' z=',z !--Finalize MPI call MPI_FINALIZE(irc) ! ---> Required statement stop end