Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone please provide a detailed implementation of what this code does/what the statement does? #include #include #include #include #include #define NUMBER_OF_POINTS 1000000 #define NUMBER_OF_THREADS

Can someone please provide a detailed implementation of what this code does/what the statement does?

#include #include #include #include #include #define NUMBER_OF_POINTS 1000000 #define NUMBER_OF_THREADS 2

void *runner(void *param); //amount of points in the circle int circle_count = 0;

//generate a double precision random number double random_double() { return random() / ((double)RAND_MAX +1); }

//master thread int main (int argc, const char * argv[]) { int i; double Pi; int points_per_thread = NUMBER_OF_POINTS / NUMBER_OF_THREADS; pthread_t workers[NUMBER_OF_THREADS];

//seed the random number generator srandom((unsigned)time(NULL)); clock_t begin = clock(); for (i = 0; i < NUMBER_OF_THREADS; i++) pthread_create(&workers[i], 0, runner, &points_per_thread); for (i = 0; i < NUMBER_OF_THREADS; i++) pthread_join(workers[i], NULL);

// estimating Pi Pi = 4.0 * circle_count / NUMBER_OF_POINTS; clock_t end = clock(); double time_spent = (double)(end - begin) / CLOCKS_PER_SEC; printf(" Number of Points = %d ",NUMBER_OF_POINTS); printf("Pi = %f ", Pi); printf("Time = %f ", time_spent); return 0; }

//slave thread void *runner(void *param) { int POINTS; POINTS = *((int *)param); int i; int hit_count = 0; double x,y;

for (int i = 0; i < POINTS; i++) { //generate random numbers between -1.0 and +1.0 (exclusive) //obtain a random (x,y) point x = random_double() * 2.0 - 1.0; y = random_double() * 2.0 - 1.0;

//checks if (x,y) point is within the circle if(sqrt(x*x + y*y) < 1.0) ++hit_count; } circle_count += hit_count; pthread_exit(0); }

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

Step: 3

blur-text-image

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

Oracle Solaris 11.2 System Administration (oracle Press)

Authors: Harry Foxwell

1st Edition

007184421X, 9780071844215

More Books

Students also viewed these Databases questions