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