1 |
C $Header: /u/gcmpack/MITgcm/eesupp/src/global_sum.F,v 1.13 2009/04/23 19:18:14 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 |
#ifndef ALWAYS_USE_MPI |
78 |
IF ( usingMPI ) THEN |
79 |
#endif |
80 |
CALL MPI_Allreduce(tmp,sumPhi,1,MPI_REAL,MPI_SUM, |
81 |
& MPI_COMM_MODEL,mpiRC) |
82 |
#ifndef ALWAYS_USE_MPI |
83 |
ENDIF |
84 |
#endif |
85 |
#endif /* ALLOW_USE_MPI */ |
86 |
C-- Write solution to place where all threads can see it |
87 |
phiGSR4(1,0) = sumPhi |
88 |
|
89 |
_END_MASTER( myThid ) |
90 |
C-- |
91 |
CALL BAR2( myThid ) |
92 |
|
93 |
C-- set result for every process |
94 |
sumPhi = phiGSR4(1,1) |
95 |
|
96 |
RETURN |
97 |
END |
98 |
|
99 |
|
100 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
101 |
CBOP |
102 |
C !ROUTINE: GLOBAL_SUM_R8 |
103 |
|
104 |
C !INTERFACE: |
105 |
SUBROUTINE GLOBAL_SUM_R8( |
106 |
U sumPhi, |
107 |
I myThid ) |
108 |
|
109 |
C !DESCRIPTION: |
110 |
C *==========================================================* |
111 |
C | SUBROUTINE GLOBAL\_SUM\_R8 |
112 |
C | o Handle sum for real*8 data. |
113 |
C *==========================================================* |
114 |
C | Perform sum an array of one value per thread and then |
115 |
C | sum result of all the processes. |
116 |
C | Notes: |
117 |
C | Within a process only one thread does the sum. |
118 |
C | The same thread also does the inter-process sum for |
119 |
C | example with MPI and then writes the result into a shared |
120 |
C | location. All threads wait until the sum is avaiailable. |
121 |
C *==========================================================* |
122 |
|
123 |
C !USES: |
124 |
IMPLICIT NONE |
125 |
|
126 |
C === Global data === |
127 |
#include "SIZE.h" |
128 |
#include "EEPARAMS.h" |
129 |
#include "EESUPPORT.h" |
130 |
#include "GLOBAL_SUM.h" |
131 |
|
132 |
C !INPUT/OUTPUT PARAMETERS: |
133 |
C === Routine arguments === |
134 |
C sumPhi :: Result of sum. |
135 |
C myThid :: My thread id. |
136 |
Real*8 sumPhi |
137 |
INTEGER myThid |
138 |
|
139 |
C !LOCAL VARIABLES: |
140 |
C === Local variables === |
141 |
C I :: Loop counters |
142 |
C mpiRC :: MPI return code |
143 |
INTEGER I |
144 |
Real*8 tmp |
145 |
#ifdef ALLOW_USE_MPI |
146 |
INTEGER mpiRC |
147 |
#endif /* ALLOW_USE_MPI */ |
148 |
CEOP |
149 |
|
150 |
C-- write local sum into array |
151 |
phiGSR8(1,myThid) = sumPhi |
152 |
|
153 |
C-- Can not start until everyone is ready |
154 |
C CALL FOOL_THE_COMPILER( phiGSR8 ) |
155 |
C CALL MS |
156 |
CALL BAR2( myThid ) |
157 |
C _BARRIER |
158 |
C _BARRIER |
159 |
C CALL FOOL_THE_COMPILER( phiGSR8 ) |
160 |
|
161 |
C-- Sum within the process first |
162 |
_BEGIN_MASTER( myThid ) |
163 |
tmp = 0. _d 0 |
164 |
DO I=1,nThreads |
165 |
tmp = tmp + phiGSR8(1,I) |
166 |
ENDDO |
167 |
sumPhi = tmp |
168 |
#ifdef ALLOW_USE_MPI |
169 |
#ifndef ALWAYS_USE_MPI |
170 |
IF ( usingMPI ) THEN |
171 |
#endif |
172 |
CALL MPI_Allreduce(tmp,sumPhi,1,MPI_DOUBLE_PRECISION,MPI_SUM, |
173 |
& MPI_COMM_MODEL,mpiRC) |
174 |
#ifndef ALWAYS_USE_MPI |
175 |
ENDIF |
176 |
#endif |
177 |
#endif /* ALLOW_USE_MPI */ |
178 |
C-- Write solution to place where all threads can see it |
179 |
phiGSR8(1,0) = sumPhi |
180 |
_END_MASTER( myThid ) |
181 |
|
182 |
C-- Do not leave until we are sure that the sum is done |
183 |
C CALL FOOL_THE_COMPILER( phiGSR8 ) |
184 |
C CALL MS |
185 |
C _BARRIER |
186 |
C _BARRIER |
187 |
CALL BAR2( myThid ) |
188 |
C CALL FOOL_THE_COMPILER( phiGSR8 ) |
189 |
|
190 |
C-- set result for every process |
191 |
sumPhi = phiGSR8(1,0) |
192 |
|
193 |
RETURN |
194 |
END |
195 |
|
196 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
197 |
CBOP |
198 |
C !ROUTINE: GLOBAL_SUM_INT |
199 |
C !INTERFACE: |
200 |
SUBROUTINE GLOBAL_SUM_INT( |
201 |
U sumPhi, |
202 |
I myThid ) |
203 |
|
204 |
C !DESCRIPTION: |
205 |
C *==========================================================* |
206 |
C | SUBROUTINE GLOBAL\_SUM\_INT |
207 |
C | o Handle sum for integer data. |
208 |
C *==========================================================* |
209 |
C | Perform sum an array of one value per thread and then |
210 |
C | sum result of all the processes. |
211 |
C | Notes: |
212 |
C | Within a process only one thread does the sum. |
213 |
C | The same thread also does the inter-process sum for |
214 |
C | example with MPI and then writes the result into a shared |
215 |
C | location. All threads wait until the sum is avaiailable. |
216 |
C *==========================================================* |
217 |
|
218 |
C !USES: |
219 |
IMPLICIT NONE |
220 |
|
221 |
C === Global data === |
222 |
#include "SIZE.h" |
223 |
#include "EEPARAMS.h" |
224 |
#include "EESUPPORT.h" |
225 |
#include "GLOBAL_SUM.h" |
226 |
|
227 |
C !INPUT/OUTPUT PARAMETERS: |
228 |
C === Routine arguments === |
229 |
C sumPhi :: Result of sum. |
230 |
C myThid :: My thread id. |
231 |
INTEGER sumPhi |
232 |
INTEGER myThid |
233 |
|
234 |
C !LOCAL VARIABLES: |
235 |
C === Local variables === |
236 |
C I :: Loop counters |
237 |
C mpiRC :: MPI return code |
238 |
INTEGER I |
239 |
INTEGER tmp |
240 |
#ifdef ALLOW_USE_MPI |
241 |
INTEGER mpiRC |
242 |
#endif /* ALLOW_USE_MPI */ |
243 |
CEOP |
244 |
|
245 |
C-- write local sum into array |
246 |
phiGSI(1,myThid) = sumPhi |
247 |
|
248 |
C-- Can not start until everyone is ready |
249 |
_BARRIER |
250 |
|
251 |
C-- Sum within the process first |
252 |
_BEGIN_MASTER( myThid ) |
253 |
tmp = 0 |
254 |
DO I=1,nThreads |
255 |
tmp = tmp + phiGSI(1,I) |
256 |
ENDDO |
257 |
sumPhi = tmp |
258 |
#ifdef ALLOW_USE_MPI |
259 |
#ifndef ALWAYS_USE_MPI |
260 |
IF ( usingMPI ) THEN |
261 |
#endif |
262 |
CALL MPI_Allreduce(tmp,sumPhi,1,MPI_INTEGER,MPI_SUM, |
263 |
& MPI_COMM_MODEL,mpiRC) |
264 |
#ifndef ALWAYS_USE_MPI |
265 |
ENDIF |
266 |
#endif |
267 |
#endif /* ALLOW_USE_MPI */ |
268 |
C-- Write solution to place where all threads can see it |
269 |
phiGSI(1,0) = sumPhi |
270 |
_END_MASTER( myThid ) |
271 |
|
272 |
C-- Do not leave until we are sure that the sum is done |
273 |
_BARRIER |
274 |
|
275 |
C-- set result for every process |
276 |
sumPhi = phiGSI(1,0) |
277 |
|
278 |
RETURN |
279 |
END |