simple loop slower with openmp than without it

General OpenMP discussion

simple loop slower with openmp than without it

Postby kwonitf » Wed Jan 18, 2012 1:05 pm

I tried to write a simple test.
I wrote a simple program:
Code: Select all

int main(int argc, char* argv[])
   int *tab = malloc(1000 * sizeof(int));
   double start = clock();

   int i;
   for( i = 0; i < 1000; i++)
      tab[i] = 2*i;
   double t = (double)clock() - (double)start/CLOCK_PER_SEC;
   printf("Elapsed time %.15f",t/1000);

And the time is 0.000000000000010

but when I added an omp to loop
Code: Select all
#pragma omp parallel for
for(i =0; i < 1000; i++)
   tab[i] = 2*i;

(or #pragma omp parallel shared(tab) private(i) )
the elapsed time is 20.000000 !
Why so slowly?
It should be faster, don't it? What is wrong in my omp code.
Help me, please.
Posts: 1
Joined: Wed Jan 18, 2012 12:56 pm

Re: simple loop slower with openmp than without it

Postby Genie » Thu Jan 19, 2012 4:17 am

I am not an expert in OpenMP but I think that the amount of work you are doing in parallel is very fine-grained.
The time spent in the runtime is significant when compared to the work you are doing in parallel.

Instead of "tab[i] = 2*i;" try to put something that needs more computation and time to execute (image processing code for example).

Programmers should always take into account the granularity of the tasks to do in parallel.
Posts: 4
Joined: Tue Jan 17, 2012 3:54 am

Re: simple loop slower with openmp than without it

Postby MarkB » Fri Feb 17, 2012 10:00 am

You have several problems here:

As Genie says, the amount of work is much too small to show any speedup.
The compiler is probably optimising the whole loop away in the sequential code, since the array tab is never used.
Your clock arithmetic is broken.
The clock resolution is probably too coarse in any case: try using omp_get_wtime() instead.
Posts: 524
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh

Return to Using OpenMP

Who is online

Users browsing this forum: Yahoo [Bot] and 5 guests