Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
This community is designed for sharing of public information. Please do not share Intel or third-party confidential information here.
27241 Discussions

function with assumed-shape dummy argument

DataScientist
Valued Contributor I
188 Views

I recently decided to modernize some existing (not so) old code that passed the size of arrays explicitly to assumed-shape dummy arguments. The following is example of such modernized code:

module test_mod

    use iso_fortran_env, only: RK => real64, IK => int32
    implicit none
    
    real(RK) :: LOGTINY = log(tiny(1._RK))
    
contains

    pure function getCumSumNormedExp(LogValue, maxLogValue) result(CumSumExp)
        real(RK)    , intent(in), contiguous    :: LogValue(:)
        real(RK)    , intent(in)                :: maxLogValue
        real(RK)                                :: CumSumExp(size(LogValue))
        real(RK)                                :: invCumSum
        integer(IK)                             :: i
        integer(IK)                             :: lenLogValue
        lenLogValue = size(LogValue)
        CumSumExp(1) = LogValue(1) - maxLogValue
        if (CumSumExp(1) < LOGTINY) then
            CumSumExp(1) = 0._RK
        else
            CumSumExp(1) = exp(CumSumExp(1))
        end if
        do i = 2, lenLogValue
            CumSumExp(i) = LogValue(i) - maxLogValue
            if (CumSumExp(i) < LOGTINY) then
                CumSumExp(i) = CumSumExp(i-1)
            else
                CumSumExp(i) = CumSumExp(i-1) + exp(CumSumExp(i))
            end if
        end do
        invCumSum = 1._RK / CumSumExp(lenLogValue)
        CumSumExp = CumSumExp * invCumSum
    end function getCumSumNormedExp

end module test_mod

program test
    use test_mod
    integer(IK) :: i
    real(RK), allocatable :: CumSumNormedExp(:)
    CumSumNormedExp = [(real(i,RK), i = 1, 20)]
    write(*,*) getCumSumNormedExp(CumSumNormedExp(1:5), maxval(CumSumNormedExp,dim=1))
end program test

 

However, after the change of the dummy argument LogValue to a CONTIGUOUS assumed-shape array, ifort generated code that crashed at runtime with the following error:

malloc(): corrupted top size

or

invalid pointer

 

When I change the function output to an allocatable array., the problem is resolved. But the above code seems to be standard-conforming. So, unless I am wrong, this could be a bug in the ifort classic compiler (2021). That said, the above simple test case, compiles and runs totally fine. But the real usecase of this function is several thousands of lines of code and requires a sophisticated build process and cannot be pasted here. GFortran compiles and runs the same code ifort cannot. Thanks for any insights into what might be going wrong with the compiler or the codebase.

 

 

0 Kudos
1 Reply
FortranFan
Honored Contributor II
177 Views

@DataScientist ,

Please note you didn't provide the steps to reproduce the run-time error.  The code as shown with `ifort /On /standard-semantics' (n=0 thru' 3) runs ok.  So the run-time exception likely depends on your combination of compiler options and as such, it is worthy of a support request at Intel OSC if you are able to make it there or for attention by Intel staff here.

Reply