views:

144

answers:

4

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

+5  A: 

Quoting from here:

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.

Jacob
I'm stuck with GCC 4.3, which implements OpenMP 2.5. I'll cast my `size_t` to `int` for now.
larsmans
+1  A: 

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 Farber
+3  A: 

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
+1 for the reason. It's always good to know **why** things are the way they are.
pmg
Which begs the question why the other expressions must be `signed`.
larsmans
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
+1  A: 

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