Skip to content

FAQ: ccall + ForwardDiff Dual silently reinterprets as Cdouble - #880

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:docs/ccall-dual-autodiff-warning
Jul 23, 2026
Merged

FAQ: ccall + ForwardDiff Dual silently reinterprets as Cdouble#880
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:docs/ccall-dual-autodiff-warning

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

Summary

Document the failure mode from DifferentialEquations.jl#888: an RHS implemented via ccall(..., Ptr{Cdouble}, ...) is not safe under ForwardDiff. Vector{Dual} is still a bitstype array, so the C ABI silently reinterprets Dual memory as doubles — Julia cannot raise a conversion error at the ccall boundary. Jacobians become garbage; stiff solvers can return wrong trajectories.

FAQ section lists the supported workarounds:

  1. autodiff = ADTypes.AutoFiniteDiff()
  2. Supply analytical jac
  3. Guard the RHS to reject non-AbstractFloat eltypes (or use FunctionWrapperSpecialize)
  4. Explicit note that automatic detection at the C ABI is not possible

Test plan

  • Docs build
  • Content cross-checked against the DE#888 reproduction notes

This PR should be ignored until reviewed by @ChrisRackauckas.

Foreign RHS via ccall(…, Ptr{Cdouble}, …) silently reinterprets
ForwardDiff Dual memory as doubles, producing garbage Jacobians
without a conversion error. Document the failure mode and the
supported workarounds (AutoFiniteDiff, analytical jac, Dual guard,
FunctionWrapperSpecialize). See DifferentialEquations.jl#888.

Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review July 23, 2026 19:36
@ChrisRackauckas
ChrisRackauckas merged commit 40aa763 into SciML:master Jul 23, 2026
4 checks passed
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

Successfully merging this pull request may close these issues.

2 participants