Skip to content

Commit

Permalink
Merge pull request #38 from JuliaStats/sjk/faster
Browse files Browse the repository at this point in the history
Rewrite colwise_dot! and colwise_sumsq! to use explict loops
  • Loading branch information
simonster committed Feb 10, 2016
2 parents 8e61686 + cc976e3 commit de168b9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
1 change: 0 additions & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
julia 0.3
ArrayViews 0.4.8-
Compat 0.7.7
1 change: 0 additions & 1 deletion src/PDMats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ VERSION >= v"0.4.0-dev+6521" && __precompile__(true)

module PDMats

using ArrayViews
using Compat

import Base: +, *, \, /, ==
Expand Down
20 changes: 14 additions & 6 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,28 @@ function wsumsq{T<:AbstractFloat}(w::AbstractVector{T}, a::AbstractVector{T})
return s
end

function colwise_dot!{T<:AbstractFloat}(r::AbstractArray{T}, a::DenseMatrix{T}, b::DenseMatrix{T})
function colwise_dot!{T<:AbstractFloat}(r::AbstractArray{T}, a::AbstractMatrix{T}, b::AbstractMatrix{T})
n = length(r)
@check_argdims n == size(a, 2) == size(b, 2) && size(a, 1) == size(b, 1)
for i = 1:n
r[i] = dot(view(a,:,i), view(b,:,i))
for j = 1:n
v = zero(T)
@simd for i = 1:size(a, 1)
@inbounds v += a[i, j]*b[i, j]
end
r[j] = v
end
return r
end

function colwise_sumsq!{T<:AbstractFloat}(r::AbstractArray{T}, a::DenseMatrix{T}, c::T)
function colwise_sumsq!{T<:AbstractFloat}(r::AbstractArray{T}, a::AbstractMatrix{T}, c::T)
n = length(r)
@check_argdims n == size(a, 2)
for i = 1:n
r[i] = sumabs2(view(a,:,i)) * c
for j = 1:n
v = zero(T)
@simd for i = 1:size(a, 1)
@inbounds v += abs2(a[i, j])
end
r[j] = v*c
end
return r
end

0 comments on commit de168b9

Please sign in to comment.