scope

General OpenMP discussion

scope

Postby enrico » Thu Sep 04, 2008 9:18 am

hello,

could anyone refer me to a good tutorial on the scoping of variables in openmp? For instance, if I call a function from within a parallelized do loop, are the local variables of that function private to each thread? I'm using fortran 90, so among the local variable I have arrays: are they private to each thread too? I read somewhere they are not... Can I make them thread private by default (I'm using the intel fortran compiler)?

thanks for helping

cheers

enrico
enrico
 
Posts: 8
Joined: Wed Jul 16, 2008 3:32 pm

Re: scope

Postby ejd » Fri Sep 05, 2008 7:19 am

There are a lot of tutorials out on the web for OpenMP. Most of them cover pretty much the same ground - the basics of OpenMP. Unfortunately I don't know them all and have no idea what to suggest as a tutorial for scoping. Maybe someone else has run across something they feel is good and could recommend.

The OpenMP spec (V2.5) has some information in Section 2.8 Data Environment. This section is divided into two sub-sections:

  • 2.8.1.1 Sharing Attribute Rules for Variables Referenced in a Construct
  • 2.8.1.2 Sharing Attribute Rules for Variables Referenced in a Region, but not in a Construct
The seccond section applies to your question and you will see the following:
Code: Select all
The sharing attributes of variables which are referenced in a region, but not in a construct, are determined as follows:
...
------------------------ Fortran ------------------------
• Local variables declared in called routines in the region and that have the save attribute, or that are data initialized, are shared unless they appear in a threadprivate directive.
...
• Other local variables declared in called routines in the region are private.

If all else fails, you can always see what the implementation is doing by writing a small program something like this:
Code: Select all
program test
  use omp_lib
  implicit none
  integer i

  call omp_set_dynamic(.false.)
  call omp_set_num_threads(2)

  !$omp parallel do
  do i=1, 2
    call sub()
  enddo   
end program test

subroutine sub()
  use omp_lib
  implicit none
  integer a1, b1(3)
  integer, save :: a2, b2(3)
  integer :: a3 = 1, b3(3) = 1

  print *, "t#:", omp_get_thread_num(), "  loc1:", loc(a1), loc(b1)
  print *, "t#:", omp_get_thread_num(), "  loc2:", loc(a2), loc(b2)
  print *, "t#:", omp_get_thread_num(), "  loc3:", loc(a3), loc(b3)
end subroutine sub

Which gives you output like this:
Code: Select all
t#: 1   loc1: -33555028 -33555040
t#: 1   loc2: 136636 136640
t#: 1   loc3: 136580 136584
t#: 0   loc1: -4196428 -4196440
t#: 0   loc2: 136636 136640
t#: 0   loc3: 136580 136584

Looking at this output, you can see that for "loc1" the addresses of the variables are different for thread 0 and 1, which means that they are private. Looking at "loc2" you will see that they are the same for thread 0 and 1, so they are shared - as are the variables for "loc3". This goes along with what the spec says they should be. And yes - for the experts out there - the program is based on an assumption that I haven't stated.
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Re: scope

Postby enrico » Mon Sep 08, 2008 12:51 am

Hi Ejd,

thanks for your reply. I need to think about it, I'll come back later

cheers

E
enrico
 
Posts: 8
Joined: Wed Jul 16, 2008 3:32 pm


Return to Using OpenMP

Who is online

Users browsing this forum: No registered users and 6 guests

cron