Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Numbers 2 and 3 need to implemented in the program given written in C. Here's the program given: /* File: pth_pi.c * Purpose: Try to

Numbers 2 and 3 need to implemented in the program given written in C.

image text in transcribed

Here's the program given:

/* File: pth_pi.c

* Purpose: Try to estimate pi using the formula

*

* pi = 4*[1 - 1/3 + 1/5 - 1/7 + 1/9 - . . . ]

*

* This version has a *very serious bug*

*

* Compile: gcc -g -Wall -o pth_pi pth_pi.c -lm -lpthread

* Run: ./pth_pi <>

* n is the number of terms of the series to use.

* n should be evenly divisible by the number of threads

* Input: none

* Output: Estimate of pi as computed by multiple threads, estimate

* as computed by one thread, and 4*arctan(1).

*

* Notes:

* 1. The radius of convergence for the series is only 1. So the

* series converges quite slowly.

*

* IPP: Section 4.4 (pp. 162 and ff.)

*/

#include

#include

#include

#include

const int MAX_THREADS = 1024;

long thread_count;

long long n;

double sum;

void* Thread_sum(void* rank);

/* Only executed by main thread */

void Get_args(int argc, char* argv[]);

void Usage(char* prog_name);

double Serial_pi(long long n);

int main(int argc, char* argv[]) {

long thread; /* Use long in case of a 64-bit system */

pthread_t* thread_handles;

/* Get number of threads from command line */

Get_args(argc, argv);

thread_handles = (pthread_t*) malloc (thread_count*sizeof(pthread_t));

sum = 0.0;

for (thread = 0; thread

pthread_create(&thread_handles[thread], NULL,

Thread_sum, (void*)thread);

for (thread = 0; thread

pthread_join(thread_handles[thread], NULL);

sum = 4.0*sum;

printf("With n = %lld terms, ", n);

printf(" Our estimate of pi = %.15f ", sum);

sum = Serial_pi(n);

printf(" Single thread est = %.15f ", sum);

printf(" pi = %.15f ", 4.0*atan(1.0));

free(thread_handles);

return 0;

} /* main */

/*------------------------------------------------------------------

* Function: Thread_sum

* Purpose: Add in the terms computed by the thread running this

* In arg: rank

* Ret val: ignored

* Globals in: n, thread_count

* Global in/out: sum

*/

void* Thread_sum(void* rank) {

long my_rank = (long) rank;

double factor;

long long i;

long long my_n = n/thread_count;

long long my_first_i = my_n*my_rank;

long long my_last_i = my_first_i + my_n;

if (my_first_i % 2 == 0)

factor = 1.0;

else

factor = -1.0;

for (i = my_first_i; i

sum += factor/(2*i+1);

}

return NULL;

} /* Thread_sum */

/*------------------------------------------------------------------

* Function: Serial_pi

* Purpose: Estimate pi using 1 thread

* In arg: n

* Return val: Estimate of pi using n terms of Maclaurin series

*/

double Serial_pi(long long n) {

double sum = 0.0;

long long i;

double factor = 1.0;

for (i = 0; i

sum += factor/(2*i+1);

}

return 4.0*sum;

} /* Serial_pi */

/*------------------------------------------------------------------

* Function: Get_args

* Purpose: Get the command line args

* In args: argc, argv

* Globals out: thread_count, n

*/

void Get_args(int argc, char* argv[]) {

if (argc != 3) Usage(argv[0]);

thread_count = strtol(argv[1], NULL, 10);

if (thread_count MAX_THREADS) Usage(argv[0]);

n = strtoll(argv[2], NULL, 10);

if (n

} /* Get_args */

/*------------------------------------------------------------------

* Function: Usage

* Purpose: Print a message explaining how to run the program

* In arg: prog_name

*/

void Usage(char* prog_name) {

fprintf(stderr, "usage: %s ", prog_name);<>

fprintf(stderr, " n is the number of terms and should be >= 1 ");

fprintf(stderr, " n should be evenly divisible by the number of threads ");

exit(0);

} /* Usage */

The program implements another way to estimate pi-by adding an infinite series of fractions. The program is essentially a global sum, in which each thread is responsible for summing a part of the series and the total sum is a shared (global) variable. The number of threads and number of terms to use are command line arguments. Execute the program with several numbers of threads and terms. Invoke the program as follows: .pthpi Note: should be larger than The program estimates pi using threads and also computes a serial estimate, displaying both. We expect the two estimates to be the same but you should notice they are not because this program has a serious bug. Adding numbers in a high level language is not an atomic operation-the total and the number added to it must be loaded into registers, added, then the result stored back to a memory location. So the statement adding terms to sum is a critical section-it should only be executed by one thread at a time. 2. One simple solution to this problem is to keep each thread busy-waiting until its turn to access the shared variable. Add code to pth_pi.c so each thread can access sum safely: before carrying out the sum use a while loop with an empty body that waits until a shared variable (flag) is equal to the rank of the thread. When the thread has carried out the addition, it should increment flag. You may realize that you don't want flag to take a value higher than thread_count - 1. The simplest way to do that is using modulo: flag = (flag + 1) % thread_count; Compile the modified program and execute it several times with various numbers of threads and terms. Are the serial and threaded estimates consistent? 3. Consider that busy-waiting inside the loop might waste a significant amount of time. If you have a private (local) variable that a thread can use inside the loop to calculate its own local sum, then add that to the shared global sum after the loop, then you only have to protect the addition to sum after the loop. Make those changes then compile and run the program several times to see if you still get good estimates

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

Students also viewed these Databases questions

Question

Explain the ways in which ROI can be improved.

Answered: 1 week ago

Question

6. Are my sources reliable?

Answered: 1 week ago

Question

5. Are my sources compelling?

Answered: 1 week ago