0

I want to write parallel code using openmp and reduction for square addition of matrix(X*X) values. Can I use "2 for loops" after #pragma omp parallel for reduction. if not kindly suggest.

#pragma omp parallel
{
#pragma omp parallel for reduction(+:SqSumLocal)
for(index=0; index<X; index++)
{
  for(i=0; i<X; i++)
  {
  SqSumLocal = SqSumLocal + pow(InputBuffer[index][i],2);
  }
 }
}

Solution: Adding int i under #pragma omp parallel solves the problem.

1 Answer 1

2

The way you've written it is correct, but not ideal: only the outer loop will be parallelized, and each of the inner loops will be executed on individual threads. If X is large enough (significantly larger than the number of threads) this may be fine. If you want to parallelize both loops, then you should add a collapse(2) clause to the directive. This tells the compiler to merge the two loops into a single loop and execute the whole thing in parallel.

Consider an example where you have 8 threads, and X=4. Without the collapse clause, only four threads will do work: each one will complete the work for one value of index. With the collapse clause, all 8 threads will each do half as much work. (Of course, parallelizing such a trivial amount of work is pointless - this is just an example.)

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

3 Comments

Actually, the code is quite incorrect. First of all, i is shared which should not be the case. There should be a private(i) clause. Also, there are two nested parallel regions. What will happen is that all threads will execute the same code and no actual worksharing will occur since nested parallelism is disabled by default, hence the nested regions will execute serially. Not to mention that the result will most likely be gibberish will all threads trying to update the shared SqSumLocal in the outer parallel region.
Thanks a lot @pburka and Hristo lliev for your suggestions. actually adding int i under parallel region solves the problem.
@Manvi, variables declared inside a parallel region are automatically of the private sharing class.

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.