wiki:mpich/point_to_point/demo1

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

--

程式碼

/*
 * 每個 node 將訊息傳送給 node 0,由,node 0 統一印出
 */

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

main(int argc, char **argv)
{
  int myrank, i, numprocs;
  char message[20];
  MPI_Status status;
  MPI_Init(&argc, &argv);
  MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
  MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
  if(myrank == 0)
  {
    printf("I am %d \n", myrank);
    for(i = 1; i < (numprocs); i++)
    {
      MPI_Recv(message, 20, MPI_CHAR, i, 99, MPI_COMM_WORLD, &status);
      printf(" I GOT %s \n", message);
    }
    printf("\n");
  }
  if(myrank != 0)
  {
    sprintf(message, "i am %d", myrank);
    MPI_Send(message, 20, MPI_CHAR, 0, 99, MPI_COMM_WORLD);
    printf("I am %d \n", myrank);
    printf("I send '%s\n\n ", message);
  }
  MPI_Finalize();
}

結果:
[[Image:demo1.png]]
[[Image(demo1.png)]]

Attachments (1)

Download all attachments as: .zip