source: codes/icosagcm/devel/Python/src/unstructured/macros.jin @ 615

Last change on this file since 615 was 615, checked in by dubos, 7 years ago

devel/unstructured : import Python layer from HEAT

File size: 7.1 KB
Line 
1#ifdef PASS_PRE1
2#define _AND_ &&
3#define _OR_ ||
4#define _NOT_ !
5#define IS_INNER_LAYER (_NOT_ (IS_BOTTOM_LEVEL _OR_ IS_TOP_LAYER))
6#define IS_INNER_INTERFACE (_NOT_ (IS_BOTTOM_LEVEL _OR_ IS_TOP_INTERFACE))
7#endif
8
9#ifdef PASS_PRE2
10
11#define BARRIER !$OMP BARRIER
12#define IS_INNER_INTERFACE (.NOT. (IS_BOTTOM_LEVEL .OR. IS_TOP_INTERFACE))
13#define KERNEL(name) {% call() define_kernel(#name) %}
14#define FORALL_CELLS(...) {% call(lrange,flags) forall_cells(__VA_ARGS__) %}
15#define FORALL_CELLS_EXT(...) {% call(lrange,flags) forall_cells(__VA_ARGS__) %}
16#define ON_PRIMAL {% call(mesh,lrange) on_mesh('primal',lrange,flags) %}
17#define ON_DUAL {% call(mesh,lrange) on_mesh('dual',lrange,flags) %}
18#define ON_EDGES {% call(mesh,lrange) on_edges('edge',lrange,flags) %}
19#define FORALL_EDGES {% call forall_edges(mesh) %}
20#define FORALL_VERTICES {% call forall_vertices(mesh) %}
21#define FORALL_TRISK {% call forall_trisk() %}
22#define END_BLOCK {% endcall %}
23#define SEQUENCE {% call sequence() %}
24#define SEQUENCE_EXT {% call sequence() %}
25#define PROLOGUE(level) {% call at_level(level) %}
26#define EPILOGUE(level) {% call at_level(level) %}
27#define BODY(range) {% call(cell_up,cell_down) body(range) %}
28
29#define CST_IF(condition, action) {{ cst_if(#condition, #action) }}
30#define CST_IFTHEN(condition) {{flat}}if condition
31#define CST_ELSEIF(condition) {{flat}}elif condition
32#define CST_ELSE {{flat}}else
33#define CST_ENDIF {{flat}}endif
34
35#endif
36
37#ifdef PASS_JINJA
38
39{#  -------------------------- Design notes ------------------------------
40
41Using its optional argument 'options', forall_cells() sets variable 'flags'
42and passes it on to on_mesh() / on_edges()
43
44Similarly variable 'lrange' is passed from forall_cells to on_mesh() / on_egdes() 
45
46-------------------------------------------------------------------------- #}
47
48{# ----------------------- SEQUENCE, FORALL ----------------------- #}
49
50{% macro sequence() -%}
51 
52!$OMP DO SCHEDULE(STATIC)                                                                           
53  DO ij=1,primal_num
54    {{ caller() }}
55  END DO
56!$OMP END DO
57
58{%- endmacro %}
59
60{% macro body(range) -%}
61  {{ define('CELL','l,ij') }}
62    DO l = {{ range }}
63      {{ caller() }}
64    END DO
65  {{ undef('CELL') }}
66{%- endmacro %}
67
68{% macro at_level(lev) -%}
69  {{ define('CELL','l,ij') }}
70  l={{ lev }}
71  {{ caller() }}
72  {{ undef('CELL') }}
73{%- endmacro %}
74
75{% macro forall_cells(start='1', end='llm', options=[]) -%}
76  {% set flags=[options] if options is string else options %}
77  {{ caller((start,end),flags) }}
78{%- endmacro %}
79
80{# ------------------------------ MESHES --------------------------- #}
81
82{% macro vloop(mesh, lrange, flags, code) %}
83{% set start,end = lrange %}
84{% set thecode, is_top_layer, is_top_inter = code(mesh), False, False %}
85
86{{ define('IS_TOP_LAYER', '_FALSE_') }}
87{{ define('IS_TOP_INTERFACE', '_FALSE_') }}
88
89{% if 'IS_BOTTOM_LEVEL' in thecode or 'KDOWN' in thecode%}
90{# the code in the loop checks whether l==1, for the sake of performance
91we shall write special code for l=1 and start the loop at l=2 #}
92{{ define('IS_BOTTOM_LEVEL', '_TRUE_') }}
93{{ 'kdown = 1' if 'KDOWN' in thecode }}
94{{ 'kup   = 1' if 'KUP'   in thecode }}
95l=1
96{{ thecode }}
97{% set start='2' %}
98{% endif %}
99
100{% if 'IS_TOP_LAYER' in thecode %}
101{{ 'ERROR : using IS_TOP_LAYER in a loop ending at l=llm+1' if end=='llm+1' }}
102{# the code checks whether l==llm, write special code for l=llm and end the loop at l=llm-1 #}
103{% set end, is_top_layer = 'llm-1', True %}
104{% endif %}
105
106{% if 'IS_TOP_INTERFACE' in thecode or 'KUP' in thecode %}
107{# the code checks whether l==llm+1, write special code for l=llm+1 and end the loop at l=llm #}
108{{ 'ERROR : using IS_TOP_INTERFACE in a loop ending at l=llm' if end=='llm' }}
109{% set end, is_top_inter ='llm', True %}
110{% endif %}
111
112{{ define('IS_BOTTOM_LEVEL', '_FALSE_') }}
113  DO l = {{start}}, {{end}}
114    {{ 'kdown = l-1' if 'KDOWN' in thecode }}
115    {{ 'kup   = l' if 'KUP'   in thecode }}
116    {{ thecode }}
117  END DO
118
119{% if is_top_layer %}
120{{ define('IS_TOP_LAYER', '_TRUE_') }}
121{{ 'kdown = llm-1' if 'KDOWN' in thecode }}
122{{ 'kup   = llm' if 'KUP'   in thecode }}
123l=llm
124{{ thecode }}
125{% endif %}
126
127{% if is_top_inter %}
128{{ define('IS_TOP_INTERFACE', '_TRUE_') }}
129{{ 'kdown = llm' if 'KDOWN' in thecode }}
130{{ 'kup   = llm' if 'KUP'   in thecode }}
131l=llm+1
132{{ thecode }}
133{% endif %}
134
135{% endmacro %}
136
137{% macro on_mesh(mesh,lrange,flags) -%}
138{{ define('CELL','l,ij') if mesh=='primal' }}
139{{ define('DUAL_CELL', 'l,ij') if mesh=='dual'}}
140!$OMP DO SCHEDULE(STATIC)                                                                           
141DO ij = 1, {{ mesh }}_num
142{{ vloop(mesh, lrange, flags, caller) }}
143END DO
144!$OMP END DO
145{{ undef('CELL') }}
146{{ undef('DUAL_CELL') }}
147{%- endmacro %}
148
149{# ------------------------------ STENCILS --------------------------- #}
150
151{% macro on_edges(mesh,lrange,flags) -%}
152{% set thecode = caller(mesh) %}
153{{ define('EDGE', 'l,edge') }}
154{{ define('LE_DE', 'le_de(edge)') }}
155{{ define('SIGN', '1.') }}
156{{ define('CELL1', 'l,ij_left') }}
157{{ define('CELL2',  'l,ij_right') }}
158{{ define('VERTEX1', 'l,ij_down') }}
159{{ define('VERTEX2', 'l,ij_up') }}
160!$OMP DO SCHEDULE(STATIC)
161DO edge = 1, edge_num
162{{ 'ij_left = left(edge)'   if 'CELL1'    in thecode }}
163{{ 'ij_right = right(edge)' if 'CELL2'   in thecode }}
164{{ 'ij_up = up(edge)'       if 'VERTEX1' in thecode }}
165{{ 'ij_down = down(edge)'   if 'VERTEX2' in thecode }}
166{{ vloop(mesh, lrange, flags, caller) }}
167END DO
168!$OMP END DO
169{{ undef('EDGE') }}
170{{ undef('LE_DE') }}
171{{ undef('SIGN') }}
172{{ undef('CELL1') }}
173{{ undef('CELL2') }}
174{{ undef('VERTEX1') }}
175{{ undef('VERTEX2') }}
176{%- endmacro %}
177
178{% macro forall_edges(mesh) -%}
179{% set thecode = caller() %}
180{{ define('EDGE', 'l,edge') }}
181{{ define('VERTEX1', 'l,ij_down') }}
182{{ define('VERTEX2', 'l,ij_up') }}
183{{ define('SIGN', mesh + '_ne(iedge,ij)') }}
184{{ define('LE_DE', 'le_de(edge)') }}
185DO iedge = 1, {{ mesh }}_deg(ij)
186  edge = {{ mesh }}_edge(iedge,ij)
187{{ 'ij_up = up(edge)'       if 'VERTEX1' in thecode }}
188{{ 'ij_down = down(edge)'   if 'VERTEX2' in thecode }}
189  {{ thecode }}
190END DO
191{{ undef('EDGE') }}
192{{ undef('SIGN') }}
193{{ undef('LE_DE') }}
194{%- endmacro %}
195
196{% macro forall_trisk() -%}
197{{ define('EDGE_TRISK', 'l,edge_trisk') }}
198DO itrisk = 1, trisk_deg(edge)
199  edge_trisk = trisk(itrisk,edge)
200  {{ caller() }}
201END DO
202{{ undef('EDGE_TRISK') }}
203{%- endmacro %}
204
205{% macro forall_vertices(mesh) -%}
206{{ define('VERTEX', 'l,vertex') }}
207DO ivertex = 1, {{ mesh }}_deg(ij)
208  vertex = {{ mesh }}_vertex(ivertex,ij)
209  {{ caller() }}
210END DO
211{{ undef('VERTEX') }}
212{%- endmacro %}
213
214{# --------------------------------------------------------- #}
215
216{% set llm='llm' %}
217
218{{ undef('SIGN') }}
219{{ undef('CELL') }}
220{{ undef('CELL1') }}
221{{ undef('CELL2') }}
222{{ undef('VERTEX1') }}
223{{ undef('VERTEX2') }}
224{{ undef('EDGE_TRISK') }}
225
226{# --------------------- END JINJA ------------------------- #}
227
228#endif
229
230#ifdef PASS_POST1
231
232#define _TRUE_ (0==0)
233#define _FALSE_ (0==1)
234
235#define RIV2 Riv2(ivertex,ij)
236#define AI Ai(ij)
237#define AV Av(ij)
238#define FV fv(ij)
239#define WEE wee(itrisk,edge)
240#define edge_ne(iedge,ij) 1.
241#endif
242
243#ifdef PASS_POST2
244#define KUP(l,ij) kup,ij
245#define KDOWN(l,ij) kdown,ij
246#define DOWN(l,ij) l-1,ij
247#define UP(l,ij) l+1,ij
248#define HIDX(l,ij) ij
249#define VIDX(l,ij) l
250#endif
Note: See TracBrowser for help on using the repository browser.