1 |
C $Header: /u/gcmpack/MITgcm/pkg/autodiff/global_max_ad.F,v 1.3 2008/06/20 21:43:19 heimbach Exp $ |
2 |
C $Name: $ |
3 |
|
4 |
#include "CPP_EEOPTIONS.h" |
5 |
|
6 |
C-- File global_max.F: Routines that perform global max reduction on an array |
7 |
C of thread values. |
8 |
C |
9 |
C Adjoint routines are identical to forward routines, |
10 |
C but parameter list is reversed to be consistent with |
11 |
C call statement generated by TAMC/TAF |
12 |
C P. Heimbach, 16-Apr-2002 |
13 |
C |
14 |
C Contents |
15 |
C o GLOBAL_ADMAX_R4 |
16 |
C o GLOBAL_ADMAX_R8 |
17 |
|
18 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
19 |
CBOP |
20 |
C !ROUTINE: GLOBAL_ADMAX_R4 |
21 |
|
22 |
C !INTERFACE: |
23 |
#ifdef AUTODIFF_TAMC_COMPATIBILITY |
24 |
SUBROUTINE GLOBAL_ADMAX_R4( |
25 |
I myThid, |
26 |
U maxPhi |
27 |
& ) |
28 |
#else |
29 |
SUBROUTINE GLOBAL_ADMAX_R4( |
30 |
U maxPhi, |
31 |
I myThid |
32 |
& ) |
33 |
#endif |
34 |
|
35 |
C !DESCRIPTION: |
36 |
C *==========================================================* |
37 |
C | SUBROUTINE GLOBAL_ADMAX_R4 |
38 |
C | o Handle max for real*4 data. |
39 |
C *==========================================================* |
40 |
C | Perform max on array of one value per thread and then |
41 |
C | max result of all the processes. |
42 |
C | Notes |
43 |
C | ===== |
44 |
C | Within a process only one thread does the max, each |
45 |
C | thread is assumed to have already maxed its local data. |
46 |
C | The same thread also does the inter-process max for |
47 |
C | example with MPI and then writes the result into a shared |
48 |
C | location. All threads wait until the max is avaiailable. |
49 |
C *==========================================================* |
50 |
|
51 |
C !USES: |
52 |
IMPLICIT NONE |
53 |
|
54 |
C == Global data == |
55 |
#include "SIZE.h" |
56 |
#include "EEPARAMS.h" |
57 |
#include "EESUPPORT.h" |
58 |
#include "GLOBAL_MAX.h" |
59 |
|
60 |
C !INPUT/OUTPUT PARAMETERS: |
61 |
C == Routine arguments == |
62 |
C maxPhi :: Result of max. |
63 |
C myThid :: My thread id. |
64 |
Real*4 maxPhi |
65 |
INTEGER myThid |
66 |
|
67 |
C !LOCAL VARIABLES: |
68 |
C == Local variables == |
69 |
C I :: Loop counters |
70 |
C mpiRC :: MPI return code |
71 |
INTEGER I |
72 |
Real*4 tmp |
73 |
#ifdef ALLOW_USE_MPI |
74 |
INTEGER mpiRC |
75 |
#endif /* ALLOW_USE_MPI */ |
76 |
CEOP |
77 |
|
78 |
C-- write local max into array |
79 |
phiGMR4(1,myThid) = maxPhi |
80 |
|
81 |
C-- Can not start until everyone is ready |
82 |
CALL BAR2( myThid ) |
83 |
|
84 |
C-- Max within the process first |
85 |
_BEGIN_MASTER( myThid ) |
86 |
tmp = phiGMR4(1,1) |
87 |
DO I=2,nThreads |
88 |
tmp = MAX(tmp,phiGMR4(1,I)) |
89 |
ENDDO |
90 |
maxPhi = tmp |
91 |
#ifdef ALLOW_USE_MPI |
92 |
#ifndef ALWAYS_USE_MPI |
93 |
IF ( usingMPI ) THEN |
94 |
#endif |
95 |
CALL MPI_Allreduce(tmp,maxPhi,1,MPI_REAL,MPI_MAX, |
96 |
& MPI_COMM_MODEL,mpiRC) |
97 |
#ifndef ALWAYS_USE_MPI |
98 |
ENDIF |
99 |
#endif |
100 |
#endif /* ALLOW_USE_MPI */ |
101 |
phiGMR4(1,0) = maxPhi |
102 |
_END_MASTER( myThid ) |
103 |
C-- |
104 |
CALL BAR2( myThid ) |
105 |
|
106 |
C-- set result for every process |
107 |
maxPhi = phiGMR4(1,0) |
108 |
|
109 |
RETURN |
110 |
END |
111 |
|
112 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
113 |
CBOP |
114 |
C !ROUTINE: GLOBAL_ADMAX_R8 |
115 |
|
116 |
C !INTERFACE: |
117 |
#ifdef AUTODIFF_TAMC_COMPATIBILITY |
118 |
SUBROUTINE GLOBAL_ADMAX_R8( |
119 |
I myThid, |
120 |
U maxPhi |
121 |
& ) |
122 |
#else |
123 |
SUBROUTINE GLOBAL_ADMAX_R8( |
124 |
U maxPhi, |
125 |
I myThid |
126 |
& ) |
127 |
#endif |
128 |
|
129 |
C !DESCRIPTION: |
130 |
C *==========================================================* |
131 |
C | SUBROUTINE GLOBAL_ADMAX_R8 |
132 |
C | o Handle max for real*8 data. |
133 |
C *==========================================================* |
134 |
C | Perform max on array of one value per thread and then |
135 |
C | max result of all the processes. |
136 |
C | Notes |
137 |
C | ===== |
138 |
C | Within a process only one thread does the max, each |
139 |
C | thread is assumed to have already maxed its local data. |
140 |
C | The same thread also does the inter-process max for |
141 |
C | example with MPI and then writes the result into a shared |
142 |
C | location. All threads wait until the max is avaiailable. |
143 |
C *==========================================================* |
144 |
|
145 |
C !USES: |
146 |
IMPLICIT NONE |
147 |
|
148 |
C === Global data === |
149 |
#include "SIZE.h" |
150 |
#include "EEPARAMS.h" |
151 |
#include "EESUPPORT.h" |
152 |
#include "GLOBAL_MAX.h" |
153 |
|
154 |
C !INPUT/OUTPUT PARAMETERS: |
155 |
C === Routine arguments === |
156 |
C maxPhi :: Result of max. |
157 |
C myThid :: My thread id. |
158 |
Real*8 maxPhi |
159 |
INTEGER myThid |
160 |
|
161 |
C !LOCAL VARIABLES: |
162 |
C === Local variables === |
163 |
C I :: Loop counters |
164 |
C mpiRC :: MPI return code |
165 |
INTEGER I |
166 |
Real*8 tmp |
167 |
#ifdef ALLOW_USE_MPI |
168 |
INTEGER mpiRC |
169 |
#endif /* ALLOW_USE_MPI */ |
170 |
CEOP |
171 |
|
172 |
C-- write local max into array |
173 |
phiGMR8(1,myThid) = maxPhi |
174 |
|
175 |
C-- Can not start until everyone is ready |
176 |
CALL BAR2( myThid ) |
177 |
|
178 |
C-- Max within the process first |
179 |
_BEGIN_MASTER( myThid ) |
180 |
tmp = phiGMR8(1,1) |
181 |
DO I=2,nThreads |
182 |
tmp = MAX(tmp,phiGMR8(1,I)) |
183 |
ENDDO |
184 |
maxPhi = tmp |
185 |
#ifdef ALLOW_USE_MPI |
186 |
#ifndef ALWAYS_USE_MPI |
187 |
IF ( usingMPI ) THEN |
188 |
#endif |
189 |
CALL MPI_Allreduce(tmp,maxPhi,1,MPI_DOUBLE_PRECISION,MPI_MAX, |
190 |
& MPI_COMM_MODEL,mpiRC) |
191 |
#ifndef ALWAYS_USE_MPI |
192 |
ENDIF |
193 |
#endif |
194 |
#endif /* ALLOW_USE_MPI */ |
195 |
C-- Write solution to place where all threads can see it |
196 |
phiGMR8(1,0) = maxPhi |
197 |
_END_MASTER( myThid ) |
198 |
|
199 |
C-- Do not leave until we are sure that the max is done |
200 |
CALL BAR2( myThid ) |
201 |
|
202 |
C-- set result for every process |
203 |
maxPhi = phiGMR8(1,0) |
204 |
|
205 |
RETURN |
206 |
END |