<div dir="ltr"><div>Hi all</div><div><br></div><div>I have a quick question regarding the use of PCMGSetResidual within fortran code. I'm calling PCMGSetResidual from within fortran: <br></div><div><br></div><div>call PCMGSetResidual(pc_mg, petsc_level, mg_residual, coarse_matrix ierr) <br></div><div><br></div><div>and just for testing purposes I've written a trivial residual evaluation routine:</div><div><br></div><div>    subroutine mg_residual(mat, b, x, r, ierr)<br>      !!< Compute the residual<br><br>      ! ~~~~~~<br>      type(tMat) :: mat<br>      type(tVec) :: b, x, r<br>      PetscErrorCode :: ierr<br>      ! ~~~~~~<br><br>      print *, "inside residual evaluation"<br>      call MatResidual(mat, b, x, r, ierr)<br>      <br>   end subroutine</div><div><br></div><div>The problem I am having is that this segfaults when the residual routine is called. Valgrind shows that it is failing in the fortran interface in ftn-custom/zmgfuncf.c, with the message:<br></div><div><br></div><div>==24742== Invalid read of size 8<br>==24742==    at 0x5B7CBC0: ourresidualfunction (in /home/projects/dependencies/petsc_main/arch-linux-c-opt/lib/libpetsc.so.3.015.0)<br>==24742==    by 0x5B6D804: PCMGMCycle_Private (in /home/projects/dependencies/petsc_main/arch-linux-c-opt/lib/libpetsc.so.3.015.0)</div><div><br></div><div>==24742== Process terminating with default action of signal 11 (SIGSEGV)<br>==24742==  Access not within mapped region at address 0x0<br>==24742==    at 0x5B7CBC0: ourresidualfunction (in /home/sdargavi/projects/dependencies/petsc_main/arch-linux-c-opt/lib/libpetsc.so.3.015.0)<br>==24742==    by 0x5B6D804: PCMGMCycle_Private (in /home/sdargavi/projects/dependencies/petsc_main/arch-linux-c-opt/lib/libpetsc.so.3.015.0)</div><div><br></div><div>I'm guessing this is because the fortran_func_pointers isn't pointing to the mg_residual routine, but I am not sure why. I noticed that in the C code of PCMGSetResidual that it calls MatDestroy on the A matrix in mg_levels and replaces it with the mat passed in:<br></div><div><br></div><div>if (mat) PetscObjectReference((PetscObject)mat);<br>MatDestroy(&mglevels[l]->A);<br>mglevels[l]->A = mat</div><div><br></div><div>so I modified my code to call PCMGSetResidual either before the operators are set, or after but passing in an extra copy, but this doesn't seem to help.</div><div><br></div><div>I'm guessing I'm doing something silly, but just wondering if anyone had any ideas? Thanks for your help</div><div>Steven<br></div></div>