!3a. Copy and modify example2.f90 to find the nearest neighbor along y+ ! of each process. !----------------------------------------------------------------------- program main !------------------------------------------------------------------ ! WHO IS MY NEIGHBOR IN THE POSITIVE Y DIRECTION? ! Strategy 1: ! I.Find out what my process's coordinate parameters are [coords(ndims)] ! (Use MPI_CART_GET for this) ! II.Use MPI_CART_RANK to query for my neighbor's rank. !------------------------------------------------------------------ !-- All processes send a message to process 0 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 parameter (ndims=2) !dimension of the Cartesian grid integer ndims,dims(ndims) integer COMM_CART logical periods(ndims),reorder integer coords(ndims) integer maxdims integer nndims,ddims(ndims) integer r_coords(ndims) integer r_rank logical l_periods(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 ) ! print *, "Process ", my_rank, " of ", numprocs, " is alive" !--Create a Cartesian topology dims(1:ndims)=int(sqrt(float(numprocs))) ! 2x2 grid of sub-domains periods(1)=.true. ! periodic bdry. cond. along x periods(2)=.true. ! 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. | ! ---------------------------------------------------------------- ! --Given my rank (my_rank), find what my coordinate parameters on the block ! grid are. call MPI_CART_COORDS(COMM_CART,my_rank,ndims,coords,ierror) print *,"my_rank=",my_rank," my coordinates parameters, coords, are ",& & coords !--Who is my TOP neighbor? ! Well... Since my corrdinate parameters are [coords(1),coords(2)] ! the coordinate parameters of my TOP neighbor are ! [coords(1),coords(2)+1] ! unless I am at the "right" bdry and bdr. conditions are NON-periodic ! What I need is the rank of the process containing this block. ! =====> Use MPI_CART_RANK --- [ note period(1)=.true. !] r_coords(1)=coords(1) r_coords(2)=coords(2)+1 call MPI_CART_RANK(COMM_CART,r_coords,r_rank,ierror) print*,"my_rank=",my_rank," my TOP neighbor is: ",r_rank !--Finilize MPI call MPI_FINALIZE(irc) stop end