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

Unexpected temporary array created when using n**2 instead of n*n to declare array size

Chenfeng_B_
Beginner
232 Views

I found out this unexpected behavior of temporary array when tracing down the root cause of a stack overflow.

In a subroutine, when multi-dimensional dummy arrays with explicit size are declared with the exponentiation operator ** (except in the last dimension), almost any operation in this subroutine that involves section of this array would create a temporary array, even if the section is obviously contiguous ( extreme case being x(:,:) ). Such temporary arrays are not created if the array size is declared using multiplication (n*n instead of n^2, or of higher power)

The following is an example:

program main
    implicit none
    real(8) x(4,2)
    
    call sub(x,2)
end program main

subroutine sub(x,n)
    implicit none
    integer,intent(in) :: n
    real(8),intent(in) :: x(n**2,2)   ! change to x(n*n,2) and everything's fine
    
    print*, x(:,:)      ! obviously contiguous, but a temporary array is created nonetheless.
end subroutine sub

Is this behavior a compiler bug or is there some deeper reason to this that I'm not aware of? Help appreciated.

I checked temporary array creation using the -check arg_temp_created compiler option. I am using  ifort version 16.0.0, on a UNIX system computing cluster at my institution.

0 Kudos
1 Reply
Kevin_D_Intel
Employee
232 Views

Thank you for reporting this. It does seem to be a bug. I reported it to the Developers for further analysis. I will share what I hear back.

(Internal tracking id: DPD200376216)

0 Kudos
Reply