Parallel execution time of one thread could be faster?

General OpenMP discussion

Parallel execution time of one thread could be faster?

Postby ClaudiaWhite » Thu Aug 02, 2018 8:58 pm


I'd run the below code on one active core at Intel i5-2410M using the command:
$ gcc -fopenmp -g a.c -liomp5 -o a.exe

Code: Select all
  #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <omp.h>

    # define NPOINTS 2000
    # define MAXITER 2000

    struct complex{
      double real;
      double imag;

    int main(){
      int i, j, iter, numoutside = 0;
      double area, error, ztemp;
      struct complex z, c;

    #pragma omp parallel default(none) private(i,j,c,z,ztemp,iter) reduction(+:numoutside)
     #pragma omp for simd schedule (dynamic) collapse (2)
      for (i=0; i<NPOINTS; i++) {
        for (j=0; j<NPOINTS; j++) {
          c.real = -2.0+2.5*(double)(i)/(double)(NPOINTS)+1.0e-7;
          c.imag = 1.125*(double)(j)/(double)(NPOINTS)+1.0e-7;
          for (iter=0; iter<MAXITER; iter++){
       if ((z.real*z.real+z.imag*z.imag)>4.0e0) {
      } // end of pragma   

    *  Calculate area and error and output the results


          printf("Area of Mandlebrot set = %12.8f +/- %12.8f\n",area,error);


I run it with -fopenmp and without -fopenmp option. I found that, the execution of code could be faster with -fopenmp. Why? Isn't that if we run it with -fopenmp, there will be extra overhead and hence the execution time should be longer than serial execution?

It would be great if you can clear my doubt. Thank you.

Best Regards,
Posts: 7
Joined: Tue Jul 04, 2017 7:44 am

Re: Parallel execution time of one thread could be faster?

Postby aniruddha » Thu Aug 09, 2018 2:46 pm

Hi Claudia,

What do you mean by compiling it without -fopenmp ( how does it compile if you don't like it with open mp library ). Extra overhead of openmp is noticeable if you run the same code on one thread using openmp (should be) but if you run it with multiple threads then it should indeed run faster. What are the number of threads that you are using ?
Posts: 1
Joined: Thu Aug 02, 2018 1:46 pm

Re: Parallel execution time of one thread could be faster?

Postby MarkB » Tue Aug 14, 2018 2:11 am

I see no difference in performance using gcc 6.2.0 on and Intel E5-2695.

Compiling with OpenMP can add some overheads (in this case they will be negligible since there is only one parallel region which takes ~10-20 seconds to execute). On the other hand, the code transformations that are done to implement OpenMP can sometimes lead to slightly more efficient compilation, especially if the compiler optimisation level is low. You might want to try repeating the experiment with optimisation (-O2 or -O3) turned on. Adding -fopenmp has some other side effects, such as forcing all local variables to be stack allocated, which may have some minor performance impact on some codes.
Posts: 801
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh

Return to Using OpenMP

Who is online

Users browsing this forum: Google [Bot] and 10 guests