wiki:mpich/point_to_point/demo2

Version 5 (modified by wade, 16 years ago) (diff)

--

/* Program:
 *   每個 node 將自己的 id 資訊送給下一個 node。
 * History:
 *   2008-06-05 BETA
 *   2008-06-06 改變 #30 printf 位置,以了解 MPI 函式執行順序關係
 *   2008-06-12 更改顯示方法。
 */

#include <mpi.h>
#include <stdio.h>

int main(int argc,char *argv[])
{
  int n, myid, numprocs;
  MPI_Status status;
  MPI_Init(&argc,&argv);
  MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
  MPI_Comm_rank(MPI_COMM_WORLD,&myid);

  /* node 0 will send the first message */
  if(myid==0)
  {
    n = myid;
    MPI_Send(&n, 1, MPI_INT, 1, 99, MPI_COMM_WORLD);
    printf("[Ndde %d]「%d」 >> [Node %d]\n\n", myid, n, myid+1);
  }

  /* node 1 to node n-2 will send message to the next node */
  if(myid>0 && myid<numprocs-1)
  {
    MPI_Recv(&n, 1, MPI_INT, myid-1, 99, MPI_COMM_WORLD, &status);
    printf("[Node %d] << 「%d」[Node %d]\n", myid, n, status.MPI_SOURCE);
    n = myid;
    MPI_Send(&n, 1, MPI_INT, myid+1, 99, MPI_COMM_WORLD);
    printf("[Ndde %d]「%d」 >> [Node %d]\n\n", myid, n, myid+1);
  }

  /* the final node n-1 will not send any message but receive*/
  if(myid==numprocs-1)
  {
    MPI_Recv(&n, 1, MPI_INT, myid-1, 99, MPI_COMM_WORLD, &status);
    printf("[Node %d] << 「%d」[Node %d]\n", myid, n, status.MPI_SOURCE);
  }
  MPI_Finalize();
 return 0;

Attachments (1)

Download all attachments as: .zip