wiki:mpich/point_to_point/demo2

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

--

/* Program:
 *   每個 node 將自己的 id 送給下一個 node。
 * History:
 *   2008-06-05 BETA
 */

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

int main(int argc,char *argv[])
{
  int n=0, myid, numprocs, i=0;
  MPI_Status status;
  MPI_Init(&argc,&argv);
  MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
  MPI_Comm_rank(MPI_COMM_WORLD,&myid);
  if(myid==0)
  {
    MPI_Send(&n, 1, MPI_INT, 1, 99, MPI_COMM_WORLD);
  }
  if(myid!=0)
  {
    MPI_Recv(&n, 1, MPI_INT, myid-1, 99, MPI_COMM_WORLD, &status);
    n++;

    if(myid<numprocs-1)

    {

      MPI_Send(&n, 1, MPI_INT, myid+1, 99, MPI_COMM_WORLD);

      printf("I am node %d\nI recive number %d from %d\n", myid, n, myid-1);
      printf("I send number %d to %d\n\n", n, myid+1);

    }

  }

  MPI_Finalize();

 return 0;

}

Attachments (1)

Download all attachments as: .zip