Implementation

This module performs source-to-source automatic differentiation of a target routine, in which dependent variables are differentiated with respect to independent variables.

This is implemented in PSyclone by parsing the source code file containing the target routine, and eventually the routines it calls, transforming it into a PSyIR AST and applying automatic differentiation transformations in either forward-mode or reverse-mode to the nodes thus obtained. The resulting PSyIR tree is then written to Fortran source code.

Implemented features

For now, only Fortran subroutines ie. neither functions nor programs can be transformed. The implementation only deals with scalar variables, which is to say that subroutines containing arrays, either as local variables or as arguments cannot yet be transformed. The statements the target routine (and eventual routines it calls) may contain are :

  • assignments (PSyIR Assignment nodes),

  • calls to subroutines (PSyIR Call nodes).

These statements may contain unary and binary linear or non-linear operations (PSyIR UnaryOperation and BinaryOperation nodes).

An optional verbose mode is available, which is especially useful when examining the transformed statements and routines in reverse-mode.

Basic simplification and substitution rules can be applied as an optional postprocessing step to shorten the transformed code and improve its readability.

Reverse-mode transformations store overwritten values using a “tape” that is implemented as a static array, rather than a LIFO stack as in many implementations, so that the transformed routines may (someday) be parallelized and/or offloaded to GPU.

Also in reverse-mode, three types of reversal schedules are available:

Missing features

What has not been implemented includes:

  • functions and programs,

  • differentiating called routines that are not found in the same file (or Container node) as the target routine,

  • nary operations,

  • loops,

  • control flow,

  • array variables and arguments,

  • activity analysis (dependence DAG),

  • to-be-recorded (TBR) analysis,

  • taping operations results to reduce the computational complexity of the adjoint,

  • and much more.