Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a multithreaded pi estimator. Create 2 0 threads. Have each thread generate 1 0 0 0 0 ( x , y ) pairs. Use

Write a multithreaded pi estimator. Create 20 threads. Have each thread generate 10000(x,y) pairs. Use srand48_r to seed the random number generator with the thread's ID (your 20 threads should be numbered 0-19, with each thread's id value passed in during the call to pthread_create). Use drand48_r to generate random floating-point numbers between 0 and 1. Do not strictly follow the website example because your answer will be "wrong"; also srand() and rand() aren't thread-safe which is why we cannot rely on them here.
struct drand48_data randbuf;
srand48_r(threadid, &randbuf);
double randx, randy;
for(int i =0; i <10000; i++){
drand48_r(&randbuf, &randx);
drand48_r(&randbuf, &randy);
//you can calculate and record circle/square points here
}
Use randx and randy to determine whether the (randx, randy) coordinate is inside of the unit circle or not. All threads should contribute to this count. Make sure you eliminate the race condition by protecting the shared variables with a mutex. Make sure you wait for each of the 20 threads to complete all of its 10,000 iterations. Calculate the estimate for pi when all threads are done.
pi =4*(double)(circle_points)/ square_points;
Finally, print the estimated value with printf("%.7g
",...) and submit the estimate by pasting

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine Ricardo

2nd Edition

1449606008, 978-1449606008

More Books

Students also viewed these Databases questions

Question

Define belongingness, competence, and autonomy.

Answered: 1 week ago