警告消息(402):为参数创建的临时数组

时间:2015-01-03 05:02:41

标签: io fortran format fortran90 intel-fortran

我一直收到警告信息:

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

当我运行以下代码时。我在论坛上翻阅了关于同一问题的旧帖子(见here),但我不太确定答案是否适用于我手边的问题。任何人都可以帮我摆脱这个警告信息吗?谢谢。

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

1 个答案:

答案 0 :(得分:3)

这与链接问题中的基本原因相同。引用mat(pcnt,:)是指在内存中不连续的数组元素存储,因此为了满足编译器对实际执行读取的运行时代码的内部要求,它会留出一些连续的存储,读入,然后将该临时存储中的元素复制到最终数组中。

通常,在相对较慢的IO语句的上下文中,与临时关联的开销并不重要。

阻止警告的最简单方法是使用-check:noarg_temp_created或类似功能禁用相关的运行时诊断。但这是一个大锤 - 可能会创建你想知道的临时工具。

另一种解决方法是使用io-implied-do明确指定要读取的元素。类似的东西:

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

适当声明i