if(my_rank != 0) { //其他进程向0号进程发消息 sprintf(greeting,"Greetings from process %d of %d!",my_rank,comm_sz); MPI_Send(greeting,strlen(greeting)+1,MPI_CHAR,0,0,MPI_COMM_WORLD); } else { printf("Greetings from process %d of %d!\n",my_rank,comm_sz); //0号进程接受来自其他进程的消息并输出 for(int q=1;q<comm_sz;q++) { MPI_Recv(greeting,MAX_STRING,MPI_CHAR,q,0,MPI_COMM_WORLD,&status); printf("%s\n",greeting); } } MPI_Finalize();
return0; }
程序运行结果
1 2 3 4
Greetings from process 0 of 4! Greetings from process 1 of 4! Greetings from process 2 of 4! Greetings from process 3 of 4!