psyclone.psyad.tl2ad

The implementation of PSyAD : the PSyclone Adjoint support. Transforms an LFRic tangent linear kernel to its adjoint.

Functions

psyclone.psyad.tl2ad.generate_adjoint_str(tl_fortran_str, active_variables, api=None, create_test=False, coord_arg_index=None, panel_id_arg_index=None)

Takes a tangent-linear kernel encoded as a string as input and returns its adjoint encoded as a string along with (if requested) a test harness, also encoded as a string.

Parameters:
  • tl_fortran_str (str) – Fortran implementation of a tangent-linear kernel.

  • active_variables (List[str]) – list of active variable names.

  • api (Optional[str]) – the PSyclone API in use, if any.

  • create_test (Optional[bool]) – whether or not to create test code for the adjoint kernel.

  • coord_arg_index (Optional[int]) – the (1-based) index of the kernel argument holding the mesh coordinates (if any). Only applies to the LFRic (dynamo0.3) API.

  • panel_id_arg_index (Optional[int]) – the (1-based) index of the kernel argument holding the panel IDs (if any). Only applies to the LFRic (dynamo0.3) API.

Returns:

a 2-tuple consisting of a string containing the Fortran implementation of the supplied tangent-linear kernel and (if requested) a string containing the Fortran implementation of a test harness for the adjoint kernel.

Return type:

Tuple[str, str]

Raises:
  • NotImplementedError – if the tangent-linear code is a function.

  • NotImplementedError – if an unsupported API is specified.

  • NotImplementedError – if test-harness generation is requested for the LFRic API.

psyclone.psyad.tl2ad.generate_adjoint(tl_psyir, active_variables)

Takes a tangent-linear kernel represented in language-level PSyIR and returns its adjoint represented in language-level PSyIR.

Parameters:
  • tl_psyir (psyclone.psyir.Node) – language-level PSyIR containing the tangent-linear kernel.

  • active_variables (List[str]) – list of active variable names.

Returns:

language-level PSyIR containing the adjoint of the supplied tangent-linear kernel.

Return type:

psyclone.psyir.Node

Raises:
  • InternalError – if the PSyIR does not contain any Routines.

  • NotImplementedError – if the PSyIR contains >1 Routine.

psyclone.psyad.tl2ad.generate_adjoint_test(tl_psyir, ad_psyir, active_variables)

Creates the PSyIR of a test harness for the supplied TL and adjoint kernels.

Parameters:
  • tl_psyir (psyclone.psyir.Container) – PSyIR of the tangent-linear kernel code.

  • ad_psyir (psyclone.psyir.Container) – PSyIR of the adjoint kernel code.

  • active_variables (List[str]) – names of active variables.

Returns:

the PSyIR of the test harness.

Return type:

psyclone.psyir.Routine

Raises:
  • NotImplementedError – if the supplied PSyIR contains more than one Routine.

  • NotImplementedError – if the supplied TL/Adjoint PSyIR contains just a Routine that is a Program (since this would have to be converted to a subroutine in order to construct the test harness).

  • NotImplementedError – if one of the kernel arguments is dimensioned by a variable that is not passed as an argument.

  • InternalError – if a kernel argument has a shape defined by something other than ArrayType.Extent or a Reference.