-
Notifications
You must be signed in to change notification settings - Fork 9
/
field_RANKSUFF_data_module.fypp
312 lines (270 loc) · 10.4 KB
/
field_RANKSUFF_data_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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#! (C) Copyright 2022- ECMWF.
#! (C) Copyright 2022- Meteo-France.
#! (C) Copyright 2023- NVIDIA
#!
#! 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}$_DATA_MODULE
#:set fieldTypeList = fieldType.getFieldTypeList (ranks=[RANK], kinds=['JP' + str (SUFF)])
USE FIELD_CONSTANTS_MODULE
${fieldType.useParkind1 ()}$
IMPLICIT NONE
PRIVATE
#:for ft in fieldTypeList
#:set ftn = ft.name
PUBLIC :: ${ftn}$_COPY
PUBLIC :: ${ftn}$_COPY_FUNC
PUBLIC :: ${ftn}$_COPY_INTF
ABSTRACT INTERFACE
SUBROUTINE ${ftn}$_COPY_INTF (HST, DEV, MAP_DEVPTR, KDIR, QUEUE)
IMPORT :: JPIM, ${ft.kind}$
${ft.type}$, POINTER :: HST (${ft.shape}$), DEV (${ft.shape}$)
LOGICAL, INTENT (IN) :: MAP_DEVPTR
INTEGER (KIND=JPIM), INTENT (IN) :: KDIR
INTEGER (KIND=JPIM), OPTIONAL, INTENT (IN) :: QUEUE
END SUBROUTINE
END INTERFACE
#:endfor
CONTAINS
#:for ft in fieldTypeList
#:set ftn = ft.name
FUNCTION ${ftn}$_COPY_FUNC (HST, DEV) RESULT (FUNC)
USE FIELD_ABORT_MODULE
PROCEDURE (${ftn}$_COPY_INTF), POINTER :: FUNC
${ft.type}$, POINTER, OPTIONAL :: HST (${ft.shape}$), DEV (${ft.shape}$)
INTEGER :: LAST_CONTIG_DIM
INTEGER :: NEXT_CONTIG_DIM
IF (PRESENT (HST)) THEN
LAST_CONTIG_DIM = ${ftn}$_GET_LAST_CONTIGUOUS_DIMENSION (HST, 0)
NEXT_CONTIG_DIM = ${ftn}$_GET_LAST_CONTIGUOUS_DIMENSION (HST, LAST_CONTIG_DIM+1)
ELSE
LAST_CONTIG_DIM = ${ft.rank}$
NEXT_CONTIG_DIM = ${ft.rank}$
ENDIF
SELECT CASE (LAST_CONTIG_DIM)
#:if defined('CUDA')
CASE (${ft.rank}$)
FUNC => ${ftn}$_COPY_DIM${ft.rank}$_CONTIGUOUS
#:for d1 in range (ft.rank)
CASE (${d1}$)
SELECT CASE (NEXT_CONTIG_DIM)
#:for d2 in range (d1+1, ft.rank+1)
CASE (${d2}$)
FUNC => ${ftn}$_COPY_2D_DIM${d1}$_${d2}$_CONTIGUOUS
#:endfor
CASE DEFAULT
CALL FIELD_ABORT ('INTERNAL ERROR: UNEXPECTED NEXT_CONTIG_DIM')
END SELECT
#:endfor
#:else
#:for d in range (ft.rank + 1)
CASE (${d}$)
FUNC => ${ftn}$_COPY_DIM${d}$_CONTIGUOUS
#:endfor
#:endif
CASE DEFAULT
CALL FIELD_ABORT ('INTERNAL ERROR: UNEXPECTED LAST_CONTIG_DIM')
END SELECT
END FUNCTION
SUBROUTINE ${ftn}$_COPY (HST, DEV, MAP_DEVPTR, KDIR, QUEUE)
USE FIELD_ABORT_MODULE
${ft.type}$, POINTER :: HST (${ft.shape}$), DEV (${ft.shape}$)
LOGICAL, INTENT (IN) :: MAP_DEVPTR
INTEGER (KIND=JPIM), INTENT (IN) :: KDIR
INTEGER (KIND=JPIM), OPTIONAL, INTENT (IN) :: QUEUE
PROCEDURE (${ftn}$_COPY_INTF), POINTER :: FUNC
FUNC => ${ftn}$_COPY_FUNC (HST, DEV)
CALL FUNC (HST, DEV, MAP_DEVPTR, KDIR, QUEUE)
END SUBROUTINE
#:for d in range (0, ft.rank+1)
SUBROUTINE ${ftn}$_COPY_DIM${d}$_CONTIGUOUS (HST, DEV, MAP_DEVPTR, KDIR, QUEUE)
#ifdef _OPENACC
USE OPENACC
#endif
USE, INTRINSIC :: ISO_FORTRAN_ENV, ONLY : INT64
${ft.type}$, POINTER :: HST (${ft.shape}$), DEV (${ft.shape}$)
LOGICAL, INTENT (IN) :: MAP_DEVPTR
INTEGER (KIND=JPIM), INTENT (IN) :: KDIR
INTEGER (KIND=JPIM), OPTIONAL, INTENT (IN) :: QUEUE
INTEGER (KIND=INT64) :: ISIZE
INTEGER :: ${', '.join (['J'] + list (map (lambda i: 'J' + str (i+1), range (d, ft.rank))))}$
#ifdef _OPENACC
TYPE(C_DEVPTR) :: DEVPTR
#endif
#:for e in range (ft.rank, d, -1)
${' ' * (ft.rank - e)}$DO J${e}$ = LBOUND (HST, ${e}$), UBOUND (HST, ${e}$)
#:endfor
#:set ar = ', '.join ([':'] * d + list (map (lambda i: 'J' + str (i+1), range (d, ft.rank))))
#:set indent = ' ' * (ft.rank - e)
#ifdef _OPENACC
${indent}$ IF(MAP_DEVPTR)THEN
${indent}$ !$acc host_data use_device(DEV)
${indent}$ DEVPTR = C_DEVLOC(DEV (${ar}$))
${indent}$ !$acc end host_data
${indent}$ ELSE
${indent}$ !$acc data deviceptr(DEVPTR, DEV)
${indent}$ DEVPTR = C_DEVLOC(DEV (${ar}$))
${indent}$ !$acc end data
${indent}$ ENDIF
#endif
#:if d == 0
${indent}$ ISIZE = KIND (HST)
#:else
${indent}$ ISIZE = SIZEOF ( HST(${ar}$) )
#:endif
${indent}$ IF (KDIR == NH2D) THEN
#ifdef _OPENACC
${indent}$ IF(PRESENT(QUEUE))THEN
${indent}$ CALL ACC_MEMCPY_TO_DEVICE_ASYNC (DEVPTR , HST (${ar}$), ISIZE, QUEUE)
${indent}$ ELSE
${indent}$ CALL ACC_MEMCPY_TO_DEVICE (DEVPTR , HST (${ar}$), ISIZE)
${indent}$ ENDIF
#else
${indent}$ DEV (${ar}$) = HST (${ar}$)
#endif
${indent}$ ELSEIF (KDIR == ND2H) THEN
#ifdef _OPENACC
${indent}$ IF(PRESENT(QUEUE))THEN
${indent}$ CALL ACC_MEMCPY_FROM_DEVICE_ASYNC (HST (${ar}$), DEVPTR, ISIZE, QUEUE)
${indent}$ ELSE
${indent}$ CALL ACC_MEMCPY_FROM_DEVICE (HST (${ar}$), DEVPTR, ISIZE)
${indent}$ ENDIF
#else
${indent}$ HST (${ar}$) = DEV (${ar}$)
#endif
${indent}$ ENDIF
#:for e in range (d, ft.rank)
${' ' * (ft.rank - e - 1)}$ENDDO
#:endfor
END SUBROUTINE
#:endfor
#:if defined('CUDA')
#:for d1 in range (0, ft.rank)
#:for d2 in range (d1+1, ft.rank+1)
SUBROUTINE ${ftn}$_COPY_2D_DIM${d1}$_${d2}$_CONTIGUOUS (HST, DEV, MAP_DEVPTR, KDIR, QUEUE)
USE OPENACC
USE CUDAFOR
USE FIELD_ABORT_MODULE
${ft.type}$, POINTER :: HST (${ft.shape}$), DEV (${ft.shape}$)
INTEGER (KIND=JPIM), INTENT (IN) :: KDIR
LOGICAL, INTENT (IN) :: MAP_DEVPTR
INTEGER (KIND=JPIM), OPTIONAL, INTENT (IN) :: QUEUE
INTEGER (KIND=JPIM) :: IHST_PITCH, IDEV_PITCH, IRET
INTEGER (KIND=JPIM) :: IWIDTH, IHEIGHT, ISHP(${ft.rank+1}$)
#:if d2 < ft.rank
INTEGER :: ${', '.join (list (map (lambda i: 'J' + str (i+1), range (d2, ft.rank))))}$
#:endif
INTEGER(KIND=CUDA_STREAM_KIND) :: STREAM
TYPE(C_PTR) :: HSTPTR
TYPE(C_DEVPTR) :: DEVPTR
ISHP(1) = 1
ISHP(2:) = SHAPE(HST)
IWIDTH = PRODUCT(ISHP(1:${d1+1}$)) * KIND(HST)
IHEIGHT = PRODUCT(ISHP(${d1+2}$:${d2+1}$))
#:set lb = lambda arr, i: f'LBOUND({arr}, {i+1})'
#:set lbnds = lambda arr, start, end: [lb(arr, i) for i in range(start, end)]
#:set this_slice = lambda arr: ', '.join(lbnds(arr, 0, ft.rank))
#:set next_slice = lambda arr: ', '.join(lbnds(arr, 0, d1) + [lb(arr, d1)+'+1'] + lbnds(arr, d1+1, ft.rank))
IHST_PITCH = LOC (HST(${next_slice('HST')}$)) - LOC (HST (${this_slice('HST')}$))
IDEV_PITCH = LOC (DEV(${next_slice('DEV')}$)) - LOC (DEV (${this_slice('DEV')}$))
#:for e in range (ft.rank, d2, -1)
${' ' * (ft.rank - e)}$DO J${e}$ = LBOUND (HST, ${e}$), UBOUND (HST, ${e}$)
#:endfor
#:set indent = ' ' * (ft.rank - d2 - 1)
#:set ar = lambda arr: ', '.join(lbnds(arr, 0, d2) + [f'J{i+1}' for i in range(d2, ft.rank)])
${indent}$ HSTPTR = C_LOC(HST (${ar('HST')}$))
${indent}$ IF (MAP_DEVPTR) THEN
${indent}$ !$acc host_data use_device(DEV)
${indent}$ DEVPTR = C_DEVLOC(DEV (${ar('DEV')}$))
${indent}$ !$acc end host_data
${indent}$ ELSE
${indent}$ !$acc data deviceptr(DEVPTR,DEV)
${indent}$ DEVPTR = C_DEVLOC(DEV (${ar('DEV')}$))
${indent}$ !$acc end data
${indent}$ ENDIF
${indent}$ IF (KDIR == NH2D) THEN
${indent}$ IF(PRESENT(QUEUE)) THEN
${indent}$ CALL ACC_SET_CUDA_STREAM(QUEUE, STREAM)
${indent}$ IRET = CUDAMEMCPY2DASYNC (DEVPTR, IDEV_PITCH, &
${indent}$ & HSTPTR, IHST_PITCH, &
${indent}$ & IWIDTH, IHEIGHT, &
${indent}$ & STREAM=STREAM)
${indent}$ ELSE
${indent}$ IRET = CUDAMEMCPY2D (DEVPTR, IDEV_PITCH, &
${indent}$ & HSTPTR, IHST_PITCH, &
${indent}$ & IWIDTH, IHEIGHT)
${indent}$ ENDIF
${indent}$ IF (IRET /= CUDASUCCESS) THEN
${indent}$ CALL FIELD_ABORT ("${ftn}$_COPY_2D_DIM${d1}$_${d2}$_CONTIGUOUS: HOST-TO-DEVICE TRANSFER FAILED")
${indent}$ ENDIF
${indent}$ ELSEIF (KDIR == ND2H) THEN
${indent}$ IF(PRESENT(QUEUE)) THEN
${indent}$ CALL ACC_SET_CUDA_STREAM(QUEUE, STREAM)
${indent}$ IRET = CUDAMEMCPY2DASYNC (HSTPTR, IHST_PITCH, &
${indent}$ & DEVPTR, IDEV_PITCH, &
${indent}$ & IWIDTH, IHEIGHT, &
${indent}$ & STREAM=STREAM)
${indent}$ ELSE
${indent}$ IRET = CUDAMEMCPY2D (HSTPTR, IHST_PITCH, &
${indent}$ & DEVPTR, IDEV_PITCH, &
${indent}$ & IWIDTH, IHEIGHT)
${indent}$ ENDIF
${indent}$ IF (IRET /= CUDASUCCESS) THEN
${indent}$ CALL FIELD_ABORT ("${ftn}$_COPY_2D_DIM${d1}$_${d2}$_CONTIGUOUS: DEVICE-TO-HOST TRANSFER FAILED")
${indent}$ ENDIF
${indent}$ ENDIF
#:for e in range (d2, ft.rank)
${' ' * (ft.rank - e - 1)}$ENDDO
#:endfor
END SUBROUTINE
#:endfor
#:endfor
#:endif
#:endfor
#:for ft in fieldTypeList
#:set ftn = ft.name
INTEGER (KIND=JPIM) FUNCTION ${ftn}$_GET_LAST_CONTIGUOUS_DIMENSION (PTR, AFTER) RESULT (JDIM)
${ft.type}$, POINTER :: PTR (${ft.shape}$)
INTEGER (KIND=JPIM) :: AFTER
INTEGER*8 :: IPREVIOUS_STRIDE, ITHIS_STRIDE, ISIZE
INTEGER (KIND=JPIM) :: J, LB(${ft.rank}$)
! assume that dimension all dimensions before AFTER are contiguous...
LB = LBOUND(PTR)
IF (AFTER == 0) THEN
IPREVIOUS_STRIDE = KIND (PTR)
ENDIF
#:for d in range (1, ft.rank+1)
#:set ind0 = ""
#:set ind1 = ""
#:for i in range(1,ft.rank+1)
#:set ind0 = ind0 + "LB({}), ".format(i)
#:set ind1 = ind1 + "LB({}){}, ".format(i,"+1"*(i==d))
#:endfor
#:set ind0 = ind0[:-2]
#:set ind1 = ind1[:-2]
IF (AFTER < ${d}$) THEN
#:if d == 1
ISIZE = 1
#:else
ISIZE = SIZE(PTR, ${d-1}$)
#:endif
IF (SIZE(PTR, ${d}$) /= 1) THEN
ITHIS_STRIDE = LOC (PTR (${ind1}$)) - LOC (PTR (${ind0}$))
IF (IPREVIOUS_STRIDE * ISIZE /= ITHIS_STRIDE) THEN
JDIM = ${d-1}$
RETURN
ENDIF
ENDIF
IPREVIOUS_STRIDE = IPREVIOUS_STRIDE * ISIZE
ELSE IF (AFTER == ${d}$) THEN
ITHIS_STRIDE = LOC (PTR (${ind1}$)) - LOC (PTR (${ind0}$))
IPREVIOUS_STRIDE = ITHIS_STRIDE
ENDIF
#:endfor
JDIM = ${ft.rank}$
END FUNCTION ${ftn}$_GET_LAST_CONTIGUOUS_DIMENSION
#:endfor
END MODULE FIELD_${RANK}$${SUFF}$_DATA_MODULE