3.2. Suppose we toss darts randomly at a square dartboard, whose bullseye is at
ID: 647668 • Letter: 3
Question
3.2. Suppose we toss darts randomly at a square dartboard, whose bullseye is at the origin, and whose sides are 2 feet in length. Suppose also that there's a circle inscribed in the square dartboard. The radius of the circle is 1 foot, and it's area is pi square feet. If the points that are hit by the darts are uniformly distributed (and we always hit the square), then the number of darts that hit inside the circle should approximately satisfy the equation number in circle/ total number of tosses = pi/4 since the ratio of the area of the circle to the area of the square is pi/4. We can use this formula to estimate the value of x with a random number generator. 148 CHAPTER 3 Distributed-Memory Programming with MPI This is called a Monte Carlo method, since it uses randomness (the dart tosses). Write an MPI program that uses a Monte Carlo method to estimate pi. Process 0 should read in the total number of tosses and broadcast it to the other processes. Use MPI.Reduce to find the global sum of the local variable number.in. circle, and have process 0 print the result. You may want to use long long ints for the number of hits in the circle and the number of tosses, since both may have to be very large to get a reasonable estimate of pi.Explanation / Answer
This is first time to coding MPI program...so any errors excuse me...I am giving idea to resolve
What I have seen in mpi programs is...syntax is almost common,,,only our desired statements have to kept in it
sscanf(argv[1], "%lf", &NN);
N = lround(NN);
MPI_Barrier(MPI_COMM_WORLD);
sTime = MPI_Wtime();
numberOfHits = 0;
srand((unsigned)(time(0)));
int lN = N/np;
for(i = 0; i<lN;i++){
x = ((double)rand())/((double)RAND_MAX);
y = ((double)rand())/((double)RAND_MAX);
if (((x*x) + (y*y)) <= 1) numberOfHits++;
}
int hit=0;
MPI_Allreduce(&numberOfHits,&hit,1,MPI_INT,MPI_SUM,MPI_COMM_WORLD);
double est;
est = (hit*4)/((double)N);
Here is the program
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <math.h>
#include <time.h>
int main(int argc, char* argv[]){
int i,id, np,N;
double x, y,NN,eTime,sTime,pTime;
int numberOfHits;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &id);
MPI_Comm_size(MPI_COMM_WORLD, &np);
if( argc !=2){
if (id==0){
fprintf(stderr,"Enter correct margin ");/* if margin incorrect is prints error */
fflush(stderr);
}
MPI_Abort(MPI_COMM_WORLD,1);
}
sscanf(argv[1], "%lf", &NN);/* initializing number of rounds*/
N = lround(NN);
MPI_Barrier(MPI_COMM_WORLD);
sTime = MPI_Wtime();
numberOfHits = 0;
srand((unsigned)(time(0))); /* Getting random number */
int lN = N/np;
for(i = 0; i<lN;i++){
x = ((double)rand())/((double)RAND_MAX);
y = ((double)rand())/((double)RAND_MAX);
if (((x*x) + (y*y)) <= 1) numberOfHits++; /* checking condition to increment hits count or not */
}
int hit=0;
MPI_Allreduce(&numberOfHits,&hit,1,MPI_INT,MPI_SUM,MPI_COMM_WORLD);
double est;
est = (hit*4)/((double)N); /* Here pi estimation done */
if (id == 0) {
printf("Estimate of Pi: %24.16f ",est);
}
MPI_Finalize();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.