#include <stdio.h>
#include <mpi.h>
#include <malloc.h>
int main (int argc ,char** argv)
{
int rank,size,i,max,MAX;
int a;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
a = 1000/size;
int rbuf1[a];
int *sbuf = (int *) malloc(sizeof(int) * a * size);
int *rbuf2 = (int*)malloc(sizeof(int) * size);
if (rank == 0){
for(i=1;i<=a*size;i++){
sbuf[i-1] = i;
}}
MPI_Scatter(sbuf, a, MPI_INT, rbuf1, a, MPI_INT, 0, MPI_COMM_WORLD);
max = rbuf1[0];
for(i=1;i<a;i++)
if(rbuf1[i] > max)
max = rbuf1[i];
printf("Process %d 's max = %d\n",rank,max);
MPI_Gather(&max, 1, MPI_INT, rbuf2, 1, MPI_INT, 0, MPI_COMM_WORLD);
if(rank == 0){
MAX = rbuf2[0];
for(i=1;i<size;i++){
if(rbuf2[i]>MAX)
MAX = rbuf2[i];
}
printf("The Maximum = %d\n",MAX);
}
MPI_Finalize();
return 0;
}
MPI编程--求最大值
最新推荐文章于 2022-11-09 19:49:38 发布