Version 1 (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(); } 結果:
Attachments (1)
- demo1-01.png (75.8 KB) - added by wade 16 years ago.
Download all attachments as: .zip