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

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

devel : created serial kernels to debug OpenMP

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