<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div> Jed,<div class=""><br class=""></div><div class=""> It seems currently for any optional callbacks from Fortran we have this ugly code</div><div class=""><br class=""></div><div class=""><div class=""> if (FORTRANNULLFUNCTION(d)) {</div><div class=""> *ierr = TSMonitorSet(*ts,ourmonitor,NULL,NULL);</div><div class=""> } else {</div><div class=""> *ierr = PetscObjectSetFortranCallback((PetscObject)*ts,PETSC_FORTRAN_CALLBACK_CLASS,&_cb.mondestroy,(PetscVoidFunction)d,mctx);</div><div class=""> *ierr = TSMonitorSet(*ts,ourmonitor,NULL,ourmonitordestroy);</div><div class=""> }</div><div class=""><br class=""></div><div class="">This posses multiply uglinesses </div><div class=""><br class=""></div><div class="">1) the same duplicate if () ugliness people need to write and maintain for each XXXSetFunctionXXX() routine in PETSc</div><div class=""><br class=""></div><div class="">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 ()</div><div class=""><br class=""></div><div class="">Could we refactor these so that </div><div class=""><br class=""></div><div class="">a) PetscObjectSetFortranCallback() handles a NULL function (by simply not calling the function in that case) and</div><div class=""><br class=""></div><div class="">b) we replace the if (FORTRANNULLFUNCTION) block monstrosity by something like </div><div class=""><br class=""></div><div class=""> CHKFORTRANNULLFUNCTION(d); <<< this does error checking</div><div class=""> *ierr = PetscObjectSetFortranCallback((PetscObject)*ts,PETSC_FORTRAN_CALLBACK_CLASS,&_cb.mondestroy,(PetscVoidFunction)d,mctx); if (*ierr) return;</div><div class=""> *ierr = TSMonitorSet(*ts,ourmonitor,NULL,ourmonitordestroy);</div><div class=""><br class=""></div><div class=""> everywhere?</div><div class=""><br class=""></div><div class=""> If this looks correct to you, just let me know and I'll implement it.</div><div class=""><br class=""></div><div class=""> Barry</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div><br class=""><blockquote type="cite" class=""><div class="">Begin forwarded message:</div><br class="Apple-interchange-newline"><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;" class=""><span style="font-family: -webkit-system-font, Helvetica Neue, Helvetica, sans-serif; color:rgba(0, 0, 0, 1.0);" class=""><b class="">From: </b></span><span style="font-family: -webkit-system-font, Helvetica Neue, Helvetica, sans-serif;" class="">Praveen C <<a href="mailto:cpraveen@gmail.com" class="">cpraveen@gmail.com</a>><br class=""></span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;" class=""><span style="font-family: -webkit-system-font, Helvetica Neue, Helvetica, sans-serif; color:rgba(0, 0, 0, 1.0);" class=""><b class="">Subject: </b></span><span style="font-family: -webkit-system-font, Helvetica Neue, Helvetica, sans-serif;" class=""><b class="">Re: [petsc-users] Segmentation fault due to TSDestroy</b><br class=""></span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;" class=""><span style="font-family: -webkit-system-font, Helvetica Neue, Helvetica, sans-serif; color:rgba(0, 0, 0, 1.0);" class=""><b class="">Date: </b></span><span style="font-family: -webkit-system-font, Helvetica Neue, Helvetica, sans-serif;" class="">March 20, 2017 at 7:46:18 AM CDT<br class=""></span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;" class=""><span style="font-family: -webkit-system-font, Helvetica Neue, Helvetica, sans-serif; color:rgba(0, 0, 0, 1.0);" class=""><b class="">To: </b></span><span style="font-family: -webkit-system-font, Helvetica Neue, Helvetica, sans-serif;" class="">petsc-users <<a href="mailto:petsc-users@mcs.anl.gov" class="">petsc-users@mcs.anl.gov</a>><br class=""></span></div><br class=""><div class=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" class=""><div dir="ltr" class="">It turns out the problem was with this<div class=""><br class=""></div><div class=""><div style="font-size:12.8px" class=""> call TSMonitorSet(ts, Monitor, ctx, PETSC_NULL_OBJECT, ierr); CHKERRQ(ierr)</div></div><div class=""><br class=""></div><div class="">The correct way is</div><div class=""><div style="font-size:12.8px" class=""><br class=""></div><div style="font-size:12.8px" class=""> call TSMonitorSet(ts, Monitor, ctx, PETSC_NULL_FUNCTION, ierr); CHKERRQ(ierr)</div></div><div class=""><br class=""></div><div class="">Thanks</div><div class="">praveen</div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Sat, Mar 18, 2017 at 9:30 PM, Satish Balay <span dir="ltr" class=""><<a href="mailto:balay@mcs.anl.gov" target="_blank" class="">balay@mcs.anl.gov</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Perhaps there is some memory corruption - you can try runnng the code with valgrind.<br class="">
<span class="HOEnZb"><font color="#888888" class=""><br class="">
Satish<br class="">
</font></span><div class="HOEnZb"><div class="h5"><br class="">
On Sat, 18 Mar 2017, Praveen C wrote:<br class="">
<br class="">
> Dear all<br class="">
><br class="">
> I get a segmentation fault when I call TSDestroy. Without TSDestroy the<br class="">
> code runs fine. I have included portion of my code below.<br class="">
><br class="">
> subroutine runts(ctx)<br class="">
> use userdata<br class="">
> use comdata<br class="">
> use mtsdata<br class="">
> implicit none<br class="">
> #include <petsc/finclude/petsc.h90><br class="">
> type(tsdata) :: ctx<br class="">
> ! Local variables<br class="">
> integer,parameter :: h = 100 ! File id for history file<br class="">
> TS :: ts<br class="">
> Vec :: u<br class="">
> PetscErrorCode :: ierr<br class="">
> external :: RHSFunction, Monitor<br class="">
><br class="">
> call VecDuplicate(ctx%p%v_res, u, ierr); CHKERRQ(ierr)<br class="">
><br class="">
> ! Copy initial condition into u<br class="">
> call VecCopy(ctx%p%v_u, u, ierr); CHKERRQ(ierr)<br class="">
><br class="">
> call TSCreate(PETSC_COMM_WORLD, ts, ierr); CHKERRQ(ierr)<br class="">
> call TSSetProblemType(ts, TS_NONLINEAR, ierr); CHKERRQ(ierr)<br class="">
> call TSSetRHSFunction(ts, PETSC_NULL_OBJECT, RHSFunction, ctx, ierr);<br class="">
> CHKERRQ(ierr)<br class="">
> call TSSetInitialTimeStep(ts, 0.0, dtg, ierr); CHKERRQ(ierr)<br class="">
> call TSSetType(ts, TSRK, ierr); CHKERRQ(ierr);<br class="">
> call TSSetDuration(ts, itmax, tfinal, ierr); CHKERRQ(ierr);<br class="">
> call TSSetExactFinalTime(ts, TS_EXACTFINALTIME_MATCHSTEP, ierr);<br class="">
> CHKERRQ(ierr);<br class="">
> call TSMonitorSet(ts, Monitor, ctx, PETSC_NULL_OBJECT, ierr);<br class="">
> CHKERRQ(ierr)<br class="">
> call TSSetSolution(ts, u, ierr); CHKERRQ(ierr)<br class="">
> call TSSetFromOptions(ts, ierr); CHKERRQ(ierr)<br class="">
> call TSSetUp(ts, ierr); CHKERRQ(ierr)<br class="">
><br class="">
> call TSSolve(ts, u, ierr); CHKERRQ(ierr)<br class="">
><br class="">
> call VecCopy(u, ctx%p%v_u, ierr); CHKERRQ(ierr)<br class="">
> call VecDestroy(u, ierr); CHKERRQ(ierr)<br class="">
> call TSDestroy(ts, ierr); CHKERRQ(ierr)<br class="">
><br class="">
> end subroutine runts<br class="">
><br class="">
> Thanks<br class="">
> praveen<br class="">
><br class="">
<br class="">
</div></div></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></div></body></html>