[petsc-users] Fortran: PetscDSRestoreTabulation + PetscDSGetTabulation
Barry Smith
bsmith at petsc.dev
Fri Aug 30 11:04:42 CDT 2024
> On Aug 30, 2024, at 3:10 AM, Martin Diehl <martin.diehl at kuleuven.be> wrote:
>
> Dear Matt and Barry:
>
> thanks for the quick reply.
> Please forget about the segmentation fault, that was a mistake in my
> code.
>
> Regarding the necessity of PetscDSRestoreTabulation:
> It cleans up "b" and "bDer". Those are defined as "PetscReal, pointer".
> If they are defined in a function or subroutine, they go out of scope
> automatically. So I believed (backed up by measuring the memory
> consumption with and without PetscDSRestoreTabulation) that the
> PetscDSRestoreTabulation does not add anything important.
Our Fortran stub restore performs a nullify(ptr) so that the ptr is no longer associated with our C array. I guess you are saying that when the ptr goes out of scope, its (little) memory is automatically freed regardless of whether the ptr is still associated with something or not. So nullify is not needed in that case.
Thus, the restore is only a "safety" feature, preventing the caller from accidentally using the associated C array later (which they should not do). Similar
to how our restore in C nullifies the C pointer so it cannot be used accidentally later, resulting in memory corruption.
Thanks for the clarification.
Barry
Note that some restores do dereference memory or objects, and those must be called, or there will be a memory loss. Thus, it is best always to call restore, though sometimes it may not be strictly necessary?
>
> Martin
>
>
> On Thu, 2024-08-29 at 17:21 -0400, Barry Smith wrote:
>>
>> The interface definition and Fortran stub look ok to my eyeballs.
>> However, eyeballs cannot compile code, so using the debugger to
>> determine the cause of the crash is best.
>>
>> Barry
>>
>>
>>
>>> On Aug 29, 2024, at 5:18 PM, Matthew Knepley <knepley at gmail.com>
>>> wrote:
>>>
>>> On Thu, Aug 29, 2024 at 9:57 AM Martin Diehl
>>> <martin.diehl at kuleuven.be> wrote:
>>>> Dear PETSc team,
>>>>
>>>> I have a question regarding the use of PetscDSGetTabulation from
>>>> Fortran.
>>>> PetscDSGetTabulation has a slightly different function signature
>>>> between Fortran and C. In addition, there is an (undocumented)
>>>> PetscDSRestoreTabulation in Fortran which cleans up the arrays.
>>>> Calling
>>>> it results in a segmentation fault.
>>>>
>>>> I believe that PetscDSRestoreTabulation is not needed. At least
>>>> our
>>>> Fortran FEM code compiles and runs without it. However, we have
>>>> convergence issues that we don't understand so any suspicious
>>>> code is
>>>> currently under investigation.
>>>>
>>>
>>>
>>> This may be due to my weak Fortran knowledge. Here is the code
>>>
>>>
>>> https://urldefense.us/v3/__https://gitlab.com/petsc/petsc/-/blob/main/src/dm/dt/interface/f90-__;!!G_uCfscf7eWS!Yupp9dT687CoWxUcIqYk9RyipgxWnDmxmOhS6kdl4ta7TWb_rVTCvVs9Zf5syoTZzj4shfQuUcwW5Aev3a8Vx04$
>>> custom/zdtdsf90.c?ref_type=heads
>>>
>>> I call F90Array1dCreate() in the GetTabulation and
>>> F90Array1dDestroy() in the RestoreTabulation(), which I thought
>>> was right. However, I remember something about interface
>>> declarations, which have now moved somewhere I cannot find.
>>>
>>> Barry, is the interface declaration for this function correct?
>>>
>>> Thanks,
>>>
>>> Matt
>>>
>>>> best regards,
>>>> Martin
>>>>
>
> --
> KU Leuven
> Department of Computer Science
> Department of Materials Engineering
> Celestijnenlaan 200a
> 3001 Leuven, Belgium
More information about the petsc-users
mailing list