reverse_index_array Module Function

module function reverse_index_array(a, indices, from, new_index_size) result(c)

Index an autodiff array

Arguments

Type IntentOptional Attributes Name
class(array_type), intent(in), target :: a
integer, intent(in), dimension(:) :: indices
logical, intent(in) :: from
integer, intent(in) :: new_index_size

Return Value type(array_type), pointer


Source Code

  module function reverse_index_array(a, indices, from, new_index_size) result(c)
    !! Index an autodiff array
    implicit none
    class(array_type), intent(in), target :: a
    integer, dimension(:), intent(in) :: indices
    logical, intent(in) :: from
    integer, intent(in) :: new_index_size
    type(array_type), pointer :: c

    integer :: i, s

    allocate(c)
    if(from) then
       call c%allocate(array_shape=[size(a%val,1), new_index_size])
       c%val = 0.0_real32
       do concurrent(s=1:size(indices), i=1:size(a%val,1))
          c%val(i, s) = a%val(i, indices(s))
       end do
    else
       call c%allocate(array_shape=[size(a%val,1), new_index_size])
       c%val = 0.0_real32
       do concurrent(s=1:size(indices), i=1:size(a%val,1))
          c%val(i, indices(s)) = a%val(i, s)
       end do
    end if
    c%indices = indices

    if(a%requires_grad) then
       c%requires_grad = .true.
       c%is_forward = a%is_forward
       c%operation = 'index'
       c%left_operand => a
       c%owns_left_operand = a%is_temporary
    end if
  end function reverse_index_array