I'm trying to compile this code that uses OpenMP. When I compile it with nvcc
, it gives an error that appears to be complaining about a token that isn't even there.
Here's a minimal version of my code:
int main() {
// this loop somehow prevents the second one from compiling
for (int foo = 0; foo < 10; foo++) {
int bar;
continue;
}
#pragma omp parallel for
for (int baz = 0; baz < 10; baz++) { }
return 0;
}
Here's the error message it produces:
exp.cu:10:1: error: for statement expected before ‘}’ token
10 | for (int baz = 0; baz < 10; baz++) { }
| ^
I'm compiling it with this command: nvcc -Xcompiler -fopenmp exp.cu
Without the first loop, this program compiles correctly. It also works if I remove either of the lines in the first loop. How does the first loop prevent the second one from compiling? Am I using invalid OpenMP syntax?
If I rename the file to exp.cpp
and compile it with g++ -fopenmp exp.cpp
, that works without errors. Is there any possibility that this is a bug in nvcc
? Unfortunately, I can't just use g++
, because I need to be able to use CUDA kernels in other places.
I'm using CUDA 11.2.
There is evidently a defect in CUDA 11.2 as far as this code example goes.
The problem appears to be resolved in CUDA 11.4 and later.
The solution is to upgrade the CUDA install to CUDA 11.4 or later.