stfc / PSyclone

Domain-specific compiler and code transformation system for Finite Difference/Volume/Element Earth-system models in Fortran
BSD 3-Clause "New" or "Revised" License
106 stars 28 forks source link

Support scalars in SIR #1856

Open rupertford opened 2 years ago

rupertford commented 2 years ago

nemo code can contain scalars. For example, from tracer advection ...

    zbtr = 1.
    DO jk = 1, jpk-1
       DO jj = 2, jpj-1     
          DO ji = 2, jpi-1
             ztra = -zbtr * ( zwx(ji,jj,jk) - zwx(ji,jj,jk+1) )
             mydomain(ji,jj,jk) = ztra
          END DO
       END DO
    END DO

There is no way of directly mapping such scalars to SIR as SIR does not support scalars that are not part of a loop nest.

rupertford commented 2 years ago

There are a few potential options that have been discussed with Matthias which are ...

1) make the scalar a global constant. 2) declare and use the scalar within the appropriate loop(s) in the SIR. 3) remove the scalar by replacing it with its value.

I don't think there are any other options.

rupertford commented 2 years ago

In order to make the scalar a global constant it must only have a single value and its value must be a compile time constant (I think). This is obviously a limited case but this is how the nemo code is written so it will work for this code.

To use the scalar within the appropriate loops, the code needs to be analysed to see where the scalars are and where they are used and then the appropriate code added (the scalar must be declared and set to the right value) within the SIR loop construct.

To replace the scalar, all reads of the scalar must be replaced with the literal value of the previous write to the scalar.

rupertford commented 2 years ago

The simplest solution is probably to replace the variable with its value. To do this the variable should be a scalar and its value should be an expression that does not contain any scalars. For each read of the variable we should replace the read with the value of the previous write. Once all reads have been replaced, the write should also be removed. This should be repeated until there are no references to the scalar. If it is a local scalar then it can be removed from the symbol table.