芙蓉妹妹 - 云代码空间
——
#include<stdio.h> #include "mpi.h" int main(int argc, char** argv) { MPI_Init(&argc, &argv); MPI_Comm comm = MPI_COMM_WORLD; int rank, size,sbuf[3], *rbuf, i; MPI_Comm_rank(comm, &rank); MPI_Comm_size(comm, &size); MPI_Status status; for(i = 0;i< 3; i++) sbuf[i] = rank*10 + i; if(rank == 0) rbuf = (int*)malloc(sizeof(int)*3*size); MPI_Gather(sbuf,3,MPI_INT,rbuf,3,MPI_INT,0,comm); if(rank ==0){ printf("process 0 recv :"); for(i = 0; i< 3*size; i++) printf(" %d",rbuf[i]); putchar('\n'); } MPI_Finalize(); return 0; }MPI_Gather(sbuf,3,MPI_INT,rbuf,3,MPI_INT,0,comm);