psyclone.psyir.transformations.intrinsics.dotproduct2code_trans#

Module providing a transformation from a PSyIR DOT_PRODUCT operator to PSyIR code. This could be useful if the DOT_PRODUCT operator is not supported by the back-end or if the performance in the inline code is better than the intrinsic.

Classes#

class psyclone.psyir.transformations.intrinsics.dotproduct2code_trans.DotProduct2CodeTrans[source]#

Provides a transformation from a PSyIR DOT_PRODUCT Operator node to equivalent code in a PSyIR tree. Validity checks are also performed.

If R is a scalar and A, and B have dimension N, The transformation replaces:

R = ... DOT_PRODUCT(A,B) ...

with the following code:

TMP = 0.0
do I=1,N
    TMP = TMP + A(i)*B(i)
R = ... TMP ...

For example:

>>> from psyclone.psyir.backend.fortran import FortranWriter
>>> from psyclone.psyir.frontend.fortran import FortranReader
>>> from psyclone.psyir.nodes import IntrinsicCall
>>> from psyclone.psyir.transformations import DotProduct2CodeTrans
>>> code = ("subroutine dot_product_test(v1,v2)\n"
...         "real,intent(in) :: v1(10), v2(10)\n"
...         "real :: result\n"
...         "result = dot_product(v1,v2)\n"
...         "end subroutine\n")
>>> psyir = FortranReader().psyir_from_source(code)
>>> trans = DotProduct2CodeTrans()
>>> trans.apply(psyir.walk(IntrinsicCall)[0])
>>> print(FortranWriter()(psyir))
subroutine dot_product_test(v1, v2)
  real, dimension(10), intent(in) :: v1
  real, dimension(10), intent(in) :: v2
  real :: result
  integer :: i
  real :: res_dot_product

  res_dot_product = 0.0
  do i = 1, 10, 1
    res_dot_product = res_dot_product + v1(i) * v2(i)
  enddo
  result = res_dot_product

end subroutine dot_product_test

Inheritance

Inheritance diagram of DotProduct2CodeTrans
apply(node, options=None, **kwargs)[source]#

Apply the DOT_PRODUCT intrinsic conversion transformation to the specified node. This node must be a DOT_PRODUCT BinaryOperation. If the transformation is successful then an assignment which includes a DOT_PRODUCT BinaryOperation node is converted to equivalent inline code.

Parameters:
validate(node, options=None, **kwargs)[source]#

Perform checks to ensure that it is valid to apply the DotProduct2CodeTran transformation to the supplied node.

Note, this validation does not check for invalid argument combinations to dot_product (e.g. different precision or different datatypes) as that should have already been picked up when creating the PSyIR.

Parameters:
Raises:
  • TransformationError – if one of the arguments is not a Reference node.

  • TransformationError – if an argument does not use array slice notation and is not a 1d array.

  • TransformationError – if an argument uses array slice notation but the array slice is not for the first dimension of the array.

  • TransformationError – if an argument uses array slice notation but it is not for the full range of the dimension.