0

I have the following problem:

In the code below I wait the same amount of iterations, but this is not true. Often I have 45 iterations(expected amount), some times I have 67, some times 23 iterations. How does #pragma omp parallel for collapse(2) works, or maybe I wrongly used #pragma omp critical?

#include <iostream>
#include <omp.h>
#include <vector>

int main() {
    std::vector<int> vec(45);
    int k = 0;
    #pragma omp parallel for collapse(2)
     for (int i = 0; i < 10; ++i) {
        for (int j = i + 1; j < 10; ++j) {
            auto tmp = big_calc();
            #pragma omp critical
                vec[k++] = tmp;
        }
    }

    return 0;
}
dikiidog
  • 15
  • 4
  • I think it is because of j initial value – dikiidog Aug 02 '20 at 20:19
  • added crutch - in nested for j now starts from 0 and added: ```if (i >= j) continue;``` – dikiidog Aug 02 '20 at 20:25
  • What about [reading the documentation](https://www.openmp.org/spec-html/5.0/openmpsu41.html#x64-1290002.9.2)? Moreover, the `#pragma omp critical` prevent any possible parallel speed-up and the sequential version should be actually much faster. You need to **redesign your algorithm** so it could *really run in parallel*. – Jérôme Richard Aug 03 '20 at 08:40
  • in this part of code - ```auto tmp = j * (i + 1);``` in original code i do some big calculation – dikiidog Aug 03 '20 at 13:43

1 Answers1

1

Your code is not compliant to the OpenMP standard. Indeed, the initializer int j = i + 1 of the inner loop shall not be dependent of the iterator value of the outer loop. You should collapse the loop yourself (or you can use OpenMP tasks which have a higher overhead).

PS: #pragma omp critical is not needed anymore if you collapse the loop yourself (resulting in a better scalability).

Jérôme Richard
  • 25,329
  • 3
  • 19
  • 45