To calculate the value of Pi, we can use the graph of a circle and find the area
ID: 3833059 • Letter: T
Question
To calculate the value of Pi, we can use the graph of a circle and find the area under the graph.
Riemann sum is an effective method for finding the area under the graph
Write a PThreads Program that will calculate Pi in C++ (using riemann sum method):
- It should run 8 threads
- Create a global variable for the total sum
- Use a Mutex variable to lock the critical section where you sum the results
- The program should output the sum at each thread and the parent will output the total sum
Copy this version and remove the Mutex variable run both versions and compare the output
I have written a serial version without pthread. I need to implement pthread into this code:
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <pthread.h>
#include <math.h>
using namespace std;
int getargs(int argc, char *argv[])
{
char* strOne="";
if (argc<1){
}
if (argc>1) {
// note argv[0] is the program name
strOne=argv[1];
printf("Argument One: %s ", strOne);
}
int intArg=0;
if (argc>2) {
// use function atoi() to convert char* to integer
intArg=atoi(argv[2]);
printf("Argument Two: %d ", intArg);
}
system("PAUSE"); // Windows pause command
return EXIT_SUCCESS;
}
// global variables
double total; //the answer
double width; // Width of each rectangle
double steps; // total number of rectangles
int numthreads; //
double interval; // size of the total interval
// function prototypes
double f(double n); // Our Curve
void * work(void *t); // our worker function for child threads
int main(int argc, char *argv[])
{
total=0;
double start=0,stop=2;
numthreads=20;
steps=10000;
interval=stop-start;
width=interval/steps;
// printf("stuff %f, %f, %f", width, interval, steps);
cout<<"stuff "<<width << " "<< interval << " " ;
// call work
for(int i=0;i<numthreads;i++){
work((void *)i);
}
cout <<"total =" << total << " ";
return EXIT_SUCCESS;
} // end main
void * work(void *t){
long tid= (long)t;
double locsum=0;
double start=tid*interval/numthreads;
double stop=(tid+1)*interval/numthreads;
double i=0;
for(i=start; i<stop; i+=width){
locsum+=f(i)*width;
// cout<< i<<" "<< f(i)*width<<" " <<locsum<< " ";
}
total+=locsum;
cout<< "current "<<i<<" "<< locsum<<" "<< total<< " ";
}
double f(double n){
double h=0;
h=sqrt(4-n*n);
return h;
}
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
Here is the example of a pthreads hello code:
Explanation / Answer
Here I would like to explain something how threading works in c/c++ program.
As you were indicating that you need to convert your program into pthread program. First we need to understand what would be critical section for us.
As problem statement indicate each thread will calculate local sum and we need to add them finally which is definitely for us is critical section.
[assuming your current linear program is correct and you wanted to convert your program into pthread program.]
So your program may looks like this
pthread_mutex_t lock; //declare globally
int main(int argc, char *argv[])
{
int err;
pthread_t tid[20];
total=0;
double start=0,stop=2;
numthreads=8;
steps=10000;
int i;
interval=stop-start;
width=interval/steps;
cout<<"stuff "<<width << " "<< interval << " " ;
if (pthread_mutex_init(&lock, NULL) != 0)
{
cout<<" mutex init failed ";
return 1;
}
for(i=0;i<numthreads;i++){
err = pthread_create(&(tid[i]), NULL, &work, (void *)i);
// work((void *)i);
if (err != 0)
cout<<" can't create thread";
}
for (i=0;i<numthreads;i++)
{
pthread_join(tid[i], NULL);
}
pthread_mutex_destroy(&lock);
cout <<"total =" << total << " ";
return EXIT_SUCCESS;
}
void * work(void *t){
long tid= (long)t;
double locsum=0;
double start=tid*interval/numthreads;
double stop=(tid+1)*interval/numthreads;
double i=0;
for(i=start; i<stop; i+=width){
locsum+=f(i)*width;
}
pthread_mutex_lock(&lock);
total+=locsum;
pthread_mutex_unlock(&lock);
cout<< "current "<<i<<" "<< locsum<<" "<< total<< " ";
}
Note: I have just made required changes in your program. Please include above main and work method in your program.
*Hope you have got an idea of parallel programming using pthread.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.