source: LMDZ5/branches/testing/libf/phylmd/rrtm/yomoml.F90 @ 1999

Last change on this file since 1999 was 1999, checked in by Laurent Fairhead, 11 years ago

Merged trunk changes r1920:1997 into testing branch

  • Property copyright set to
    Name of program: LMDZ
    Creation date: 1984
    Version: LMDZ5
    License: CeCILL version 2
    Holder: Laboratoire de m\'et\'eorologie dynamique, CNRS, UMR 8539
    See the license file in the root directory
File size: 5.7 KB
Line 
1MODULE YOMOML
2
3!-- the following system specific omp_lib-module is not always available (e.g. pgf90)
4!! use omp_lib
5
6USE PARKIND1  ,ONLY : JPIM, JPIB
7
8!**SS/18-Feb-2005
9!--Dr.Hook references removed, because these locks may also be
10!  called from within drhook.c itself !!
11!--Also, there could be considerable & unjustified overhead
12!  when using Dr.Hook in such a low level
13
14!**SS/15-Dec-2005
15!--The size of lock-variables are now OMP_LOCK_KIND as of in OMP_LIB,
16!  and OMP_LOCK_KIND is aliased to OML_LOCK_KIND
17!  OMP_LOCK_KIND is usually 4 in 32-bit addressing mode
18!                           8 in 64-bit addressing mode
19!--M_OML_LOCK changed to M_EVENT and kept as 32-bit int
20!--OML_FUNCT changed to OML_TEST_EVENT
21!--M_LOCK initialized to -1
22!--M_EVENT initialized to 0
23!--Added intent(s)
24!--Support for omp_lib (but not always available)
25!--Locks can now also be set/unset OUTSIDE the parallel regions
26!--Added routine OML_TEST_LOCK (attempts to set lock, but if *un*successful, does NOT  block)
27!--Buffer-zone for M_LOCK; now a vector of 2 elements in case problems/inconsistencies with OMP_LOCK_KIND 4/8
28
29!**SS/22-Feb-2006
30!--Locking routines are doing nothing unless OMP_GET_MAX_THREADS() > 1
31!  This is to avoid unacceptable deadlocks/timeouts with signal handlers when
32!  the only thread receives signal while inside locked region
33!--Affected routines: OML_TEST_LOCK()  --> always receives .TRUE.
34!                     OML_SET_LOCK()   --> sets nothing
35!                     OML_UNSET_LOCK() --> unsets nothing
36!                     OML_INIT_LOCK()  --> inits nothing
37
38!**SS/11-Sep-2006
39!--Added OML_DEBUG feature
40
41IMPLICIT NONE
42
43SAVE
44
45PRIVATE
46
47LOGICAL :: OML_DEBUG = .FALSE.
48
49PUBLIC OML_WAIT_EVENT, OML_SET_EVENT, OML_INCR_EVENT, &
50   &   OML_MY_THREAD,  OML_MAX_THREADS , OML_OMP, &
51   &   OML_IN_PARALLEL, OML_TEST_EVENT, &
52   &   OML_UNSET_LOCK, OML_INIT_LOCK, OML_SET_LOCK, OML_DESTROY_LOCK, &
53   &   OML_LOCK_KIND, OML_TEST_LOCK, OML_DEBUG
54
55!-- The following should normally be 4 in 32-bit addressing mode
56!                                    8 in 64-bit addressing mode
57! Since system specific omp_lib-module is not always available (e.g. pgf90)
58! we hardcode OML_LOCK_KIND to JPIB (usually 8) for now
59!!INTEGER(KIND=JPIM), PARAMETER :: OML_LOCK_KIND = OMP_LOCK_KIND
60INTEGER(KIND=JPIM), PARAMETER :: OML_LOCK_KIND = JPIB
61
62!-- Note: Still JPIM !!
63INTEGER(KIND=JPIM) :: M_EVENT = 0
64
65!-- Note: OML_LOCK_KIND, not JPIM !!
66INTEGER(KIND=OML_LOCK_KIND) :: M_LOCK(2) = (/-1, -1/)
67
68CONTAINS
69
70FUNCTION OML_OMP()
71LOGICAL :: OML_OMP
72OML_OMP=.FALSE.
73!$ OML_OMP=.TRUE.
74END FUNCTION OML_OMP
75
76FUNCTION OML_IN_PARALLEL()
77LOGICAL :: OML_IN_PARALLEL
78!$ LOGICAL :: OMP_IN_PARALLEL
79!$ INTEGER(KIND=JPIM) OMP_GET_MAX_THREADS
80OML_IN_PARALLEL=.FALSE.
81!$ OML_IN_PARALLEL=((OMP_GET_MAX_THREADS() > 1).AND.OMP_IN_PARALLEL())
82END FUNCTION OML_IN_PARALLEL
83
84FUNCTION OML_TEST_LOCK(MYLOCK)
85INTEGER(KIND=OML_LOCK_KIND),intent(inout),optional :: MYLOCK
86LOGICAL :: OML_TEST_LOCK
87!$ LOGICAL :: OMP_TEST_LOCK
88!$ INTEGER(KIND=JPIM) OMP_GET_MAX_THREADS
89OML_TEST_LOCK = .TRUE.
90!$ IF(OMP_GET_MAX_THREADS() > 1) THEN
91!$   IF(PRESENT(MYLOCK))THEN
92!$     OML_TEST_LOCK = OMP_TEST_LOCK(MYLOCK)
93!$   ELSE
94!$     OML_TEST_LOCK = OMP_TEST_LOCK(M_LOCK(1))
95!$   ENDIF
96!$ ENDIF
97END FUNCTION OML_TEST_LOCK
98
99SUBROUTINE OML_UNSET_LOCK(MYLOCK)
100INTEGER(KIND=OML_LOCK_KIND),intent(inout),optional :: MYLOCK
101!$ INTEGER(KIND=JPIM) OMP_GET_MAX_THREADS
102!$ IF(OMP_GET_MAX_THREADS() > 1) THEN
103!$   IF(PRESENT(MYLOCK))THEN
104!$     CALL OMP_UNSET_LOCK(MYLOCK)
105!$   ELSE
106!$     CALL OMP_UNSET_LOCK(M_LOCK(1))
107!$   ENDIF
108!$ ENDIF
109END SUBROUTINE OML_UNSET_LOCK
110
111SUBROUTINE OML_SET_LOCK(MYLOCK)
112INTEGER(KIND=OML_LOCK_KIND),intent(inout),optional :: MYLOCK
113!$ INTEGER(KIND=JPIM) OMP_GET_MAX_THREADS
114!$ IF(OMP_GET_MAX_THREADS() > 1) THEN
115!$   IF(PRESENT(MYLOCK))THEN
116!$     CALL OMP_SET_LOCK(MYLOCK)
117!$   ELSE
118!$     CALL OMP_SET_LOCK(M_LOCK(1))
119!$   ENDIF
120!$ ENDIF
121END SUBROUTINE OML_SET_LOCK
122
123SUBROUTINE OML_INIT_LOCK(MYLOCK)
124INTEGER(KIND=OML_LOCK_KIND),intent(inout),optional :: MYLOCK
125!$ INTEGER(KIND=JPIM) OMP_GET_MAX_THREADS
126!$ IF(OMP_GET_MAX_THREADS() > 1) THEN
127!$   IF(PRESENT(MYLOCK))THEN
128!$     CALL OMP_INIT_LOCK(MYLOCK)
129!$   ELSE
130!$     CALL OMP_INIT_LOCK(M_LOCK(1))
131!$   ENDIF
132!$ ENDIF
133END SUBROUTINE OML_INIT_LOCK
134
135SUBROUTINE OML_DESTROY_LOCK(MYLOCK)
136INTEGER(KIND=OML_LOCK_KIND),intent(inout),optional :: MYLOCK
137!$ IF(PRESENT(MYLOCK))THEN
138!$   CALL OMP_DESTROY_LOCK(MYLOCK)
139!$ ELSE
140!$   CALL OMP_DESTROY_LOCK(M_LOCK(1))
141!$ ENDIF
142END SUBROUTINE OML_DESTROY_LOCK
143
144FUNCTION OML_TEST_EVENT(K,MYEVENT)
145LOGICAL :: OML_TEST_EVENT
146INTEGER(KIND=JPIM),intent(in) :: K,MYEVENT
147IF(K.EQ.MYEVENT) THEN
148 OML_TEST_EVENT =.TRUE.
149ELSE
150 OML_TEST_EVENT=.FALSE.
151ENDIF
152END FUNCTION OML_TEST_EVENT
153
154SUBROUTINE OML_WAIT_EVENT(K,MYEVENT)
155INTEGER(KIND=JPIM),intent(in) :: K
156INTEGER(KIND=JPIM),intent(in),OPTIONAL :: MYEVENT
157IF(PRESENT(MYEVENT))THEN
158  DO
159    IF(OML_TEST_EVENT(K,MYEVENT)) EXIT
160  ENDDO
161ELSE
162  DO
163    IF(OML_TEST_EVENT(K,M_EVENT)) EXIT
164  ENDDO
165ENDIF
166END SUBROUTINE OML_WAIT_EVENT
167
168SUBROUTINE OML_SET_EVENT(K,MYEVENT)
169INTEGER(KIND=JPIM),intent(in) :: K
170INTEGER(KIND=JPIM),intent(out),OPTIONAL :: MYEVENT
171IF(PRESENT(MYEVENT))THEN
172  MYEVENT=K
173ELSE
174  M_EVENT=K
175ENDIF
176END SUBROUTINE OML_SET_EVENT
177
178SUBROUTINE OML_INCR_EVENT(K,MYEVENT)
179INTEGER(KIND=JPIM) :: K
180INTEGER(KIND=JPIM),intent(inout),OPTIONAL :: MYEVENT
181IF(PRESENT(MYEVENT))THEN
182  MYEVENT=MYEVENT+K
183ELSE
184  M_EVENT=M_EVENT+K
185ENDIF
186END SUBROUTINE OML_INCR_EVENT
187
188FUNCTION OML_MY_THREAD()
189INTEGER(KIND=JPIM) :: OML_MY_THREAD
190!$ INTEGER(KIND=JPIM) OMP_GET_THREAD_NUM
191OML_MY_THREAD = 1
192!$ OML_MY_THREAD = OMP_GET_THREAD_NUM() + 1
193END FUNCTION OML_MY_THREAD
194
195FUNCTION OML_MAX_THREADS()
196INTEGER(KIND=JPIM) :: OML_MAX_THREADS
197!$ INTEGER(KIND=JPIM) OMP_GET_MAX_THREADS
198OML_MAX_THREADS = 1
199!$ OML_MAX_THREADS = OMP_GET_MAX_THREADS()
200END FUNCTION OML_MAX_THREADS
201
202END MODULE YOMOML
Note: See TracBrowser for help on using the repository browser.