Why must loop variables be signed in a parallel fo

2020-07-10 08:24发布

问题:

I'm just learning OpenMP from online tutorials and resources. I want to square a matrix (multiply it with itself) using a parallel for loop. In IBM compiler documentation, I found the requirement that "the iteration variable must be a signed integer." Is this also true in the GCC implementation? Is it specified in the OpenMP standard? If so, is there a reason for this requirement?

(It doesn't matter much as the expected dimensions are far smaller than INT_MAX, but it does cost me some casts.)

回答1:

According to OpenMP 3.0 specification: http://www.openmp.org/mp-documents/spec30.pdf, for variable may be of a signed or unsigned integer type, see 2.5.1 Loop Construct. The question is whether given OpenMP implementation matches this latest specification.



回答2:

Quoting from Why aren't unsigned OpenMP index variables allowed? :

According to the OpenMP 2.0 C/C++ API specification (pdf), section 2.4.1, that's one of the restrictions of the for loop. No reason is given for it, but I suspect it's just to simplify the assumptions that the code and compiler have to make, since there's special code to ensure that the range doesn't overflow the maximum value of the type.

OpenMP 3.0 apparently allows for unsigned types too, but I haven't seen it in action yet.

In short, it's part of the standard and the next version will allow unsigned integers.



回答3:

Here's a possible reason behind that. The same article says that

  • b, ub, incr are loop invariant signed integer expressions and
  • exit_cond takes form: iv <= ub or iv < ub or iv >= ub or iv > ub (where iv is the iteration variable you ask about)

since the exit_cond condition involves a comparison and the comparison is done against a signed ub variable the loop variable iv has to be signed to avoid possible problems with signed/unsigned comparison.



回答4:

To answer your first question about gcc. No, it seems that gcc easily accepts unsigned or size_t loop variables in something like

#pragma omp parallel for
for (size_t i = 0; i < N; ++i) {
  /* do it */
}

at least mine (gcc v 4.4 on a 64bit ubuntu) doesn't complain and does the right thing.