Bu Blogda Ara

7 Şubat 2010 Pazar

Estimate of the integral f(x) using MPI parallel programming 3

/*
* Author : Emin Y�ce
* Input:
* n: number of trapezoids.
* Output: Estimate of the integral from 1 to 500 of f(x) = 6x^6-7x^5+3x^2+11
* using the trapezoidal rule and n trapezoids.
*
*/
#include
#include "mpi.h"

main(int argc, char** argv) {
int my_rank; /* My process rank */
int p; /* The number of processes */
float a = 1; /* Left endpoint */
float b = 500; /* Right endpoint */
int n; /* Number of trapezoids */
float h; /* Trapezoid base length */
float local_a; /* Left endpoint my process */
float local_b; /* Right endpoint my process */
int local_n; /* Number of trapezoids for */
/* my calculation */
float integral; /* Integral over my interval */
float total; /* Total integral */
int source; /* Process sending integral */
int dest = 0; /* All messages go to 0 */
int tag = 0;
MPI_Status status;

void Send_Trapezoid(int* n_ptr, int my_rank);
float Calculate_Integral(float local_a, float local_b, int local_n,
float h); /* Calculate local integral */

/* Let the system do what it needs to start up MPI */
MPI_Init(&argc, &argv);

/* Get my process rank */
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

/* Find out how many processes are being used */
MPI_Comm_size(MPI_COMM_WORLD, &p);

Send_Trapezoid(&n, my_rank);

h = (b-a)/n; /* h is the same for all processes */
local_n = n/p; /* So is the number of trapezoids */

/* Length of each process' interval of
* integration = local_n*h. So my interval
* starts at: */
local_a = a + my_rank*local_n*h;
local_b = local_a + local_n*h;
integral = Calculate_Integral(local_a, local_b, local_n, h);

/* Add up the integrals calculated by each process */
MPI_Reduce(&integral, &total, 1, MPI_FLOAT,
MPI_SUM, 0, MPI_COMM_WORLD);

/*PE0 displays the global sums on the screen*/
if (my_rank == 0) {

printf("\nn = %d trapezoids\n", n);
printf("p = %d processor number\n",p);
printf("Send each processor %d trapezoid by using MPI_Bcast and calculate its local integration\n",n/p);
printf("Collect local computations at process 0 by using MPI_Reduce\n");
printf("Inform global sum to the all other processes by using MPI_Bcast\n");
printf("The integral of 6x^6-7x^5+3x^2+11 function from %f to %f is %f\n", a, b, total);

}

/* PE0 sends the total integral value to other PEs using MPI_Bcast*/
MPI_Bcast(&total, 1, MPI_INT, 0, MPI_COMM_WORLD);

/* Print the total integral value in each PEs */
printf("my rank: %d total integral = %f\n",my_rank,total);

/* Shut down MPI */
MPI_Finalize();
} /* main */


/********************************************************************/
void Send_Trapezoid(int* n_ptr /* out */,int my_rank /* in */) {
/* Reads in the user input number of trapezoids.
*/
if (my_rank == 0) {
printf("The Program is written to calculate integral of f(x) = 6x^6-7x^5+3x^2+11 by using parallel computing \n");
printf("Enter number of trapezoids\n");
scanf("%d",n_ptr);
}
MPI_Bcast(n_ptr, 1, MPI_INT, 0, MPI_COMM_WORLD);
} /* Get_data2 */


/********************************************************************/
float Calculate_Integral(float local_a /* in */,float local_b /* in */,int local_n /* in */,float h /* in */) {
float integral; /* Store result in integral */
float x;
int i;

float f(float x); /* function we're integrating */

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;
} /* Calculate_Integral */


/********************************************************************/
float f(float x) {
/*
return the value of the function for given x parameter
*/
float return_val;
float power (float base, int n); // the function that calculate power of given number

/* Calculate f(x). */
/* Store calculation in return_val. */
return_val = 6 * power(x,6) - 7 * power(x,5) + 3 * power(x,2) + 11;
return return_val;
} /* f */

/********************************************************************/
float power(float base, int n) {
/*
calculate the n th power of base number
*/
int i;
float p;
p = 1;
for (i = 1; i <= n; ++i){
p *= base;
}
return p;
}

Hiç yorum yok:

Yorum Gönder