Skip to content

Commit 4700bbf

Browse files
author
Andy Ferris
committed
Use Size in eig, chol, inv...
1 parent ecdb46e commit 4700bbf

File tree

5 files changed

+175
-142
lines changed

5 files changed

+175
-142
lines changed

perf/benchmark3.jl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ using BenchmarkTools
33

44
import BenchmarkTools: prettytime, prettymemory
55

6+
@noinline plus(a,b) = a+b
7+
@noinline plus!(c,a,b) = broadcast!(+, c, a, b)
8+
9+
@noinline mul(a,b) = a*b
10+
@noinline mul!(c,a,b) = A_mul_B!(c, a, b)
11+
12+
613
for T [Int64, Float64]
714
for N [1,2,4,8,16,32,64,128,256]
815
println("=====================================================================")
@@ -16,15 +23,15 @@ for T ∈ [Int64, Float64]
1623
maxnamelength = maximum(namelengths)
1724

1825
for v instances
19-
result = mean(@benchmark +($(copy(v)), $(copy(v))))
26+
result = mean(@benchmark plus($(copy(v)), $(copy(v))))
2027
padding = maxnamelength - length(string(typeof(v).name.name))
21-
println(typeof(v).name.name, ":", " " ^ padding, " v3 = v1 + v2 takes ", prettytime(time(result)), ", ", prettymemory(memory(result)), "(GC ", prettytime(gctime(result)) , ")")
28+
println(typeof(v).name.name, ":", " " ^ padding, " v3 = v1 + v2 takes ", prettytime(time(result)), ", ", prettymemory(memory(result)), " (GC ", prettytime(gctime(result)) , ")")
2229
end
2330

2431
println()
2532

2633
for v mutables
27-
result = mean(@benchmark broadcast!(+, $(copy(v)), $(copy(v)), $(copy(v))))
34+
result = mean(@benchmark plus!($(copy(v)), $(copy(v)), $(copy(v))))
2835
padding = maxnamelength - length(string(typeof(v).name.name))
2936
println(typeof(v).name.name, ":", " " ^ padding, " v3 .= +.(v1, v2) takes ", prettytime(time(result)), ", ", prettymemory(memory(result)), " (GC ", prettytime(gctime(result)) , ")")
3037
end
@@ -45,15 +52,15 @@ for T ∈ [Int64, Float64]
4552
maxnamelength = maximum(namelengths)
4653

4754
for m instances
48-
result = mean(@benchmark *($(copy(m)), $(copy(m))))
55+
result = mean(@benchmark mul($(copy(m)), $(copy(m))))
4956
padding = maxnamelength - length(string(typeof(m).name.name))
5057
println(typeof(m).name.name, ":", " " ^ padding, " m3 = m1 * m2 takes ", prettytime(time(result)), ", ", prettymemory(memory(result)), " (GC ", prettytime(gctime(result)) , ")")
5158
end
5259

5360
println()
5461

5562
for m mutables
56-
result = mean(@benchmark A_mul_B!($(copy(m)), $(copy(m)), $(copy(m))))
63+
result = mean(@benchmark mul!($(copy(m)), $(copy(m)), $(copy(m))))
5764
padding = maxnamelength - length(string(typeof(m).name.name))
5865
println(typeof(m).name.name, ":", " " ^ padding, " A_mul_B!(m3, m1, m2) takes ", prettytime(time(result)), ", ", prettymemory(memory(result)), " (GC ", prettytime(gctime(result)) , ")")
5966
end

src/SizedArray.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ end
5151
# Overide some problematic default behaviour
5252
@inline convert{SA<:SizedArray}(::Type{SA}, sa::SizedArray) = SA(sa.data)
5353

54+
# Back to Array (unfortunately need both convert and construct to overide other methods)
55+
@inline (::Type{Array})(sa::SizedArray) = sa.data
56+
@inline (::Type{Array{T}}){T,S}(sa::SizedArray{S,T}) = sa.data
57+
@inline (::Type{Array{T,N}}){T,S,N}(sa::SizedArray{S,T,N}) = sa.data
58+
59+
@inline convert(::Type{Array}, sa::SizedArray) = sa.data
60+
@inline convert{T,S}(::Type{Array{T}}, sa::SizedArray{S,T}) = sa.data
61+
@inline convert{T,S,N}(::Type{Array{T,N}}, sa::SizedArray{S,T,N}) = sa.data
62+
5463
@pure _ndims{N}(::NTuple{N,Int}) = N
5564

