For codes that use PMTM in multiple modules and threads, the timer handles will need to be made available to all modules that want to control the timers. The easiest way is to make them explicitly threadprivate as shown below. If the timers and their uses do not occur in the same parallel region as might be suggested if using a solution similar to the serial version of the subroutine below, then it is important to observe the conditions about preservation of threadprivate variables as noted in section ”2.9.2 threadprivate Directive” of the OpenMP 3.0 standard. To preserve values, this suggests parallel regions should not be nested, should have identical thread counts and have the dynamic adjustment feature set to false.
:::fortran
module openmp_pmtm_timers
use PMTM
implicit none
type timers_type
type(pmtm_timer) :: timer1
type(pmtm_timer) :: timer2
type(pmtm_timer) :: timer3
end type
type(timers_type) :: timers
common /timers_private/ timers
!$omp threadprivate (/timers_private/)
contains
! Call this interface if the timers should be created from a serial context prior to
! any parallel work.
subroutine create_timers_serial()
!$omp parallel
call create_timers_parallel
!$omp end parallel
end subroutine
! Call this interface if already within a "parallel" region.
subroutine create_timers_parallel()
call create_timer(timers%timer1 , "Timer 1")
call create_timer(timers%timer2 , "Timer 2")
call create_timer(timers%timer3 , "Timer 3")
endsubroutine create_timers_parallel
subroutine create_timer(timer, timer_name)
type(pmtm_timer), intent(out) :: timer
character(len=*), intent(in) :: timer_name
call PMTM_create_timer(PMTM_DEFAULT_GROUP, timer, &
timer_name , PMTM_TIMER_ALL , pmtm_status)
if (pmtm_status /= 0) then
write(6,*) PMTM_get_error_message(pmtm_status)
endif
endsubroutine create_timer
end module