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

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

devel/unstructured : more loop unrolling

File size: 10.8 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, degree=0) on_mesh('primal',lrange,flags) %}
17#define ON_DUAL {% call(mesh, degree=0) on_mesh('dual',lrange,flags) %}
18#define ON_EDGES {% call(mesh, degree=0) on_edges('edge',lrange,flags) %}
19#define FORALL_EDGES {% call forall_edges(mesh,degree) %}
20#define FORALL_VERTICES {% call forall_vertices(mesh,degree) %}
21#define FORALL_TRISK {% call forall_trisk(degree) %}
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
41The syntax
42{% call(ARGNAMES) macro(ARGS) %}
43...
44{% endcall %}
45passes ARGS to macro ; this macro "calls" the body using the syntax 'caller(arg1,...)' ; these arguments
46are available in the body under the names ARGNAMES, as if it were a macro caller(ARGNAMES).
47
48Using its optional argument 'options', forall_cells() sets variable 'flags'
49and passes it on to on_mesh() / on_edges()
50
51Similarly variable 'lrange' is passed from forall_cells to on_mesh() / on_egdes() 
52
53-------------------------------------------------------------------------- #}
54
55
56{# ----------------------- SEQUENCE, FORALL ----------------------- #}
57
58{% macro sequence() -%}
59 
60!$OMP DO SCHEDULE(STATIC)                                                                           
61  DO ij=1,primal_num
62    {{ caller() }}
63  END DO
64!$OMP END DO
65
66{%- endmacro %}
67
68{% macro body(range) -%}
69  {{ define('CELL','l,ij') }}
70    DO l = {{ range }}
71      {{ caller() }}
72    END DO
73  {{ undef('CELL') }}
74{%- endmacro %}
75
76{% macro at_level(lev) -%}
77  {{ define('CELL','l,ij') }}
78  l={{ lev }}
79  {{ caller() }}
80  {{ undef('CELL') }}
81{%- endmacro %}
82
83{% macro forall_cells(start='1', end='llm', options=[]) -%}
84  {% set flags=[options] if options is string else options %}
85  {{ caller((start,end),flags) }}
86{%- endmacro %}
87
88{# ---------------------- PRELOAD STENCIL INDICES ---------------------- #}
89
90{% macro get_edges(thecode, mesh,degree) %}
91{% for iedge in range(1,degree+1) %}
92{{ 'edge%d = %s_edge(%d,ij)'%(iedge,mesh,iedge) if 'EDGE' in thecode }}
93{% endfor %}
94{% for iedge in range(1,degree+1) %}
95{{ 'le_de%d = le_de(edge%d)'%(iedge,iedge) if 'LE_DE' in thecode }}     
96{% endfor %}
97{% for iedge in range(1,degree+1) %}
98{{ 'sign%d = %s_ne(%d,ij)'%(iedge,mesh,iedge) if 'SIGN' in thecode }}   
99{% endfor %}
100{% for iedge in range(1,degree+1) %}
101{{ 'ij_up%d = up(edge%d)'%(iedge,iedge) if 'VERTEX1' in thecode }}
102{% endfor %}
103{% for iedge in range(1,degree+1) %}
104{{ 'ij_down%d = down(edge%d)'%(iedge,iedge) if 'VERTEX2' in thecode }}
105{% endfor %}
106{% endmacro %}
107
108{% macro get_vertices(thecode, mesh,degree) %}
109{% for ivertex in range(1,degree+1) %}
110{{ 'vertex%d = %s_vertex(%d,ij)'%(ivertex,mesh,ivertex) if 'VERTEX' in thecode }}
111{% endfor %}
112{% endmacro %}
113
114{# ------------------------------ MESHES --------------------------- #}
115
116{# argument 'code' is the body enclosed by ON_XXX ... END_BLOCK ; it takes arguments : mesh,degree=0 #}
117
118{% macro vloop_unroll(mesh, lrange, flags, code) %}
119
120{% set thecode, has_primal, has_dual, has_trisk, has_none = code(mesh), False, False, False, True %}
121
122{% if 'primal_deg' in thecode %}
123! this VLOOP iterates over primal cell edges
124{% set has_primal, has_none = True,False %}
125{% endif %}
126{% if 'dual_deg' in thecode %}
127! this VLOOP iterates over dual cell edges
128{% set has_dual,has_none = True,False %}
129{% endif %}
130{% if 'trisk_deg' in thecode %}
131! this VLOOP iterates over the TRISK stencil
132{% set has_trisk,has_none = True,False %}
133{% endif %}
134
135{% if has_none %}
136{{ vloop(mesh, lrange, flags, code) }}
137{% endif %}
138
139{% if has_primal %}
140SELECT CASE(primal_deg(ij))
141CASE(4)
142{{ get_edges(thecode, mesh, 4) }}
143{{ vloop(mesh, lrange, flags, code, 4) }}
144CASE(5)
145{{ get_edges(thecode, mesh, 5) }}
146{{ vloop(mesh, lrange, flags, code, 5) }}
147CASE(6)
148{{ get_edges(thecode, mesh, 6) }}
149{{ vloop(mesh, lrange, flags, code, 6) }}
150CASE DEFAULT
151{{ vloop(mesh, lrange, flags, code) }}
152END SELECT
153{% endif %}
154
155{% if has_dual %}
156SELECT CASE(dual_deg(ij))
157CASE(3)
158{{ get_edges(thecode, mesh, 3) }}
159{{ get_vertices(thecode,mesh,3) }}
160{{ vloop(mesh, lrange, flags, code, 3) }}
161CASE(4)
162{{ get_edges(thecode, mesh, 4) }}
163{{ get_vertices(thecode,mesh,4) }}
164{{ vloop(mesh, lrange, flags, code, 4) }}
165CASE DEFAULT
166{{ vloop(mesh, lrange, flags, code) }}
167END SELECT
168{% endif %}
169
170{% if has_trisk %}
171SELECT CASE(trisk_deg(edge))
172CASE(10)
173{{ vloop(mesh, lrange, flags, code, 10) }}
174CASE(4)
175{{ vloop(mesh, lrange, flags, code, 4) }}
176CASE DEFAULT
177{{ vloop(mesh, lrange, flags, code) }}
178END SELECT
179{% endif %}
180
181{%- endmacro %}
182
183{% macro vloop(mesh, lrange, flags, code, degree=0) %}
184
185{% set start,end = lrange %}
186{% set thecode, is_top_layer, is_top_inter = code(mesh,degree), False, False %}
187
188{{ define('IS_TOP_LAYER', '_FALSE_') }}
189{{ define('IS_TOP_INTERFACE', '_FALSE_') }}
190
191{% if 'IS_BOTTOM_LEVEL' in thecode or 'KDOWN' in thecode%}
192{# the code in the loop checks whether l==1, for the sake of performance
193we shall write special code for l=1 and start the loop at l=2 #}
194{{ define('IS_BOTTOM_LEVEL', '_TRUE_') }}
195{{ 'kdown = 1' if 'KDOWN' in thecode }}
196{{ 'kup   = 1' if 'KUP'   in thecode }}
197l=1
198{{ thecode }}
199{% set start='2' %}
200{% endif %}
201
202{% if 'IS_TOP_LAYER' in thecode %}
203{{ 'ERROR : using IS_TOP_LAYER in a loop ending at l=llm+1' if end=='llm+1' }}
204{# the code checks whether l==llm, write special code for l=llm and end the loop at l=llm-1 #}
205{% set end, is_top_layer = 'llm-1', True %}
206{% endif %}
207
208{% if 'IS_TOP_INTERFACE' in thecode or 'KUP' in thecode %}
209{# the code checks whether l==llm+1, write special code for l=llm+1 and end the loop at l=llm #}
210{{ 'ERROR : using IS_TOP_INTERFACE in a loop ending at l=llm' if end=='llm' }}
211{% set end, is_top_inter ='llm', True %}
212{% endif %}
213
214{{ define('IS_BOTTOM_LEVEL', '_FALSE_') }}
215!DIR$ SIMD
216  DO l = {{start}}, {{end}}
217    {{ 'kdown = l-1' if 'KDOWN' in thecode }}
218    {{ 'kup   = l' if 'KUP'   in thecode }}
219    {{ thecode }}
220  END DO
221
222{% if is_top_layer %}
223{{ define('IS_TOP_LAYER', '_TRUE_') }}
224{{ 'kdown = llm-1' if 'KDOWN' in thecode }}
225{{ 'kup   = llm' if 'KUP'   in thecode }}
226l=llm
227{{ thecode }}
228{% endif %}
229
230{% if is_top_inter %}
231{{ define('IS_TOP_INTERFACE', '_TRUE_') }}
232{{ 'kdown = llm' if 'KDOWN' in thecode }}
233{{ 'kup   = llm' if 'KUP'   in thecode }}
234l=llm+1
235{{ thecode }}
236{% endif %}
237
238{% endmacro %}
239
240{% macro on_mesh(mesh,lrange,flags) -%}
241{{ define('CELL','l,ij') if mesh=='primal' }}
242{{ define('DUAL_CELL', 'l,ij') if mesh=='dual'}}
243!$OMP DO SCHEDULE(STATIC)                                                                           
244DO ij = 1, {{ mesh }}_num
245{{ vloop_unroll(mesh, lrange, flags, caller) }}
246END DO
247!$OMP END DO
248{{ undef('CELL') }}
249{{ undef('DUAL_CELL') }}
250{%- endmacro %}
251
252{# ------------------------------ STENCILS --------------------------- #}
253
254{% macro on_edges(mesh,lrange,flags) -%}
255{% set thecode = caller(mesh) %}
256{{ define('EDGE', 'l,edge') }}
257{{ cdef(thecode, 'LE_DE', 'le_de(edge)') }}
258{{ cdef(thecode, 'SIGN', '1.') }}
259{{ cdef(thecode, 'CELL1', 'l,ij_left') }}
260{{ cdef(thecode, 'CELL2',  'l,ij_right') }}
261{{ cdef(thecode, 'VERTEX1', 'l,ij_down') }}
262{{ cdef(thecode, 'VERTEX2', 'l,ij_up') }}
263!$OMP DO SCHEDULE(STATIC)
264DO edge = 1, edge_num
265{{ 'ij_left = left(edge)'   if 'CELL1'    in thecode }}
266{{ 'ij_right = right(edge)' if 'CELL2'   in thecode }}
267{{ 'ij_up = up(edge)'       if 'VERTEX1' in thecode }}
268{{ 'ij_down = down(edge)'   if 'VERTEX2' in thecode }}
269{{ vloop_unroll(mesh, lrange, flags, caller) }}
270END DO
271!$OMP END DO
272{{ cundef(thecode, ('EDGE','LE_DE','SIGN','CELL1','CELL2','VERTEX1','VERTEX2') ) }}
273{%- endmacro %}
274
275{% macro forall_edges(mesh,degree) -%}
276{% set thecode = caller() %}
277
278{% if degree>1 %}
279{% for iedge in range(1,degree+1) %}
280{{ cdef(thecode, 'EDGE', 'l,edge%d'%iedge) }}
281{{ cdef(thecode, 'SIGN', 'sign%d'%iedge) }}
282{{ cdef(thecode, 'LE_DE', 'le_de%d'%iedge) }}
283{{ cdef(thecode, 'VERTEX1', 'l,ij_up%d'%iedge) }}
284{{ cdef(thecode, 'VERTEX2', 'l,ij_down%d'%iedge) }}
285  {{ thecode }}
286{% endfor %}
287{% else %}
288{{ cdef(thecode, 'EDGE', 'l,edge') }}
289{{ cdef(thecode, 'SIGN', mesh + '_ne(iedge,ij)') }}
290{{ cdef(thecode, 'LE_DE', 'le_de(edge)') }}
291{{ cdef(thecode, 'VERTEX1', 'l,ij_up') }}
292{{ cdef(thecode, 'VERTEX2', 'l,ij_down') }}
293DO iedge = 1, {{ mesh }}_deg(ij)
294  edge = {{ mesh }}_edge(iedge,ij)
295  {{ 'ij_up = up(edge)'       if 'VERTEX1' in thecode }}
296  {{ 'ij_down = down(edge)'   if 'VERTEX2' in thecode }}
297  {{ thecode }}
298END DO
299{% endif %}
300{{ cundef(thecode, ('EDGE', 'SIGN', 'LE_DE', 'VERTEX1', 'VERTEX2') ) }}
301{%- endmacro %}
302
303{% macro forall_trisk(degree) -%}
304{% set thecode = caller() %}
305{{ define('EDGE_TRISK', 'l,edge_trisk') }}
306{% if degree>1 %}
307{% for itrisk in range(1,degree+1) %}
308  itrisk = {{ itrisk }}
309  edge_trisk = trisk({{ itrisk }},edge)
310  {{ thecode }}
311{% endfor %}
312{% else %}
313DO itrisk = 1, trisk_deg(edge)
314  edge_trisk = trisk(itrisk,edge)
315  {{ thecode }}
316END DO
317{% endif %}
318{{ undef('EDGE_TRISK') }}
319{%- endmacro %}
320
321{% macro forall_vertices(mesh,degree) -%}
322{% set thecode = caller() %}
323{% if degree>1 %}
324{% for ivertex in range(1,degree+1) %}
325{{ define('RIV2', 'Riv2(%d,ij)'%ivertex ) }}
326{{ define('VERTEX', 'l,vertex%d'%ivertex ) }}
327  {{ thecode }}
328{% endfor %}
329{% else %}
330{{ define('RIV2', 'Riv2(ivertex,ij)') }}
331{{ define('VERTEX', 'l,vertex') }}
332DO ivertex = 1, {{ mesh }}_deg(ij)
333  vertex = {{ mesh }}_vertex(ivertex,ij)
334  {{ thecode }}
335END DO
336{% endif %}
337{{ undef('VERTEX') }}
338{{ undef('RIV2') }}
339{%- endmacro %}
340
341{# --------------------------------------------------------- #}
342
343{% set llm='llm' %}
344
345{{ undef('SIGN') }}
346{{ undef('CELL') }}
347{{ undef('CELL1') }}
348{{ undef('CELL2') }}
349{{ undef('VERTEX1') }}
350{{ undef('VERTEX2') }}
351{{ undef('EDGE_TRISK') }}
352
353{# --------------------- END JINJA ------------------------- #}
354
355#endif
356
357#ifdef PASS_POST1
358
359#define _TRUE_ (0==0)
360#define _FALSE_ (0==1)
361
362#define AI Ai(ij)
363#define AV Av(ij)
364#define FV fv(ij)
365#define WEE wee(itrisk,edge)
366#define edge_ne(iedge,ij) 1.
367#endif
368
369#ifdef PASS_POST2
370#define KUP(l,ij) kup,ij
371#define KDOWN(l,ij) kdown,ij
372#define DOWN(l,ij) l-1,ij
373#define UP(l,ij) l+1,ij
374#define HIDX(l,ij) ij
375#define VIDX(l,ij) l
376#endif
Note: See TracBrowser for help on using the repository browser.