Strange behaviour of openmp collapse directive

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;
}

Upvotes: 0

Views: 273

Answers (1)

J&#233;r&#244;me Richard
J&#233;r&#244;me Richard

Reputation: 50808

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).

Upvotes: 1

Related Questions