0

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.

4
  • Did you use the compiler flag -fopenmp? Commented Feb 24, 2018 at 19:33
  • Yes I did, this should not be the problem then. Binding the openmp libraries is working because I checked this already with the function giving thread number. Commented Feb 24, 2018 at 19:41
  • I timed the code with cpp ctime library clock() but also with an external bash timer that is independent of the executed cpp code and returns real time. And if parallelism is working I should see a number higher than 100% in the cpu usage. Or am I missing something. The values for N=100,10000,100000. Yes and the results are only printed later to see if the sum is correct what is the case. It should be only an example for a more complicated loop that I want to parallelize Commented Feb 24, 2018 at 19:43
  • No this question has a different issue since my code is not speeding up at all and my cpu usage stays at 100%. I also tried now compiling with -O0 what should remove all optimizations because you argued the compiler optimization but without luck Commented Feb 24, 2018 at 19:52

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

(Added note about the -Wall flag, &c.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.