Quantcast
Channel: Intel® Fortran Compiler for Linux* and macOS*
Viewing all articles
Browse latest Browse all 2583

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

$
0
0

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.


Viewing all articles
Browse latest Browse all 2583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>