#include #include float fn(float x) { return x*x; } float compute(float a,float b,float h) { int cnt = 0; float to_return = 0.0; while(a<=b) { if(cnt%2) to_return += 4*fn(a); else to_return += 2*fn(a); cnt++; a += h; } return to_return; } int main(int argc,char *argv[]) { int size,rank; float a,b,n,h; float local,global; a = 1; b = 3; n = 10000; MPI_Status status; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&size); MPI_Comm_rank(MPI_COMM_WORLD,&rank); h = (b-a)/n; local = (h*compute(a+(rank*n*h)/size,a+((rank+1)*n*h)/size,h))/3.0; //printf("%f %f\n",h,local); MPI_Reduce(&local,&global,1,MPI_FLOAT,MPI_SUM,0,MPI_COMM_WORLD); if(rank == 0) { printf("%f",global); } MPI_Finalize(); return 0; }