wiki:mpich/point_to_point/demo2

Version 2 (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;

}

有問題,為什麼 node 0 沒有 send 任何 message ,而其它的 node 卻能執行。怒…。

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

int main(int argc,char *argv[])
{
  int n, 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);

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

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

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

{{{

}}}

Attachments (1)

Download all attachments as: .zip