[Omp] slow performance

Alexander Spiegel spiegel at rz.rwth-aachen.de
Fri Dec 17 00:45:29 PST 2004


Hi, Andrew.

Try to accumulate the time inside the parallel region
(for example the thread with id 0 can do this)
and you will see the overhead costs of the parallel directive.

Some other hints:
- Do not use the 'time' function because of its low granularity.
'gettimeofday' is a better choice for such small work.

- The id variable should be declared as thread private in your example.


Alexander Spiegel



andrew wang wrote:
> Hello,
> 
> I am pretty new to OpenMP, before do some real thing, I write a simple 
> code to test my understanding. But to my
> big suprise, I see that the result is quite different from what I can 
> imagine. The more threads I have, the more
> slow the calculation is.
> 
> Can you help me to take a look at the source code and the result I got? 
> Is it too much fork/join, but little time on calculation in each thread?
> 
> Thanks
> Andrew
> 
> 
> 
> 
> 
> source:
> 
> #include <omp.h>
> #include <time.h>
> #include <stdio.h>
> 
> static long num_steps = 1000000000L;
> double step;
> 
> int omp_threads = 1;
> 
> void main (int argc,char **argv) {
> 
>   double  x, sum, pi=0.0;
>   int id;
>   time_t t1,t2,t11,t22;
>   int ii, jj, kk, i;
>   int total_time=0;
> 
>   (void) time(&t1);
> 
> 
>   omp_threads=atoi(argv[1]);
> 
> 
>   printf("omp_get_num_procs=%d\n",omp_get_num_procs());
> 
>   step = 1.0/(double) num_steps;
>   omp_set_num_threads(omp_threads);
> 
> 
>   for (i=0;i<16200;i++) {
> 
>        for (ii=0; ii<50; ii++)
>        {
> 
>            (void) time(&t11);
>         #pragma omp parallel private (jj,kk,x, sum)
>         {
> 
>                id = omp_get_thread_num();
> 
>                for (jj=id;jj<3; jj=jj+omp_threads )
>                 {
> 
>                    for (kk=0; kk< 50; kk ++){
> 
> 
>                        x = (kk+0.5)*step;
>                        sum += 4.0/(1.0+x*x);   // more complicated 
> calculation here.
>                    }
> 
>                    pi += sum;
>              }
>         }
> 
>         (void) time(&t22);
>                total_time += (long)(t22-t11);
> 
>        }
>   }
> 
> 
>   id = omp_get_thread_num();
> 
> 
>   printf("Parallel region time=%d seconds\n", (int) total_time);
> 
>   (void) time(&t2);
>   printf("Total time = %d seconds\n", (int) (t2-t1));
> 
> }
> 
> result:
> 
> 
> atlas1> a.out 1
> omp_get_num_procs=4
> Parallel region time=2 seconds
> Total time = 3 seconds
> atlas1> a.out 2
> omp_get_num_procs=4
> Parallel region time=41 seconds
> Total time = 41 seconds
> atlas1> a.out 3
> omp_get_num_procs=4
> Parallel region time=71 seconds
> Total time = 72 seconds
> 
> 
> 
> _______________________________________________
> Omp mailing list
> Omp at openmp.org
> http://openmp.org/mailman/listinfo/omp_openmp.org
> 






More information about the Omp mailing list