ICON Community Interface 0.4.0
Loading...
Searching...
No Matches
yaxt_fortran_plugin.F90
Go to the documentation of this file.
1! --------------------------------------------------------------------
3! using the YAXT library to gather one ICON variable (temperature) on
4! task zero to calculate the global average near surface temperature.
5!
6! @authors 08/2021 :: ICON Community Interface <icon@dwd.de>
7!
8! Note that in order to demonstrate ComIn's language interoperability,
9! a similar plugin has been implemented in C, see the subdirectory
10! "yaxt_c".
11! --------------------------------------------------------------------
13
14 USE iso_c_binding, ONLY : c_int
15 USE comin_plugin_interface, ONLY : comin_callback_register &
16 , comin_var_get &
17 , t_comin_var_descriptor &
18 , t_comin_var_handle &
19 , comin_descrdata_get_domain &
20 , t_comin_descrdata_domain &
21 , comin_descrdata_get_global &
22 , t_comin_descrdata_global &
23 , t_comin_setup_version_info &
24 , comin_setup_get_version &
25 , ep_secondary_constructor &
26 , ep_destructor &
27 , ep_atm_write_output_before &
28 , comin_flag_read &
29 , comin_parallel_get_host_mpi_rank &
30 , comin_current_get_domain_id &
31 , comin_parallel_get_plugin_mpi_comm &
32 , comin_domain_outside_loop &
33 , t_comin_plugin_info &
34 , comin_current_get_plugin_info &
35 , comin_plugin_finish &
36 , comin_metadata_get &
37 , comin_error_check, comin_print_info &
38 , comin_print_debug
39 USE yaxt, ONLY: xt_redist, xi => xt_int_kind
40
41 IMPLICIT NONE
42
43 CHARACTER(LEN=*), PARAMETER :: pluginname = "yaxt_fortran_plugin"
44
46 INTEGER, PARAMETER :: wp = selected_real_kind(12,307)
47 TYPE(t_comin_setup_version_info) :: version
48
49 TYPE(t_comin_var_handle) :: temp ! temperature
50 INTEGER :: rank
51 CHARACTER(LEN=:), ALLOCATABLE :: units
52
54 TYPE(t_comin_descrdata_domain), POINTER :: p_patch
55 TYPE(t_comin_descrdata_global), POINTER :: p_global
56
58 TYPE(xt_redist) :: yaxt_redist
59
60 CHARACTER(LEN=120) :: text
61
62CONTAINS
63
64 ! --------------------------------------------------------------------
65 ! ComIn primary constructor.
66 ! --------------------------------------------------------------------
67 SUBROUTINE comin_main() BIND(C)
68
69 USE yaxt, ONLY: xt_initialize, xt_idxlist, xt_idxlist_delete &
70 , xt_xmap, xt_xmap_delete, xt_idxstripes_new, xt_idxvec_new &
71 , xt_stripe, xt_idxempty_new, xt_xmap_all2all_new &
72 , xt_redist_p2p_new, xt_initialized
73 USE mpi, ONLY: mpi_double_precision
74
75 CHARACTER(LEN=*), PARAMETER :: substr = 'comin_main (yaxt_fortran_plugin)'
76 TYPE(t_comin_plugin_info) :: this_plugin
77 INTEGER :: p_all_comm ! communicator of all ICON tasks
78 TYPE(xt_idxlist) :: src_idxlist, tgt_idxlist
79 TYPE(xt_xmap) :: xmap
80 INTEGER(kind=xi), DIMENSION(:), ALLOCATABLE :: idxvec, idxmap
81 INTEGER :: k
82
84 rank = comin_parallel_get_host_mpi_rank()
85 CALL comin_print_info("setup")
86
88 version = comin_setup_get_version()
89 IF (version%version_no_major > 1) THEN
90 CALL comin_plugin_finish(substr, "incompatible ComIn library version!")
91 END IF
92
94 CALL comin_current_get_plugin_info(this_plugin)
95 WRITE (text,'(a,a,a,i4)') " plugin " &
96 , trim(this_plugin%name), " has id: ", this_plugin%id
97 CALL comin_print_info(text)
98
100 ! not applicable for this example
101
103 CALL comin_callback_register(ep_secondary_constructor &
105 CALL comin_callback_register(ep_atm_write_output_before &
107 CALL comin_callback_register(ep_destructor &
109
111 p_patch => comin_descrdata_get_domain(1)
112 p_global => comin_descrdata_get_global()
113
115 p_all_comm = comin_parallel_get_plugin_mpi_comm()
116 IF (.NOT. xt_initialized()) THEN
117 CALL comin_print_info("Initialize yaxt...")
118 CALL xt_initialize(p_all_comm)
119 ENDIF
120
122 ! ... get halo info using decomp_domain :-
123 ! ... 0=core, 1=shared edge with owned, 2=shared vertex with owned, <0: undefined
124 idxmap = reshape(p_patch%cells%decomp_domain &
125 , (/ SIZE(p_patch%cells%decomp_domain) /))
126 ! ... get local ids of all core cells
127 idxvec = int(pack( [(k,k=1,p_patch%cells%ncells)], idxmap == 0 ), xi)
128 ! ... convert local ids to global ids
129 idxvec = p_patch%cells%glb_index(idxvec)
130 ! ... generate idxlist for all core cells
131 src_idxlist = xt_idxvec_new(idxvec)
132
133 IF (rank == 0) THEN
134 tgt_idxlist = xt_idxstripes_new( &
135 (/ xt_stripe(1, 1, p_patch%cells%ncells_global) /))
136 ELSE
137 ! ... empty on all other pe
138 tgt_idxlist = xt_idxempty_new()
139 ENDIF
140
141 ! ... create exchange map ...
142 xmap = xt_xmap_all2all_new(src_idxlist, tgt_idxlist, p_all_comm)
143
144 ! ... create redistribution instance for DP ...
145 yaxt_redist = xt_redist_p2p_new(xmap, mpi_double_precision)
146
147 ! ... clean up
148 CALL xt_xmap_delete(xmap)
149 CALL xt_idxlist_delete(src_idxlist)
150 CALL xt_idxlist_delete(tgt_idxlist)
151 DEALLOCATE(idxvec)
152 DEALLOCATE(idxmap)
153
154 END SUBROUTINE comin_main
155
156 ! --------------------------------------------------------------------
157 ! ComIn secondary constructor.
158 ! --------------------------------------------------------------------
159 SUBROUTINE yaxt_fortran_constructor() BIND(C)
160
161 USE yaxt, ONLY: xt_idxlist, xt_idxlist_delete &
162 , xt_xmap, xt_xmap_delete &
163 , xt_redist
164
165 CHARACTER(LEN=*), PARAMETER :: substr = 'yaxt_fortran_constructor (yaxt_fortran_plugin)'
166 TYPE(t_comin_var_descriptor) :: var_desc
167
168 CALL comin_print_info("secondary constructor")
169
170 CALL comin_print_info("request temperature")
171 var_desc = t_comin_var_descriptor('temp', 1)
172 CALL comin_var_get([ep_atm_write_output_before], &
173 var_desc, comin_flag_read, temp)
174
175 CALL comin_metadata_get(var_desc, 'units', units)
176
177 END SUBROUTINE yaxt_fortran_constructor
178
179 ! --------------------------------------------------------------------
180 ! ComIn callback function.
181 ! --------------------------------------------------------------------
182 SUBROUTINE yaxt_fortran_gather() BIND(C)
183
184 USE yaxt, ONLY: xt_redist_s_exchange1
185 USE iso_c_binding, ONLY: c_loc
186
187 CHARACTER(LEN=*), PARAMETER :: substr = 'yaxt_fortran_gather (yaxt_fortran_plugin)'
188 TYPE(t_comin_plugin_info) :: this_plugin
189 INTEGER :: domain_id
190 REAL(kind=wp), DIMENSION(:,:), POINTER :: src
191 REAL(kind=wp), DIMENSION(:,:,:), POINTER :: src3d
192 REAL(kind=wp), DIMENSION(:), POINTER :: tgt, area
193
194 CALL comin_print_info("callback before output")
195
197 CALL comin_current_get_plugin_info(this_plugin)
198
199 domain_id = comin_current_get_domain_id()
200 IF (domain_id == comin_domain_outside_loop) THEN
201 CALL comin_print_debug("currently not in domain loop")
202 ELSE
203 WRITE(text,'(a,a,i0)') "currently on domain ", domain_id
204 CALL comin_print_debug(text)
205 END IF
206
208 NULLIFY(src, src3d, tgt, area)
209
210 CALL temp%to_3d(src3d)
211
213 ALLOCATE(src(p_global%nproma,p_patch%cells%nblks))
214 src(:,:) = src3d(:,p_patch%nlev,:)
215
217 ALLOCATE(tgt(p_patch%cells%ncells_global))
218 ALLOCATE(area(p_patch%cells%ncells_global))
219
221 CALL xt_redist_s_exchange1(yaxt_redist, c_loc(src), c_loc(tgt))
222 CALL xt_redist_s_exchange1(yaxt_redist, c_loc(p_patch%cells%area) &
223 , c_loc(area) )
224
225 IF (rank == 0) &
226 WRITE(0,*) substr, ': global average temperature is ' &
227 , sum(tgt*area)/sum(area), trim(units)
228
230 DEALLOCATE(src)
231 DEALLOCATE(tgt)
232 DEALLOCATE(area)
233
234 END SUBROUTINE yaxt_fortran_gather
235
236 ! --------------------------------------------------------------------
237 ! ComIn callback function.
238 ! --------------------------------------------------------------------
239 SUBROUTINE yaxt_fortran_destructor() BIND(C)
240
241 USE yaxt, ONLY: xt_finalize, xt_redist_delete
242
243 CALL comin_print_info("destructor")
244
246 CALL xt_redist_delete(yaxt_redist)
247
249 CALL xt_finalize()
250
251 END SUBROUTINE yaxt_fortran_destructor
252
253END MODULE yaxt_fortran_plugin
Example plugin for the ICON Community Interface (ComIn)
integer, parameter wp
working precision (will be compared to ComIn's and ICON's)
subroutine yaxt_fortran_constructor()
character(len=120) text
type(t_comin_descrdata_global), pointer p_global
type(t_comin_setup_version_info) version
type(t_comin_var_handle) temp
character(len=:), allocatable units
character(len= *), parameter pluginname
type(t_comin_descrdata_domain), pointer p_patch
access descriptive data structures
void comin_main()