1 |
C $Header: /u/gcmpack/MITgcm/eesupp/src/global_sum.F,v 1.15 2010/04/27 16:09:47 jmc Exp $ |
2 |
C $Name: $ |
3 |
|
4 |
#include "CPP_EEOPTIONS.h" |
5 |
|
6 |
C-- File global_sum.F: Routines that perform global sum on an array |
7 |
C of thread values. |
8 |
C Contents |
9 |
C o GLOBAL_SUM_R4 |
10 |
C o GLOBAL_SUM_R8 |
11 |
C o GLOBAL_SUM_INT |
12 |
|
13 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
14 |
CBOP |
15 |
C !ROUTINE: GLOBAL_SUM_R4 |
16 |
|
17 |
C !INTERFACE: |
18 |
SUBROUTINE GLOBAL_SUM_R4( |
19 |
U sumPhi, |
20 |
I myThid ) |
21 |
|
22 |
C !DESCRIPTION: |
23 |
C *==========================================================* |
24 |
C | SUBROUTINE GLOBAL\_SUM\_R4 |
25 |
C | o Handle sum for real*4 data. |
26 |
C *==========================================================* |
27 |
C | Perform sum an array of one value per thread and then |
28 |
C | sum result of all the processes. |
29 |
C | Notes: |
30 |
C | Within a process only one thread does the sum. |
31 |
C | The same thread also does the inter-process sum for |
32 |
C | example with MPI and then writes the result into a shared |
33 |
C | location. All threads wait until the sum is avaiailable. |
34 |
C *==========================================================* |
35 |
|
36 |
C !USES: |
37 |
IMPLICIT NONE |
38 |
|
39 |
C == Global data == |
40 |
#include "SIZE.h" |
41 |
#include "EEPARAMS.h" |
42 |
#include "EESUPPORT.h" |
43 |
#include "GLOBAL_SUM.h" |
44 |
|
45 |
C !INPUT/OUTPUT PARAMETERS: |
46 |
C == Routine arguments == |
47 |
C sumPhi :: Result of sum. |
48 |
C myThid :: My thread id. |
49 |
Real*4 sumPhi |
50 |
INTEGER myThid |
51 |
|
52 |
C !LOCAL VARIABLES: |
53 |
C == Local variables == |
54 |
C I :: Loop counters |
55 |
C mpiRC :: MPI return code |
56 |
INTEGER I |
57 |
Real*4 tmp |
58 |
#ifdef ALLOW_USE_MPI |
59 |
INTEGER mpiRC |
60 |
#endif /* ALLOW_USE_MPI */ |
61 |
CEOP |
62 |
|
63 |
C-- write local sum into array |
64 |
phiGSR4(1,myThid) = sumPhi |
65 |
|
66 |
C-- Can not start until everyone is ready |
67 |
CALL BAR2( myThid ) |
68 |
|
69 |
C-- Sum within the process first |
70 |
_BEGIN_MASTER( myThid ) |
71 |
tmp = 0. |
72 |
DO I=1,nThreads |
73 |
tmp = tmp + phiGSR4(1,I) |
74 |
ENDDO |
75 |
sumPhi = tmp |
76 |
#ifdef ALLOW_USE_MPI |
77 |
IF ( usingMPI ) THEN |
78 |
CALL MPI_Allreduce(tmp,sumPhi,1,MPI_REAL,MPI_SUM, |
79 |
& MPI_COMM_MODEL,mpiRC) |
80 |
ENDIF |
81 |
#endif /* ALLOW_USE_MPI */ |
82 |
C-- Write solution to place where all threads can see it |
83 |
phiGSR4(1,0) = sumPhi |
84 |
|
85 |
_END_MASTER( myThid ) |
86 |
C-- |
87 |
CALL BAR2( myThid ) |
88 |
|
89 |
C-- set result for every process |
90 |
sumPhi = phiGSR4(1,0) |
91 |
|
92 |
RETURN |
93 |
END |
94 |
|
95 |
|
96 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
97 |
CBOP |
98 |
C !ROUTINE: GLOBAL_SUM_R8 |
99 |
|
100 |
C !INTERFACE: |
101 |
SUBROUTINE GLOBAL_SUM_R8( |
102 |
U sumPhi, |
103 |
I myThid ) |
104 |
|
105 |
C !DESCRIPTION: |
106 |
C *==========================================================* |
107 |
C | SUBROUTINE GLOBAL\_SUM\_R8 |
108 |
C | o Handle sum for real*8 data. |
109 |
C *==========================================================* |
110 |
C | Perform sum an array of one value per thread and then |
111 |
C | sum result of all the processes. |
112 |
C | Notes: |
113 |
C | Within a process only one thread does the sum. |
114 |
C | The same thread also does the inter-process sum for |
115 |
C | example with MPI and then writes the result into a shared |
116 |
C | location. All threads wait until the sum is avaiailable. |
117 |
C *==========================================================* |
118 |
|
119 |
C !USES: |
120 |
IMPLICIT NONE |
121 |
|
122 |
C === Global data === |
123 |
#include "SIZE.h" |
124 |
#include "EEPARAMS.h" |
125 |
#include "EESUPPORT.h" |
126 |
#include "GLOBAL_SUM.h" |
127 |
|
128 |
C !INPUT/OUTPUT PARAMETERS: |
129 |
C === Routine arguments === |
130 |
C sumPhi :: Result of sum. |
131 |
C myThid :: My thread id. |
132 |
Real*8 sumPhi |
133 |
INTEGER myThid |
134 |
|
135 |
C !LOCAL VARIABLES: |
136 |
C === Local variables === |
137 |
C I :: Loop counters |
138 |
C mpiRC :: MPI return code |
139 |
INTEGER I |
140 |
Real*8 tmp |
141 |
#ifdef ALLOW_USE_MPI |
142 |
INTEGER mpiRC |
143 |
#endif /* ALLOW_USE_MPI */ |
144 |
CEOP |
145 |
|
146 |
C-- write local sum into array |
147 |
phiGSR8(1,myThid) = sumPhi |
148 |
|
149 |
C-- Can not start until everyone is ready |
150 |
C CALL FOOL_THE_COMPILER( phiGSR8 ) |
151 |
C CALL MS |
152 |
CALL BAR2( myThid ) |
153 |
C _BARRIER |
154 |
C _BARRIER |
155 |
C CALL FOOL_THE_COMPILER( phiGSR8 ) |
156 |
|
157 |
C-- Sum within the process first |
158 |
_BEGIN_MASTER( myThid ) |
159 |
tmp = 0. _d 0 |
160 |
DO I=1,nThreads |
161 |
tmp = tmp + phiGSR8(1,I) |
162 |
ENDDO |
163 |
sumPhi = tmp |
164 |
#ifdef ALLOW_USE_MPI |
165 |
IF ( usingMPI ) THEN |
166 |
CALL MPI_Allreduce(tmp,sumPhi,1,MPI_DOUBLE_PRECISION,MPI_SUM, |
167 |
& MPI_COMM_MODEL,mpiRC) |
168 |
ENDIF |
169 |
#endif /* ALLOW_USE_MPI */ |
170 |
C-- Write solution to place where all threads can see it |
171 |
phiGSR8(1,0) = sumPhi |
172 |
_END_MASTER( myThid ) |
173 |
|
174 |
C-- Do not leave until we are sure that the sum is done |
175 |
C CALL FOOL_THE_COMPILER( phiGSR8 ) |
176 |
C CALL MS |
177 |
C _BARRIER |
178 |
C _BARRIER |
179 |
CALL BAR2( myThid ) |
180 |
C CALL FOOL_THE_COMPILER( phiGSR8 ) |
181 |
|
182 |
C-- set result for every process |
183 |
sumPhi = phiGSR8(1,0) |
184 |
|
185 |
RETURN |
186 |
END |
187 |
|
188 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
189 |
CBOP |
190 |
C !ROUTINE: GLOBAL_SUM_INT |
191 |
C !INTERFACE: |
192 |
SUBROUTINE GLOBAL_SUM_INT( |
193 |
U sumPhi, |
194 |
I myThid ) |
195 |
|
196 |
C !DESCRIPTION: |
197 |
C *==========================================================* |
198 |
C | SUBROUTINE GLOBAL\_SUM\_INT |
199 |
C | o Handle sum for integer data. |
200 |
C *==========================================================* |
201 |
C | Perform sum an array of one value per thread and then |
202 |
C | sum result of all the processes. |
203 |
C | Notes: |
204 |
C | Within a process only one thread does the sum. |
205 |
C | The same thread also does the inter-process sum for |
206 |
C | example with MPI and then writes the result into a shared |
207 |
C | location. All threads wait until the sum is avaiailable. |
208 |
C *==========================================================* |
209 |
|
210 |
C !USES: |
211 |
IMPLICIT NONE |
212 |
|
213 |
C === Global data === |
214 |
#include "SIZE.h" |
215 |
#include "EEPARAMS.h" |
216 |
#include "EESUPPORT.h" |
217 |
#include "GLOBAL_SUM.h" |
218 |
|
219 |
C !INPUT/OUTPUT PARAMETERS: |
220 |
C === Routine arguments === |
221 |
C sumPhi :: Result of sum. |
222 |
C myThid :: My thread id. |
223 |
INTEGER sumPhi |
224 |
INTEGER myThid |
225 |
|
226 |
C !LOCAL VARIABLES: |
227 |
C === Local variables === |
228 |
C I :: Loop counters |
229 |
C mpiRC :: MPI return code |
230 |
INTEGER I |
231 |
INTEGER tmp |
232 |
#ifdef ALLOW_USE_MPI |
233 |
INTEGER mpiRC |
234 |
#endif /* ALLOW_USE_MPI */ |
235 |
CEOP |
236 |
|
237 |
C-- write local sum into array |
238 |
phiGSI(1,myThid) = sumPhi |
239 |
|
240 |
C-- Can not start until everyone is ready |
241 |
_BARRIER |
242 |
|
243 |
C-- Sum within the process first |
244 |
_BEGIN_MASTER( myThid ) |
245 |
tmp = 0 |
246 |
DO I=1,nThreads |
247 |
tmp = tmp + phiGSI(1,I) |
248 |
ENDDO |
249 |
sumPhi = tmp |
250 |
#ifdef ALLOW_USE_MPI |
251 |
IF ( usingMPI ) THEN |
252 |
CALL MPI_Allreduce(tmp,sumPhi,1,MPI_INTEGER,MPI_SUM, |
253 |
& MPI_COMM_MODEL,mpiRC) |
254 |
ENDIF |
255 |
#endif /* ALLOW_USE_MPI */ |
256 |
C-- Write solution to place where all threads can see it |
257 |
phiGSI(1,0) = sumPhi |
258 |
_END_MASTER( myThid ) |
259 |
|
260 |
C-- Do not leave until we are sure that the sum is done |
261 |
_BARRIER |
262 |
|
263 |
C-- set result for every process |
264 |
sumPhi = phiGSI(1,0) |
265 |
|
266 |
RETURN |
267 |
END |