9

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

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • This question was marked as a dupe of [this one](http://stackoverflow.com/questions/2820621), but there's a difference, which is that I was specifically interested in GCC's OpenMP. – Fred Foo May 16 '14 at 12:40

4 Answers4

10

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.

Community
  • 1
  • 1
Jacob
  • 34,255
  • 14
  • 110
  • 165
8

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.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • 1
    +1 for the reason. It's always good to know **why** things are the way they are. – pmg Oct 21 '10 at 14:38
  • 3
    Which begs the question why the other expressions must be `signed`. – Fred Foo Oct 21 '10 at 14:40
  • 1
    Hm, that is no real reason. If the reason would be, you have to have the bounds and the counter of the same type, including signedness. That one I would buy. – Jens Gustedt Oct 21 '10 at 15:31
7

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.

Alex F
  • 42,307
  • 41
  • 144
  • 212
  • Accepted because the restriction has been lifted in 3.0, so there probably wasn't a good reason for it in the first place. – Fred Foo Dec 08 '10 at 19:52
3

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.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177