torch_tensor_to_array_real32_4d Subroutine

public subroutine torch_tensor_to_array_real32_4d(tensor, data_out, sizes)

Return the array data associated with a Torch tensor of rank 4 and data type real32

Arguments

Type IntentOptional Attributes Name
type(torch_tensor), intent(in) :: tensor

Returned tensor

real(kind=real32), intent(out), pointer :: data_out(:,:,:,:)

Pointer to tensor data

integer, intent(in), optional :: sizes(4)

Number of entries for each rank


Source Code

  subroutine torch_tensor_to_array_real32_4d(tensor, data_out, sizes)
    use, intrinsic :: iso_c_binding, only : c_int, c_int64_t, c_loc
    use, intrinsic :: iso_fortran_env, only : real32, int64
    type(torch_tensor), intent(in) :: tensor !! Returned tensor
    real(kind=real32), pointer, intent(out) :: data_out(:,:,:,:) !! Pointer to tensor data
    integer, optional, intent(in) :: sizes(4) !! Number of entries for each rank
    integer(kind=int64), allocatable :: my_shape(:) !! Number of entries for each rank

    ! Local data
    integer(c_int), parameter :: c_dtype = torch_kFloat32 !! Data type
    type(c_ptr) :: cptr

    my_shape = tensor%get_shape()

    if (present(sizes)) then
      if (.not. all(my_shape == sizes)) then
        write(*,*) 'Error :: sizes argument does not match shape of tensor'
        write(*,'(A, 4(I0, " "), A)') 'sizes        :: [ ', sizes(:), ']'
        write(*,'(A, 4(I0, " "), A)') 'tensor shape :: [ ', my_shape(:), ']'
        stop 1
      end if
    end if

    ! Have the data_out array point to the Tensor data
    cptr = torch_to_blob_c(tensor%p, c_dtype)
    call c_f_pointer(cptr, data_out, my_shape)

  end subroutine torch_tensor_to_array_real32_4d