Skip to content

Commit

Permalink
Merge pull request #6 from vepiteski/type-stable-version
Browse files Browse the repository at this point in the history
Type stable version, go version 0.2.5.
  • Loading branch information
tmigot committed Feb 1, 2021
2 parents 88712c0 + 00bab25 commit 04bc236
Show file tree
Hide file tree
Showing 57 changed files with 3,448 additions and 1,239 deletions.
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ language: julia
os:
- linux
- osx
- windows
#- windows
julia:
- 1.1 #Long-term support (LTS) release: v1.0.5 (Sep 9, 2019)
- 1.2
- 1.3
- 1.4
#- 1.2
#- 1.3
#- 1.4
- 1.5
- nightly
#- nightly
notifications:
email: false

Expand Down
13 changes: 5 additions & 8 deletions Project.toml
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"
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Stopping

[![Build Status](https://travis-ci.org/vepiteski/Stopping.jl.svg?branch=master)](https://travis-ci.org/vepiteski/Stopping.jl)
[![Coverage Status](https://coveralls.io/repos/vepiteski/Stopping.jl/badge.svg?branch=master&service=github)](https://coveralls.io/github/vepiteski/Stopping.jl?branch=julia-0.7)
[![Coverage Status](https://coveralls.io/repos/github/vepiteski/Stopping.jl/badge.svg?branch=master)](https://coveralls.io/github/vepiteski/Stopping.jl?branch=master)
[![](https://img.shields.io/badge/docs-dev-blue.svg)](https://vepiteski.github.io/Stopping.jl/dev/)


## Purpose

Tools to ease the uniformization of stopping criteria in iterative solvers.
Expand Down
4 changes: 3 additions & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ makedocs(
# Documenter can also automatically deploy documentation to gh-pages.
# See "Hosting Documentation" and deploydocs() in the Documenter manual
# for more information.
deploydocs(repo = "github.com/vepiteski/Stopping.jl")#
deploydocs(repo = "github.com/vepiteski/Stopping.jl")
#deploydocs(repo = "github.com/Goysa2/Stopping.jl")#
#https://juliadocs.github.io/Documenter.jl/stable/man/hosting/ ?
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
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)

;
116 changes: 116 additions & 0 deletions paper/LinearAlgebra-test/compare-krylov-stopping.jl
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;
Loading

0 comments on commit 04bc236

Please sign in to comment.