Skip to content

Commit e3324d2

Browse files
committed
Updated precompile and minor progress.
1 parent 4510812 commit e3324d2

File tree

2 files changed

+48
-31
lines changed

2 files changed

+48
-31
lines changed

src/graphs.jl

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,36 @@
33

44
isdense(::Type{<:DenseArray}) = true
55

6-
# """
7-
# ShortVector{T} simply wraps a Vector{T}, but uses a different hash function that is faster for short vectors to support using it as the keys of a Dict.
8-
# This hash function scales O(N) with length of the vectors, so it is slow for long vectors.
9-
# """
10-
# struct ShortVector{T} <: DenseVector{T}
11-
# data::Vector{T}
12-
# end
13-
# Base.@propagate_inbounds Base.getindex(x::ShortVector, I...) = x.data[I...]
14-
# Base.@propagate_inbounds Base.setindex!(x::ShortVector, v, I...) = x.data[I...] = v
15-
# @inbounds Base.length(x::ShortVector) = length(x.data)
16-
# @inbounds Base.size(x::ShortVector) = size(x.data)
17-
# @inbounds Base.strides(x::ShortVector) = strides(x.data)
18-
# @inbounds Base.push!(x::ShortVector, v) = push!(x.data, v)
19-
# @inbounds Base.append!(x::ShortVector, v) = append!(x.data, v)
20-
# function Base.hash(x::ShortVector, h::UInt)
21-
# @inbounds for n ∈ eachindex(x)
22-
# h = hash(x[n], h)
23-
# end
24-
# h
25-
# end
26-
27-
6+
"""
7+
ShortVector{T} simply wraps a Vector{T}, but uses a different hash function that is faster for short vectors to support using it as the keys of a Dict.
8+
This hash function scales O(N) with length of the vectors, so it is slow for long vectors.
9+
"""
10+
struct ShortVector{T} <: DenseVector{T}
11+
data::Vector{T}
12+
end
13+
Base.@propagate_inbounds Base.getindex(x::ShortVector, I...) = x.data[I...]
14+
Base.@propagate_inbounds Base.setindex!(x::ShortVector, v, I...) = x.data[I...] = v
15+
ShortVector{T}(::UndefInitializer, N::Integer) where {T} = ShortVector{T}(Vector{T}(undef, N))
16+
@inbounds Base.length(x::ShortVector) = length(x.data)
17+
@inbounds Base.size(x::ShortVector) = size(x.data)
18+
@inbounds Base.strides(x::ShortVector) = strides(x.data)
19+
@inbounds Base.push!(x::ShortVector, v) = push!(x.data, v)
20+
@inbounds Base.append!(x::ShortVector, v) = append!(x.data, v)
21+
function Base.hash(x::ShortVector, h::UInt)
22+
@inbounds for n eachindex(x)
23+
h = hash(x[n], h)
24+
end
25+
h
26+
end
27+
function Base.isequal(a::ShortVector{T}, b::ShortVector{T}) where {T}
28+
length(a) == length(b) || return false
29+
@inbounds for i 1:length(a)
30+
a[i] === b[i] || return false
31+
end
32+
true
33+
end
34+
Base.convert(::Type{Vector}, sv::ShortVector) = sv.data
35+
Base.convert(::Type{Vector{T}}, sv::ShortVector{T}) where {T} = sv.data
2836

