routines with initialization step

General OpenMP discussion

routines with initialization step

Postby FJacq » Fri Oct 26, 2012 2:30 am

I have many Fortran routines which contain an initialization phase at the first pass :

Code: Select all
subroutine a(...)
  integer,save :: ipass=0,ival
  if(ipass == 0) then
     ipass=1
     ival=...
     ...
  endif
  ...
end subroutine


Of course, I would like to call them in // regions. What is the best way to manage that ? Here is for instance a working solution when the initialization step is thread safe :

Code: Select all
subroutine a(...)
  integer,save :: ipass=0,ival
  !$OMP THREADPRIVATE(ipass,ival)
  if(ipass == 0) then
     ipass=1
     ival=...
     ...
  endif
  ...
end subroutine


But what to do if the initialization contains critical parts ? Should I choose for instance :

Code: Select all
subroutine a(...)
  integer,save :: ipass=0,ival
  !$OMP SINGLE
  if(ipass == 0) then
     ipass=1
     ival=...
     ...
     !$OMP FLUSH(ipass,ival)
  endif
  !$OMP END SINGLE
  ...
end subroutine
F. Jacq
FJacq
 
Posts: 2
Joined: Mon Jun 18, 2012 1:11 am
Location: France

Re: routines with initialization step

Postby ftinetti » Fri Oct 26, 2012 3:45 am

Hi F. Jacq,

I think that making threadprivate variable is fine if all you need is having thread-independent initialized and managed data, i.e. you do not need to share those variables among threads (and that depends on the subroutine's work, of course). Otherwise, the single + flush is fine, I think, but only for initialization, remember to synchronize and flush those variables each time you change their value.

Fernando.
ftinetti
 
Posts: 567
Joined: Wed Feb 10, 2010 2:44 pm

Re: routines with initialization step

Postby MarkB » Thu Nov 01, 2012 5:28 am

In your example with SINGLE all threads must call the routine in order to avoid deadlock at the implicit barrier (or did you mean CRITICAL here?). In any case, there is no need for a FLUSH directive, since this is implied at exit from the SINGLE (or CRITICAL) region.
MarkB
 
Posts: 422
Joined: Thu Jan 08, 2009 10:12 am


Return to Using OpenMP

Who is online

Users browsing this forum: No registered users and 4 guests