Skip to content

Commit

Permalink
Update FixedEffectSolverCPU.jl (#53)
Browse files Browse the repository at this point in the history
* Update FixedEffectSolverCPU.jl

Correct GLFixedEffectModels

* Update Project.toml

* Update FixedEffectSolverCPU.jl

* Update solve.jl

* Update FixedEffectSolverGPU.jl

* Update Project.toml
  • Loading branch information
matthieugomez authored Oct 3, 2021
1 parent 7b390d5 commit 6a5874c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 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.0.8"
version = "2.1.0"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
16 changes: 8 additions & 8 deletions src/FixedEffectSolvers/FixedEffectSolverCPU.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ end


function scale!(scale::AbstractVector, refs::AbstractVector, interaction::AbstractVector, weights::AbstractVector)
fill!(scale, 0)
@inbounds @simd for i in eachindex(refs)
scale[refs[i]] += abs2(interaction[i]) * weights[i]
end
Expand All @@ -153,14 +154,13 @@ function solve_residuals!(r::AbstractVector, feM::FixedEffectSolverCPU{T}; tol::
feM.r .*= sqrt.(feM.weights)
end
copyto!(feM.b, feM.r)
if length(feM.x.x) == 1
mul!(feM.x, feM.m', feM.b, 1, 0)
iter, converged = 1, true
else
mul!(feM.x, feM.m', feM.b, 1, 0)
x, ch = lsmr!(feM.x, feM.m, feM.b, feM.v, feM.h, feM.hbar; atol = tol, btol = tol, maxiter = maxiter)
iter, converged = ch.mvps + 1, ch.isconverged
end
mul!(feM.x, feM.m', feM.b, 1, 0)
if length(feM.x.x) > 1
x, ch = lsmr!(feM.x, feM.m, feM.b, feM.v, feM.h, feM.hbar; atol = tol, btol = tol, maxiter = maxiter)
iter, converged = ch.mvps + 1, ch.isconverged
else
iter, converged = 1, true
end
mul!(feM.r, feM.m, feM.x, -1, 1)
if !(feM.weights isa UnitWeights)
feM.r ./= sqrt.(feM.weights)
Expand Down
1 change: 1 addition & 0 deletions src/FixedEffectSolvers/FixedEffectSolverGPU.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ end

function scale!(scale::CuVector, refs::CuVector, interaction::CuVector, weights::CuVector, nthreads::Integer)
nblocks = cld(length(refs), nthreads)
fill!(scale, 0)
@cuda threads=nthreads blocks=nblocks scale_kernel!(scale, refs, interaction, weights)
@cuda threads=nthreads blocks=nblocks inv_kernel!(scale)
end
Expand Down
21 changes: 12 additions & 9 deletions test/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ r_ols = [-0.2015993617092453, 0.2015993617092464, -0.2015993617092463, 0.2015
(c, iter, conv) = solve_residuals!([x x], fes)


# test update_weights
weights = ones(10)
feM = FixedEffects.AbstractFixedEffectSolver{Float64}(fes, Weights(weights), Val{:cpu})
weights = Weights([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
FixedEffects.update_weights!(feM, weights)
solve_residuals!(deepcopy(x), feM)[1] solve_residuals!(deepcopy(x), fes, weights)[1]



method_s = [:cpu]
if FixedEffects.has_CUDA()
push!(method_s, :gpu)
Expand All @@ -45,3 +36,15 @@ end
fe = FixedEffect([1, 2])
@test_throws "FixedEffects must have the same length as y"= solve_residuals!(ones(100), [fe])


# test update_weights
weights = ones(10)
fes = [FixedEffect(p1)]
feM = FixedEffects.AbstractFixedEffectSolver{Float64}(fes, Weights(weights), Val{:cpu})
weights = Weights([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
FixedEffects.update_weights!(feM, weights)
solve_residuals!(deepcopy(x), feM)[1] solve_residuals!(deepcopy(x), fes, weights)[1]

weights = Weights(reverse([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
FixedEffects.update_weights!(feM, weights)
solve_residuals!(deepcopy(x), feM)[1] solve_residuals!(deepcopy(x), fes, weights)[1]

2 comments on commit 6a5874c

@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/45979

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.1.0 -m "<description of version>" 6a5874c8cccb4c766977c33492ff9f271b97cef6
git push origin v2.1.0

Please sign in to comment.