Unexpected parallelism

General OpenMP discussion

Unexpected parallelism

Postby bimargulies » Sun Sep 28, 2008 10:37 am

I've just started to use openmp. I'm using the version provided with g++ 4.2 by Apple on MacOS.

According to the reading I did on an Intel web site, I expected

#pragma omp parallel

to have no effect except to set up some mechanism, which would then get used the first time I said something like

#pragma omp sections

or something like that.

So, I went to a function that contained some parallelization fodder. I added the initial pragma at the top, and then decorated some for loops.

Imagine my surprise when I started to see two copies of some messages printed by the code after the first pragma and before the first loop or section! Not to mention heap explosions from two threads trying to do some other things that should only have been done once.

Could someone point me at whatever 'for dummies' thing I'm missing?
bimargulies
 
Posts: 7
Joined: Sun Sep 28, 2008 10:30 am

Re: Unexpected parallelism

Postby ejd » Sun Sep 28, 2008 5:06 pm

From the OpenMP V3.0 spec, section 2.4 parallel Construct:
When a thread encounters a parallel construct, a team of threads is created to execute the parallel region.
The thread that encountered the parallel construct becomes the master thread of the new team, with a thread number of zero for the duration of the new parallel region.
All threads in the new team, including the master thread, execute the region.

So your understanding is not quite correct. Take the following simple example:
Code: Select all
#include <omp.h>
#include <stdio.h>
int main(void) {
  #pragma omp parallel num_threads(2)
  {
    printf("executed by thread #: %i\n", omp_get_thread_num());
  }
  return 0;
}

% cc -xopenmp a.c
% a.out
executed by thread #: 0
executed by thread #: 1

So the "initial" thread starts execution of the program. When it encounters the parallel directive, a team of two threads is created (the "initial" thread becomes the master thread of the team and a new thread is added to the team). All threads in the team then execute the code within the parallel region (in this case the printf).

The OpenMP for directive and sections directive are worksharing constructs. They have a different meaning. For these constructs, the work is shared by members of the team and "each unit of work is executed exactly once by one of the threads in the team". For eample, take the following program:
Code: Select all
#include <omp.h>
#include <stdio.h>
int main(void) {
  #pragma omp parallel sections num_threads(2)
  {
    #pragma omp section
    {
      printf("section 1 executed by thread #: %i\n", omp_get_thread_num());
    }
    #pragma omp section
    {
      printf("section 2 executed by thread #: %i\n", omp_get_thread_num());
    }
  }
  return 0;
}

% cc -xopenmp a.c
% a.out
section 1 executed by thread #: 0
section 2 executed by thread #: 1

Now you see each section only executed by one thread.
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Re: Unexpected parallelism

Postby bimargulies » Mon Sep 29, 2008 7:17 am

Thanks. I note that the example in the following carefully avoids any ordinary code between the plain parallel pragma and the for loops. Still, it strikes me as a bit misleading.

http://software.intel.com/en-us/article ... ith-openmp
bimargulies
 
Posts: 7
Joined: Sun Sep 28, 2008 10:30 am

Re: Unexpected parallelism

Postby ejd » Mon Sep 29, 2008 8:38 am

Thank you for your comments. The section on "Master and Single" looks like it sort of covers this. However, I will forward your comment to some of the Intel people and let them decide.
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am


Return to Using OpenMP

Who is online

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