Skip to content

cg! is type unstable #192

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

Closed
mohamed82008 opened this issue Feb 7, 2018 · 5 comments
Closed

cg! is type unstable #192

mohamed82008 opened this issue Feb 7, 2018 · 5 comments

Comments

@mohamed82008
Copy link
Member

The following code is type unstable:

T = Float64
n = 10
A = rand(T, n, n)
A = A' * A + I
b = rand(T, n)
tol = eps(real(T))
x0 = randn(size(b))
xJAC, hJAC = cg!(copy(x0), A, b; Pl=Identity(), tol=tol, maxiter=100, log=true)
@code_warntype cg!(copy(x0), A, b; Pl=Identity(), tol=tol, maxiter=100, log=true)

Here is the output:

Variables:
  #unused# <optimized out>
  #temp#@_2::Array{Any,1}
  ::IterativeSolvers.#cg!
  x::Array{Float64,1}
  A::Array{Float64,2}
  b::Array{Float64,1}
  #temp#@_7::Int64
  #temp#@_8::Int64
  #temp#@_9::Any
  #temp#@_10::Int64
  tol::Any
  maxiter::Any
  log::Bool
  verbose::Bool
  Pl::Any
  #temp#@_16::Bool
  #temp#@_17::Bool
  #temp#@_18::Bool
  kwargs...::Array{Any,1}

Body:
  begin
      NewvarNode(:(tol::Any))
      NewvarNode(:(maxiter::Any))
      NewvarNode(:(Pl::Any))
      #temp#@_16::Bool = true
      #temp#@_17::Bool = true
      log::Bool = false
      verbose::Bool = false
      #temp#@_18::Bool = true
      kwargs...::Array{Any,1} = $(Expr(:foreigncall, :(:jl_alloc_array_1d), Array{Any,1}, svec(Any, Int64), Array{Any,1}, 0, 0, 0))
      SSAValue(3) = (Base.arraylen)(#temp#@_2::Array{Any,1})::Int64
      SSAValue(4) = (Base.select_value)((Base.sle_int)(0, 1)::Bool, (Base.ashr_int)(SSAValue(3), (Base.bitcast)(UInt64, 1))::Int64, (Base.shl_int)(SSAValue(3), (Base.bitcast)(UInt64, (Base.neg_int)(1)::Int64))::Int64)::Int64
      SSAValue(5) = (Base.select_value)((Base.sle_int)(1, SSAValue(4))::Bool, SSAValue(4), (Base.sub_int)(1, 1)::Int64)::Int64
      #temp#@_10::Int64 = 1
      14:
      unless (Base.not_int)((#temp#@_10::Int64 === (Base.add_int)(SSAValue(5), 1)::Int64)::Bool)::Bool goto 48
      SSAValue(6) = #temp#@_10::Int64
      SSAValue(7) = (Base.add_int)(#temp#@_10::Int64, 1)::Int64
      #temp#@_7::Int64 = SSAValue(6)
      #temp#@_10::Int64 = SSAValue(7)
      #temp#@_8::Int64 = (Base.sub_int)((Base.mul_int)(#temp#@_7::Int64, 2)::Int64, 1)::Int64
      #temp#@_9::Any = (Core.arrayref)(#temp#@_2::Array{Any,1}, #temp#@_8::Int64)::Any
      unless (#temp#@_9::Any === :Pl)::Bool goto 26
      Pl::Any = (Core.arrayref)(#temp#@_2::Array{Any,1}, (Base.add_int)(#temp#@_8::Int64, 1)::Int64)::Any
      #temp#@_18::Bool = false
      goto 46
      26:
      unless (#temp#@_9::Any === :verbose)::Bool goto 30
      verbose::Bool = (Core.typeassert)((Core.arrayref)(#temp#@_2::Array{Any,1}, (Base.add_int)(#temp#@_8::Int64, 1)::Int64)::Any, IterativeSolvers.Bool)::Bool
      goto 46
      30:
      unless (#temp#@_9::Any === :log)::Bool goto 34
      log::Bool = (Core.typeassert)((Core.arrayref)(#temp#@_2::Array{Any,1}, (Base.add_int)(#temp#@_8::Int64, 1)::Int64)::Any, IterativeSolvers.Bool)::Bool
      goto 46
      34:
      unless (#temp#@_9::Any === :maxiter)::Bool goto 39
      maxiter::Any = (Core.arrayref)(#temp#@_2::Array{Any,1}, (Base.add_int)(#temp#@_8::Int64, 1)::Int64)::Any
      #temp#@_17::Bool = false
      goto 46
      39:
      unless (#temp#@_9::Any === :tol)::Bool goto 44
      tol::Any = (Core.arrayref)(#temp#@_2::Array{Any,1}, (Base.add_int)(#temp#@_8::Int64, 1)::Int64)::Any
      #temp#@_16::Bool = false
      goto 46
      44:
      $(Expr(:foreigncall, :(:jl_array_ptr_1d_push), Void, svec(Any, Any), :(kwargs...), 0, :((Core.tuple)(#temp#@_9, (Core.arrayref)(#temp#@_2, (Base.add_int)(#temp#@_8, 1)::Int64))::Tuple{Any,Any}), 0))
      46:
      goto 14
      48:
      unless #temp#@_16::Bool goto 51
      tol::Any = (Base.Math.sqrt_llvm)(2.220446049250313e-16)::Float64
      51:
      unless #temp#@_17::Bool goto 54
      maxiter::Any = (Base.arraysize)(A::Array{Float64,2}, 2)::Int64
      54:
      unless #temp#@_18::Bool goto 57
      Pl::Any = $(QuoteNode(IterativeSolvers.Identity()))
      57:
      return (IterativeSolvers.#cg!#39)(tol::Any, maxiter::Any, log::Bool, verbose::Bool, Pl::Any, kwargs...::Array{Any,1}, ::IterativeSolvers.#cg!, x::Array{Float64,1}, A::Array{Float64,2}, b::Array{Float64,1})::Union{Array{Float64,1}, Tuple{Array{Float64,1},IterativeSolvers.ConvergenceHistory{_,_} where _ where _}}
  end::Union{Array{Float64,1}, Tuple{Array{Float64,1},IterativeSolvers.ConvergenceHistory{_,_} where _ where _}}
@mohamed82008
Copy link
Member Author

I am using Julia v0.6.2 with IterativeSolvers v0.4.2+

@mohamed82008
Copy link
Member Author

Same result on master.

@mohamed82008
Copy link
Member Author

mohamed82008 commented Feb 7, 2018

Changing keyword arguments to positional ones and making log a parameter passed as Val{log} fixes the type instabilities.

@ChrisRackauckas
Copy link
Member

That's just a v0.6 issue that's fixed in v0.7. I wouldn't change the API for that.

@mohamed82008
Copy link
Member Author

Cool! Closing the issue then. The above is a quick and dirty fix for v0.6 users to use on their machines if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants