原理:
与计算cosx相似,通过将数组分成若干份,分配给子进程计算,最后通过根进程收集并处理输出。
代码如下:
#include <stdio.h>
#include "mpi.h"
#include <string.h>
#include <math.h>
#define PI 3.1415926
#define N 2000000
int rand_array[N];
int get_max_num(int start_num,int end_num)
{
int max = -1;
for(int p = start_num; p < end_num; p++)
{
if(rand_array[p] > max)
max = rand_array[p];
}
return max;
}
int get_final_max_num(int x1,int x2,int x3,int x4)
{
int max = x1;
if(x2 > max)
max = x2;
if(x3 > max)
max = x3;
if(x4 > max)
max = x4;
return max;
}
int main(int argc,char **argv)
{
for(int j = 0; j < N; j++)
{
rand_array[j] = rand();
}
MPI_Comm comm = MPI_COMM_WORLD;
MPI_Status status;
int size,rank,dest;
char str[100];
MPI_Init(&argc,&argv);
MPI_Comm_size(comm,&size);
MPI_Comm_rank(comm,&rank);
if(rank==0)
{
int max,max1,max2,max3,max4;
MPI_Recv(&max1,1,MPI_INT,1,1,comm,&status);
MPI_Recv(&max2,1,MPI_INT,2,2,comm,&status);
MPI_Recv(&max3,1,MPI_INT,3,3,comm,&status);
MPI_Recv(&max4,1,MPI_INT,4,4,comm,&status);
max = get_final_max_num(max1,max2,max3,max4);
printf("the max num in array is : %d\n",max);
}
else if(rank==1)
{
int max = get_max_num(0,500000);
printf("i am rank1 , my max num is %d\n",max);
MPI_Send(&max,1,MPI_INT,0,1,comm);
}
else if(rank==2)
{
int max = get_max_num(500001,1000000);
MPI_Send(&max,1,MPI_INT,0,2,comm);
printf("i am rank2 , my max num is %d\n",max);
}
else if(rank==3)
{
int max = get_max_num(1000001,1500000);
MPI_Send(&max,1,MPI_INT,0,3,comm);
printf("i am rank3 , my max num is %d\n",max);
}
else if(rank==4)
{
int max = get_max_num(150001,2000000);
MPI_Send(&max,1,MPI_INT,0,4,comm);
printf("i am rank4 , my max num is %d\n",max);
}
MPI_Finalize();
return 0;
}