-
Notifications
You must be signed in to change notification settings - Fork 9
/
field_RANKSUFF_shuffle_module.fypp
286 lines (209 loc) · 7.61 KB
/
field_RANKSUFF_shuffle_module.fypp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#! (C) Copyright 2022- ECMWF.
#! (C) Copyright 2022- Meteo-France.
#!
#! This software is licensed under the terms of the Apache Licence Version 2.0
#! which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
#! In applying this licence, ECMWF does not waive the privileges and immunities
#! granted to it by virtue of its status as an intergovernmental organisation
#! nor does it submit to any jurisdiction.
MODULE FIELD_${RANK}$${SUFF}$_SHUFFLE_MODULE
#:set fieldTypeList = fieldType.getFieldTypeList (ranks=[RANK], kinds=['JP' + SUFF], hasView=True)
USE FIELD_${RANK}$${SUFF}$_MODULE
USE FIELD_${RANK}$${SUFF}$_ACCESS_MODULE
USE FIELD_${RANK}$${SUFF}$_FACTORY_MODULE
${fieldType.useParkind1 ()}$
IMPLICIT NONE
PRIVATE
#:for what in ['DEVICE', 'HOST']
#:for mode in ['RDONLY', 'RDWR']
INTERFACE PAIR_GATHER_${what}$_DATA_${mode}$
#:for ft in fieldTypeList
MODULE PROCEDURE :: PAIR_${ft.name}$_GATHER_${what}$_DATA_${mode}$
#:endfor
END INTERFACE PAIR_GATHER_${what}$_DATA_${mode}$
PUBLIC :: PAIR_GATHER_${what}$_DATA_${mode}$
#:endfor
#:endfor
INTERFACE PAIR_SCATTER_DATA
#:for ft in fieldTypeList
MODULE PROCEDURE :: PAIR_${ft.name}$_SCATTER_DATA
#:endfor
END INTERFACE PAIR_SCATTER_DATA
PUBLIC :: PAIR_SCATTER_DATA
#:for ft in fieldTypeList
TYPE ${ft.name}$_SHUFFLE_PAIR
INTEGER (KIND=JPIM) :: IMODE = -1
INTEGER (KIND=JPIM) :: IWHAT = -1
CLASS (${ft.name}$), POINTER :: YLSCAT => NULL ()
CLASS (${ft.name}$), POINTER :: YLGATH => NULL ()
TYPE (${ft.name}$_SHUFFLE_PAIR), POINTER :: NEXT => NULL ()
END TYPE ${ft.name}$_SHUFFLE_PAIR
PUBLIC :: ${ft.name}$_SHUFFLE_PAIR
#:endfor
INTEGER (KIND=JPIM), PARAMETER :: NRDWR = 0, NRDONLY = 1, NHOST = 2, NDEVICE = 3
INTEGER (KIND=JPIM), PARAMETER :: NLONDIM = 1, NBLKDIM = 2
CONTAINS
#:for ft in fieldTypeList
#:for what in ['DEVICE', 'HOST']
#:for mode in ['RDONLY', 'RDWR']
SUBROUTINE PAIR_${ft.name}$_GATHER_${what}$_DATA_${mode}$ (YDPAIR, LDNULL, LDFULL, KLON, KGPBLKS, YDFINDS, PTR, YLF, KBLKMIN, KBLKMAX)
USE FIELD_3IM_MODULE
USE FIELD_3IM_ACCESS_MODULE
USE FIELD_3IM_FACTORY_MODULE
TYPE (${ft.name}$_SHUFFLE_PAIR), POINTER :: YDPAIR
LOGICAL, INTENT (IN) :: LDNULL, LDFULL
INTEGER (KIND=JPIM), INTENT (IN) :: KLON, KGPBLKS
CLASS (FIELD_3IM), POINTER :: YDFINDS
${ft.type}$, POINTER :: PTR(${ft.shape}$)
CLASS (${ft.name}$), POINTER, OPTIONAL :: YLF
INTEGER (KIND=JPIM), INTENT (IN) :: KBLKMIN
INTEGER (KIND=JPIM), INTENT (IN) :: KBLKMAX
${ft.type}$, POINTER :: ZTRG(${ft.shape}$), ZTRS(${ft.shape}$)
${ft.type}$, POINTER :: PTR1(${ft.shape}$)
TYPE (${ft.name}$_SHUFFLE_PAIR), POINTER :: YLPAIR
CLASS (${ft.name}$), POINTER :: YLGATH_DUMM
INTEGER (KIND=JPIM) :: ILBOUNDS (${ft.rank}$), IUBOUNDS (${ft.rank}$)
INTEGER (KIND=JPIM), POINTER :: INDS (:,:,:)
IF (LDNULL) THEN
! Return dummy pointer
YLGATH_DUMM => NULL ()
PTR => GET_${what}$_DATA_${mode}$ (YLGATH_DUMM)
ELSEIF (LDFULL) THEN
! Return pointer on packed array
PTR => GET_${what}$_DATA_${mode}$ (YLF)
! Set last lbound to 1
ILBOUNDS = LBOUND (PTR)
PTR1 => PTR (${','.join ([':'] * (ft.rank-1))}$, KBLKMIN:KBLKMAX)
PTR (${','.join (list (map (lambda i: "ILBOUNDS(" + str (i) + "):", range (1, ft.rank))))}$, 1:) => PTR1
ELSE
PTR => GET_${what}$_DATA_RDONLY (YLF)
ALLOCATE (YLPAIR)
YLPAIR%IMODE = N${mode}$
YLPAIR%IWHAT = N${what}$
YLPAIR%NEXT => YDPAIR
YDPAIR => YLPAIR
YLPAIR%YLSCAT => YLF
ILBOUNDS = LBOUND (PTR)
IUBOUNDS = UBOUND (PTR)
ILBOUNDS (1) = 1
IUBOUNDS (1) = KLON
ILBOUNDS (${ft.rank}$) = 1
IUBOUNDS (${ft.rank}$) = KGPBLKS
CALL FIELD_NEW (YLPAIR%YLGATH, LBOUNDS=ILBOUNDS, UBOUNDS=IUBOUNDS, PERSISTENT=.TRUE.)
INDS => GET_${what}$_DATA_RDONLY (YDFINDS)
ZTRS => GET_${what}$_DATA_RDONLY (YLPAIR%YLSCAT)
ZTRG => GET_${what}$_DATA_RDWR (YLPAIR%YLGATH)
CALL PAIR_${ft.name}$_${what}$_${mode}$ (INDS, ZTRG, ZTRS)
! Return pointer on packed array
PTR => GET_${what}$_DATA_${mode}$ (YLPAIR%YLGATH)
ENDIF
CONTAINS
SUBROUTINE PAIR_${ft.name}$_${what}$_${mode}$ (KNDS, PTRG, PTRS)
${ft.type}$, POINTER :: PTRG(${ft.shape}$)
${ft.type}$, POINTER :: PTRS(${ft.shape}$)
INTEGER (KIND=JPIM), INTENT (IN) :: KNDS (:,:,:)
INTEGER (KIND=JPIM) :: JLONS, JBLKS, JLONG, JBLKG
#:set ind = ', '.join (map (lambda i: "J" + str (i), range (2, ft.rank)))
#:if ft.rank > 2
INTEGER (KIND=JPIM) :: ${ind}$
#:set ind = ind + ', '
#:endif
#:if what == 'DEVICE'
!$acc parallel loop gang present (PTRG, PTRS, KNDS)
#:elif what == 'HOST'
!$OMP PARALLEL DO PRIVATE (${ind}$JBLKG, JLONG, JBLKS, JLONS)
#:endif
DO JBLKG = 1, SIZE (KNDS, 3)
#:if what == 'DEVICE'
!$acc loop vector private (${ind}$JLONG, JBLKS, JLONS)
#:endif
DO JLONG = 1, SIZE (KNDS, 2)
JLONS = KNDS (NLONDIM, JLONG, JBLKG)
JBLKS = KNDS (NBLKDIM, JLONG, JBLKG)
IF (JLONS > 0) THEN
#:for i in reversed (range (2, ft.rank))
DO J${i}$ = LBOUND (PTRG, ${i}$), UBOUND (PTRG, ${i}$)
#:endfor
PTRG (JLONG, ${ind}$JBLKG) = PTRS (JLONS, ${ind}$JBLKS)
#:for i in (range (2, ft.rank))
ENDDO
#:endfor
ENDIF
ENDDO
ENDDO
END SUBROUTINE
END SUBROUTINE PAIR_${ft.name}$_GATHER_${what}$_DATA_${mode}$
#:endfor
#:endfor
#:endfor
#:for ft in fieldTypeList
SUBROUTINE PAIR_${ft.name}$_SCATTER_DATA (YDPAIR, YDFINDS)
USE FIELD_3IM_MODULE
USE FIELD_3IM_ACCESS_MODULE
USE FIELD_3IM_FACTORY_MODULE
TYPE (${ft.name}$_SHUFFLE_PAIR), POINTER :: YDPAIR
CLASS (FIELD_3IM), POINTER :: YDFINDS
TYPE (${ft.name}$_SHUFFLE_PAIR), POINTER :: YLPAIR
${ft.type}$, POINTER :: PTRG(${ft.shape}$), PTRS(${ft.shape}$)
INTEGER (KIND=JPIM), POINTER :: INDS (:,:,:)
DO WHILE (ASSOCIATED (YDPAIR))
YLPAIR => YDPAIR%NEXT
! Scatter back results
IF (YDPAIR%IMODE == NRDWR) THEN
IF (YDPAIR%IWHAT == NDEVICE) THEN
INDS => GET_DEVICE_DATA_RDONLY (YDFINDS)
PTRG => GET_DEVICE_DATA_RDONLY (YDPAIR%YLGATH)
PTRS => GET_DEVICE_DATA_RDWR (YDPAIR%YLSCAT)
CALL ${ft.name}$_SCATTER_DEVICE_KERNEL (INDS, PTRG, PTRS)
ELSEIF (YDPAIR%IWHAT == NHOST) THEN
INDS => GET_HOST_DATA_RDONLY (YDFINDS)
PTRG => GET_HOST_DATA_RDONLY (YDPAIR%YLGATH)
PTRS => GET_HOST_DATA_RDWR (YDPAIR%YLSCAT)
CALL ${ft.name}$_SCATTER_HOST_KERNEL (INDS, PTRG, PTRS)
ENDIF
ENDIF
CALL FIELD_DELETE (YDPAIR%YLGATH)
DEALLOCATE (YDPAIR)
NULLIFY (YDPAIR)
YDPAIR => YLPAIR
ENDDO
CONTAINS
#:for what in ['DEVICE', 'HOST']
SUBROUTINE ${ft.name}$_SCATTER_${what}$_KERNEL (KNDS, PTRG, PTRS)
${ft.type}$, POINTER :: PTRG(${ft.shape}$)
${ft.type}$, POINTER :: PTRS(${ft.shape}$)
INTEGER (KIND=JPIM), INTENT (IN) :: KNDS (:,:,:)
INTEGER (KIND=JPIM) :: JLONS, JBLKS, JLONG, JBLKG
#:set ind = ', '.join (map (lambda i: "J" + str (i), range (2, ft.rank)))
#:if ft.rank > 2
INTEGER (KIND=JPIM) :: ${ind}$
#:set ind = ind + ', '
#:endif
#:if what == 'DEVICE'
!$acc parallel loop gang present (PTRG, PTRS, KNDS)
#:elif what == 'HOST'
!$OMP PARALLEL DO PRIVATE (${ind}$JBLKG, JLONG, JBLKS, JLONS)
#:endif
DO JBLKG = 1, SIZE (KNDS, 3)
#:if what == 'DEVICE'
!$acc loop vector private (${ind}$JLONG, JBLKS, JLONS)
#:endif
DO JLONG = 1, SIZE (KNDS, 2)
JLONS = KNDS (NLONDIM, JLONG, JBLKG)
JBLKS = KNDS (NBLKDIM, JLONG, JBLKG)
IF (JLONS > 0) THEN
#:for i in reversed (range (2, ft.rank))
DO J${i}$ = LBOUND (PTRG, ${i}$), UBOUND (PTRG, ${i}$)
#:endfor
PTRS (JLONS, ${ind}$JBLKS) = PTRG (JLONG, ${ind}$JBLKG)
#:for i in (range (2, ft.rank))
ENDDO
#:endfor
ENDIF
ENDDO
ENDDO
END SUBROUTINE
#:endfor
END SUBROUTINE PAIR_${ft.name}$_SCATTER_DATA
#:endfor
END MODULE FIELD_${RANK}$${SUFF}$_SHUFFLE_MODULE