torch_tensor_get_shape Function

public function torch_tensor_get_shape(self) result(sizes)

Uses

  • proc~~torch_tensor_get_shape~~UsesGraph proc~torch_tensor_get_shape torch_tensor%torch_tensor_get_shape iso_c_binding iso_c_binding proc~torch_tensor_get_shape->iso_c_binding

Determines the shape of a tensor.

Type Bound

torch_tensor

Arguments

Type IntentOptional Attributes Name
class(torch_tensor), intent(in) :: self

Tensor to get the shape of

Return Value integer(kind=int64), allocatable, (:)

Array holding the shape of the tensor


Source Code

  function torch_tensor_get_shape(self) result(sizes)
    use, intrinsic :: iso_c_binding, only : c_f_pointer, c_int64_t, c_ptr
    class(torch_tensor), intent(in) :: self         !! Tensor to get the shape of
    integer(kind=int64), allocatable :: sizes(:)    !! Array holding the shape of the tensor

    ! Local data
    integer(kind=int32) :: ndims(1)
    integer(kind=c_int64_t), pointer :: sizes_c_int64_ptr(:)
        !! Temporary pointer to Torch-owned memory containing c_int64
    type(c_ptr) :: cptr

    interface
      function torch_tensor_get_sizes_c(tensor_c) result(sizes_c) &
          bind(c, name = "torch_tensor_get_sizes")
        use, intrinsic :: iso_c_binding, only : c_ptr
        implicit none
        type(c_ptr), value, intent(in) :: tensor_c
        type(c_ptr) :: sizes_c
      end function torch_tensor_get_sizes_c
    end interface

    if (.not. c_associated(self%p)) then
      write(*,*) "Error :: tensor has not been constructed so its shape is unset"
      stop 1
    end if
    ndims(1) = self%rank()
    cptr = torch_tensor_get_sizes_c(self%p)
    call c_f_pointer(cptr, sizes_c_int64_ptr, ndims)

    ! Copy out of the Torch-owned memory so the result remains valid even if
    ! the tensor is subsequently deleted
    allocate(sizes(ndims(1)))
    sizes(:) = sizes_c_int64_ptr(:)
  end function torch_tensor_get_shape