Incomprehensible OpenMP Behavior

Hello, I have the following code that I am compiling with gcc (> 4.2) with the -fopenmp flag:

int main(void) { #pragma omp parallel for int i; for(i=0;i<4;i++) while(1); return 0; } 

I get SIGSEGV on OSX Lion (ver 1.7.3, llvm-gcc 4.2.1) and CentOS 6.2. What am I doing wrong here? Thanks

+7
source share
3 answers

There was an error in gcc regarding this issue, I reported this and they will provide a fix. Here is the link: GCC Error

+1
source

Not sure if this applies to the compiler version and configuration, but while(true){} completes

More precisely, if you write a cycle that

  • does not call library I / O functions and
  • does not have access or change of volatile objects, and
  • does not perform any synchronization operations (1.10) or atomic operations (Article 29)

and doesn’t end, you have undefined behavior.

This may not affect your situation, but as C ++ 11 becomes more established, watch out.

+2
source

Very interesting.

I changed the code a bit so

 int main(void) { int i; #pragma omp parallel { while(1); } return 0; } 

and therefore

 inline void func() { while (1) ; } int main(void) { int i; #pragma omp parallel for for(i=0;i<8;i++) { func(); } return 0; } 

And they both worked fine.

+1
source

All Articles