reverse_mode_ptr Subroutine

recursive subroutine reverse_mode_ptr(array, upstream_grad, depth)

Backward operation for arrays

Arguments

Type IntentOptional Attributes Name
class(array_type), intent(inout) :: array
type(array_type), intent(in), pointer :: upstream_grad
integer, intent(in) :: depth

Source Code

  recursive subroutine reverse_mode_ptr(array, upstream_grad, depth)
    !! Backward operation for arrays
    implicit none
    class(array_type), intent(inout) :: array
    type(array_type), pointer, intent(in) :: upstream_grad
    integer, intent(in) :: depth

    type(array_type), pointer :: left_partial, right_partial

    ! write(*,'("Performing backward operation for: ",A,T60,"id: ",I0)') &
    !      trim(array%operation), array%id
    if(depth.gt.diffstruc__max_recursion_depth)then
       write(0,*) "MAX RECURSION DEPTH REACHED IN REVERSE MODE", depth
       return
    end if
    array%is_forward = .false.
    if(associated(array%left_operand))then
       if(array%left_operand%requires_grad)then
          allocate(left_partial)
          left_partial = array%get_partial_left(upstream_grad)
          left_partial%is_temporary = .true.
          call accumulate_gradient_ptr(array%left_operand, left_partial, depth)
       end if
    end if
    if(associated(array%right_operand))then
       if(array%right_operand%requires_grad)then
          allocate(right_partial)
          right_partial = array%get_partial_right(upstream_grad)
          right_partial%is_temporary = .true.
          call accumulate_gradient_ptr(array%right_operand, right_partial, depth)
       end if
    end if
    ! write(*,*) "done operation: ", trim(array%operation)
  end subroutine reverse_mode_ptr