1 |
C $Header: /u/gcmpack/MITgcm/eesupp/src/global_max.F,v 1.12 2009/06/10 03:47:03 jmc 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 Contents |
9 |
C o GLOBAL_MAX_R4 |
10 |
C o GLOBAL_MAX_R8 |
11 |
|
12 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
13 |
CBOP |
14 |
C !ROUTINE: GLOBAL_MAX_R4 |
15 |
|
16 |
C !INTERFACE: |
17 |
SUBROUTINE GLOBAL_MAX_R4( |
18 |
U maxPhi, |
19 |
I myThid ) |
20 |
|
21 |
C !DESCRIPTION: |
22 |
C *==========================================================* |
23 |
C | SUBROUTINE GLOBAL\_MAX\_R4 |
24 |
C | o Handle max for real*4 data. |
25 |
C *==========================================================* |
26 |
C | Perform max on array of one value per thread and then |
27 |
C | max result of all the processes. |
28 |
C | Notes |
29 |
C | ===== |
30 |
C | Within a process only one thread does the max, each |
31 |
C | thread is assumed to have already maxed its local data. |
32 |
C | The same thread also does the inter-process max for |
33 |
C | example with MPI and then writes the result into a shared |
34 |
C | location. All threads wait until the max is avaiailable. |
35 |
C *==========================================================* |
36 |
|
37 |
C !USES: |
38 |
IMPLICIT NONE |
39 |
|
40 |
C == Global data == |
41 |
#include "SIZE.h" |
42 |
#include "EEPARAMS.h" |
43 |
#include "EESUPPORT.h" |
44 |
#include "GLOBAL_MAX.h" |
45 |
|
46 |
C !INPUT/OUTPUT PARAMETERS: |
47 |
C == Routine arguments == |
48 |
C maxPhi :: Result of max. |
49 |
C myThid :: My thread id. |
50 |
Real*4 maxPhi |
51 |
INTEGER myThid |
52 |
|
53 |
C !LOCAL VARIABLES: |
54 |
C == Local variables == |
55 |
C I :: Loop counters |
56 |
C mpiRC :: MPI return code |
57 |
INTEGER I |
58 |
Real*4 tmp |
59 |
#ifdef ALLOW_USE_MPI |
60 |
INTEGER mpiRC |
61 |
#endif /* ALLOW_USE_MPI */ |
62 |
CEOP |
63 |
|
64 |
C-- write local max into array |
65 |
phiGMR4(1,myThid) = maxPhi |
66 |
|
67 |
C-- Can not start until everyone is ready |
68 |
CALL BAR2( myThid ) |
69 |
|
70 |
C-- Max within the process first |
71 |
_BEGIN_MASTER( myThid ) |
72 |
tmp = phiGMR4(1,1) |
73 |
DO I=2,nThreads |
74 |
tmp = MAX(tmp,phiGMR4(1,I)) |
75 |
ENDDO |
76 |
maxPhi = tmp |
77 |
#ifdef ALLOW_USE_MPI |
78 |
IF ( usingMPI ) THEN |
79 |
CALL MPI_Allreduce(tmp,maxPhi,1,MPI_REAL,MPI_MAX, |
80 |
& MPI_COMM_MODEL,mpiRC) |
81 |
ENDIF |
82 |
#endif /* ALLOW_USE_MPI */ |
83 |
phiGMR4(1,0) = maxPhi |
84 |
_END_MASTER( myThid ) |
85 |
|
86 |
C-- Do not leave until we are sure that the max is done |
87 |
CALL BAR2( myThid ) |
88 |
|
89 |
C-- set result for every process |
90 |
maxPhi = phiGMR4(1,0) |
91 |
|
92 |
RETURN |
93 |
END |
94 |
|
95 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
96 |
CBOP |
97 |
C !ROUTINE: GLOBAL_MAX_R8 |
98 |
|
99 |
C !INTERFACE: |
100 |
SUBROUTINE GLOBAL_MAX_R8( |
101 |
O maxPhi, |
102 |
I myThid ) |
103 |
|
104 |
C !DESCRIPTION: |
105 |
C *==========================================================* |
106 |
C | SUBROUTINE GLOBAL\_MAX\_R8 |
107 |
C | o Handle max for real*8 data. |
108 |
C *==========================================================* |
109 |
C | Perform max on array of one value per thread and then |
110 |
C | max result of all the processes. |
111 |
C | Notes |
112 |
C | ===== |
113 |
C | Within a process only one thread does the max, each |
114 |
C | thread is assumed to have already maxed its local data. |
115 |
C | The same thread also does the inter-process max for |
116 |
C | example with MPI and then writes the result into a shared |
117 |
C | location. All threads wait until the max is avaiailable. |
118 |
C *==========================================================* |
119 |
|
120 |
C !USES: |
121 |
IMPLICIT NONE |
122 |
|
123 |
C === Global data === |
124 |
#include "SIZE.h" |
125 |
#include "EEPARAMS.h" |
126 |
#include "EESUPPORT.h" |
127 |
#include "GLOBAL_MAX.h" |
128 |
|
129 |
C !INPUT/OUTPUT PARAMETERS: |
130 |
C === Routine arguments === |
131 |
C maxPhi :: Result of max. |
132 |
C myThid :: My thread id. |
133 |
Real*8 maxPhi |
134 |
INTEGER myThid |
135 |
|
136 |
C !LOCAL VARIABLES: |
137 |
C === Local variables === |
138 |
C I :: Loop counters |
139 |
C mpiRC :: MPI return code |
140 |
INTEGER I |
141 |
Real*8 tmp |
142 |
#ifdef ALLOW_USE_MPI |
143 |
INTEGER mpiRC |
144 |
#endif /* ALLOW_USE_MPI */ |
145 |
CEOP |
146 |
|
147 |
C-- write local max into array |
148 |
phiGMR8(1,myThid) = maxPhi |
149 |
|
150 |
C-- Can not start until everyone is ready |
151 |
CALL BAR2( myThid ) |
152 |
|
153 |
C-- Max within the process first |
154 |
_BEGIN_MASTER( myThid ) |
155 |
tmp = phiGMR8(1,1) |
156 |
DO I=2,nThreads |
157 |
tmp = MAX(tmp,phiGMR8(1,I)) |
158 |
ENDDO |
159 |
maxPhi = tmp |
160 |
#ifdef ALLOW_USE_MPI |
161 |
IF ( usingMPI ) THEN |
162 |
CALL MPI_Allreduce(tmp,maxPhi,1,MPI_DOUBLE_PRECISION,MPI_MAX, |
163 |
& MPI_COMM_MODEL,mpiRC) |
164 |
ENDIF |
165 |
#endif /* ALLOW_USE_MPI */ |
166 |
C-- Write solution to place where all threads can see it |
167 |
phiGMR8(1,0) = maxPhi |
168 |
_END_MASTER( myThid ) |
169 |
|
170 |
C-- Do not leave until we are sure that the max is done |
171 |
CALL BAR2( myThid ) |
172 |
|
173 |
C-- set result for every process |
174 |
maxPhi = phiGMR8(1,0) |
175 |
|
176 |
RETURN |
177 |
END |