1

Consider an old Fortran program that's organised like this:

      program main
      implicit none
      integer rr_sz
      parameter(rr_sz=10)
      real,dimension(:),allocatable :: rr
      allocate(rr(rr_sz))
      call sub1(rr_sz/2,rr)
      print *, rr_sz, rr
      deallocate(rr)
      end

      subroutine sub1(cc_sz,cc)
      implicit none
      integer cc_sz,min_cc_sz,i
      complex cc
      dimension cc(cc_sz)
c here we can determine what array size is really required
      min_cc_sz = 6
      if (cc_sz.lt.min_cc_sz)
     & error stop
        do i=1,min_cc_sz
      cc(i) = complex(i,i)
        end do
      end

I would like to put the rr array allocation into sub1, so that when rr needs more space than the hardcoded parameter rr_sz, the program can continue if the system have enough RAM.

It seems like doing so requires an explicit interface for sub1, which makes gfortran fail with a Type mismatch in argument ‘cc’: REAL(4) to COMPLEX(4).

How can I get around this problem with the least possible modifications to the original code?


EDIT

Here's what I tried to do to make the allocation work, that leads to other problems being exposed:

      program main
      implicit none
      integer rr_sz
      real,dimension(:),allocatable :: rr
      call sub1(rr_sz,rr)
      print *, rr_sz, rr
      deallocate(rr)

      contains

      subroutine sub1(rr_sz,cc)
      implicit none
      integer rr_sz,cc_sz,i
      complex,dimension(:),allocatable :: cc
c here we determine what array size is required
      cc_sz = 6
      allocate(cc(cc_sz))
      rr_sz = cc_sz*2
        do i=1,cc_sz
      cc(i) = complex(i,i)
        end do
      end

      end
# gfortran -fallow-argument-mismatch main.for
main.for:5:72:

    5 |       call sub1(rr_sz,rr)
      |                                                                        1
Error: Type mismatch in argument ‘cc’ at (1); passed REAL(4) to COMPLEX(4)

For the past two days I've been reading a lot of discussions about how REAL to COMPLEX casting is "broken", but at the same time people acknowledge that it's expected to work because the standard explicitly states that a COMPLEX shall be stored as two contiguous REALs.

14
  • 1
    If you want a dummy argument to be allocatable you'll need the procedure to have an explicit interface where it's referenced. Explicit interface or not, though, you aren't allowed to associate a real actual argument with a complex dummy argument. Which of those two entirely unrelated problems do you want us to address? Commented Apr 20, 2024 at 11:11
  • 1
    That is, the code in the question before you try to modify is broken, and the compiler is telling you how it is broken. You'll need to fix that aspect either way, but do you know how to? Commented Apr 20, 2024 at 11:17
  • 1
    I say the code is broken because it is broken. FFTs are a reality in Fortran old and new, but it's still the case that there are standard conforming ways to work with casting real and complex that work now rather than waiting until 2030. If you're wanting to move to dynamic allocation inside the subroutine then it's a great time to address hacks that made life easier 40 years ago but are unsafe and no longer considered best practice. (Explicit interfaces are good, don't throw them away just to save 200 bytes on a temporary copy.) Commented Apr 20, 2024 at 11:49
  • 1
    @GhorbanM.Tavakoly, -fallow-argument-mismatch (which I'm guessing is the part of -std=legacy you're interested in) does not allow argument mismatch when an explicit interface is involved. An explicit interface being required for the allocatable dummy argument makes this approach unuseful. Commented Apr 20, 2024 at 12:23
  • 1
    @francescalus In my domain casting between real and complex aims at saving Gbytes, not 200 bytes. And it's not only about the amount of occupied RAM but also about saturating the memory bandwidth with data duplications. The tricks to associate real and complex variables are here to stay, and there are so many codes that rely on them that no compiler vendor will break that. IMO it should even be standardized, as proposed here. Commented Apr 21, 2024 at 21:03

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.