Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28996 Discussions

Should an assumed shape argument cause a stack overflow in read?

Mark_Lewy
Valued Contributor I
668 Views

Consider the following program:

[fortran]

module io_mod
    implicit none
contains
    subroutine read_vals_assumed_shape(vals, ios)
        character(*), intent(inout), contiguous :: vals(:)
        integer, intent(out) :: ios

        read(1, pos = 1, iostat = ios) vals
    end subroutine

    subroutine read_vals_assumed_size(vals, size_vals, ios)
        character(*), intent(inout) :: vals(*)
        integer, intent(in) :: size_vals
        integer, intent(out) :: ios

        read(1, pos = 1, iostat = ios) vals(:size_vals)
    end subroutine
end module
    
program Console24
    use io_mod

    implicit none

    character(64), allocatable :: vals(:)
    integer :: ios

    allocate (vals(300000))
    vals = ' '

    open(1, file = 'test.out', access='stream', form = 'unformatted')
    write(1, pos = 1, iostat = ios) vals
    close(1)

    open(1, file = 'test.out', access='stream', form = 'unformatted')
    ! The following three statements should do the same thing
    read(1, pos = 1, iostat = ios) vals
    call read_vals_assumed_size(vals, size(vals), ios)
    call read_vals_assumed_shape(vals, ios)
    close(1)
end program Console24

[/fortran]

This crashes on line 8 with a stack overflow in recent releases of the compiler (I've tried this with XE 2013 update 1 and XE 2013 SP1 update 1).

Workarounds:

1) Use the "/heap_arrays:0" option.

2) Use an assumed size argument.

3) Read the array in stack friendly chunks.

Or should the compiler be able to cope with this as is, given the contiguous attribute on vals (which seems to make no difference in this case)?

0 Kudos
4 Replies
Steven_L_Intel1
Employee
668 Views

I agree with you that the stack temporary should not have been created. especially as you added the CONTIGUOUS attribute. I will ask the developers to improve this. Issue ID is DPD200249510.

0 Kudos
Mark_Lewy
Valued Contributor I
668 Views

Thanks Steve,

Aside: the forum syntax highlighting strips out blank lines, so the stack overflow is on line 7 of the posted listing (read(1, pos = 1, iostat = ios) vals) in io_mod::read_vals_assumed_shape.

Mark

0 Kudos
Steven_L_Intel1
Employee
668 Views

Yeah, I figured that out...

0 Kudos
Steven_L_Intel1
Employee
668 Views

For a release later this year, we've fixed the compiler to use the CONTIGUOUS attribute to avoid creating stack temps.

0 Kudos
Reply