-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from vepiteski/type-stable-version
Type stable version, go version 0.2.5.
- Loading branch information
Showing
57 changed files
with
3,448 additions
and
1,239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,21 @@ | ||
name = "Stopping" | ||
uuid = "c4fe5a9e-e7fb-5c3d-89d5-7f405ab2214f" | ||
authors = ["Jean-Pierre Dussault <Jean-Pierre.Dussault@USherbrooke.CA>","Tangi Migot <tangi.migot@gmail.com>","Sam Goyette <samuel.goyette@usherbrooke.ca>"] | ||
version = "0.2.4" | ||
version = "0.2.5" | ||
|
||
|
||
[deps] | ||
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" | ||
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | ||
LinearOperators = "5c8ed15e-5a4c-59e4-a42b-c7e8811fb125" | ||
NLPModels = "a4795742-8479-5a88-8948-cc11e1c8c1a6" | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" | ||
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | ||
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" | ||
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" | ||
LinearOperators = "5c8ed15e-5a4c-59e4-a42b-c7e8811fb125" | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
|
||
[compat] | ||
#NLPModels = "0.10.0, 1" | ||
julia = "^1.0.0" | ||
|
||
[extras] | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" | ||
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | ||
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
paper/LinearAlgebra-test/cheap_stop_random_coordinate_descent_method.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using LinearAlgebra, Krylov, LinearOperators, SparseArrays, Stopping | ||
|
||
#Krylov @kdot | ||
macro kdot(n, x, y) | ||
return esc(:(Krylov.krylov_dot($n, $x, 1, $y, 1))) | ||
end | ||
|
||
using SolverTools, Logging | ||
|
||
""" | ||
Randomized coordinate descent | ||
Sect. 3.7 in Gower, R. M., & Richtárik, P. (2015). | ||
Randomized iterative methods for linear systems. | ||
SIAM Journal on Matrix Analysis and Applications, 36(4), 1660-1690. | ||
Using Stopping | ||
""" | ||
function StopRandomizedCD2(A :: AbstractMatrix, | ||
b :: AbstractVector{T}; | ||
is_zero_start :: Bool = true, | ||
x0 :: AbstractVector{T} = zeros(T,size(A,2)), | ||
atol :: AbstractFloat = 1e-7, | ||
rtol :: AbstractFloat = 1e-15, | ||
max_iter :: Int = size(A,2)^2, | ||
verbose :: Int = 100, | ||
kwargs...) where T <: AbstractFloat | ||
|
||
stp = LAStopping(LinearSystem(A,b), | ||
GenericState(x0, similar(b)), | ||
max_cntrs = Stopping._init_max_counters_linear_operators(), | ||
atol = atol, rtol = rtol, max_iter = max_iter, | ||
tol_check = (atol, rtol, opt0)->(atol + rtol * opt0), | ||
retol = false, | ||
optimality_check = (pb, state) -> state.res, | ||
kwargs...) | ||
|
||
return StopRandomizedCD2(stp, is_zero_start = is_zero_start, verbose = verbose, kwargs...) | ||
end | ||
|
||
function StopRandomizedCD2(stp :: AbstractStopping; | ||
is_zero_start :: Bool = true, | ||
verbose :: Int = 100, | ||
kwargs...) | ||
state = stp.current_state | ||
|
||
A,b = stp.pb.A, stp.pb.b | ||
m,n = size(A) | ||
x = state.x | ||
T = eltype(x) | ||
|
||
state.res = is_zero_start ? b : b - A*x | ||
#res = state.res | ||
|
||
OK = start!(stp, no_start_opt_check = true) | ||
stp.meta.optimality0 = norm(b) | ||
|
||
#@info log_header([:iter, :nrm, :time], [Int, T, T], | ||
# hdr_override=Dict(:nrm=>"||Ax-b||")) | ||
#@info log_row(Any[0, res[1], state.current_time]) | ||
|
||
while !OK | ||
|
||
#rand a number between 1 and n | ||
#224.662 ns (4 allocations: 79 bytes) - independent of the n | ||
i = mod(stp.meta.nb_of_stop,n)+1#Int(floor(rand() * n) + 1) | ||
Ai = A[:,i] | ||
|
||
#ei = zeros(n); ei[i] = 1.0 #unit vector in R^n | ||
#xk = Ai == 0 ? x0 : x0 - dot(Ai,res)/norm(Ai,2)^2 * ei | ||
Aires = @kdot(m, Ai, state.res) | ||
nAi = @kdot(m, Ai, Ai) | ||
state.x[i] -= Aires/nAi | ||
|
||
#state.res = b - A*state.x #TO IMPROVE!! | ||
state.res += Ai * Aires/nAi | ||
|
||
OK = cheap_stop!(stp) #make a copy of res in current_score? | ||
|
||
if mod(stp.meta.nb_of_stop, verbose) == 0 #print every 20 iterations | ||
#@info log_row(Any[stp.meta.nb_of_stop, res[1], state.current_time]) | ||
end | ||
|
||
end | ||
|
||
return stp | ||
end |
46 changes: 46 additions & 0 deletions
46
paper/LinearAlgebra-test/compare-krylov-stopping-fake-linesearch.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using Krylov, Stopping | ||
|
||
include("random_coordinate_descent_method.jl") | ||
include("stop_random_coordinate_descent_method.jl") | ||
include("stop_random_coordinate_descent_method_LS.jl") | ||
|
||
n=5000; | ||
A=rand(n,n) | ||
sol=rand(n) | ||
b=A*sol | ||
|
||
#@time stp = StopRandomizedCD(A,b, max_iter = 1000) | ||
|
||
x0 = zeros(size(A,2)) | ||
pb = LinearSystem(A,b) | ||
s0 = GenericState(x0, similar(b)) | ||
mcnt = Stopping._init_max_counters_linear_operators() | ||
# | ||
@time meta = StoppingMeta(max_cntrs = mcnt, | ||
atol = 1e-7, rtol = 1e-15, max_iter = 99, | ||
retol = false, | ||
tol_check = (atol, rtol, opt0)->(atol + rtol * opt0), | ||
optimality_check = (pb, state) -> state.res) | ||
s0 = GenericState(x0, similar(b)) | ||
@time stp = LAStopping(pb, meta, cheap_stop_remote_control(), s0) | ||
@time StopRandomizedCD(stp) | ||
|
||
@time x, OK = RandomizedCD(A,b, max_iter = 100) | ||
|
||
############################################################################### | ||
# | ||
# We compare the stopping_randomized_CD with the same version with an | ||
# artificial substopping created and called at each iteration. | ||
# It appears that the expansive part is to create the SubStopping, and in | ||
# particular create the StoppingMeta. | ||
# | ||
@time meta = StoppingMeta(max_cntrs = mcnt, | ||
atol = 1e-7, rtol = 1e-15, max_iter = 99, | ||
retol = false, | ||
tol_check = (atol, rtol, opt0)->(atol + rtol * opt0), | ||
optimality_check = (pb, state) -> state.res) | ||
s0 = GenericState(x0, similar(b)) | ||
@time stp2 = LAStopping(pb, meta, cheap_stop_remote_control(), s0) | ||
@time StopRandomizedCD_LS(stp2) | ||
|
||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
using Krylov, Stopping | ||
|
||
#https://github.com/JuliaSmoothOptimizers/Krylov.jl | ||
|
||
include("random_coordinate_descent_method.jl") | ||
include("stop_random_coordinate_descent_method.jl") | ||
include("cheap_stop_random_coordinate_descent_method.jl") | ||
include("instate_coordinate_descent_method.jl") | ||
|
||
#using ProfileView, | ||
using BenchmarkTools | ||
|
||
n=5000; | ||
A=rand(n,n) | ||
sol=rand(n) | ||
b=A*sol | ||
|
||
x0 = zeros(size(A,2)) | ||
pb = LinearSystem(A,b) | ||
s0 = GenericState(x0, similar(b)) | ||
mcnt = Stopping._init_max_counters_linear_operators() | ||
|
||
############################################################################### | ||
# | ||
# The original algorithm | ||
# | ||
############################################################################### | ||
@time x, OK, k = RandomizedCD(A, b, max_iter = 100) | ||
|
||
############################################################################### | ||
# | ||
# Stopping with memory optimization using the State and with cheap_stop! | ||
# | ||
############################################################################### | ||
@time meta3 = StoppingMeta(max_cntrs = mcnt, | ||
atol = 1e-7, rtol = 1e-15, max_iter = 100, | ||
retol = false, | ||
tol_check = (atol, rtol, opt0) -> (atol + rtol * opt0), | ||
optimality_check = (pb, state) -> state.res) | ||
s3 = GenericState(zeros(size(A,2)), similar(b)) | ||
@time stp3 = LAStopping(pb, meta3, s3) | ||
@time StopRandomizedCD2(stp3) | ||
|
||
############################################################################### | ||
# | ||
# Stopping version of the algorithm | ||
# | ||
############################################################################### | ||
@time meta2 = StoppingMeta(max_cntrs = mcnt, | ||
atol = 1e-7, rtol = 1e-15, max_iter = 100, | ||
retol = false, | ||
tol_check = (atol, rtol, opt0)->(atol + rtol * opt0), | ||
optimality_check = (pb, state) -> state.res) | ||
s2 = GenericState(zeros(size(A,2)), similar(b)) | ||
@time stp2 = LAStopping(pb, meta2, s2) | ||
@time StopRandomizedCD(stp2) | ||
|
||
|
||
############################################################################### | ||
# | ||
# Stopping version of the algorithm with cheap remote control | ||
# | ||
############################################################################### | ||
@time meta1 = StoppingMeta(max_cntrs = mcnt, | ||
atol = 1e-7, rtol = 1e-15, max_iter = 100, | ||
retol = false, | ||
tol_check = (atol, rtol, opt0)->(atol + rtol * opt0), | ||
optimality_check = (pb, state) -> state.res) | ||
s1 = GenericState(zeros(size(A,2)), similar(b)) | ||
@time stp1 = LAStopping(pb, meta1, cheap_stop_remote_control(), s1) | ||
@time StopRandomizedCD(stp1) | ||
|
||
############################################################################### | ||
# | ||
# Stopping version of the algorithm with cheap remote control | ||
# | ||
############################################################################### | ||
@time meta = StoppingMeta(max_cntrs = mcnt, | ||
atol = 1e-7, rtol = 1e-15, max_iter = 100, | ||
retol = false, | ||
tol_check = (atol, rtol, opt0)->(atol + rtol * opt0), | ||
optimality_check = (pb, state) -> state.res) | ||
s0 = GenericState(zeros(size(A,2)), similar(b)) | ||
@time stp = LAStopping(pb, meta, StopRemoteControl(), s0) | ||
@time StopRandomizedCD(stp) | ||
|
||
############################################################################### | ||
# | ||
# Stopping with memory optimization using the State and with cheap_stop! | ||
# uses the @ macro instate. | ||
# | ||
#DOESN'T WORK ?? | ||
# | ||
############################################################################### | ||
#= | ||
@time meta4 = StoppingMeta(max_cntrs = mcnt, | ||
atol = 1e-7, rtol = 1e-15, max_iter = 100, | ||
retol = false, | ||
tol_check = (atol, rtol, opt0)->(atol + rtol * opt0), | ||
optimality_check = (pb, state) -> state.res) | ||
s4 = GenericState(zeros(size(A,2)), similar(b)) | ||
@time stp4 = LAStopping(pb, meta4, s4) | ||
@time stp4 = StopRandomizedCD3(stp4) | ||
=# | ||
|
||
Lnrm = [norm(stp.current_state.current_score), | ||
norm(stp1.current_state.current_score), | ||
norm(stp2.current_state.current_score), | ||
norm(stp3.current_state.current_score), #norm(stp4.current_state.current_score), | ||
norm(b-A*x) | ||
] | ||
|
||
using Test | ||
@test Lnrm ≈ minimum(Lnrm) * ones(length(Lnrm)) atol = 1e-7 | ||
|
||
nothing; |
Oops, something went wrong.