JuliaArrays / JuliaArrays/ArrayViews.jl
UnsafeStridedView Type Problem
Nobody has claimed this yet.
- Dominant language
- Julia
- Stars
- 20
- Forks
- 14
- PR merge metrics
- No merged PRs in 30d
Description
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
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the minimal example with ArrayViews 0.6.2 and inspect runtest with @code_warntype, comparing it with runtest2. Trace the unsafe_view call and the reported Tuple{Vararg{Int64}} type, then verify that the cause of the allocation and slowdown is identified and addressed by rerunning the timing comparison.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- julia
- Domain
- performance
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100