The efficiency of SECTIONS is better than FOR ??

General OpenMP discussion

The efficiency of SECTIONS is better than FOR ??

Postby TRX » Tue Dec 09, 2008 12:17 am

I have some sub sunctions f1(), f2(), ... ,fn() can be parallel execute.
I have two code version
V1 :
Code: Select all
#pragma omp parallel sections
{
  #pragma omp section
  {
     f1() ;
  }
  #pragma omp section
  {
     f2() ;
  }
  ...
  #pragma omp section
  {
     fn() ;
  }
}


and V2 :
Code: Select all
#pragma omp parallel for
for( int i=0; i<n; ++i )
{
  fn i () ;
}


The code size of V2 is much smaller than V1, but I find that V2 is a little slower than V1 ?
( just a few seconds measure by omp_get_wtime() )
Is the efficiency of SECTIONS better than FOR ??

the version info. of my gcc is following:
XXXXX :86_64-redhat-linux
XXXXX:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=x86_64-redhat-linux
thread model:posix
gcc version 4.1.2 20071124 (Red Hat 4.1.2-42)



And there is another problem about my openMP compiler :
the version of my gcc is 4.1.2, so I using openMP 2.5 to compile my code.
If I want to using openMP 3 .0 to compile my code, I have to use gcc gcc-4.3-branch,
but I fail to update my gcc due to some reasons .
My friend suggest that if I can link the binary file of gcc 4.3-branch,
my openMP will upgrade to 3.0. Is that true ??
If it is true, may I ask the binary file of gcc 4.3-branch ?

thax for help
Last edited by TRX on Wed Dec 10, 2008 10:27 pm, edited 1 time in total.
TRX
 

Re: The efficiency of SECTIONS is better than FOR ??

Postby ejd » Tue Dec 09, 2008 9:40 am

The code size of V2 is much slower than V1, but I find that V2 is a little slower than V1 ?
( just a few seconds measure by omp_get_wtime() )
Is the efficiency of SECTIONS better than FOR ??

I am afraid that I don't understand what you are saying. First you say "code size" and then you say "V2 is much slower than V1" and then "V2 is a little slower than V1".

What I can say, is that every implementation is different. I wrote a quick "benchmark" program to see what the overhead was for "parallel for" and "parallel sections" and ran it using the Sun Studio 12 compiler. For our implementation, the overhead cost is almost identical (within the margin of error for multiple runs). I don't know about the gcc implementation, but I know of no reason it should be different.

As for your second question:
And there is another problem about my openMP compiler :
the version of my gcc is 4.1.2, so I using openMP 2.5 to compile my code.
If I want to using openMP 3 .0 to compile my code, I have to use gcc gcc-4.3-branch,
but I fail to update my gcc due to some reasons .
My friend suggest that if I can link the binary file of gcc 4.3-branch,
my openMP will upgrade to 3.0. Is that true ??
If it is true, may I ask the binary file of gcc 4.3-branch ?

Again, I don't know the internals of the gcc implementation - but I would not recommend this approach. It might work for the OpenMP V2.5 features, but it is totally dependent on the implementation. As for the OpenMP V3.0 features, you need to have the compiler as well as the run time library to get it working. I would try and understand why the newer version of gcc isn't installing correctly, rather than trying this approach.
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Re: The efficiency of SECTIONS is better than FOR ??

Postby TRX » Tue Dec 09, 2008 11:36 pm

ejd wrote:I am afraid that I don't understand what you are saying. First you say "code size" and then you say "V2 is much slower than V1" and then "V2 is a little slower than V1".


I mean the following :
V1
Code: Select all
    #pragma omp parallel sections
    {
      #pragma omp section
      {
             x = a[1] ;         y = b[1] ;         x + y +  ...   
      }
      #pragma omp section
      {
             x = a[2] ;         y = b[2] ;         x + y +  ... 
      }
      ...
      #pragma omp section
      {
             x = a[n] ;         y = b[n] ;         x + y +  ... 
      }
    }


