Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29285 Discussions

pointer and allocatable operation failure

avidard
Beginner
765 Views
Hi,
I am having a segmentation faul when running the following program :

[fortran]PROGRAM test
   TYPE mytype
      INTEGER ::  i_i, i_j, i_k
      REAL(KIND=8), POINTER, DIMENSION(:,:,:) :: &
	 & vmod, &
	 & rmsk
   END TYPE mytype
   INTEGER, PARAMETER :: kpi=360, kpj=180, kpk=50
   TYPE(mytype) :: ptype
   REAL(KIND=8), ALLOCATABLE, DIMENSION (:,:,:) :: ztab
   

   ptype%i_i = kpi
   ptype%i_j = kpj
   ptype%i_k = kpk
   ALLOCATE ( ptype%rmsk(   kpi, kpj, kpk ))
   ALLOCATE ( ptype%vmod(   kpi, kpj, kpk ))
   ALLOCATE ( ztab( kpi, kpj, kpk ) )

   DO k = 1, kpk
      DO j = 1, kpj
         DO i = 1, kpi
            ztab(i,j,k) = i*j*k
            ptype%rmsk(i,j,k) = i+j+k
         END DO
      END DO
   END DO
   
   ptype%vmod(:,:,:) = ztab(:,:,:) + ptype%rmsk(:,:,:)

   PRINT*,MINVAL(ptype%vmod),MAXVAL(ptype%vmod),SUM(ptype%vmod)

   DEALLOCATE (ptype%rmsk, ptype%vmod,ztab)

END PROGRAM test[/fortran]

it fails when doing the ptype%vmod(:,:,:) = ztab(:,:,:) + ptype%rmsk(:,:,:). If I do the loops explicitely then it works. If I define ptype's arrays as allocatable instead of pointer it works as well, but then AFAIK it won't be fortran95-standard anymore. I encounter this problem both on linux and OSX using the 11.1.088 version of ifort.
with other compilers such as gfortran or g95, this example works fine.

Am I doing something dodgy or is it a problem with the compiler ?

thanks,

Arthur
0 Kudos
2 Replies
avidard
Beginner
765 Views
Hi again,

If I reduce the size of the arrays, for instance using kpi=36, kpj=18, kpk=5, it does not fail anymore
0 Kudos
Steven_L_Intel1
Employee
765 Views
Two options:

1. Change the declarations of vmod and rmsk to ALLOCATABLE rather than POINTER
2. Add -heap-arrays to the compilation switches.

The problem is that in the assignment to ptype%vmod, the compiler can't tell, since vmod and rmsk are pointers, whether they overlap, so it creates a temporary array for the expression. By default, ifort creates this on the stack and you are getting a stack overflow. Adding -heap-arrays allocates the temp on the heap, which I think is what the other compiler does, and you avoid the error, though a copy is always bad.

By making these components ALLOCATABLE, which is supported in all reasonable Fortran compilers today, the compiler knows there is no overlap and avoids making a temp. This would be my preference.
0 Kudos
Reply