[petsc-users] How to check NULL pointer in Fortran
Barry Smith
bsmith at petsc.dev
Fri Nov 26 10:56:17 CST 2021
I believe I have fixed the bug in the branch barry/2021-11-26/fix-petscsfgetgraph-fortran/release https://gitlab.com/petsc/petsc/-/merge_requests/4605 <https://gitlab.com/petsc/petsc/-/merge_requests/4605>
See src/vec/is/sf/tutorials/ex1f.F90 for how to check if the returned value is a FORTRAN_NULL_INTEGER
Barry
> On Nov 24, 2021, at 3:19 AM, 袁煕 <yuanxi at advancesoft.jp> wrote:
>
> Thank you for the clarification. I am looking forward to this bug fix.
>
> Yuan
>
> 2021年11月22日(月) 23:17 Barry Smith <bsmith at petsc.dev <mailto:bsmith at petsc.dev>>:
>
> 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
> 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.
>
> As I noted before whoever wrote the incorrect petscsfgetgraph_ Fortran wrapper needs to fix it.
>
> Barry
>
>
>> On Nov 21, 2021, at 8:05 PM, 袁煕 <yuanxi at advancesoft.jp <mailto:yuanxi at advancesoft.jp>> wrote:
>>
>> Thank you very much.
>>
>> The NULL pointer could be identified by if( loc(gmine)==PETSC_NULL_INTEGER) as above @Mark Adams <mailto:mfadams at lbl.gov> suggested. However, It seems that there is a memory leak here?
>>
>> Yuan
>>
>> 2021年11月19日(金) 22:20 Barry Smith <bsmith at petsc.dev <mailto:bsmith at petsc.dev>>:
>>
>> 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.
>>
>> 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))
>> {
>> const PetscInt *ilocal;
>> const PetscSFNode *iremote;
>>
>> *ierr = PetscSFGetGraph(*sf,nroots,nleaves,&ilocal,&iremote);if (*ierr) return;
>> *ierr = F90Array1dCreate((void*)ilocal,MPIU_INT,1,*nleaves, ailocal PETSC_F90_2PTR_PARAM(pilocal));
>> /* this creates a memory leak */
>> f90arraysfnodecreate_((PetscInt*)iremote,nleaves, airemote PETSC_F90_2PTR_PARAM(piremote));
>> }
>>
>> PetscErrorCode F90Array1dCreate(void *array,MPI_Datatype type,PetscInt start,PetscInt len,F90Array1d *ptr PETSC_F90_2PTR_PROTO(ptrd))
>> {
>> PetscFunctionBegin;
>> if (type == MPIU_SCALAR) {
>> if (!len) array = PETSC_NULL_SCALAR_Fortran;
>> f90array1dcreatescalar_(array,&start,&len,ptr PETSC_F90_2PTR_PARAM(ptrd));
>> } else if (type == MPIU_REAL) {
>> if (!len) array = PETSC_NULL_REAL_Fortran;
>> f90array1dcreatereal_(array,&start,&len,ptr PETSC_F90_2PTR_PARAM(ptrd));
>> } else if (type == MPIU_INT) {
>> if (!len) array = PETSC_NULL_INTEGER_Fortran;
>> f90array1dcreateint_(array,&start,&len,ptr PETSC_F90_2PTR_PARAM(ptrd));
>> } else if (type == MPIU_FORTRANADDR) {
>> f90array1dcreatefortranaddr_(array,&start,&len,ptr PETSC_F90_2PTR_PARAM(ptrd));
>> } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported MPI_Datatype");
>> PetscFunctionReturn(0);
>> }
>>
>> It blindly calls F90Array1dCreate() with a len = *nleaves > 0 but a NULL array ilocal then f90array1dcreateint_ must create an invalid object.
>>
>> Barry
>>
>>
>>
>> > On Nov 19, 2021, at 12:34 AM, 袁煕 <yuanxi at advancesoft.jp <mailto:yuanxi at advancesoft.jp>> wrote:
>> >
>> > Dear PETSc-team,
>> >
>> > I am using function PetscSFGetGraph in my program like
>> > -----------------------
>> > call PetscSFGetGraph(sf,gnroots,gnleaves,gmine,gremote,ierr)
>> > -----------------------
>> >
>> > In some cases, it works well. But in some cases, I encountered following error
>> > ------------------
>> > PETSC ERROR: Caught signal number 11 SEGV: Segmentation Violation, probably memory access out of range
>> > ------------------
>> > 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.
>> > 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"
>> > 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.
>> > Is there any means to check the NULL pointer in Fortran in above cases.
>> > Many thanks,
>> > Yuan
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20211126/348a5cbf/attachment.html>
More information about the petsc-users
mailing list