Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update FixedEffectSolverCPU.jl #53

Merged
merged 6 commits into from
Oct 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]