zodiac
zodiac

Reputation: 311

parallel openMP loop in cpp

I am trying to learn parallelization with openMP in cpp. I am using the following test example

 #pragma parallel for num_threads( 4 )
 for ( int i = 0 ; i < N ; i++ ){
     for ( int j = 0 ; j < 100000 ; j++ ){
         data[ i ]  =  data[ i ] + ( double ) i ;
     } 
 }  

I am using 4 threads; with top ( in unix ) I should see then in the col %CPU 400% or something similar. But I get 100% what would be the case for serial execution. And if I measure the time there is no velocity gain compared to serial execution. I can not figure out what I am doing wrong.

Upvotes: 0

Views: 1899

Answers (1)

RHertel
RHertel

Reputation: 23818

You missed the omp in the pragma directive.

Try:

#pragma omp parallel for num_threads( 4 )

As described here, this is one of the most common mistakes when using OpenMP in C++.

With the GCC compiler it is possible to catch this problem by compiling with the -Wall or -Wunknown pragmas flag. It's a good habit to use -Wall as it preempts many otherwise mysterious issues. Other compilers have similar options.

Upvotes: 2

Related Questions