source: codes/icosagcm/devel/Python/src/hexagonal/macros.jin @ 685

Last change on this file since 685 was 685, checked in by dubos, 6 years ago

devel/unstructured : towards vertical remapping

File size: 8.9 KB
Line 
1#ifdef PASS_PRE1
2
3#define _AND_ &&
4#define _OR_ ||
5#define _NOT_ !
6#define IS_INNER_LAYER (_NOT_ (IS_BOTTOM_LEVEL _OR_ IS_TOP_LAYER))
7#define IS_INNER_INTERFACE (_NOT_ (IS_BOTTOM_LEVEL _OR_ IS_TOP_INTERFACE))
8
9#define CELL ij,LL
10#define VERTEX1 ij+{{vertex1}},LL
11#define VERTEX2 ij+{{vertex2}},LL
12#define EDGE ij+{{edge}},LL
13#define EDGE_TRISK ij+{{edge_trisk}},LL
14#define DUAL_CELL ij+{{triangle}},LL
15#define CELL1 ij,LL
16#define CELL2 ij+{{neighbour}},LL
17#define VERTEX {{vertex}},LL
18#define SIGN {{sign}}
19#define LE_DE le_de(ij+{{edge}})
20#define WEE wee(ij+{{edge}},{{itrisk}})
21#define DE de(ij+{{edge}})
22#define RIV2 Riv2({{vertex}},{{vertex_dir}})
23#define AI Ai(ij)
24#define FV fv(ij+{{triangle}})
25#define AV Av(ij+{{triangle}})
26
27#endif
28
29#ifdef PASS_PRE2
30
31#define BARRIER !$OMP BARRIER
32
33#define KERNEL(name) {% call define_kernel(#name) %}
34#define FORALL_CELLS(...) {% call forall_cells(__VA_ARGS__) %}
35#define FORALL_CELLS_EXT(...) {% call forall_cells_ext(__VA_ARGS__) %}
36#define ON_PRIMAL {% call(forall_edges) on_hexagons() %}
37#define ON_DUAL {% call(triangle,forall_edges,forall_vertices) on_triangles() %}
38#define ON_EDGES {% call(edge,neighbour,sign,vertex1,vertex2,forall_trisk) on_edges() %}
39#define FORALL_EDGES {% call(edge,sign,vertex1,vertex2) forall_edges() %}
40#define FORALL_VERTICES {% call(vertex,vertex_dir) forall_vertices() %}
41#define FORALL_TRISK {% call(itrisk,edge_trisk) forall_trisk() %}
42#define END_BLOCK {% endcall %}
43#define SEQUENCE_C0 {% call(body,at_level,ext) sequence() %}
44#define SEQUENCE_C1 {% call(body,at_level,ext) sequence_ext() %}
45#define SEQUENCE_E0 {% call(body,at_level,ext) sequence_edge() %}
46#define PROLOGUE(level) {% call(edge,neighbour) at_level(level,ext) %}
47#define BODY(range){% call(edge,neighbour) body(range,ext) %}
48#define EPILOGUE(level){% call(edge,neighbour) at_level(level,ext) %}
49
50#define CST_IF(condition, action) {{ cst_if(#condition, #action) }}
51#define CST_IFTHEN(condition) {{flat}}if condition
52#define CST_ELSEIF(condition) {{flat}}elif condition
53#define CST_ELSE {{flat}}else
54#define CST_ENDIF {{flat}}endif
55
56#endif
57
58#if PASS_JINJA
59
60{% macro sequence() -%}
61{{ caller(body_primal,at_level_primal,'') }}
62{%- endmacro %}
63
64{% macro sequence_ext() -%}
65{{ caller(body_primal,at_level_primal,'_ext') }}
66{%- endmacro %}
67
68{% macro at_level_primal(lev,ext) -%}
69{{ define('LL', lev) }}
70DO ij=ij_omp_begin{{ext}},ij_omp_end{{ext}}
71{{ caller() }}
72END DO
73{%- endmacro %}
74
75{% macro body_primal(range,ext) -%}
76{{ define('LL', 'l') }}
77DO l = {{ range }}
78  DO ij=ij_omp_begin{{ext}},ij_omp_end{{ext}}
79    {{ caller() }}
80  END DO
81END DO
82{%- endmacro %}
83
84{% macro sequence_edge() -%}
85{{ caller(body_edge,at_level_edge,'') }}
86{%- endmacro %}
87
88{% macro at_level_edge(lev,ext) -%}
89{{ define('LL', lev) }}
90DO ij=ij_omp_begin{{ext}},ij_omp_end{{ext}}
91      {{ caller('u_right','t_right') }}
92      {{ caller('u_lup','t_lup') }}
93      {{ caller('u_ldown','t_ldown') }}
94END DO
95{%- endmacro %}
96
97{% macro body_edge(range,ext) -%}
98{{ define('LL', 'l') }}
99DO l = {{ range }}
100   DO ij=ij_omp_begin{{ext}},ij_omp_end{{ext}}
101      {{ caller('u_right','t_right') }}
102      {{ caller('u_lup','t_lup') }}
103      {{ caller('u_ldown','t_ldown') }}
104  END DO
105END DO
106{%- endmacro %}
107
108{#---------------------- big macro generating nested DO loops -------------------------#}
109{# specialized code is generated for l=1 and l=llm,llm+1 if necessary #}
110{# start/end values for l are replaced by corresponding OpenMP values #}
111
112{% macro define_levels(ll,kdown,kup,thecode) %}
113  {{ define('LL', ll) }}
114  {{ define_cppmacro('KDOWN','(ij,ll)', kdown) if 'KDOWN' in thecode }}
115  {{ define_cppmacro('KUP','(ij,ll)', kup) if 'KUP'   in thecode }}
116{%- endmacro %}
117
118{% macro forall(start,end,ij_range,thecode) %}
119{% set start_omp={'1':'ll_begin' , '2':'ll_beginp1'}[start] %} 
120{% set end_omp={'llm-1':'ll_endm1' , 'llm':'ll_end', 'llm+1':'ll_endp1'}[end] %}
121{% set is_top_layer, is_top_inter = False, False %}
122
123{{ define('IS_TOP_LAYER', '_FALSE_') }}
124{{ define('IS_TOP_INTERFACE', '_FALSE_') }}
125
126{% if 'IS_BOTTOM_LEVEL' in thecode or 'KDOWN' in thecode%}
127{{ 'ERROR : using IS_BOTTOM_LEVEL in a loop starting at l=2' if start=='2' }}
128{# the code in the loop checks whether l==1, for the sake of performance
129we shall write special code for l=1 and start the loop at l=2 #}
130
131IF (ll_begin==1) THEN
132  {{ define('IS_BOTTOM_LEVEL', '_TRUE_') }}
133  {{ define_levels('1', 'ij,1', 'ij,1', thecode) }}
134   !DIR$ SIMD
135    DO ij={{ij_range}}
136      {{ thecode }}
137    END DO
138END IF
139{% set start_omp='ll_beginp1' %}
140{% endif %}
141
142{% if 'IS_TOP_LAYER' in thecode %}
143{{ 'ERROR : using _IS_TOP_LAYER_ in a loop ending at l=llm+1' if end=='llm+1' }}
144{# the code checks whether l==llm, write special code for l=llm and end the loop at l=llm-1 #}
145{% set end_omp, is_top_layer = 'll_endm1', True %}
146{% endif %}
147
148{% if 'IS_TOP_INTERFACE' in thecode or 'KUP' in thecode %}
149{# the code checks whether l==llm+1, write special code for l=llm+1 and end the loop at l=llm #}
150{{ 'ERROR : using _IS_TOP_INTERFACE_ in a loop ending at l=llm' if end=='llm' }}
151{% set end_omp, is_top_inter ='ll_end', True %}
152{% endif %}
153
154{{ define('IS_BOTTOM_LEVEL', '_FALSE_') }}
155{{ define_levels('l', 'ij,l-1', 'ij,l', thecode) }}
156DO l = {{start_omp}}, {{end_omp}}
157!DIR$ SIMD
158  DO ij={{ij_range}}
159  {{ thecode }}
160  END DO
161END DO
162
163{% if is_top_layer %}
164IF(ll_end==llm) THEN
165  {{ define('IS_TOP_LAYER', '_TRUE_') }}
166  {{ define_levels('llm', 'ij,llm-1', 'ij,llm', thecode) }}
167  !DIR$ SIMD
168  DO ij={{ij_range}}
169    {{ thecode }}
170  END DO
171END IF
172{% endif %}
173
174{% if is_top_inter %}
175IF(ll_endp1==llm+1) THEN
176  {{ define('IS_TOP_INTERFACE', '_TRUE_') }}
177  {{ define_levels('llm+1', 'ij,llm', 'ij,llm', thecode) }}
178  !DIR$ SIMD
179  DO ij={{ij_range}}
180    {{ thecode }}
181  END DO
182END IF
183{% endif %}
184
185{% endmacro %}
186
187{#-------------------------------------------------------------------------------------#}
188
189{% macro forall_cells(start='1', end='llm') -%}
190{{ forall(start,end,'ij_begin, ij_end', caller()) }}
191{% endmacro %}
192
193{% macro forall_cells_ext(start='1', end='llm') -%}
194{{ forall(start,end,'ij_begin_ext, ij_end_ext', caller()) }}
195{% endmacro %}
196
197{% macro forall_edges_hex() -%}
198{{ caller('u_rup','ne_rup', 'z_rup','z_up') }}
199{{ caller('u_lup','ne_lup', 'z_lup','z_up') }}
200{{ caller('u_left','ne_left','z_lup','z_ldown') }}
201{{ caller('u_ldown','ne_ldown', 'z_ldown','z_down') }}
202{{ caller('u_rdown','ne_rdown', 'z_rdown','z_down') }}
203{{ caller('u_right','ne_right', 'z_rup','z_rdown') }}
204{%- endmacro %}
205
206{% macro on_hexagons() -%}
207{{ caller(forall_edges_hex) }}
208{%- endmacro %}
209
210{% macro forall_edges_zup() -%}
211{{ caller('u_rup','ne_rup', 'TODO','TODO') }}
212{{ caller('t_rup+u_left','ne_left', 'TODO','TODO') }}
213{{ caller('u_lup','(-ne_lup)', 'TODO','TODO') }}
214{%- endmacro %}
215
216{% macro forall_edges_zdown() -%}
217{{ caller('u_rdown','(-ne_rdown)', 'TODO', 'TODO') }}
218{{ caller('t_ldown+u_right','ne_right', 'TODO', 'TODO') }}
219{{ caller('u_ldown','ne_ldown', 'TODO', 'TODO') }}
220{%- endmacro %}
221
222{% macro forall_vertices_zup() -%}
223{{ caller('ij','vup') }}
224{{ caller('ij+t_rup','vldown') }}
225{{ caller('ij+t_lup','vrdown') }}
226{%- endmacro %}
227
228{% macro forall_vertices_zdown() -%}
229{{ caller('ij','vdown') }}
230{{ caller('ij+t_ldown','vrup') }}
231{{ caller('ij+t_rdown','vlup') }}
232{%- endmacro %}
233
234{% macro on_triangles() -%}
235{{ caller('z_up',forall_edges_zup,forall_vertices_zup) }}
236{{ caller('z_down',forall_edges_zdown,forall_vertices_zdown) }}
237{%- endmacro %}
238
239{% macro trisk_right() -%}
240{{ caller('1,1', 'u_rup') }}
241{{ caller('2,1', 'u_lup') }}
242{{ caller('3,1', 'u_left') }}
243{{ caller('4,1', 'u_ldown') }}
244{{ caller('5,1', 'u_rdown') }}
245{{ caller('1,2', 't_right+u_ldown') }}
246{{ caller('2,2', 't_right+u_rdown') }}
247{{ caller('3,2', 't_right+u_right') }}
248{{ caller('4,2', 't_right+u_rup') }}
249{{ caller('5,2', 't_right+u_lup') }}
250{%- endmacro %}
251
252{% macro trisk_lup() -%}
253{{ caller('1,1', 'u_left') }}
254{{ caller('2,1', 'u_ldown') }}
255{{ caller('3,1', 'u_rdown') }}
256{{ caller('4,1', 'u_right') }}
257{{ caller('5,1', 'u_rup') }}
258{{ caller('1,2', 't_lup+u_right') }}
259{{ caller('2,2', 't_lup+u_rup') }}
260{{ caller('3,2', 't_lup+u_lup') }}
261{{ caller('4,2', 't_lup+u_left') }}
262{{ caller('5,2', 't_lup+u_ldown') }}
263{%- endmacro %}
264
265{% macro trisk_ldown() -%}
266{{ caller('1,1', 'u_rdown') }}
267{{ caller('2,1', 'u_right') }}
268{{ caller('3,1', 'u_rup') }}
269{{ caller('4,1', 'u_lup') }}
270{{ caller('5,1', 'u_left') }}
271{{ caller('1,2', 't_ldown+u_lup') }}
272{{ caller('2,2', 't_ldown+u_left') }}
273{{ caller('3,2', 't_ldown+u_ldown') }}
274{{ caller('4,2', 't_ldown+u_rdown') }}
275{{ caller('5,2', 't_ldown+u_right') }}
276{%- endmacro %}
277
278{% macro on_edges() -%}
279{{ caller('u_right','t_right','ne_right','z_rdown','z_rup',trisk_right) }}
280{{ caller('u_lup','t_lup','ne_lup','z_up','z_lup', trisk_lup) }}
281{{ caller('u_ldown','t_ldown','ne_ldown','z_ldown','z_down', trisk_ldown) }}
282{%- endmacro %}
283
284{% set llm = 'llm' %}
285
286#endif
287
288#ifdef PASS_POST1
289#define _TRUE_ (0==0)
290#define _FALSE_ (0==1)
291#define CELL ij,LL
292#endif
293
294#ifdef PASS_POST2
295#define UP(ij,l) ij,l+1
296#define DOWN(ij,l) ij,l-1
297#define HIDX(ij,l) ij
298#define VIDX(ij,l) l
299#define AT_LEVEL(ij,l,lev) ij,lev
300#endif
Note: See TracBrowser for help on using the repository browser.