Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

the code given to us 1. pth_pi_buzy1.c * the critical section. * * Compile: gcc -g -Wall -o pth_pi_busy1 pth_pi_busy1.c -lm -lpthread * Needs the

image text in transcribed

the code given to us

1. pth_pi_buzy1.c

* the critical section. * * Compile: gcc -g -Wall -o pth_pi_busy1 pth_pi_busy1.c -lm -lpthread * Needs the timer.h header file * Run: pth_pi_busy1 * n is the number of terms of the Maclaurin 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 threads, and 4*arctan(1). * This version also prints the elapsed time required for * the multi-threaded and single-threaded calculations. * * Notes: * 1. The radius of convergence for the series is only 1. So the * series converges quite slowly. * 2. This version is likely to become buggy with compiler optimization * turned on. * * IPP: Section 4.5 (pp. 165 and ff.) */ <>

#include #include #include #include #include "timer.h"

const int MAX_THREADS = 1024;

long thread_count; long long n; int flag; 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; double start, finish, elapsed;

/* Get number of threads and number of terms from command line */ Get_args(argc, argv);

thread_handles = (pthread_t*) malloc (thread_count*sizeof(pthread_t)); GET_TIME(start); sum = 0.0; flag = 0; for (thread = 0; thread

for (thread = 0; thread

sum = 4.0*sum; printf("With n = %lld terms, ", n); printf(" Multi-threaded estimate of pi = %.15f ", sum); printf(" Elapsed time = %e seconds ", elapsed);

GET_TIME(start); sum = Serial_pi(n); GET_TIME(finish); elapsed = finish - start; printf(" Single-threaded estimate of pi = %.15f ", sum); printf(" Elapsed time = %e seconds ", elapsed); printf(" Math library estimate of 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

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

} /* 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

/*------------------------------------------------------------------ * 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 */<>

2. pth_pi_mutex.c

/* File: pth_pi_mutex.c * Purpose: Estimate pi using series * * pi = 4*[1 - 1/3 + 1/5 - 1/7 + 1/9 - . . . ] * * This version uses a mutex to protect the critical section * * Compile: gcc -g -Wall -o pth_pi_mutex pth_pi_mutex.c -lm -lpthread * timer.h needs to be available * Run: ./pth_pi_mutex * n is the number of terms of the Maclaurin series to use * n should be evenly divisible by the number of threads * * Input: none * Output: The estimate of pi using multiple threads, one thread, and the * value computed by the math library arctan function * Also elapsed times for the multithreaded and singlethreaded * computations. * * Notes: * 1. The radius of convergence for the series is only 1. So the * series converges quite slowly. * * IPP: Section 4.6 (pp. 168 and ff.) */ <>

#include #include #include #include #include "timer.h"

const int MAX_THREADS = 1024;

long thread_count; long long n; double sum; pthread_mutex_t mutex;

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; double start, finish, elapsed;

/* Get number of threads from command line */ Get_args(argc, argv);

thread_handles = (pthread_t*) malloc (thread_count*sizeof(pthread_t)); pthread_mutex_init(&mutex, NULL); sum = 0.0;

GET_TIME(start); for (thread = 0; thread

for (thread = 0; thread

sum = 4.0*sum; printf("With n = %lld terms, ", n); printf(" Our estimate of pi = %.15f ", sum); printf("The elapsed time is %e seconds ", elapsed); GET_TIME(start); sum = Serial_pi(n); GET_TIME(finish); elapsed = finish - start; printf(" Single thread est = %.15f ", sum); printf("The elapsed time is %e seconds ", elapsed); printf(" pi = %.15f ", 4.0*atan(1.0)); pthread_mutex_destroy(&mutex); free(thread_handles); return 0; } /* main */

/*------------------------------------------------------------------*/ 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; double my_sum = 0.0;

if (my_first_i % 2 == 0) factor = 1.0; else factor = -1.0;

for (i = my_first_i; i

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

} /* 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

/*------------------------------------------------------------------ * 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 */ <>

In-class Assignments (4-3) 4.5. Modify the mutex version of the a calculation program so that the critical section is in the for loop. How does the performance of this version compare to the performance of the original busy-wait version? How might we explain this? pth_pi_mutex.c: mutex version of the pi calculation pth_pi_busy1.c: original busy-wait version Threads Busy-wait Mutex 1 2 4 8 16 32 64

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

More Books

Students also viewed these Databases questions