wiki:mpich/point_to_point/demo4

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

--

Source code

/* Program:
 *   讓 node 0 可以接受來自任何 node 的訊息,每個 node 將訊息標上不同 tag 後傳給 node 0
 * History:
 *   2008-06-24 BETA
 */

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

main (int argc, char **argv)
{
  int numprocs, myrank, i=0, buf;
  MPI_Status status;
  MPI_Init(&argc, &argv);
  MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
  MPI_Comm_rank(MPI_COMM_WORLD, &myrank);

  /* 除了 Node 0 以外的所有 node 都要送 5 個訊息給 node 0 , 將 i 當成 tag 送出 */
  if (myrank > 0)
  {
    for(i = 0; i < 5; i++)
    {
      buf = myrank * 100 + i;
      MPI_Send(&buf, 1, MPI_INT, 0, i, MPI_COMM_WORLD);
    }
  }
  if (myrank == 0)
  {
    for (i = 0; i < 5*(numprocs-1); i++)
    {

      /* MPI_ANY_SOURCE 接收來自任何 node , MPI_ANY_TAG 接收來自任何 tag */
      MPI_Recv(&buf, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
      printf("[Node %d][Tag %d] => %d\n", status.MPI_SOURCE, status.MPI_TAG, buf);
    }
  }
  MPI_Finalize();
}

Result

Attachments (1)

Download all attachments as: .zip