Hello there , I need to modify this code in MPI to calculate the value of Pie wh
ID: 3684604 • Letter: H
Question
Hello there ,
I need to modify this code in MPI to calculate the value of Pie which is 3.14
/* File: mpi_trap1.c
* Purpose: Use MPI to implement a parallel version of the trapezoidal
* rule. In this version the endpoints of the interval and
* the number of trapezoids are hardwired.
*
* Input: None.
* Output: Estimate of the integral from a to b of f(x)
* using the trapezoidal rule and n trapezoids.
*
* Compile: mpicc -g -Wall -o mpi_trap1 mpi_trap1.c -lm
* Run: mpiexec -n <number of processes> ./mpi_trap1
*
* 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.
*
* Note: f(x), a, b, and n are all hardwired.
*
* IPP: Section 3.2.2 (pp. 96 and ff.)
*/
#include <stdio.h>
/* We'll be using MPI routines, definitions, etc. */
#include <mpi.h>
#include <time.h>
#include <math.h>
/* Calculate local integral */
double Trap(double left_endpt, double right_endpt, int trap_count,
double base_len);
/* Function we're integrating */
double f(double x);
int main(void) {
int my_rank, comm_sz, n = 100000, local_n;
double a = 0, b = 0, h, local_a, local_b;
double local_int, total_int;
int source;
clock_t clockStart, clockEnd;
clockStart = clock();
/* Let the system do what it needs to start up MPI */
MPI_Init(NULL, NULL);
/* 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, &comm_sz);
h = (b-a)/n; /* h is the same for all processes */
local_n = n/comm_sz; /* 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;
local_int = Trap(local_a, local_b, local_n, h);
/* Add up the integrals calculated by each process */
if (my_rank != 0) {
MPI_Send(&local_int, 1, MPI_DOUBLE, 0, 0,
MPI_COMM_WORLD);
} else {
total_int = local_int;
for (source = 1; source < comm_sz; source++) {
MPI_Recv(&local_int, 1, MPI_DOUBLE, source, 0,
MPI_COMM_WORLD, MPI_STATUS_IGNORE);
total_int += local_int;
}
}
/* Print the result */
if (my_rank == 0) {
clockEnd = clock();
printf("Time for MPI in parallel %3.5f seconds, estimate %.15e ", (clockEnd - clockStart) / (float)CLOCKS_PER_SEC, total_int);
}
/* Shut down MPI */
MPI_Finalize();
if (my_rank == 0) {
//Serial One
double estimate = 0, x = 0;
double left_endpt = a;
double right_endpt = b;
clockStart = clock();
estimate = (f(left_endpt) + f(right_endpt)) / 2.0;
int i = 1;
for (i = 1; i <= n - 1; i++)
{
x = a + i*h;
estimate += f(x);
}
estimate = estimate*h;
clockEnd = clock();
printf("Time for MPI in serial %3.5f seconds, estimate %.15e ", (clockEnd - clockStart) / (float)CLOCKS_PER_SEC, estimate);
}
return 0;
} /* main */
/*------------------------------------------------------------------
* Function: Trap
* Purpose: Serial function for estimating a definite integral
* using the trapezoidal rule
* Input args: left_endpt
* right_endpt
* trap_count
* base_len
* Return val: Trapezoidal rule estimate of integral from
* left_endpt to right_endpt using trap_count
* trapezoids
*/
double Trap(
double left_endpt /* in */,
double right_endpt /* in */,
int trap_count /* in */,
double base_len /* in */) {
double estimate, x;
int i;
estimate = (f(left_endpt) + f(right_endpt))/2.0;
for (i = 1; i <= trap_count-1; i++) {
x = left_endpt + i*base_len;
estimate += f(x);
}
estimate = estimate*base_len;
return estimate;
} /* Trap */
/*------------------------------------------------------------------
* Function: f
* Purpose: Compute value of function to be integrated
* Input args: x
*/
double f(double x) {
return sqrt(x*x*x);
} /* f */
____
extra information about Pie calculation in C# to add it to MPI code.
long num_steps = 100000;
double h, y, area=0;
h = 1.0 / (double)num_steps;
for (int i = 0; i < num_steps; i++)
{
y = Math.Sqrt(1 - (i * h) * (i * h));
area = area + y * h;
}
pi = 4 * area;
thank you
Explanation / Answer
pi value is 22/7=3.14
If a circle of radius R is inscribed inside a square with side length 2R, then the area of the circle will be pi*R^2 and the area of the square will be (2R)^2. So the ratio of the area of the circle to the area of the square will be pi/4.
This means that, if you pick N points at random inside the square, approximately N*pi/4 of those points should fall inside the circle.
This program picks points at random inside the square. It then checks to see if the point is inside the circle (it knows it's inside the circle if x^2 + y^2 < R^2, where x and y are the coordinates of the point and R is the radius of the circle). The program keeps track of how many points it's picked so far (N) and how many of those points fell inside the circle (M).
Pi is then approximated as follows:
4*M
pi = ---
N
Although the Monte Carlo Method is often useful for solving problems in physics and mathematics which cannot be solved by analytical means, it is a rather slow method of calculating pi.
Here is a sample code logic-
S = 0
n=100
for i = 1, n
do
x=math.random()
y=(1-x^2)^0.5
S=S+y
end
T=4*S/n
print(T)
In the given code:
try replacing
pi=4*area/num_steps;
note-the above explanation may help to answwer the given question.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.