函数向输入参数添加维度时的类型稳定性问题

问题描述 投票:3回答:1

我有一个函数返回一个与输入数组相同的元素类型的数组,但有一个额外的维度。这是一个简单的例子:

function myfun(a::Array{T,N}) where {T,N}
   b = Array{T,N+1}(size(a)...,2)
   b[:] = 42
   return b
end

在2x2阵列上调用此函数时,它返回一个2x2x2数组。

myfun(zeros(2,2))
2×2×2 Array{Float64,3}:
[:, :, 1] =
 42.0  42.0
 42.0  42.0

[:, :, 2] =
 42.0  42.0
 42.0  42.0

但是,此功能不是类型稳定的。根据@code_warntypebAny类型。

即使在b上使用了类型注释,结果在维数方面也不是类型稳定的:

function myfun(a::Array{T,N}) where {T,N}
      b = Array{T,N+1}(size(a)...,2) :: Array{T,N+1}
      b[:] = T(42)
      return b
end

@code_warntype myfun(zeros(2,2))现在返回Array{Float64,_} where _b类型。当输入参数有2个维度时,Julia是否应该无法确定维数为3?

我正在使用julia 0.6.2(在linux上)。

types julia generic-programming
1个回答
3
投票

这是因为构造函数(Array{T,N+1}(size(a)...,2))在运行时执行,你可以使用@generated functions在编译时预先计算N

julia> @generated function myfun(a::Array{T,N}) where {T,N}
           NN = N+1
           quote 
               b = Array{$T,$NN}(size(a)...,2)
               b[:] = 42
               return b
           end
       end
myfun (generic function with 1 method)

julia> @code_warntype myfun(zeros(2,2))
Variables:
  #self# <optimized out>
  a::Array{Float64,2}
  b::Array{Float64,3}

Body:
  begin  # line 2:
      # meta: location REPL[1] # line 4:
      SSAValue(2) = (Base.arraysize)(a::Array{Float64,2}, 1)::Int64
      SSAValue(1) = (Base.arraysize)(a::Array{Float64,2}, 2)::Int64
      b::Array{Float64,3} = $(Expr(:foreigncall, :(:jl_alloc_array_3d), Array{Float64,3}, svec(Any, Int64, Int64, Int64), Array{Float64,3}, 0, SSAValue(2), 0, SSAValue(1), 0, :($(QuoteNode(2))), 0)) # line 5:
      $(Expr(:invoke, MethodInstance for fill!(::Array{Float64,3}, ::Int64), :(Base.fill!), :(b), 42))
      # meta: pop location
      return b::Array{Float64,3}
  end::Array{Float64,3}

julia> myfun(zeros(2,2))
2×2×2 Array{Float64,3}:
[:, :, 1] =
 42.0  42.0
 42.0  42.0

[:, :, 2] =
 42.0  42.0
 42.0  42.0
© www.soinside.com 2019 - 2024. All rights reserved.