fortrancontiguousfujitsu-fortran

Passing non-contiguous argument to contiguous dummy array in a Fortran procecdue


Recently I encountered unexpected behavior of Fujitsu Fortran Version 2.0.0 while dealing with a subroutine which has dummy array with the contiguous attribute.

I have reduced the problem to a simple example which is here:

program test
  INTEGER, DIMENSION(:,:), ALLOCATABLE :: t
  INTEGER :: i, j
  ALLOCATE(t(3,3))
  DO i = 1, 4
   DO j = 1, 4
     t(i,j) = i*j
     !!PRINT *, t(i,j)
   END DO
  END DO
  CALL fun(t(2,1:4:2))
  DEALLOCATE(t)

  CONTAINS
    SUBROUTINE fun(arg)
      ! Contiguous dummy argument arg
      INTEGER, CONTIGUOUS :: arg(:)
      PRINT *, arg(2)
    END SUBROUTINE
end program test

That pice of code can be successfully compiled by gfortran (GNU Fortran (GCC) 6.3.0) but fails on cluster with Fujitsu Fortran compiler (mentioned above) giving the following error code:

"test_contiguous.f90", line 13: The actual argument number 1 of procedure 'fun' corresponding to a dummy argument 'arg' with the CONTIGUOUS attribute must be contiguous.

I am confused because as I understood compiler should make a contiguous temporary at the entrance of the subroutine (as is shown for example here: Fortran 2008 contiguous)

In fact I have two questions:

  1. What is specification for such situation?
  2. Can it be a flag for compiler forcing him to create a contiguous temporary for such case?

I am trying to build third-party software and cannot change the source as I want.


Solution

  • The Fortran 2008 specification says (5.3.7) of assumed-shape dummy arguments with the contiguous attribute:

    The CONTIGUOUS attribute specifies that an assumed-shape array can only be argument associated with a contiguous effective argument, or that an array pointer can only be pointer associated with a contiguous target.

    The Fortran 2018 specification, on the other hand, says (8.5.7):

    The CONTIGUOUS attribute specifes that an assumed-shape array is contiguous, that an array pointer can only be pointer associated with a contiguous target, or that an assumed-rank dummy data object is contiguous.

    As can be seen, one restricts the effective argument (in this case t(2,1:4:2) of the main program) and the other does not.

    It should be noted, though, that the restriction here is a restriction on the program which the compiler is not required to detect/enforce. The compiler is free to accept, making a temporary copy, a non-contiguous argument even in Fortran 2008 mode. In neither case must the compiler make a temporary copy.


    As to whether a specific compiler has options to relax its assumptions, I can't say in this case: I have no access to this one to test. I'm happy for someone to edit into this answer such an option should one be found.