[petsc-dev] Fortran callbacks when null
Jed Brown
jed at jedbrown.org
Tue Mar 21 15:15:40 CDT 2017
This looks fine to me.
Barry Smith <bsmith at mcs.anl.gov> writes:
> Jed,
>
> It seems currently for any optional callbacks from Fortran we have this ugly code
>
> if (FORTRANNULLFUNCTION(d)) {
> *ierr = TSMonitorSet(*ts,ourmonitor,NULL,NULL);
> } else {
> *ierr = PetscObjectSetFortranCallback((PetscObject)*ts,PETSC_FORTRAN_CALLBACK_CLASS,&_cb.mondestroy,(PetscVoidFunction)d,mctx);
> *ierr = TSMonitorSet(*ts,ourmonitor,NULL,ourmonitordestroy);
> }
>
> This posses multiply uglinesses
>
> 1) the same duplicate if () ugliness people need to write and maintain for each XXXSetFunctionXXX() routine in PETSc
>
> 2) If the user passes in, by mistake something like PETSC_NULL_SOMETHING else as the function we cannot error check for that since FORTRANNULLFUNCTION is inside an if ()
>
> Could we refactor these so that
>
> a) PetscObjectSetFortranCallback() handles a NULL function (by simply not calling the function in that case) and
>
> b) we replace the if (FORTRANNULLFUNCTION) block monstrosity by something like
>
> CHKFORTRANNULLFUNCTION(d); <<< this does error checking
> *ierr = PetscObjectSetFortranCallback((PetscObject)*ts,PETSC_FORTRAN_CALLBACK_CLASS,&_cb.mondestroy,(PetscVoidFunction)d,mctx); if (*ierr) return;
> *ierr = TSMonitorSet(*ts,ourmonitor,NULL,ourmonitordestroy);
>
> everywhere?
>
> If this looks correct to you, just let me know and I'll implement it.
>
> Barry
>
>
>
>
>> Begin forwarded message:
>>
>> From: Praveen C <cpraveen at gmail.com>
>> Subject: Re: [petsc-users] Segmentation fault due to TSDestroy
>> Date: March 20, 2017 at 7:46:18 AM CDT
>> To: petsc-users <petsc-users at mcs.anl.gov>
>>
>> It turns out the problem was with this
>>
>> call TSMonitorSet(ts, Monitor, ctx, PETSC_NULL_OBJECT, ierr); CHKERRQ(ierr)
>>
>> The correct way is
>>
>> call TSMonitorSet(ts, Monitor, ctx, PETSC_NULL_FUNCTION, ierr); CHKERRQ(ierr)
>>
>> Thanks
>> praveen
>>
>> On Sat, Mar 18, 2017 at 9:30 PM, Satish Balay <balay at mcs.anl.gov <mailto:balay at mcs.anl.gov>> wrote:
>> Perhaps there is some memory corruption - you can try runnng the code with valgrind.
>>
>> Satish
>>
>> On Sat, 18 Mar 2017, Praveen C wrote:
>>
>> > Dear all
>> >
>> > I get a segmentation fault when I call TSDestroy. Without TSDestroy the
>> > code runs fine. I have included portion of my code below.
>> >
>> > subroutine runts(ctx)
>> > use userdata
>> > use comdata
>> > use mtsdata
>> > implicit none
>> > #include <petsc/finclude/petsc.h90>
>> > type(tsdata) :: ctx
>> > ! Local variables
>> > integer,parameter :: h = 100 ! File id for history file
>> > TS :: ts
>> > Vec :: u
>> > PetscErrorCode :: ierr
>> > external :: RHSFunction, Monitor
>> >
>> > call VecDuplicate(ctx%p%v_res, u, ierr); CHKERRQ(ierr)
>> >
>> > ! Copy initial condition into u
>> > call VecCopy(ctx%p%v_u, u, ierr); CHKERRQ(ierr)
>> >
>> > call TSCreate(PETSC_COMM_WORLD, ts, ierr); CHKERRQ(ierr)
>> > call TSSetProblemType(ts, TS_NONLINEAR, ierr); CHKERRQ(ierr)
>> > call TSSetRHSFunction(ts, PETSC_NULL_OBJECT, RHSFunction, ctx, ierr);
>> > CHKERRQ(ierr)
>> > call TSSetInitialTimeStep(ts, 0.0, dtg, ierr); CHKERRQ(ierr)
>> > call TSSetType(ts, TSRK, ierr); CHKERRQ(ierr);
>> > call TSSetDuration(ts, itmax, tfinal, ierr); CHKERRQ(ierr);
>> > call TSSetExactFinalTime(ts, TS_EXACTFINALTIME_MATCHSTEP, ierr);
>> > CHKERRQ(ierr);
>> > call TSMonitorSet(ts, Monitor, ctx, PETSC_NULL_OBJECT, ierr);
>> > CHKERRQ(ierr)
>> > call TSSetSolution(ts, u, ierr); CHKERRQ(ierr)
>> > call TSSetFromOptions(ts, ierr); CHKERRQ(ierr)
>> > call TSSetUp(ts, ierr); CHKERRQ(ierr)
>> >
>> > call TSSolve(ts, u, ierr); CHKERRQ(ierr)
>> >
>> > call VecCopy(u, ctx%p%v_u, ierr); CHKERRQ(ierr)
>> > call VecDestroy(u, ierr); CHKERRQ(ierr)
>> > call TSDestroy(ts, ierr); CHKERRQ(ierr)
>> >
>> > end subroutine runts
>> >
>> > Thanks
>> > praveen
>> >
>>
>>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 832 bytes
Desc: not available
URL: <http://lists.mcs.anl.gov/pipermail/petsc-dev/attachments/20170321/968b24aa/attachment.sig>
More information about the petsc-dev
mailing list