Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This code isn't running because of the rand _ r in line 8 3 . Can you help me fix it ? #include #include #include

This code isn't running because of the rand_r in line 83. Can you help me fix it?
#include
#include
#include
#define MAX_THREADS 512
void *compute_pi(void *);
long sample_points;
long total_hits;
long total_misses;
long hits[MAX_THREADS];
long sample_points_per_thread;
int num_threads;
int main(int argc, char *argv[])
{
/* local variables */
int ii;
int retval;
pthread_t p_threads[MAX_THREADS];
pthread_attr_t attr;
double computed_pi;
/* initialize local variables */
retval =0;
pthread_attr_init(&attr);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
/* parse command line arguments into sample points and number of threads */
/* there is no error checking here!!!!! */
sample_points = atol(argv[1]);
num_threads = atoi(argv[2]);
/* uncomment this block if you want interactive input!!!!
/* if so...comment out the two statements above */
printf( "Enter number of sample points: ");
scanf("%d", &sample_points );
printf( "Enter number of threads: ");
scanf("%d%", &num_threads );
total_hits =0;
sample_points_per_thread = sample_points / num_threads;
for (ii =0; ii < num_threads; ii++)
{
hits[ii]= ii;
pthread_create(&p_threads[ii], &attr, compute_pi,(void *)&hits[ii]);
}
for (ii =0; ii < num_threads; ii++)
{
pthread_join(p_threads[ii], NULL);
total_hits += hits[ii];
}
computed_pi =4.0*(double)total_hits /((double)(sample_points));
printf("Computed PI =%lf
", computed_pi);
/* return to calling environment */
return (retval);
}
void *compute_pi(void *s)
{
int seed;
long ii;
long *hit_pointer;
long local_hits;
double rand_no_x;
double rand_no_y;
hit_pointer =(long *)s;
seed =*hit_pointer;
local_hits =0;
for (ii =0; ii < sample_points_per_thread; ii++)
{
rand_no_x =(double)(rand_r(&seed))/(double)RAND_MAX;
rand_no_y =(double)(rand_r(&seed))/(double)RAND_MAX;
if (((rand_no_x -0.5)*(rand_no_x -0.5)+
(rand_no_y -0.5)*(rand_no_y -0.5))<=0.25)
local_hits++;
seed *= ii;
}
*hit_pointer = local_hits;
pthread_exit(0);
}
Promppt: The first part of this assignment is to explore the use of threads in an application that computes PI. Download the compute_pi_montecarlo.c (compile: gcc compute_pi_montecarlo.c -o pi -lpthread)(execute: ./pi 800008) program to your Project3 subdirectory. Compile and run this program a few times to understand the basic operation and to verify that it does approximate PI. Next, time your program and/or add appropriate timing system calls to your code. Run the application varying the number of threads and number of sample points. You may get a core dump when you exceed the number of threads permitted (note that value). Record the time(s) it takes your program to execute when changing the number of threads and number of sample points.
1. Create a plot of time vs. number of threads when the number of sample points is fixed.
2. Create a plot of time vs. number of sample points when the number of threads is fixed.
Use an Excel like application to create your plot

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

Murach's SQL Server 2012 For Developers

Authors: Bryan Syverson, Joel Murach, Mike Murach

1st Edition

1890774693, 9781890774691

Students also viewed these Databases questions

Question

Describe Montaignes position on child rearing.

Answered: 1 week ago

Question

Can knowledge workers and/or professionals be performance-managed?

Answered: 1 week ago

Question

Does a PMS enhance strategic integration within HRM?

Answered: 1 week ago