5665
@pure size{S}(::Type{SizedArray{S}}) = S
@@ -65,11 +74,15 @@ typealias SizedVector{S,T,M} SizedArray{S,T,1,M}
6574
@pure size{S}(::Type{SizedVector{S}}) = S
6675
@inline (::Type{SizedVector{S}}){S,T,M}(a::Array{T,M}) = SizedArray{S,T,1,M}(a)
6776
@inline (::Type{SizedVector{S}}){S,T,L}(x::NTuple{L,T}) = SizedArray{S,T,1,1}(x)
77+
@inline (::Type{Vector})(sa::SizedVector) = sa.data
78+
@inline convert(::Type{Vector}, sa::SizedVector) = sa.data
6879

6980
typealias SizedMatrix{S,T,M} SizedArray{S,T,2,M}
7081
@pure size{S}(::Type{SizedMatrix{S}}) = S
7182
@inline (::Type{SizedMatrix{S}}){S,T,M}(a::Array{T,M}) = SizedArray{S,T,2,M}(a)
7283
@inline (::Type{SizedMatrix{S}}){S,T,L}(x::NTuple{L,T}) = SizedArray{S,T,2,2}(x)
84+
@inline (::Type{Matrix})(sa::SizedMatrix) = sa.data
85+
@inline convert(::Type{Matrix}, sa::SizedMatrix) = sa.data
7386

7487

7588
"""

src/cholesky.jl

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
# Generic Cholesky decomposition for fixed-size matrices, mostly unrolled
2+
@inline function Base.chol(A::StaticMatrix)
3+
ishermitian(A) || Base.LinAlg.non_hermitian_error("chol")
4+
_chol(Size(A), A)
5+
end
26

3-
# Currently all sanity checks are disabled!
4-
@generated function Base.chol(A::StaticMatrix)
5-
if size(A) === (1, 1)
6-
return :(_chol1(A))
7-
elseif size(A) === (2, 2)
8-
#ishermitian(A) || Base.LinAlg.non_hermitian_error("chol")
9-
return :(_chol2(A))
10-
elseif size(A) === (3, 3)
11-
#ishermitian(A) || Base.LinAlg.non_hermitian_error("chol")
12-
return :(_chol3(A))
13-
else
14-
return :(chol(Array(A)))
15-
end
7+
@inline function Base.chol{T<:Real, SM <: StaticMatrix}(A::Base.LinAlg.RealHermSymComplexHerm{T,SM})
8+
ishermitian(A) || Base.LinAlg.non_hermitian_error("chol")
9+
_chol(Size(A), A)
10+
end
11+
12+
@inline function Base.chol{SM<:StaticMatrix}(A::Symmetric{SM})
13+
eltype(A) <: Real && (ishermitian(A) || Base.LinAlg.non_hermitian_error("chol"))
14+
_chol(Size(A), A)
1615
end
1716

18-
@generated function _chol1(A::StaticMatrix)
17+
@generated function _chol(::Size{(1,1)}, A::StaticMatrix)
1918
@assert size(A) == (1,1)
2019
T = promote_type(typeof(sqrt(one(eltype(A)))), Float32)
2120
newtype = similar_type(A,T)
@@ -26,8 +25,7 @@ end
2625
end
2726
end
2827

29-
30-
@generated function _chol2(A::StaticMatrix)
28+
@generated function _chol(::Size{(2,2)}, A::StaticMatrix)
3129
@assert size(A) == (2,2)
3230
T = promote_type(typeof(sqrt(one(eltype(A)))), Float32)
3331
newtype = similar_type(A,T)
@@ -41,7 +39,7 @@ end
4139
end
4240
end
4341

44-
@generated function _chol3(A::StaticMatrix)
42+
@generated function _chol(::Size{(3,3)}, A::StaticMatrix)
4543
@assert size(A) == (3,3)
4644
T = promote_type(typeof(sqrt(one(eltype(A)))), Float32)
4745
newtype = similar_type(A,T)
@@ -57,3 +55,6 @@ end
5755
($newtype)((a11, $(zero(T)), $(zero(T)), a12, a22, $(zero(T)), a13, a23, a33))
5856
end
5957
end
58+
59+
# Otherwise default algorithm returning wrapped SizedArray
60+
@inline _chol(s::Size, A::StaticArray) = s(full(chol(Hermitian(Array(A)))))

0 commit comments

Comments
 (0)