#include #include float fn(double x) { return x*x; } float getAns(double a,double b,double h) { float to_return = 0.0; while(a<=b) { to_return += fn(a); a = a + h; } return to_return; } int main(int argc,char *argv[]) { int rank,size,itr; float a,b,n,h; float global,local; a = 1; b = 10; n = 10000; double msg; 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*getAns(a+(rank*n*h)/size,a+((rank+1)*n*h)/size,h); MPI_Reduce(&local,&global,1,MPI_FLOAT,MPI_SUM,0,MPI_COMM_WORLD); if(rank == 0) { printf("The ans is %f",global); } MPI_Finalize(); return 0; }