QuestionQuestion

Estimate Value of PI using OpenMP And Monte Carlo Method With Pthread.
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 πœ‹ 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       4

since the ratio of the area of the circle to the area of the square is πœ‹/4. We can use this formula to estimate the value of ir with a random number generator:
number_in_circle = 0;
for (toss =0; toss < number_of_tosses ; toss++) {
x = random double between β€”1 and 1;
y = random double between β€”1 and 1;
distance_squared = xβˆ—x + yβˆ—y;
if (distance_squared <= 1) number_in_circle++;
}
pi_estimate = 4βˆ—number_in_circle/((double) number_of_tosses);

This is called a "Monte Carlo" method, since it uses randomness (the dart tosses). Write an OpenMP program that uses a Monte Carlo method to estimate πœ‹. Read in the total number of tosses before forking any threads. Use a reduction clause to find the total number of darts hitting inside the circle. Print the result after joining all the threads. 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 πœ‹ .

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <time.h>



int main(int argc, char* argv[]){
long long number_in_circle = 0;
long long thread_count = 5;
double pi_estimate;
long long total_number_of_tosh, number_tossed_per_thread;
srand ( (unsigned)time ( NULL ) );
total_number_of_tosh = strtol(argv[1], NULL, 10);

thread_count = total_number_of_tosh > thread_count ? thread_count : 1;

printf("Please wait for a while\n");
number_tossed_per_thread = total_number_of_tosh/thread_count + 1...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

$54.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats