Skip to content

Commit

Permalink
remove multithreaded gather (#64)
Browse files Browse the repository at this point in the history
* copy! -> copyto!

* remove threads

* remove multithreaded gather

* Update Project.toml

* Update SolverCPU.jl

* Update AbstractFixedEffectSolver.jl
  • Loading branch information
matthieugomez authored Mar 8, 2024
1 parent 99b4f8e commit 474dea9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "FixedEffects"
uuid = "c8885935-8500-56a7-9867-7708b20db0eb"
version = "2.3.0"
version = "2.3.1"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
7 changes: 4 additions & 3 deletions src/AbstractFixedEffectSolver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ p2 = repeat(1:5, outer = 2)
solve_residuals!(rand(10), [FixedEffect(p1), FixedEffect(p2)])
```
"""
function solve_residuals!(y::Union{AbstractVector{<: Number}, AbstractMatrix{<: Number}}, fes::AbstractVector{<: FixedEffect}, w::AbstractWeights = uweights(eltype(y), size(y, 1));
function solve_residuals!(y::Union{AbstractVector{<: Real}, AbstractMatrix{<: Real}}, fes::AbstractVector{<: FixedEffect}, w::AbstractWeights = uweights(eltype(y), size(y, 1));
method::Symbol = :cpu, double_precision::Bool = method == :cpu,
tol::Real = double_precision ? 1e-8 : 1e-6, maxiter::Integer = 10000,
nthreads = method == :cpu ? Threads.nthreads() : 256)
Expand All @@ -47,7 +47,7 @@ end



function solve_residuals!(r::AbstractVector, feM::AbstractFixedEffectSolver{T}; tol::Real = sqrt(eps(T)), maxiter::Integer = 100_000) where {T}
function solve_residuals!(r::AbstractVector{<:Real}, feM::AbstractFixedEffectSolver{T}; tol::Real = sqrt(eps(T)), maxiter::Integer = 100_000) where {T}
# One cannot copy view of Vector (r) on GPU, so first collect the vector
if works_with_view(feM)
copyto!(feM.r, r)
Expand All @@ -71,7 +71,7 @@ function solve_residuals!(r::AbstractVector, feM::AbstractFixedEffectSolver{T};
feM.r ./= sqrt.(feM.weights)
end
if works_with_view(feM)
copy!(r, feM.r)
copyto!(r, feM.r)
else
copyto!(feM.tmp, feM.r)
copyto!(r, feM.tmp)
Expand Down Expand Up @@ -104,6 +104,7 @@ function solve_residuals!(xs::AbstractVector{<: AbstractVector}, feM::AbstractFi
return xs, iterations, convergeds
end

# to depreciate
function solve_residuals!(X::AbstractMatrix, feM::AbstractFixedEffectSolver; kwargs...)
xs, iterations, convergeds = solve_residuals!(eachcol(X), feM; kwargs...)
return X, iterations, convergeds
Expand Down
58 changes: 28 additions & 30 deletions src/SolverCPU.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,37 @@ mutable struct FixedEffectLinearMapCPU{T} <: AbstractFixedEffectLinearMap{T}
fes::Vector{<:FixedEffect}
scales::Vector{<:AbstractVector}
caches::Vector{<:AbstractVector}
tmp::Vector{Union{Nothing, <:AbstractVector}}
nthreads::Int
end

function FixedEffectLinearMapCPU{T}(fes::Vector{<:FixedEffect}, ::Type{Val{:cpu}}, nthreads) where {T}
scales = [zeros(T, fe.n) for fe in fes]
caches = [zeros(T, length(fes[1].interaction)) for fe in fes]
fecoefs = [[zeros(T, fe.n) for _ in 1:nthreads] for fe in fes]
return FixedEffectLinearMapCPU{T}(fes, scales, caches, fecoefs, nthreads)
return FixedEffectLinearMapCPU{T}(fes, scales, caches, nthreads)
end

function LinearAlgebra.mul!(fecoefs::FixedEffectCoefficients,
Cfem::Adjoint{T, FixedEffectLinearMapCPU{T}},
y::AbstractVector, α::Number, β::Number) where {T}
fem = adjoint(Cfem)
rmul!(fecoefs, β)
for (fecoef, fe, cache, tmp) in zip(fecoefs.x, fem.fes, fem.caches, fem.tmp)
gather!(fecoef, fe.refs, α, y, cache, tmp, fem.nthreads)
for (fecoef, fe, cache) in zip(fecoefs.x, fem.fes, fem.caches)
gather!(fecoef, fe.refs, α, y, cache, fem.nthreads)
end
return fecoefs
end

# multithreaded gather seemds to be slower
function gather!(fecoef::AbstractVector, refs::AbstractVector, α::Number,
y::AbstractVector, cache::AbstractVector, tmp::AbstractVector, nthreads::Integer)
n_each = div(length(y), nthreads)
Threads.@threads for t in 1:nthreads
fill!(tmp[t], 0.0)
gather!(tmp[t], refs, α, y, cache, ((t - 1) * n_each + 1):(t * n_each))
end
for x in tmp
fecoef .+= x
end
gather!(fecoef, refs, α, y, cache, (nthreads * n_each + 1):length(y))
end

function gather!(fecoef::AbstractVector, refs::AbstractVector, α::Number,
y::AbstractVector, cache::AbstractVector, irange::AbstractRange)
@inbounds @simd for i in irange
fecoef[refs[i]] += α * y[i] * cache[i]
y::AbstractVector, cache::AbstractVector, nthreads::Integer)
if α == 1
@fastmath @inbounds @simd for i in eachindex(y)
fecoef[refs[i]] += y[i] * cache[i]
end
else
@fastmath @inbounds @simd for i in eachindex(y)
fecoef[refs[i]] += α * y[i] * cache[i]
end
end
end

Expand All @@ -61,11 +53,21 @@ end

function scatter!(y::AbstractVector, α::Number, fecoef::AbstractVector,
refs::AbstractVector, cache::AbstractVector, irange::AbstractRange)
@inbounds @simd for i in irange
y[i] += α * fecoef[refs[i]] * cache[i]
# α is actually only 1 or -1 so do special path for them
if α == 1
@fastmath @inbounds @simd for i in irange
y[i] += fecoef[refs[i]] * cache[i]
end
elseif α == -1
@fastmath @inbounds @simd for i in irange
y[i] -= fecoef[refs[i]] * cache[i]
end
else
@fastmath @inbounds @simd for i in irange
y[i] += α * fecoef[refs[i]] * cache[i]
end
end
end

##############################################################################
##
## Implement AbstractFixedEffectSolver interface
Expand Down Expand Up @@ -109,7 +111,7 @@ end

function scale!(scale::AbstractVector, refs::AbstractVector, interaction::AbstractVector, weights::AbstractVector)
fill!(scale, 0)
@inbounds @simd for i in eachindex(refs)
@fastmath @inbounds @simd for i in eachindex(refs)
scale[refs[i]] += abs2(interaction[i]) * weights[i]
end
# Case of interaction variatble equal to zero in the category (issue #97)
Expand All @@ -119,11 +121,7 @@ function scale!(scale::AbstractVector, refs::AbstractVector, interaction::Abstra
end

function cache!(cache::AbstractVector, refs::AbstractVector, interaction::AbstractVector, weights::AbstractVector, scale::AbstractVector)
@inbounds @simd for i in eachindex(cache)
@fastmath @inbounds @simd for i in eachindex(cache)
cache[i] = interaction[i] * sqrt(weights[i]) * scale[refs[i]]
end
end




2 comments on commit 474dea9

@matthieugomez
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/102605

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v2.3.1 -m "<description of version>" 474dea98542b2c5e540b0f3651a2747024ba7c4c
git push origin v2.3.1

Please sign in to comment.