芙蓉妹妹 - 云代码空间
——
#include <stdio.h> #include "mpi.h" #include <malloc.h> int main(int argc, char ** argv){ int rank,size,rbuf[3],sbuf[3],i; MPI_Comm comm = MPI_COMM_WORLD; MPI_Status status; MPI_Init(&argc,&argv); MPI_Comm_size(comm, &size); MPI_Comm_rank(comm, &rank); printf("Process %d has:",rank); for(i = 0;i< 3;i++){ sbuf[i] = rank*10 + i; printf(" %d",sbuf[i]); } putchar('\n'); MPI_Allreduce(sbuf,rbuf,3,MPI_INT,MPI_SUM,comm); printf("process %d :total sum =:",rank); for(i = 0;i< 3;i++) printf(" %d",rbuf[i]); putchar('\n'); MPI_Finalize(); return 0; }与Reduce的区别在于: