mpi-cos(x) 的积分
实现代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "mpi.h"
#define PI 3.141593
int main(int argc,char** argv){
int rank,size;
double a,b;
a=0;
b=PI/2;
int p=size-1;
int n=10;
double h;
h=(b-a)/p/n;
MPI_Comm comm= MPI_COMM_WORLD;
MPI_Status status;
MPI_Init(&argc,&argv);
MPI_Comm_size(comm,&size);
MPI_Comm_rank(comm,&rank);
if(rank== size-1){
int j=0;
double fin=0;
double tem;
for(j=0;j<size-1;j++){
MPI_Recv(&tem,1,MPI_DOUBLE,j,99,comm,MPI_STATUS_IGNORE);
fin=fin+tem;
//printf("%d:%lf.\n",j,tem);
}
printf("cos(%f,%f):%f\n",a,b,fin);
}
else{
int j=0;
double part=0;
for(j=0;j<n;j++){
part= part+ cos(a+(rank*n+j)*h+h/2)*h;
}
MPI_Send(&part,1,MPI_DOUBLE,size-1,99,comm);
}
MPI_Finalize();
return 0;
}
结果截图: