芙蓉妹妹 - 云代码空间
——
#include<stdio.h> #include"mpi.h" int main(int argc, char **argv) { MPI_Comm comm = MPI_COMM_WORLD; int size, rank, num = 0; MPI_Status status; MPI_Init(&argc,&argv); MPI_Comm_size(comm,&size); MPI_Comm_rank(comm,&rank); while(num < 9) { if(rank == 0) { if(num == 0) { MPI_Send(&num,1,MPI_INT,1,99,comm); printf("Process 0 send %d to process 1.\n",num); } MPI_Recv(&num,1,MPI_INT,1,99,comm,&status); num += 1; MPI_Send(&num,1,MPI_INT,1,99,comm); printf("Process 0 send %d to process 1.\n",num); } else if (rank == 1) { MPI_Recv(&num,1,MPI_INT,0,99,comm,&status); num += 1; MPI_Send(&num,1,MPI_INT,0,99,comm); printf("Process 1 send %d to process 0.\n",num); } } MPI_Finalize(); return 0; }