Does the Intel Fortran 95 compiler allow module arrays to be of non-constant size?

六眼飞鱼酱① 提交于 2019-12-10 20:18:45

问题


I have downloaded a Fortran 90/95 adaptive mesh refinment library (Paramesh), and now I'm trying to compile an example program that came with it. In the process I modified the Makefile to use gfortran instead of the Intel Fortran compiler.

In the library code, there is a module containing this snippet:

       module physicaldata

! Many many lines of variable definitions here
!....
       Public :: nfluxvar
       Integer,Save :: nfluxvar

! Many many lines of variable definitions here
!....

       end module physicaldata

Elsewhere there is

       module flux_assign
        use physicaldata
        integer :: iflux_target(nfluxvar)
       end module flux_assign

which is causing this error:

advance_soln_vdt.F90:16.40:
    Included at amr_main_prog.F90:29:

       integer :: iflux_target(nfluxvar)
                                    1
Error: The module or main program array 'iflux_target' at (1) must have constant shape

Would that code work if compiled using another compiler? I know that with standard Fortran, or at least the one used by gfortran, requires that integer variables which are used to denote array sizes should have the parameter keyword attached to them. Is that not the case for other Fortran compilers? Do other compiler include non-standard features such as this?


回答1:


Current Intel Fortran issues an error for this code.

The standard language requires that non-allocatable, non-pointer arrays declared in the specification part of a module (or main program or block data or submodule, and arrays used in a few other places) must have constant array bounds.

iflux_target is such an array.

A program with such an array is non-conforming, and will not be accepted without diagnostics by a standard conforming Fortran processor. If portability is your goal, then do not use this sort of feature. Lack of a diagnostic from previous versions of Intel Fortran was presumably an oversight.

Module arrays that need to have their size specified by a variable should be made allocatable, with the array allocated in an "initialise" procedure or similar before the operations proper provided by the module are used.




回答2:


The integer used to specify a statically allocated array cannot have the save attribute in fortran as this implies it will change during the run (otherwise why use save). As the array is statically allocated, its bounds cannot change. Check out this answer for more details.

This flags the following error on the intel compiler too,

An automatic object must not appear in the specification part of a module

Note that specifying the value of nfluxvar, e.g.

integer :: nfluxvar=5

does not mean you can use it to define an array size unless you explicitly tell the compiler that it's a parameter.

There is no need to use a parameter statement for the array size nfluxvar if you use a dynamically allocated array. If you want to avoid this problem, using dynamic allocation is the best solution as it explicitly sets the array size to the current value of nfluxvar. You can even reallocate if this changes, for example,

module physicaldata

    ! Many many lines of variable definitions here
    !....
    Public :: nfluxvar
    Integer,Save :: nfluxvar

    ! Many many lines of variable definitions here
    !....

end module physicaldata




module flux_assign
    use physicaldata

    integer, allocatable, dimension(:) :: iflux_target

end module flux_assign


program main
    use flux_assign

    if (.not. allocated(iflux_target)) then
        allocate(iflux_target(nfluxvar))
    elseif (size(iflux_target) .ne. nfluxvar) then
        deallocate(iflux_target)
        allocate(iflux_target(nfluxvar)) 
    endif


end program main



回答3:


In a way this isn't truly an answer. However, any compliant Fortran compiler must have the ability to detect and report violations of specified constraints within the Fortran language specification. The constraint you come up against in the code is indeed one of those. So, does there exist a Fortran compiler which has the ability to detect this but chooses not to? I don't know. But I'd think not.

So, what is the constraint? Unless nfluxvar is a constant expression iflux_target will be an automatic object. Such an automatic object is not allowed in the scoping unit of a module - see C554 and surrounding text in Fortran 2008.

To answer the question in the title: ifort will complain loudly about such attempts.



来源:https://stackoverflow.com/questions/31256569/does-the-intel-fortran-95-compiler-allow-module-arrays-to-be-of-non-constant-siz

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!