From 7a822af2780a7cc3173efccce29d97093c82254b Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Fri, 16 Aug 2024 10:08:19 -0400 Subject: [PATCH 1/2] Allow bigfloats through dualcaches We could in theory embed a LBC into the DualCache so that it reuses this vector, but I'm not sure it's worth it. Basically, if you have a T that's not bitstype then it's not going to reuse memory and it's going to have to re-allocate every number, so you have lots of memory operations already as requirements for making the T being acted on. In this case, you're really just looking for a functional fallback as there is no such thing as preallocation, so simply making the array you wish to see is a good option. --- src/PreallocationTools.jl | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/PreallocationTools.jl b/src/PreallocationTools.jl index ced6354..7c26381 100644 --- a/src/PreallocationTools.jl +++ b/src/PreallocationTools.jl @@ -128,28 +128,40 @@ const dualcache = DiffCache Returns the `Dual` or normal cache array stored in `dc` based on the type of `u`. """ -function get_tmp(dc::DiffCache, u::T) where {T <: ForwardDiff.Dual} - nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du) - if nelem > length(dc.dual_du) - enlargediffcache!(dc, nelem) +function get_tmp(dc::DiffCache, u::T) where {T <: ForwardDiff.Dual{T2}} + if isbitstype(T) + nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du) + if nelem > length(dc.dual_du) + enlargediffcache!(dc, nelem) + end + _restructure(dc.du, reinterpret(T, view(dc.dual_du, 1:nelem))) + else + _restructure(dc.du, zeros(T, size(dc.du))) end - _restructure(dc.du, reinterpret(T, view(dc.dual_du, 1:nelem))) end function get_tmp(dc::DiffCache, ::Type{T}) where {T <: ForwardDiff.Dual} - nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du) - if nelem > length(dc.dual_du) - enlargediffcache!(dc, nelem) + if isbitstype(T) + nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du) + if nelem > length(dc.dual_du) + enlargediffcache!(dc, nelem) + end + _restructure(dc.du, reinterpret(T, view(dc.dual_du, 1:nelem))) + else + _restructure(dc.du, zeros(T, size(dc.du))) end - _restructure(dc.du, reinterpret(T, view(dc.dual_du, 1:nelem))) end function get_tmp(dc::DiffCache, u::AbstractArray{T}) where {T <: ForwardDiff.Dual} - nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du) - if nelem > length(dc.dual_du) - enlargediffcache!(dc, nelem) + if isbitstype(T) + nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du) + if nelem > length(dc.dual_du) + enlargediffcache!(dc, nelem) + end + _restructure(dc.du, reinterpret(T, view(dc.dual_du, 1:nelem))) + else + _restructure(dc.du, zeros(T, size(dc.du))) end - _restructure(dc.du, reinterpret(T, view(dc.dual_du, 1:nelem))) end function get_tmp(dc::DiffCache, u::Union{Number, AbstractArray}) From 2cf2bd287e43bb8c1333479602ea029abcf26894 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Fri, 16 Aug 2024 10:32:35 -0400 Subject: [PATCH 2/2] Update src/PreallocationTools.jl Co-authored-by: Qingyu Qu <52615090+ErikQQY@users.noreply.github.com> --- src/PreallocationTools.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PreallocationTools.jl b/src/PreallocationTools.jl index 7c26381..9834c81 100644 --- a/src/PreallocationTools.jl +++ b/src/PreallocationTools.jl @@ -128,7 +128,7 @@ const dualcache = DiffCache Returns the `Dual` or normal cache array stored in `dc` based on the type of `u`. """ -function get_tmp(dc::DiffCache, u::T) where {T <: ForwardDiff.Dual{T2}} +function get_tmp(dc::DiffCache, u::T) where {T <: ForwardDiff.Dual} if isbitstype(T) nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du) if nelem > length(dc.dual_du)