0

I'm new to OpenMP and got an error that I can't fix.

Suppose I have a two-dimensional vector:

vector<vector<int>> a{{...}, {...}, ...};

I want to traverse it as

#pragma omp parallel for collapse(2)
for (int i = 0; i < a.size(); i++){
    for (int j = 0; j < a[i].size(); j++){
        work(a[i][j]);
    }
}

However, there is an error: condition expression refers to iteration variable ā€˜i’.

So how can I traverse the two-dimensional vector correctly?

1 Answers1

0

The problem is that the end condition of second loop depends on the first loop's index variable (a[i].size()). Only OpenMP 5.0 (or above) supports so-called non-rectangular collapsed loops, so if you use earlier OpenMP version you cannot use the collapse(2) clause here. Just remove collapse(2) clause and it will work.

Note that, if a[i].size() is the same for all i, then you can easily remove this dependency.

Laci
  • 2,738
  • 1
  • 13
  • 22
  • Thanks a lot. However it has a bad effect when the fluctuation of a[i].size() is large. By the way, the version of OpenMP is 4.5, how can I upgrade it to 5.0. – user6940625 Aug 05 '21 at 09:00
  • Try to use different schedule schemes (e.g. schedule(dynamic) and different chunk sizes). More details: https://stackoverflow.com/questions/10850155/whats-the-difference-between-static-and-dynamic-schedule-in-openmp Unfortunately you cannot upgrade OpenMP version just the whole compiler. Clang based compilers support OpenMP 5.0, for more details see: https://www.openmp.org/resources/openmp-compilers-tools/ ps: Do not forget to accept the answer if you find it useful, so others can see that your problem is answered – Laci Aug 05 '21 at 09:42