PreallocationTools.jl icon indicating copy to clipboard operation
PreallocationTools.jl copied to clipboard

DualCache doesn't work for BigFloat?

Open JianghuiDu opened this issue 2 years ago • 1 comments

Using the example on the website but change to BigFloat

using ForwardDiff, PreallocationTools
randmat = big.(rand(5, 3))
sto = similar(randmat)
stod = DiffCache(sto)

function claytonsample!(sto, τ, α; randmat = randmat)
    sto = get_tmp(sto, τ)
    sto .= randmat
    τ == zero(eltype(sto)) && return sto

    n = size(sto, 1)
    for i in 1:n
        v = sto[i, 2]
        u = sto[i, 1]
        sto[i, 1] = (1 - u^(-τ) + u^(-τ) * v^(-(τ / (1 + τ))))^(-1 / τ) * α
        sto[i, 2] = (1 - u^(-τ) + u^(-τ) * v^(-(τ / (1 + τ))))^(-1 / τ)
    end
    return sto
end

ForwardDiff.derivative(τ -> claytonsample!(stod, τ, big(0.0)), big(0.3))

Throws error:

ERROR: ArgumentError: cannot reinterpret `BigFloat`
as `ForwardDiff.Dual{ForwardDiff.Tag{var"#61#62", Bi
gFloat}, BigFloat, 1}`, type `ForwardDiff.Dual{Forwa
rdDiff.Tag{var"#61#62", BigFloat}, BigFloat, 1}` is
not a bits type
Stacktrace:
 [1] (::Base.var"#throwbits#323")(S::Type, T::Type,
U::Type)
   @ Base .\reinterpretarray.jl:16
 [2] reinterpret(#unused#::Type{ForwardDiff.Dual{For
wardDiff.Tag{var"#61#62", BigFloat}, BigFloat, 1}},
a::SubArray{BigFloat, 1, Vector{BigFloat}, Tuple{Uni
tRange{Int64}}, true})
   @ Base .\reinterpretarray.jl:62
 [3] get_tmp(dc::DiffCache{Matrix{BigFloat}, Vector{
BigFloat}}, u::ForwardDiff.Dual{ForwardDiff.Tag{var"
#61#62", BigFloat}, BigFloat, 1})
   @ PreallocationTools C:\Users\Jianghui\.julia\pac
kages\PreallocationTools\nhCNl\src\PreallocationTool
s.jl:120
 [4] claytonsample!(sto::DiffCache{Matrix{BigFloat},
 Vector{BigFloat}}, τ::ForwardDiff.Dual{ForwardDiff.
Tag{var"#61#62", BigFloat}, BigFloat, 1}, α::BigFloa
t; randmat::Matrix{BigFloat})
   @ Main c:\Users\Jianghui\Documents\Manuscripts\Ha
waiiREE\model\DOM\REE\main.HawaiiREEDOC.jl:437
 [5] claytonsample!(sto::DiffCache{Matrix{BigFloat},
 Vector{BigFloat}}, τ::ForwardDiff.Dual{ForwardDiff.
Tag{var"#61#62", BigFloat}, BigFloat, 1}, α::BigFloa
t)
   @ Main c:\Users\Jianghui\Documents\Manuscripts\Ha
waiiREE\model\DOM\REE\main.HawaiiREEDOC.jl:436
 [6] (::var"#61#62")(τ::ForwardDiff.Dual{ForwardDiff
.Tag{var"#61#62", BigFloat}, BigFloat, 1})
   @ Main c:\Users\Jianghui\Documents\Manuscripts\Ha
waiiREE\model\DOM\REE\main.HawaiiREEDOC.jl:451
 [7] derivative(f::var"#61#62", x::BigFloat)
   @ ForwardDiff C:\Users\Jianghui\.julia\packages\F
orwardDiff\PcZ48\src\derivative.jl:14
 [8] top-level scope
   @ c:\Users\Jianghui\Documents\Manuscripts\HawaiiR
EE\model\DOM\REE\main.HawaiiREEDOC.jl:451

JianghuiDu avatar Sep 03 '23 14:09 JianghuiDu

Yes DualCache needs bitstypes. FixedSizeDiffCache and LazyBufferCache should be fine with BigFloat though. Since BigFloat always allocates it's a little silly to cache it though.

ChrisRackauckas avatar Sep 03 '23 15:09 ChrisRackauckas

Fixed with https://github.com/SciML/PreallocationTools.jl/pull/112

ChrisRackauckas avatar Aug 16 '24 14:08 ChrisRackauckas