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

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

devel : created serial kernels to debug OpenMP (bugfix)

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