program main !-- Create a Cartesian 2D topology and print out the ! coordinate parameters of each process. !--Include the mpi header file use mpi implicit none integer i,j,k,ierr,rank,my_rank,numprocs,itag,ierror integer status(MPI_STATUS_SIZE) integer irc parameter (ndims=2) !dimension of the cartesian grid integer ndims,dims(ndims) integer COMM_CART logical periods(ndims),reorder integer coords(ndims) integer count,tag, datatype,comm,source,nxb,nyb integer, parameter :: nx=4, ny=4 integer x_corner, y_corner real :: x(2,2), y(2,2),x0(4),y0(4) real, parameter :: dx=0.2, dy=0.2 ! The cell size is dx=dy=0.2 nxb=nx/ndims nyb=ny/ndims !--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 ) !--Create a cartesian topology dims(1:ndims)=2 ! 2x2 grid of sub-domains periods(1)=.true. ! periodic bdry. cond. along x periods(2)=.false. ! non-periodic bdry. cond. along y call MPI_CART_CREATE(MPI_COMM_WORLD,ndims,dims,periods, & reorder,COMM_CART,ierror) ! ---------------------------------------------------------------- ! | MPI_CART_CREATE will provide a 2D cartesian array of processes | ! ======>| of dimensions 2x2. | ! | Each process rank is associated with a given sub-domain. | ! ---------------------------------------------------------------- ! --Find and print the coordinate parameters of each process in the ! Cartesian topology. ! --the common calling arguments count = 2 datatype=MPI_REAL tag = 1 comm = MPI_COMM_WORLD if (my_rank == 0) then !build x,y matrices x0(:)=(/0.1, 0.3, 0.5, 0.7/) y0(:)=(/0.1, 0.3, 0.5, 0.7/) do i=1,numprocs rank=i-1 call MPI_CART_COORDS(COMM_CART,rank,2,coords,ierror) do j=1,nx x(j,:)=x0(coords(1)*nxb+j) y(:,j)=y0(coords(2)*nyb+j) end do !send the subportions call MPI_SEND(x(1),count,datatype, rank,tag,comm,ierr) call MPI_SEND(y(1),count,datatype, rank,tag,comm,ierr) end do endif !get the x,y subarrays source=0 call MPI_CART_COORDS(COMM_CART,my_rank,2,coords,ierror) call MPI_RECV(x(1),count,datatype, source,tag,comm,status, ierr) call MPI_RECV(y(1),count,datatype, source,tag,comm,status, ierr) write(*,"('Rank[',i2,'] Coords=[',2i2,'], x=[',4f4.1,'] y=[',4f4.1,']\n')" ) & my_rank, coords, x,y !--Finilize MPI call MPI_FINALIZE(irc) stop end