24

I have a loop in my C++/OpenMP code that looks like this:

#pragma omp parallel for
for(unsigned int i=0; i<count; i++)
{
    // do stuff
}

When I compile it (with Visual Studio 2005) I get the following error:

error C3016: 'i' : index variable in OpenMP 'for' statement must have signed integral type

I understand that the error occurs because i is unsigned instead of signed, and changing i to be signed removed this error. What I want to know is why is this an error? Why aren't unsigned index variables allowed? Looking at the MSDN page for this error gives me no clues.

Moe
  • 28,607
  • 10
  • 51
  • 67

2 Answers2

18

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.

Head Geek
  • 38,128
  • 22
  • 77
  • 87
  • 1
    It's been over a decade, and VS 2022's `cl.exe` still only supports OpenMP 2.0 via `/openmp`, unless one chooses `/openmp:llvm`, which [according to Microsoft](https://learn.microsoft.com/en-us/cpp/build/reference/openmp-enable-openmp-2-0-support?view=msvc-170) is only supported for x64 targets and unsigned loop indices like in OpenMP 3.0. Kind of unfortunate... GCC/Clang is way ahead. – phetdam Nov 22 '22 at 02:09
4

The Microsoft C/C++ Compiler 12.0 integrated with Visual Studio 2013 still only support OpenMP 2.5 and doesn't allow unsigned int for the loop counter.

GCC support OpenMP 3.0 since its version 4.4 and allows unsigned int for the loop counter.

Kyle_the_hacker
  • 1,394
  • 1
  • 11
  • 27
  • Let's be precise here: the MS C/C++ compiler(s) bundled with VS 2013 may not support OpenMP after v2.5 but the Intel compilers, which integrate well enough with VS, do support OpenMP 3.1. Whether other compilers which integrate with VS support the more recent standard I don't know. – High Performance Mark Jul 22 '13 at 09:06