1 | MODULE YOMOML |
---|
2 | |
---|
3 | !-- the following system specific omp_lib-module is not always available (e.g. pgf90) |
---|
4 | !! use omp_lib |
---|
5 | |
---|
6 | USE 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 | |
---|
41 | IMPLICIT NONE |
---|
42 | |
---|
43 | SAVE |
---|
44 | |
---|
45 | PRIVATE |
---|
46 | |
---|
47 | LOGICAL :: OML_DEBUG = .FALSE. |
---|
48 | |
---|
49 | PUBLIC 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 |
---|
60 | INTEGER(KIND=JPIM), PARAMETER :: OML_LOCK_KIND = JPIB |
---|
61 | |
---|
62 | !-- Note: Still JPIM !! |
---|
63 | INTEGER(KIND=JPIM) :: M_EVENT = 0 |
---|
64 | |
---|
65 | !-- Note: OML_LOCK_KIND, not JPIM !! |
---|
66 | INTEGER(KIND=OML_LOCK_KIND) :: M_LOCK(2) = (/-1, -1/) |
---|
67 | |
---|
68 | CONTAINS |
---|
69 | |
---|
70 | FUNCTION OML_OMP() |
---|
71 | LOGICAL :: OML_OMP |
---|
72 | OML_OMP=.FALSE. |
---|
73 | !$ OML_OMP=.TRUE. |
---|
74 | END FUNCTION OML_OMP |
---|
75 | |
---|
76 | FUNCTION OML_IN_PARALLEL() |
---|
77 | LOGICAL :: OML_IN_PARALLEL |
---|
78 | !$ LOGICAL :: OMP_IN_PARALLEL |
---|
79 | !$ INTEGER(KIND=JPIM) OMP_GET_MAX_THREADS |
---|
80 | OML_IN_PARALLEL=.FALSE. |
---|
81 | !$ OML_IN_PARALLEL=((OMP_GET_MAX_THREADS() > 1).AND.OMP_IN_PARALLEL()) |
---|
82 | END FUNCTION OML_IN_PARALLEL |
---|
83 | |
---|
84 | FUNCTION OML_TEST_LOCK(MYLOCK) |
---|
85 | INTEGER(KIND=OML_LOCK_KIND),intent(inout),optional :: MYLOCK |
---|
86 | LOGICAL :: OML_TEST_LOCK |
---|
87 | !$ LOGICAL :: OMP_TEST_LOCK |
---|
88 | !$ INTEGER(KIND=JPIM) OMP_GET_MAX_THREADS |
---|
89 | OML_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 |
---|
97 | END FUNCTION OML_TEST_LOCK |
---|
98 | |
---|
99 | SUBROUTINE OML_UNSET_LOCK(MYLOCK) |
---|
100 | INTEGER(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 |
---|
109 | END SUBROUTINE OML_UNSET_LOCK |
---|
110 | |
---|
111 | SUBROUTINE OML_SET_LOCK(MYLOCK) |
---|
112 | INTEGER(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 |
---|
121 | END SUBROUTINE OML_SET_LOCK |
---|
122 | |
---|
123 | SUBROUTINE OML_INIT_LOCK(MYLOCK) |
---|
124 | INTEGER(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 |
---|
133 | END SUBROUTINE OML_INIT_LOCK |
---|
134 | |
---|
135 | SUBROUTINE OML_DESTROY_LOCK(MYLOCK) |
---|
136 | INTEGER(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 |
---|
142 | END SUBROUTINE OML_DESTROY_LOCK |
---|
143 | |
---|
144 | FUNCTION OML_TEST_EVENT(K,MYEVENT) |
---|
145 | LOGICAL :: OML_TEST_EVENT |
---|
146 | INTEGER(KIND=JPIM),intent(in) :: K,MYEVENT |
---|
147 | IF(K.EQ.MYEVENT) THEN |
---|
148 | OML_TEST_EVENT =.TRUE. |
---|
149 | ELSE |
---|
150 | OML_TEST_EVENT=.FALSE. |
---|
151 | ENDIF |
---|
152 | END FUNCTION OML_TEST_EVENT |
---|
153 | |
---|
154 | SUBROUTINE OML_WAIT_EVENT(K,MYEVENT) |
---|
155 | INTEGER(KIND=JPIM),intent(in) :: K |
---|
156 | INTEGER(KIND=JPIM),intent(in),OPTIONAL :: MYEVENT |
---|
157 | IF(PRESENT(MYEVENT))THEN |
---|
158 | DO |
---|
159 | IF(OML_TEST_EVENT(K,MYEVENT)) EXIT |
---|
160 | ENDDO |
---|
161 | ELSE |
---|
162 | DO |
---|
163 | IF(OML_TEST_EVENT(K,M_EVENT)) EXIT |
---|
164 | ENDDO |
---|
165 | ENDIF |
---|
166 | END SUBROUTINE OML_WAIT_EVENT |
---|
167 | |
---|
168 | SUBROUTINE OML_SET_EVENT(K,MYEVENT) |
---|
169 | INTEGER(KIND=JPIM),intent(in) :: K |
---|
170 | INTEGER(KIND=JPIM),intent(out),OPTIONAL :: MYEVENT |
---|
171 | IF(PRESENT(MYEVENT))THEN |
---|
172 | MYEVENT=K |
---|
173 | ELSE |
---|
174 | M_EVENT=K |
---|
175 | ENDIF |
---|
176 | END SUBROUTINE OML_SET_EVENT |
---|
177 | |
---|
178 | SUBROUTINE OML_INCR_EVENT(K,MYEVENT) |
---|
179 | INTEGER(KIND=JPIM) :: K |
---|
180 | INTEGER(KIND=JPIM),intent(inout),OPTIONAL :: MYEVENT |
---|
181 | IF(PRESENT(MYEVENT))THEN |
---|
182 | MYEVENT=MYEVENT+K |
---|
183 | ELSE |
---|
184 | M_EVENT=M_EVENT+K |
---|
185 | ENDIF |
---|
186 | END SUBROUTINE OML_INCR_EVENT |
---|
187 | |
---|
188 | FUNCTION OML_MY_THREAD() |
---|
189 | INTEGER(KIND=JPIM) :: OML_MY_THREAD |
---|
190 | !$ INTEGER(KIND=JPIM) OMP_GET_THREAD_NUM |
---|
191 | OML_MY_THREAD = 1 |
---|
192 | !$ OML_MY_THREAD = OMP_GET_THREAD_NUM() + 1 |
---|
193 | END FUNCTION OML_MY_THREAD |
---|
194 | |
---|
195 | FUNCTION OML_MAX_THREADS() |
---|
196 | INTEGER(KIND=JPIM) :: OML_MAX_THREADS |
---|
197 | !$ INTEGER(KIND=JPIM) OMP_GET_MAX_THREADS |
---|
198 | OML_MAX_THREADS = 1 |
---|
199 | !$ OML_MAX_THREADS = OMP_GET_MAX_THREADS() |
---|
200 | END FUNCTION OML_MAX_THREADS |
---|
201 | |
---|
202 | END MODULE YOMOML |
---|