Question
Newton's Pi approximation formula can be written as: pi = 4 [ 1 - 1/3 + 1/5 - 1/7 + 1/9 ... +((-1)^n)/(2n+1) ] Write
Newton's Pi approximation formula can be written as:
pi = 4 [ 1 - 1/3 + 1/5 - 1/7 + 1/9 ... +((-1)^n)/(2n+1) ]
Write a C program using pthreads that calculates in parallel the nth (nth product) approximation of PI using Newton's formula, using m threads and j number of products to iterate; where each thread computes a different set of products.
Describe the following (briefly) how you are going to parallel the series formula, how your implementation will compute the nth Pi approximation in parallel. (thread deployment, work per thread and computation of final results). Describe how each runner function will compute its part of the series, present an example pseudocode. And describe how you are going to handle/avoid race conditions and how the use of shared memory might aid you on this issue.
Code stub:
#include#include /* this data is shared by the thread(s) */ int threads; unsigned long long iterations; double * pi; void * runner(void * param); /* the thread */ int main(int argc, char * argv[]) {
if (argc != 3) { fprintf(stderr, "usage: a.out"); /*exit(1);*/ return -1; } if (atoi(argv[1]) < 0 || atoi(argv[2]) < 0) { fprintf(stderr, "Arguments must be non-negative "); /*exit(1);*/ return -1; }
...
/* create the thread identifiers */ ... /* create set of attributes for the thread */ ...
/* populate variables... */ ...
/* get the default attributes */ ... /* create threads */ ... /* now wait for the threads to exit */ ...
/* compute and print results */ ...
...printf("pi = %.15f ",...
} /** * The thread will begin control in this function */ void * runner(void * param) { int threadid=... pi[threadid] = 1;
//complete function
pthread_exit(0); }
Paste your parallel-computed implementation of the Newtonws' Pi approximation using pThreads below.
Paste the output of running your implementation with 10000 iterations and 4 threads.
Compile with: gcc -o pi pi.c -lpthread
Useful references: http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
Arguments: Iterations, threads.
Note: Main program thread + runner threads, you are not required to use synchronization.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started