<div dir="ltr">Awesome, thanks! I'll check it out.<div><br></div><div>Yuan<br><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">2021年11月27日(土) 1:56 Barry Smith <<a href="mailto:bsmith@petsc.dev" target="_blank">bsmith@petsc.dev</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div><br></div>  I believe I have fixed the bug in the branch <b style="color:rgb(200,20,201);font-family:Menlo;font-size:14px">barry/2021-11-26/fix-petscsfgetgraph-fortran/release </b><a href="https://gitlab.com/petsc/petsc/-/merge_requests/4605" target="_blank">https://gitlab.com/petsc/petsc/-/merge_requests/4605</a><br><div><br></div><div>  See <span style="font-family:Menlo;font-size:14px">src/vec/is/sf/tutorials/ex1f.F90 for how to check if the returned value is a FORTRAN_NULL_INTEGER</span></div><div><span style="font-family:Menlo;font-size:14px"><br></span></div><div>  Barry</div><div><br><div><br><blockquote type="cite"><div>On Nov 24, 2021, at 3:19 AM, 袁煕 <<a href="mailto:yuanxi@advancesoft.jp" target="_blank">yuanxi@advancesoft.jp</a>> wrote:</div><br><div><div dir="ltr">Thank you for the clarification.  I am looking forward to this bug fix.<br><div><br></div><div>Yuan</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">2021年11月22日(月) 23:17 Barry Smith <<a href="mailto:bsmith@petsc.dev" target="_blank">bsmith@petsc.dev</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div><br></div>   The address of PETSC_NULL_INTEGER is not 0, it in some common block location. PETSC_NULL_INTEGER should not be used in the form <div>if idx == PETSC_NULL_INTEGER, it is only to be used as an argument to a PETSc function that on the C side can take a NULL (integer) argument.</div><div><br></div><div>   As I noted before whoever wrote the incorrect petscsfgetgraph_ Fortran wrapper needs to fix it.</div><div><br></div><div>  Barry</div><div><br><div><br><blockquote type="cite"><div>On Nov 21, 2021, at 8:05 PM, 袁煕 <<a href="mailto:yuanxi@advancesoft.jp" target="_blank">yuanxi@advancesoft.jp</a>> wrote:</div><br><div><div dir="ltr"><div>Thank you very much.</div><div><br></div>The NULL pointer could be identified by if( loc(gmine)==PETSC_NULL_INTEGER) as above <a class="gmail_plusreply" id="m_1587546953511251336gmail-m_-1020317925120959375gmail-m_-789483520662939723plusReplyChip-0" href="mailto:mfadams@lbl.gov" target="_blank">@Mark Adams</a> suggested. However, It seems that there is a memory leak here? <div><br></div><div>Yuan</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">2021年11月19日(金) 22:20 Barry Smith <<a href="mailto:bsmith@petsc.dev" target="_blank">bsmith@petsc.dev</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
  it is very possible there is a bug in the Fortran interface for this function. It looks like whoever wrote the Fortran interface did not think through the special case of contiguous entries.<br>
<br>
PETSC_EXTERN void  petscsfgetgraph_(PetscSF *sf,PetscInt *nroots,PetscInt *nleaves, F90Array1d  *ailocal, F90Array1d  *airemote, PetscErrorCode *ierr PETSC_F90_2PTR_PROTO(pilocal) PETSC_F90_2PTR_PROTO(piremote))<br>
{<br>
  const PetscInt    *ilocal;<br>
  const PetscSFNode *iremote;<br>
<br>
  *ierr = PetscSFGetGraph(*sf,nroots,nleaves,&ilocal,&iremote);if (*ierr) return;<br>
  *ierr = F90Array1dCreate((void*)ilocal,MPIU_INT,1,*nleaves, ailocal PETSC_F90_2PTR_PARAM(pilocal));<br>
  /* this creates a memory leak */<br>
  f90arraysfnodecreate_((PetscInt*)iremote,nleaves, airemote PETSC_F90_2PTR_PARAM(piremote));<br>
}<br>
<br>
PetscErrorCode F90Array1dCreate(void *array,MPI_Datatype type,PetscInt start,PetscInt len,F90Array1d *ptr PETSC_F90_2PTR_PROTO(ptrd))<br>
{<br>
  PetscFunctionBegin;<br>
  if (type == MPIU_SCALAR) {<br>
    if (!len) array = PETSC_NULL_SCALAR_Fortran;<br>
    f90array1dcreatescalar_(array,&start,&len,ptr PETSC_F90_2PTR_PARAM(ptrd));<br>
  } else if (type == MPIU_REAL) {<br>
    if (!len) array = PETSC_NULL_REAL_Fortran;<br>
    f90array1dcreatereal_(array,&start,&len,ptr PETSC_F90_2PTR_PARAM(ptrd));<br>
  } else if (type == MPIU_INT) {<br>
    if (!len) array = PETSC_NULL_INTEGER_Fortran;<br>
    f90array1dcreateint_(array,&start,&len,ptr PETSC_F90_2PTR_PARAM(ptrd));<br>
  } else if (type == MPIU_FORTRANADDR) {<br>
    f90array1dcreatefortranaddr_(array,&start,&len,ptr PETSC_F90_2PTR_PARAM(ptrd));<br>
  } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported MPI_Datatype");<br>
  PetscFunctionReturn(0);<br>
}<br>
<br>
It blindly calls F90Array1dCreate() with a len = *nleaves > 0 but a NULL array ilocal then f90array1dcreateint_ must create an invalid object.<br>
<br>
Barry<br>
<br>
<br>
<br>
> On Nov 19, 2021, at 12:34 AM, 袁煕 <<a href="mailto:yuanxi@advancesoft.jp" target="_blank">yuanxi@advancesoft.jp</a>> wrote:<br>
> <br>
> Dear PETSc-team,<br>
> <br>
> I am using function PetscSFGetGraph in my program like<br>
> -----------------------<br>
> call PetscSFGetGraph(sf,gnroots,gnleaves,gmine,gremote,ierr)<br>
> -----------------------<br>
> <br>
> In some cases, it works well. But in some cases, I encountered following error<br>
> ------------------<br>
> PETSC ERROR: Caught signal number 11 SEGV: Segmentation Violation, probably memory access out of range <br>
> ------------------<br>
> I found it is due to the action of reading gmine, which is a fortran pointer point to an array. It is reasonable because PETSc manual tells me "if returned value is NULL, it means leaves are in contiguous storage". The problem is that I cannot find if gmine is a null pointer. <br>
> 1)  I cannot use "if (gmine==PETSC_NULL_INTEGER)" because my intel compiler would return a compile error "A scalar-valued expression is required in this context"<br>
> 2)  When using standard style of checking a null pointer in fortran, "associated(gmine)", it returns "T". Even in cases such action of "print *, gmine(1)" would give rise to above Segmentation Violation error.<br>
> Is there any means to check the NULL pointer in Fortran in above cases.<br>
> Many thanks,<br>
> Yuan<br>
<br>
</blockquote></div>
</div></blockquote></div><br></div></div></blockquote></div>
</div></blockquote></div><br></div></div></blockquote></div></div></div>