Skip to content

Instantly share code, notes, and snippets.

@yudanta
Created December 16, 2013 02:23
Show Gist options
  • Save yudanta/7981504 to your computer and use it in GitHub Desktop.
Save yudanta/7981504 to your computer and use it in GitHub Desktop.
compiling : $ mpicc parallel_integral.c -o pararel testing : $ ./pararel 1 2 3
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>
#include <mpi.h>
double f(double a){
return (a*exp(2*a));
//return a;
}
int main(int argc,char *argv[])
{
int i, x0, xn, jumpros, idpros;
double n, h, sum, xa, xb, total;
double startwtime, endwtime;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&jumpros);
MPI_Comm_rank(MPI_COMM_WORLD,&idpros);
x0 = atoi ( argv[1]);
xn = atoi ( argv[2]);
n = atoi ( argv[3]);
startwtime = MPI_Wtime();
MPI_Bcast (&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
for (i = idpros; i <=n-1; i+=jumpros)
{
h= (xn-x0)/ (double) n;
sum = 0.0;
xa = x0 + i * h;
xb = x0 + (i+1) * h;
sum= sum + 0.5 * h * (f(xa)+f(xb));
}
MPI_Reduce(&sum, &total, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
endwtime = MPI_Wtime();
printf("Hasil Integral = %.16f\n",total);
printf("Waktu Eksekusi = %f\n", endwtime-startwtime);
MPI_Finalize();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment