Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

[RESOLVED] Pointer created with c_f_pointer gives segfault when accessed

Heath_J_
Beginner
1,263 Views

Hello,

I am having a problem with a code that is trying to use c_f_pointer.  It has a C routine which gets shared memory space and returns the C pointer to Fortran which creates a Fortran pointer to the space.  A code snippet looks like this:

      SUBROUTINE inshm_SegmentPtr_XXXXX_REAL4( handle, data_ptr, idims, iverb, ier )
      INTEGER(KIND=4), INTENT(IN) :: handle
      REAL   (KIND=4), DIMENSION(:,:,:,:,:), POINTER, INTENT(OUT) :: data_ptr
      INTEGER(KIND=4), dimension(5), INTENT(IN) :: idims
      INTEGER(KIND=4), INTENT(IN) :: iverb
      INTEGER(KIND=4), INTENT(OUT) :: ier
      TYPE(C_PTR) :: mem_ptr

      mem_ptr = inshm_SegmentPointer( handle, iverb )
      ier = 0
      call c_f_pointer(mem_ptr, data_ptr, idims)
      if( .NOT.associated( data_ptr ) ) ier = 1

      write(6,*) '-- Assigned pointer, rank= ',idims
      write(6,*) '   size   data_ptr = ',size(data_ptr)
      write(6,*) '   lbound data_ptr = ',lbound(data_ptr)
      write(6,*) '   ubound data_ptr = ',ubound(data_ptr)

      data_ptr= 0.0
      write(6,*) '-- Initialized data in pointer'

      END SUBROUTINE inshm_SegmentPtr_XXXXX_REAL4

When it runs, 

I get:

 [inSHM] global            0  created handle            0
 [inSHM] global            0  got seg. id    996180052
 [inSHM] global            0  has handle            0
    size   data_ptr =      3461472
    lbound data_ptr =            1           1           1           1           1
    ubound data_ptr =           12         101          56           1          51

Thread 1 "us3d-general.bi" received signal SIGSEGV, Segmentation fault.
0x00001555375eaab1 in fpv::fpvinit (ier=0) at ../../module/FPV.f:426
426              EVMq = 0.0

Fortran thinks the pointer is associated, it knows the size and upper and lower bounds.  However, as soon as I try to access the memory (initializing data_ptr= 0.0) I get a segmentation fault.

The same code works as expected without errors in GCC and PGI compilers, but it dies with Intel.  I am not sure if it is something the routines are doing incorrectly or if it is a compiler bug.  Help would be greatly appreciated.

A small working example which illustrates the problem can be found here:

https://github.com/nompelis/INshm.git

The fdemo1.f routine which is included works fine with other compilers but dies using Intel.  We have tried different Intel Fortran versions including the latest 2019 release.

0 Kudos
3 Replies
jimdempseyatthecove
Honored Contributor III
1,263 Views

It may help to print out the hex value of mem_ptr after it is initialized. Compare this with the base address of the array as known by C.

*** Note, this base address must be a contiguous blob of data representing your multi-dimensional C array. IOW not the address of a typical array of pointers to dis-contiguous memory. Example relating to the "I get" information:

float* Blob = new float[12*101*56*1*51];
// now create the pointer tables within the blob...

Jim Dempsey

0 Kudos
Heath_J_
Beginner
1,263 Views

The mem_ptr is a contiguous chunk of memory, not a pointer to an array of pointers.  I think we are missing something with how mem_ptr is used or interpreted.

0 Kudos
Heath_J_
Beginner
1,263 Views

Update: by modifying the Fortran to use proper bind(C) interfaces, it works now as expected in all compilers.  Resolved.

0 Kudos
Reply