error C3017: termination test in OpenMP 'for&#

2020-02-15 02:17发布

问题:

I have a for loop that has all variables defined

#pragma omp parallel for
for(long long l = 1; l<=sqrtt; l++) ...

When I compile this with the /openmp command line option in Visual Studio 2012, it gives me

error C3017: termination test in OpenMP 'for' statement has improper form

I don't know why 'for' statement has improper form.

What is a proper for statement to OpenMP? How do I apply it to my for loop?

回答1:

The OpenMP 3.1 standard prescribes a very strict form for the for-loop construct (see pag.39):

for (init-expr; test-expr; incr-expr) structured-block

In particular, test-expr must look like one of the following:

var relational-op b
b relational-op var

where relational-op is one of <,<=,>,>= and b is a loop invariant expressions of a type compatible with the type of var.

Other than that you must ensure that:

The values of the loop control expressions of the loops associated with the loop construct must be the same for all the threads in the team.

So, coming back to your case, I would check sqrtt to be a loop invariant and to have the same value for all threads.

A little side note

long long isn't standard in C++ prior to C++11, see for instance this question on SO.



回答2:

I was having the same problem. I had this:

#pragma omp parallel for
for(int i = 0; i < stop; i++){ 
    //My code
}

Then I discovered that the problem was that my variable stop was a double, so, how in my case I needed that stop was a double, I had to do a casting:

#pragma omp parallel for
for(int i = 0; i < (int)stop; i++){ 
    //My code
}

And then all worked :)

I hope this can help.