MeteoSwiss-APN / dawn

Compiler toolchain to enable generation of high-level DSLs for geophysical fluid dynamics models
MIT License
28 stars 30 forks source link

Design Concepts for For / Do While innermost loops #460

Open mroethlin opened 5 years ago

mroethlin commented 5 years ago

In ICON there is pattern where a stencil has an innermost loop. This can for example be a summation over a cell local array of fixed length (e.g. computation of Gauss quadrature weights) or a DO ... WHILE loop to ensure convergence of a quantity (c.f. upwind_vflux_ppm_cfl for an example).

This work item is about the elicitation of detailed requirements for introducing for loops as innermost loops as an IIR concept. No code shall be written for the completion of this work item, but a detailed plan to actually follow through with a specific implementation is to be made.

tobiasgrosser commented 5 years ago

This is also something we are interested in here at ETH.

Stagno commented 5 years ago

My suggestion on how they should be

  1. A for loop should be a statement element of the AST, so a class derived from Stmt. It should contain a block of statements, the loop statements, as children. It should refer (by name or id) to an integer loop variable, whose scope is restricted to the for's block of statements. The loop variable's range and step should be specified as 3 generic expressions (Expr) which are members (also children) of the for statement: range_start:step:range_end, with step!=0. Their interpretation (same as MATLAB) corresponds to the following C implementation:
    for(int i=range_start; step>0 ? i<=range_end : i>=range_start; i+=step) {...}
  2. I would go for a while loop instead of do-while. It should be a statement inheriting from Stmt and should contain a block of loop statements as children. No explicit loop variable here, but just a condition (Expr) that needs to be evaluated before each loop iteration.

Another approach, which may simplify our lives, would be to keep just the while in the SIR and the IIR and let the frontend translate the for into a while by adding the loop variable, its initialization, its step-increment and the within-range check as loop condition. Note: adding it to the AST will also allow to use this in the control flow.