2937
@enum OperationType begin
3038
memload
@@ -183,22 +191,19 @@ struct LoopSet
183191
loops::Dict{Symbol,Loop} # sym === loops[sym].itersymbol
184192
opdict::Dict{Symbol,Operation}
185193
operations::Vector{Operation} # Split them to make it easier to iterate over just a subset
186-
# computeops::Vector{Operation}
187-
# storeops::Vector{Operation}
188-
outer_reductions::Set{UInt} # IDs of reduction operations that need to be reduced at end.
194+
outer_reductions::Vector{UInt} # IDs of reduction operations that need to be reduced at end.
189195
loop_order::LoopOrder
196+
stridesets::Dict{ShortVector{Symbol},ShortVector{Symbol}}
190197
preamble::Expr # TODO: add preamble to lowering
191198
end
192199
function LoopSet()
193200
LoopSet(
194201
Dict{Symbol,Loop}(),
195202
Dict{Symbol,Operation}(),
196203
Operation[],
197-
# Operation[],
198-
# Operation[],
199-
# Set{UInt}(),
200-
Set{UInt}(),
204+
UInt[], #Set{UInt}(),
201205
LoopOrder(),
206+
Dict{ShortVector{Symbol},ShortVector{Symbol}},
202207
Expr(:block,)
203208
)
204209
end
@@ -271,8 +276,17 @@ function add_loop!(ls::LoopSet, looprange::Expr)
271276
end
272277
function add_load!(ls::LoopSet, indexed::Symbol, indices::AbstractVector)
273278
Ninds = length(indices)
274-
275-
279+
inds = ShortVector{Symbol}(indices)
280+
nsets = length(ls.stridesets)
281+
get!(ls.stridesets, inds) do
282+
strides = ShortVector{Symbol}(undef, Ninds - 1)
283+
@inbounds for i 2:Ninds
284+
sᵢ = Symbol(:stride_, nsets, :_, inds[i])
285+
strides[i-1] = sᵢ
286+
push!(ls.preamble, Expr(:(=), sᵢ, Expr(:call, :stride, indexed, i)))
287+
end
288+
strides
289+
end
276290

277291
end
278292
function add_load_getindex!(ls::LoopSet, ex::Expr)

src/precompile.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ function _precompile_()
1111
precompile(Tuple{typeof(LoopVectorization.add_masks),Expr,Symbol,Dict{Tuple{Symbol,Symbol},Symbol},Module})
1212
precompile(Tuple{typeof(LoopVectorization.add_masks),Expr,Symbol,Dict{Tuple{Symbol,Symbol},Symbol},Symbol})
1313
precompile(Tuple{typeof(LoopVectorization.vectorize_assign_linear_index),Symbol,Expr,Symbol,Dict{Symbol,Symbol},Symbol,Symbol,Module})
14+
precompile(Tuple{typeof(LoopVectorization.vectorize_body),Int64,Int64,Type{NTuple{8,VecElement{Float64}}},Int64,Symbol,Array{Any,1},Dict{Symbol,Tuple{Symbol,Symbol}},Bool,Int64,Int64,Module})
15+
precompile(Tuple{typeof(LoopVectorization.vectorize_body),Int64,Int64,Type{NTuple{8,VecElement{Float64}}},Int64,Symbol,Array{Any,1},Dict{Symbol,Tuple{Symbol,Symbol}},Bool,Int64,Int64,Symbol})
1416
precompile(Tuple{typeof(LoopVectorization.vectorize_body),Int64,Type{Float64},Int64,Symbol,Array{Any,1},Dict{Symbol,Tuple{Symbol,Symbol}},Any,Bool,Module})
1517
precompile(Tuple{typeof(LoopVectorization.vectorize_body),Int64,Type{Float64},Int64,Symbol,Array{Any,1},Dict{Symbol,Tuple{Symbol,Symbol}},Any,Bool})
18+
precompile(Tuple{typeof(LoopVectorization.vectorize_body),Symbol,Symbol,Type{NTuple{8,VecElement{Float64}}},Int64,Symbol,Array{Any,1},Dict{Symbol,Tuple{Symbol,Symbol}},Bool,Int64,Int64,Symbol})
1619
precompile(Tuple{typeof(LoopVectorization.vectorize_body),Symbol,Type{Float64},Int64,Symbol,Array{Any,1},Dict{Symbol,Tuple{Symbol,Symbol}},Any,Bool})
1720
precompile(Tuple{typeof(LoopVectorization.vectorize_linear_index!),Expr,Dict{Expr,Symbol},Dict{Symbol,Symbol},Symbol,Expr,Symbol,Symbol,Symbol,Type})
1821
precompile(Tuple{typeof(LoopVectorization.vectorize_linear_index!),Expr,Dict{Expr,Symbol},Dict{Symbol,Symbol},Symbol,Symbol,Symbol,Symbol,Module,Type})

0 commit comments

Comments
 (0)