program main !-- On a 1D grid of 4 blocks. Each block updates its guard ! cell (v0) with data from its left neighbor. implicit none !--Include the mpi header file include 'mpif.h' integer i,ierr,my_rank,numprocs,itag integer message,mess_received,iroot_pe,itag,icount,ierror integer status(MPI_STATUS_SIZE) integer irc integer sendtag,recvtag parameter (ndims=1) !dimension of the cartesian grid integer ndims,dims(ndims) logical periods(ndims),reorder integer comm_cart integer coords(ndims),neig_coords(ndims) integer direction,disp,source,dest integer MPI_PROC_NULL real vel(0:2),v0 !--Initialize MPI call MPI_INIT( ierr ) !--Who am I? --- get my rank=my_rank call MPI_COMM_RANK( MPI_COMM_WORLD, my_rank, ierr ) !--How many processes in the global group? call MPI_COMM_SIZE( MPI_COMM_WORLD, numprocs, ierr ) print *, "Process ", my_rank, " of ", numprocs, " is alive" !--Create a cartesian topology dims(1:ndims)=4 periods=.true. reorder=.false. call MPI_CART_CREATE(MPI_COMM_WORLD,ndims,dims,periods, & & reorder,comm_cart,ierror) !--Find rank of left and right (along x) blocks direction=0 !(along x) disp=1 !immediate neighbors call MPI_CART_SHIFT(comm_cart,direction,disp,source, & & dest,ierror) write(*,*)'my_rank=',my_rank,' source=',source,' dest=',dest !--Am I at a physical boundary? if (dest==MPI_PROC_NULL) & write(*,*)'dest=MPI_PROC_NULL at proc ',my_rank if (source==MPI_PROC_NULL) & write(*,*)'source=MPI_PROC_NULL at proc ',my_rank !--Find the coordinate parameters of this block call MPI_CART_COORDS(comm_cart,my_rank,ndims,coords,ierror) !--Assign some values to vel(0:2) do i=0,2 vel(i)=float(i+100*coords(1)) end do !--Send v(2) to the block to my right and receive v0 from my left sendtag=1 recvtag=1 call MPI_SENDRECV(vel(2),1,MPI_REAL,dest,sendtag, & v0,1,MPI_REAL,source,recvtag, & comm_cart,status,ierror) !--Print out the updated v0 write(*,*)'my coords are: ',coords,' v0= ',v0 !--Finilize MPI call MPI_FINALIZE(irc) stop end