Question: Hi all, I have a following multithreaded code for numerical integration. I need to fill in two lines of code in order to make it

Hi all,

I have a following multithreaded code for numerical integration. I need to fill in two lines of code in order to make it work.

#include

#include

#define INTERVALS 1000000

#define THNUMS 3

// thread parametes

struct ThreadParams

{

int id; // id

int low; // start

int high; // end

double ysum; // return partial sum

};

#define BLOCK_LOW(id, p, n) ((id)*(n)/(p))

#define BLOCK_HIGH(id, p, n) (BLOCK_LOW((id)+1,p,n)-1)

#define BLOCK_SIZE(id, p, n) (BLOCK_HIGH(id, p, n)-BLOCK_LOW(id, p, n)+1)

// calculate pi partial sum

void *calcpi(void *arg)

{

double ysum;

double xi;

int i;

struct ThreadParams *pm = (struct ThreadParams*)arg;

ysum = 0.0;

for (i = pm->low; i <= pm->high; i++)

{

xi=((1.0/INTERVALS)*(i+0.5));

ysum=ysum+4.0/(1.0+xi*xi);

}

pm->ysum = ysum;

}

int main(int arc, char* argv[])

{

struct ThreadParams ptharg[THNUMS];

double area;

double ysum;

int i;

pthread_t tid;

void *status;

// create multithreads to calculate partial sum

ysum = 0.0;

for (i = 0; i < THNUMS; i++)

{

ptharg[i].id = i;

// NEED HELP WITH THE FOLLOWING TWO LINES

ptharg[i].low = _____________A__________________;

ptharg[i].high = ____________ B__________________;

if (pthread_create(&tid, NULL, calcpi, (void*)&ptharg[i]) != 0)

{

perror("error creating child");

return -1;

}

pthread_join(tid, &status);

}

// calculate total area

area = 0;

for (i = 0; i < THNUMS; i++)

{

area = area + ptharg[i].ysum;

}

area = area * (1.0/INTERVALS);

printf("Area is %13.11f ", area);

return 0;

}

Thank you so much!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!