Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

–4.17*: An interesting way of calculating p is to use a technique known as Monte

ID: 3770954 • Letter: #

Question

–4.17*: An interesting way of calculating p is to use a technique known as Monte Carlo, which involves randomization. This technique works as follows:
Suppose you have a circle inscribed within a square, as shown in Figure 4.18. (Assume that the radius of this circle is 1.)

First, generate a series of random points as simple (x,y) coordinates. These points must fall within the Cartesian coordinates that bound the square. Of the total number of random points that are generated, some will occur within the circle.
Next, estimate p by performing the following calculation:
p =4*(number of points in circle)/(total number of points)

Write a multithreaded version of this algorithm that creates a separate thread to generate a number of random points. The thread will count the number of points that occur within the circle and store the result in a global variable. When this thread has exited, the parent thread will calculate and output the estimated value of p.

–4.18*: Repeat exercise 4.17, but instead of using a separate thread to generate random points, use OpenMP to parallelize the generation of points. Be careful not to place the calculation of p in the parallel region, since you want to calculate p only once.

Explanation / Answer

Can help u with this

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define NR_PTS 10000000
#define NR_THREADS 5
#define NR_PTRS_PER_THREAD NR_PTS/NR_THREADS
#define handle_error_en(en, msg) do
{
errno = en; perror(msg);
exit(EXIT_FAILURE);
}
while (0)
long circleCount = 0;  
pthread_t *threads;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void *monteCarloPi(void *thread_ID)
{
long i;                          
int a = (int) thread_ID;      
long incircle_thread = 0;       
unsigned int rand_state = rand();
for (i = 0; i < NR_PTRS_PER_THREAD; i++)
{
double x = rand_r(&rand_state) / ((double)RAND_MAX + 1) * 2.0 - 1.0;
double y = rand_r(&rand_state) / ((double)RAND_MAX + 1) * 2.0 - 1.0;
if (x * x + y * y < 1)
{
incircle_thread++;
}
}
float Pi = (4. * (double)incircle_thread) /((double)NR_PTRS_PER_THREAD * 1);
pthread_mutex_lock(&mutex);
circleCount += incircle_thread;
pthread_mutex_unlock(&mutex);
return NULL;
}
void createThreads()
{
int i, s;
threads = malloc(NR_THREADS * sizeof(pthread_t));
pthread_attr_t attr;
pthread_attr_init(&attr);
printf(" ---------------------------------------- *Creating [%d] Threads ", NR_THREADS);
for (i = 0; i < NR_THREADS; i++)
{
s = pthread_create(&threads[i], &attr, monteCarloPi, (void *) i);
if (s != 0){
handle_error_en(s, "pthread_create");
}
}
}
void joinThreads()
{
int i,s;
for (i = 0; i < NR_THREADS; i++)
{
s = pthread_join(threads[i], NULL);
if (s != 0)
{
handle_error_en(s, "pthread_join");
}
}
pthread_mutex_destroy(&mutex);
printf(" *[%d] Threads Rejoined ", NR_THREADS);
free(threads);
}
int main()
{
float Pi;
createThreads();
joinThreads();
Pi = (4. * (double)circleCount) / ((double)NR_PTRS_PER_THREAD * NR_THREADS);
printf("Final Estimation of Pi: %f ---------------------------------------- ", Pi);
return 0;
}