Evgenij Reznik
Evgenij Reznik

Reputation: 18614

Parallelizing for loop

I want to parallelize the following code with OpenMP:

  for(i=0; i<n; i++)
    {
        int sum=0;
        for(j=0; j<m; j++)
        {
            sum += A[i][j]*x[j];
        }
        y[i]=sum
    }

Would it work if I just add #pragma omp parallel for at the top? Or are there other (better) ways?

Upvotes: 2

Views: 141

Answers (1)

Michael Aquilina
Michael Aquilina

Reputation: 5540

A #pragma omp parallel for for your external loop is fine, you should get your intended results using the code below:

#pragma omp parallel for private(i, j, sum) shared(y, n, m, A, x)
for(i=0; i<n; i++)
{
    int sum=0;
    for(j=0; j<m; j++)
    {
        sum += A[i][j]*x[j];
    }
    y[i]=sum
}

Note that in order to get any form of noticeable improvement your n variable is going to have to be very large. Small n values will actually degrade your performance substantially due to thread overhead and a phenomenon called False Sharing.

As an end note, If your final intention is to sum up y - you can make use of the OpenMP reduction clause.

Upvotes: 2

Related Questions