Paralleling the independent logics

General OpenMP discussion

Paralleling the independent logics

Postby pavelt » Sun Sep 28, 2008 12:05 am

Hi,

Could someone confirm my understanding of OpenMp is correct. I have code like

Code: Select all
void main()
{
    Logic1();
    Logic2();
}


By the design Logic1 and Logic2 are not depended on each other and they could be executed simultaneously. Is the following correct way to re-write this code using OpenMp

Code: Select all
void main()
{
   #pragma omp parallel
   {
      #pragma omp for schedule (static)
      for( int i=0; i<2; i++)
      {
         if( i == 0 )
            Logic1();
         if( i == 1 )
            Logic2();
      }
   }
}


Is there a simple notation?

Thank you.
pavelt
 
Posts: 1
Joined: Sat Sep 27, 2008 11:59 pm

Re: Paralleling the independent logics

Postby ejd » Sun Sep 28, 2008 7:40 am

What you have shown is one way to do it. Another way is:
Code: Select all
void main()
{
    #pragma omp parallel sections num_threads(2)
    {
        #pragma omp section
        {
            Logic1();
        }
        #pragma omp section
        {
            Logic2();
        }
    }
}

Note that you don't have to use the num_threads() clause. You could use a call to omp_set_dynamic() and omp_set_num_threads() or set environment variables to say how many processors to use.
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 5 guests