Skip to content

TSGetConvergedReason is unusable: out-parameter wrapped as an input #247

Closed

Description

@singhharsh1708

TSGetConvergedReason can't be called usefully. The only method takes reason::TSConvergedReason by value and ccalls Ptr{TSConvergedReason} without allocating a Ref, so there's no way to get the value back:

julia> methods(PETSc.LibPETSc.TSGetConvergedReason)
# (petsclib, ts::TS, reason::TSConvergedReason)   <- the only one

The same function is wrapped correctly for the sibling solver classes. KSP_wrappers.jl:4066:

@for_petsc function KSPGetConvergedReason(petsclib::$UnionPetscLib, ksp::PetscKSP)
    reason = Ref{KSPConvergedReason}()
    @chk ccall((:KSPGetConvergedReason, $petsc_library), PetscErrorCode,
               (CKSP, Ptr{KSPConvergedReason}), ksp, reason)
    return reason[]
end

TS_wrappers.jl:3664:

@for_petsc function TSGetConvergedReason(petsclib::$UnionPetscLib, ts::TS, reason::TSConvergedReason)
    @chk ccall((:TSGetConvergedReason, $petsc_library), PetscErrorCode,
               (CTS, Ptr{TSConvergedReason}), ts, reason)
    return nothing
end

SNESGetConvergedReason has both forms, the 2-arg one that allocates the Ref and the 3-arg one that doesn't. TS only got the second. TSGetSolveTime, further down the same file, allocates its Ref correctly, so the generator seems to classify the out-parameter for some signatures and not others.

I hit this writing a TS wrapper and worked around it by deriving convergence from TSGetSolveTime and TSGetStepNumber, which can't tell a diverged step from a step-limit stop.

A crude scan for the same shape (a *Get* function whose last argument is passed as Ptr{T} with no Ref allocated, returning nothing) turns up around 247 candidates, DMGetGlobalSection and DMGetLocalToGlobalMapping among them. I haven't checked those individually, so that's a hint rather than a count.

Is wrapping/ the right place to fix this, or are the generated files edited directly?

Activity

  1. boriskaus commented on Sep 10, 2026

    @boriskaus
    Collaborator

    Thanks for reporting!

    I initially used /wrapping to generate the wrappers, but than modified the resulting files directly.
    This implies that the two are currently out of sync, which is a non-ideal situation. I'll try fixing this as soon as I have some tokens left. For now, the best way is to edit the files directly.

  2. filoferra commented on Sep 11, 2026

    @filoferra
    Collaborator

    Fixed by #254: TSGetConvergedReason now allocates the Ref and returns the reason, matching KSPGetConvergedReason. Same thing for TSGetTolerances, since it dropped the caller's vatol/vrtol handles the same way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions