4

I kept getting the warning message:

forrtl: warning (402): fort: (1): In call to I/O Read routine, an array temporary was created for argument #1

when I run the following codes. I had rummaged through old posts about the same issue on the forum (see here) but I was not quite sure the answer is applicable to my problem at hand. Could anyone help me get rid of this warning message? Thanks.

program main
    implicit none
    integer :: ndim, nrow, ncol
    real, dimension(:,:), allocatable :: mat
    ndim = 13
    ncol = 1
    allocate( mat(ncol,ndim))
    call read_mat( mat, 'matrix.txt' )    
contains
    subroutine read_mat( mat, parafile)
    implicit none
    real, dimension(:,:), intent(out) :: mat
    character(len=*), intent(in) :: parafile
    character(len=500) :: par
    character(len=80) :: fname1
    integer :: n, m, pcnt, iostat
    m = size(mat,dim=2)
    n = size(mat,dim=1)
    mat = 0.
    open( unit= 10, file=parafile, status='old', action='read', iostat=iostat )
    if( iostat==0 )then
        pcnt = 1
        write(fname1,'(a,i3,a)'),'(',m,'(f10.5))' 
        do 
            read( unit=10, fmt=*, iostat=iostat ) mat(pcnt,:)
            pcnt = pcnt + 1
            if( pcnt > n ) exit 
        enddo
    else
        print*, 'The file does not exist.'
    endif
    close( 10 )
    end subroutine read_mat 
end
Community
  • 1
  • 1
Li-Pin Juan
  • 1,156
  • 1
  • 13
  • 22
  • 1
    another,possibly preferred, fix to this is to reverse the order of the array. That obviously depends on how extensive the code is and how you use it elsewhere. – agentp Jan 03 '15 at 13:48

1 Answers1

5

It is the same underlying cause as in the linked question. The reference mat(pcnt,:) refers to array element storage that is not contiguous in memory, so to satisfy the compiler's internal requirements for the runtime code that actually does the read, it sets aside some storage that is contiguous, reads into that, then copies the elements out of that temporary storage into the final array.

Usually, in the context of a relatively slow IO statement, the overhead associated with the temporary is not significant.

The easiest way to prevent the warning is to disable the relevant runtime diagnostic with -check:noarg_temp_created or similar. But that is a bit of a sledgehammer — argument temporaries may be created that you do want to know about.

An alternative work around is to explicitly designate the elements to be read using an io-implied-do. Something like:

read (unit=10, fmt=*, iostat=iostat) (mat(pcnt,i),i=1,m)

with an appropriate declaration of i.

Alexander Pozdneev
  • 1,289
  • 1
  • 13
  • 31
IanH
  • 21,026
  • 2
  • 37
  • 59