= Source code = {{{ /* * 每個 node 將訊息傳送給 node 0,由,node 0 統一印出 */ #include #include #include 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(); } }}} = Result = [[Image(demo1.png)]] = Related functions = [wiki:MPI_Comm_size]、[wiki:MPI_Comm_rank]。