0

I am trying to calculate the integral of 4/(1+x^2) from 0 to 1 in c++ with multi-threading using openMP. I took a serial program (which is correct) and changed it. My idea is: Assume that X is the number of threads. Divide the area beneath the function into X parts, first from 0 to 1/X, 1/X to 2/X... Each thread will calculate it's area, and I will sum it all up.

This is how I implemented it:

`//N.o. of threads to do the task
cout<<"Enter num of threads"<<endl;
int num_threads;
cin>>num_threads;

int i; double x,pi,sum=0.0;
step=1.0/(double)num_steps;
int steps_for_thread=num_steps/num_threads;
cout<<"Steps for thread : "<<steps_for_thread<<endl;

//Split to threads
omp_set_num_threads(num_threads);
#pragma omp parallel
{
    int thread_id = omp_get_thread_num();
    thread_id++;

    if (thread_id == 1) 
    {
        double sum1=0.0;
        double x1;
        for(i=0;i<num_steps/num_threads;i++)
        {
            x1=(i+0.5)*step;
            sum1 = sum1+4.0/(1.0+x1*x1);
        }
        sum+=sum1;
    }
    else 
    {
        double sum2=0.0;
        double x2;
        for(i=num_steps/thread_id;i<num_steps/(num_threads-thread_id+1);i++)
        {
            x2=(i+0.5)*step;
            sum2 = sum2+4.0/(1.0+x2*x2);
        }
        sum+=sum2;
    }
} '

Explanation: The i'th thread will calculate the area between i/n to (i+1)/n and add it to the sum.

The problem is that not only that the output is wrong, but also each time I run the program I get different output.

Any help will be welcomed Thanks

3
  • Add some print statements. Commented Oct 15, 2014 at 12:36
  • What are some of the outputs that you are getting? Commented Oct 15, 2014 at 12:45
  • Depends on the number of threads. For 2, I get 3.6,3.8,2.9 while the correct answer is pi Commented Oct 15, 2014 at 13:13

1 Answer 1

2

You're making this problem much harder than it needs to be. One of OpenMP's goals is to not have to change your serial code. You usually only need to add some pragma statements. So you should write the serial method first.

#include <stdio.h>    
double pi(int n) {
        int i;
        double dx, sum, x;
        dx = 1.0/n;
        #pragma omp parallel for reduction(+:sum) private(x)
        for(i=0; i<n; i++) {
                x = i*dx;
                sum += 1.0/(1+x*x);
        }
        sum *= 4.0/n;
        return sum;
}
int main(void) {
        printf("%f\n",pi(100000000));
}

Output: 3.141593

Notice that in the function pi the only difference between the serial code and the parallel version is the statement

#pragma omp parallel for reduction(+:sum) private(x)

You should also not normally worry about setting the number of threads.

Sign up to request clarification or add additional context in comments.

1 Comment

@HighPerformanceMark, thanks, but I just realized this is a C++ question and not C. I could simplify it a bit with C++. It's annoying that for some reason people think loop initial declarations don't work with OpenMP and C++. I saw for(i=0; and assumed this was a C89 question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.