V2
Code: Select all
#pragma omp parallel for
for( int i=0; i<n; ++i )
{
    x = a[i] ;         y = b[i] ;         x + y +  ...  and some if - else judgment
}


so the code size of V1 is n* V2.
I have find why V1 is faster than V2. Because V2 has some if else judgment, V2 will slower than v1.
Sorry for my careless coding.

ejd wrote:Again, I don't know the internals of the gcc implementation - but I would not recommend this approach. It might work for the OpenMP V2.5 features, but it is totally dependent on the implementation. As for the OpenMP V3.0 features, you need to have the compiler as well as the run time library to get it working. I would try and understand why the newer version of gcc isn't installing correctly, rather than trying this approach.


Thanks for help~, I will keep trying install gcc-4.3-branch.
TRX
 

Re: The efficiency of SECTIONS is better than FOR ??

Postby ejd » Wed Dec 10, 2008 6:32 am

Thank you for your response, but I am still not sure I understand what you are saying. In your original example you were using a function call. In that case, the code size should not be that much different, since you are going to have a branch table lookup in the for loop. The sections code will be slightly larger, but not that much. In your second post, it is more obvious that the code size for Option V1 will be larger - but that wasn't your first example.

As for your second question, Option V2 should be the faster of the two options. The sections code is the one that will use "if-else" judgment. In fact, that is one way of implementing sections, by using a worksharing for with if-else statements.
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Re: The efficiency of SECTIONS is better than FOR ??

Postby TRX » Wed Dec 10, 2008 11:19 pm

ejd wrote: In your second post, it is more obvious that the code size for Option V1 will be larger - but that wasn't your first example.


Thank you for your response very much and sorry for my broken English. My first example want to sat that "The code size of V2 is much smaller than V1" but I write in "The code size of V2 is much slower than V1"..., so sorry~~ Orz


I have another Question about "sections" now. Th following code show us that O1 and O2 shared the global variable g, and their both use the same Critical Construct to protect g.
Code: Select all
#include <omp.h>
#include <stdio.h>
#include <iostream>
using namespace::std ;

int g = 0 ;

class A{
public :
  int id ;

  A( int id ) {
   id = id ;
  }

  void add( bool op )  {
    #pragma omp critical (id)
    {
      if( op==1 )   g++ ;
      else          g-- ;
    }
  }
};


int main()
{
  A O1 = A(0) ;
  A O2 = A(1) ;

  #pragma omp parallel sections
  {
    #pragma omp section
    {
      cout << omp_get_thread_num() << "  " ;   fflush( stdout ) ;

      for( int i=0; i<1000000; i++ )
      {
        O1.add( 1 ) ;
      }

    }

    #pragma omp section
    {
      cout << omp_get_thread_num() << "  " ;   fflush( stdout ) ;

      for( int i=0; i<1000000; i++ )
      {
        O2.add( 0 ) ;
      }
    }
  }

  cout << "g = "<< g << "\n" ;

  return 0 ;
}


Every thing looks good but that was not I want. we can see that O1 and O2 use the same Critical Construct id. How can I make O1 and O2 have their own Critical Construct ?? The compiler think the id is a string, not a integer I want. I have try many kind of ways but fail. How can I make it or it is impossible to do that ?

Again thanks foe everyone's response. :)
TRX
 

Re: The efficiency of SECTIONS is better than FOR ??

Postby ejd » Thu Dec 11, 2008 5:40 pm

If you look at the OpenMP V3.0 spec, section 2.8.2 critical Construct (which is pretty much the same as in the OpenMP V2.5 spec) states:
The syntax of the critical construct is as follows:
#pragma omp critical [(name)] new-line
structured-block
...
Identifiers used to identify a critical construct have external linkage and are in a name space that is separate from the name spaces used by labels, tags, members, and ordinary identifiers.

So you can see that "name" is an identifier and not a variable. As such, the code you have written has only one critical section with a name of "id".
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am


Return to Using OpenMP

Who is online

Users browsing this forum: Google [Bot] and 1 guest

cron