Skip to content

initialize and finalize and destroy #155

Closed

Description

@jkozdon

Do we ever see a use case where we might have something like:

PETSc.initialize(petsclib)
# ... work....
PETSc.finalize(petsclib)

# start PETSc again...
PETSc.initialize(petsclib)

I ask because this can cause problems with the garbage collector in when it's used. For instance, if the garbage collector is run on a PETSc object allocated in the first chunk of work AFTER petsc has been re-initialized this is an error since the actual PETSc object doesn't exist anymore. The destroy has a check for whether PETSc is finalized, but this will not catch PETSc having been re-initialized.

Activity

  1. changed the title [-]initialize and finalize[/-] [+]initialize and finalize and destroy[/+] on Aug 5, 2021
  2. boriskaus commented on Aug 6, 2021

    @boriskaus
    Collaborator

    I see no reason why this would be done. In the testing we do this but with a finalize at the end

  3. psanan commented on Aug 6, 2021

    @psanan
    Contributor

    PETSc supports this. It is done sometimes in practice because someone uses PETSc within a larger code and wants to encapsulate as much as possible, so they initialize and finalize PETSc for each solve.

    Is it already an issue without the second initialize()? You aren't allowed to destroy PETSc objects after you finalize PETSc.

  4. jkozdon commented on Aug 6, 2021

    @jkozdon
    MemberAuthor

    Is it already an issue without the second initialize()? You aren't allowed to destroy PETSc objects after you finalize PETSc.

    That is true, but we catch it with a check to see if PETSc is finalized

    PETSc.jl/src/mat.jl

    Lines 191 to 195 in 73f12e0

    function destroy(nullspace::MatNullSpace{$PetscScalar})
    finalized($petsclib) ||
    @chk ccall((:MatNullSpaceDestroy, $libpetsc), PetscErrorCode, (Ptr{CMatNullSpace},), nullspace)
    return nothing
    end

    OK. I have a few ideas for how to work around this, so will leave the issue open. Some smarter people than me may weigh in as well.

  5. linked a pull request that will close this issueAutomatically wrapping with `Clang.jl` #149on Aug 7, 2021
  6. filoferra commented on Sep 11, 2026

    @filoferra
    Collaborator

    The age mechanism does both: petsclib.age is bumped on initialize and finalize, every object records the age it was created under. isdestroyable compares them, so a handle from a previous cycle is a no-op instead of a crash. Fixed in #252

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