Skip to content

Instantly share code, notes, and snippets.

@yudanta
Created December 16, 2013 02:01
Show Gist options
  • Save yudanta/7981346 to your computer and use it in GitHub Desktop.
Save yudanta/7981346 to your computer and use it in GitHub Desktop.
example
/* Program ini membandingkan estimasi trapezoidal rule integral dari F(x)= exp (x)
Program ini memerlukan panggilan berikut :
-MPI_Init
-MPI_Comm_rank
-MPI_Comm_size
-MPI_Finalize
Program Membutuhkan Input:
1. batas kiri integral
2. batas kanan integral
3. jumlah n trapezoidal
Hasil: Perkiraan nilai integral dari kiri ke kanan pada fungsi f(x) menggunakan trapezoidal rule and n trapezoids.
Algorithm:
1. Each process calculates "its" interval of integration.
2. Each process estimates the integral of f(x) over its interval using the trapezoidal rule.
3a. Each process != 0 sends its integral to 0.
3b. Process 0 sums the calculations received from the individual processes and prints the result.
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <mpi.h>
int main(int argc, char** argv) {
int my_rank; /* proces rank */
int p; /* Jumlah proses */
float a; /* Batas Kiri */
float b; /* Batas Kanan */
int n; /* Jumlah trapezoids */
float h;
float local_a;
float local_b;
int local_n;
double t1,t2;
float integral,serial;
float total; /* Total integral */
int source;
int dest = 0;
int tag = 0;
MPI_Status status;
printf("Masukkan batas kiri, batas kanan dan jumlah trapezoida sebanyak N \n");
scanf("%f %f %d", &a, &b, &n);
float seri(float local_a, float local_b, int local_n); /* Menghitung dengan satu proses */
float Trap(float local_a, float local_b, int local_n, float h); /* Menghitung local integral */
/* Inisialisasi MPI */
MPI_Init(&argc, &argv);
/* Mendapatkan peringkat proces */
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
/* Mendapatkan banyaknya procesyang sedang digunakan */
MPI_Comm_size(MPI_COMM_WORLD, &p);
printf("\nPerhitungan trapezoida dengan jumlah proses = %d\n", p);
t1 = MPI_Wtime();
seri(a,b,n);
h = (b-a)/n; /* h untuk semua proses */
local_n = n/p; /* Jumlah trapezoids = Jumlah Trapezoid / Jumlah proses */
/* interval masing-masing proses: */
local_a = a + my_rank*local_n*h;
local_b = local_a + local_n*h;
integral = Trap(local_a, local_b, local_n, h);
/* Manambahkan masing-masing hasil perhitungan proses integral */
if (my_rank == 0) {
total = integral;
for (source = 1; source < p; source++) {
MPI_Recv(&integral, 1, MPI_FLOAT, source, tag,
MPI_COMM_WORLD, &status);
total = total + integral;}
}
else {
MPI_Send(&integral, 1, MPI_FLOAT, dest,
tag, MPI_COMM_WORLD);
}
/* Mencetak Hasil */
if (my_rank == 0) {
printf("Dengan Jumlah trapezoida N = %d\n",n);
printf("Maka Integral dari %f sampai %f = %f\n", a, b, total);
}
t2 = MPI_Wtime();
printf( "Elapsed time is %f\n", t2 - t1 );
/* Mematikan MPI */
MPI_Finalize();
} /* Akhir dari main */
float Trap(float local_a /* in */, float local_b /* in */, int local_n /* in */, float h /* in */)
{
float integral; /* Menyimpan nilai integral */
float x;
int i;
float f(float x); /* Memanggil fungsi integral */
integral = (f(local_a) + f(local_b))/2.0;
x = local_a;
for (i = 1; i <= local_n-1; i++) {
x = x + h;
integral = integral + f(x);
}
integral = integral*h;
return integral;
} /* Akhir dari fungsi Trap */
float seri(float a,float b, int n)
{
float luas; /* Menyimpan nilai integral */
float x,h;
int i;
float f(float x); /* Memanggil fungsi integral */
h = (b-a)/n;
luas = (f(a) + f(b))/2.0;
x = a;
for (i = 1; i <= n-1; i++) {
x = x + h;
luas = luas + f(x);
}
luas = luas*h;
printf("\nPerhitungan trapezoida dengan satu proses = %f\n",luas);
return luas;
} /* Akhir dari fungsi seri */
float f(float x) {
float return_val;
/* Menghitung nilai f(x). */
/* Menghitung kemudian mengembalikan nilai return_val. */
return_val = x*x;
return return_val;
} /* f */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment