芙蓉妹妹 - 云代码空间
——
#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_Scan(sbuf,rbuf,3,MPI_INT,MPI_SUM,comm); printf("process %d results:",rank); for(i = 0;i< 3;i++) printf(" %d",rbuf[i]); putchar('\n'); MPI_Finalize(); return 0; }功能:后一个进程的结果为自身的数值加上前一个计算所得