JuliaArrays / ArrayViews.jl

A Julia package to explore a new system of array views
MIT License
19 stars 18 forks source link

UnsafeStridedView Type Problem #30

Open JaredCrean2 opened 9 years ago

JaredCrean2 commented 9 years ago

I switched from manually inlining code within a for loop to using ArrayViews, and I discovered that when the array used to create the view is the field of a type, then there is a significant slowdown, and @code_warntype show an abstract type. Here is a minimal example

using ArrayViews

type mytype{T}
  arr::Array{T, 4}
end

function func1(q::AbstractArray)
# do some work
  tmp = q.*q
  return nothing
end

function runtest(obj)
(tmp, tmp, nnodes, numel) = size(obj.arr)

for i=1:numel
  for j=1:nnodes
    for k=1:2
      q_vals = unsafe_view(obj.arr, k, :, j, i)
      func1(q_vals)
     end
  end
end

end

function runtest2(obj)
(tmp, tmp, nnodes, numel) = size(obj.arr)

q_vals = zeros(2)
for i=1:numel
  for j=1:nnodes
    for k=1:2
      q_vals[1] = obj.arr[k, 1, j, i]
      q_vals[2] = obj.arr[k, 2, j, i]
      func1(q_vals)
     end
  end
end

end

# run the test
big_array = rand(2,2, 3, 50000)
obj = mytype{Float64}(big_array)

runtest(obj)
@time runtest(obj)

runtest2(obj)
@time runtest2(obj)

The output is

   1.083 seconds      (9291 k allocations: 407 MB, 4.21% gc time)
   312.813 milliseconds (3000 k allocations: 110 MB, 3.02% gc time)

The concerning line from @code_warntype runtest(obj) is:

      q_vals = call(UnsafeStridedView,GenSym(13),(GlobalRef(ArrayViews,:roffset))(GenSym(13),k::Int64,GenSym(12),j::Int64,i::Int64)::Int64,##shp#1550::Tuple{Int64,Int64},ArrayViews.ContRank{0},(GlobalRef(ArrayViews,:_vstrides))(strides(GenSym(13))::Tuple{Vararg{Int64}},1,k::Int64,GenSym(12),j::Int64,i::Int64)::Tuple{Int64,Int64})::ArrayViews.UnsafeStridedView{Float64,2,0} # line 22:

Something gets turned into a ::Tuple{Vararg{Int64}}, although I can't tell exactly what (I'm new to looking at partially compiled code). Do you think this could be the cause of the performance loss?

I'm using ArrayViews version 0.6.2 and Julia Version 0.4.0-dev+5149, Commit 317a4d1* (2015-06-01 18:58 UTC).

Thanks in advance, Jared Crean

andreasnoack commented 6 years ago

Is this still a problem?

JaredCrean2 commented 6 years ago

Yes:

julia> a = rand(2, 3, 4, 5);

julia> @code_warntype aview(a, 1, :, 2, 3)
Variables:
  #self# <optimized out>
  a::Array{Float64,4}
  i1::Int64
  i2::Colon
  i3::Int64
  i4::Int64
  shp::Tuple{Int64,Vararg{Any,N} where N}

Body:
  begin 
      shp::Tuple{Int64,Vararg{Any,N} where N} = $(Expr(:invoke, MethodInstance for vshape(::Array{Float64,4}, ::Int64, ::Colon, ::Int64, ::Int64), :(ArrayViews.vshape), :(a), :(i1), :(i2), :(i3), :(i4)))
      return (ArrayViews.make_aview)(a::Array{Float64,4}, (ArrayViews.restrict_crank)(ArrayViews.ContRank{0}, shp::Tuple{Int64,Vararg{Any,N} where N})::Any, shp::Tuple{Int64,Vararg{Any,N} where N}, i1::Int64, i2::Colon, i3::Int64, i4::Int64)::Union{ArrayViews.ContiguousView{Float64,_,Array{Float64,4}} where _, ArrayViews.StridedView{Float64,1,_,Array{Float64,4}} where _}
  end::Union{ArrayViews.ContiguousView{Float64,_,Array{Float64,4}} where _, ArrayViews.StridedView{Float64,1,_,Array{Float64,4}} where _}

To fix this, we would have to special case the computation of vshape for 4D arrays (3D works fine) in more cases here.