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

array subscript violation

Rabindran
Beginner
1,011 Views

Hello,

I’m getting an error, which is really strange and i don’t understand why.
I have attached a reduced version, so that only the relevant code lines are included. I have also attached a screenshot of the error message.


I’m using the intel fortran compiler: 12.1.4.325 Build 20120410 with the Microsoft visual studio 2010.
The compile flags: ifort /debug:full /Zi /Od /traceback /free /w /check:all MultivariateInterpolatorIDW.for

I have found some modifications, which don’t cause an error, but it makes no sense to me, that the attached one is not working. I hope someone can explain it to me. Do i violate a fortran specification without knowing? Thank you for reading.


Modification which are fine
delete the keyword: target
OR
Change
integer( kind=i18 ), intent(inout), target :: axisIntervalIds(:)  to
integer( kind=i18 ), intent(in), target :: axisIntervalIds(:)
OR
Change
integer( kind=i18 ), intent(in) :: axisDataPointIntervalId(:) to
integer( kind=i18 ), intent(in) :: axisDataPointIntervalId(12)
OR
Change the line to

weights( 1:12:2 ) = dataPerAxis( axisDataPointIntervalId( 1:12:2 ) - 1 + axisIntervalIds( 2:12:2 ) ) to

test(:) = axisDataPointIntervalId( 1:12:2 ) - 1 + axisIntervalIds( 2:12:2 )
weights( 1:12:2 ) = dataPerAxis(test)

 

bug.png
      module MultivariateInterpolatorIDW

        implicit none
        private

        integer, parameter :: r15 = SELECTED_REAL_KIND( 15, 307 )
        integer, parameter :: i18 = SELECTED_INT_KIND( 18 )

        public :: MVI_interpolate

      contains

        subroutine MVI_interpolate()

          real( kind=r15 ) :: interpolatedValueV(1)
! local
          real( kind=r15 ) :: dataPerAxis(12)
          integer( kind=i18 ) :: axisDataPointIntervalId(12)
          integer( kind=i18 ), target :: axisIntervalIds(12)


          dataPerAxis(:) = 10
          axisDataPointIntervalId( : ) = 1
          axisIntervalIds(:) = 1

            interpolatedValueV( 1 ) = MVI_interpolateSub( dataPerAxis, axisDataPointIntervalId, axisIntervalIds )

        end subroutine


        function MVI_interpolateSub( dataPerAxis, axisDataPointIntervalId, axisIntervalIds )

! in
          real( kind=r15 ), intent(in) :: dataPerAxis(12)
          integer( kind=i18 ), intent(in) :: axisDataPointIntervalId(:)
! inout
          integer( kind=i18 ), intent(inout), target :: axisIntervalIds(:)
! return
          real( kind=r15 ) :: MVI_interpolateSub
! local
          real( kind=r15 ) :: weights(12)

          weights( 1:12:2 ) = dataPerAxis( axisDataPointIntervalId( 1:12:2 ) - 1 + axisIntervalIds( 2:12:2 ) )

           MVI_interpolateSub = 0.0

        end function

      end module

      program main

        use MultivariateInterpolatorIDW

        implicit none

        call MVI_interpolate( )

      end program

 

0 Kudos
8 Replies
Roman1
New Contributor I
1,011 Views

I have tried compiling your program with Intel(R) Visual Fortran Compiler 18.0.2.185  and with  gfortran (5.4.0) .  In both cases it ran without crashing.

 

0 Kudos
John_Campbell
New Contributor II
1,011 Views

I interpreted the identified line as:

          weights( 1:12:2 ) = dataPerAxis( axisDataPointIntervalId( 1:12:2 ) - 1 + axisIntervalIds( 2:12:2 ) ) ! ###
          write (*,*) 'weights :',weights

          write (*,*) ' '
          write (*,*) 'weights :'
          do k = 1,12,2
             kk = axisDataPointIntervalId( k ) - 1 + axisIntervalIds( k+1 )
             weights(k) = dataPerAxis( kk ) ! ###
             write (*,*) k,kk, weights(k)
          end do

I may have it wrong, but is that what you intended ?

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,011 Views

The compiler may have a question as to how to handle the scalar (which may or may not be a bug). Try

weights( 1:12:2 ) = dataPerAxis( (axisDataPointIntervalId( 1:12:2 ) - 1) + axisIntervalIds( 2:12:2 ) )
or
weights( 1:12:2 ) = dataPerAxis( (axisDataPointIntervalId( 1:12:2 ) - 1_i18) + axisIntervalIds( 2:12:2 ) )

If you continue to get the error, add a diagnostic print in front of the above line

print *,axisDataPointIntervalId( 1:12:2 )
print *,axisIntervalIds( 2:12:2 )
print *,axisDataPointIntervalId( 1:12:2 ) - 1 + axisIntervalIds( 2:12:2 )

All three must produce a valid index into dataPerAxis (a value between 1:12, for use as an index into dataPerAxis)

Jim Dempsey

0 Kudos
Rabindran
Beginner
1,011 Views

Hello,

thanks for the replys.

@Roman: i have the feeling, it could be an bug with the older version. Unfortunately I have no access to a newer one, but I will try it out, if possible.

@John Campbell: You have identified the line correct. Your disassembled code is working, because it is the same way like storing the index into an array, before using. Actually it should make no difference from my point of view.

test(:) = axisDataPointIntervalId( 1:12:2 ) - 1 + axisIntervalIds( 2:12:2 )
weights( 1:12:2 ) = dataPerAxis(test)

@Jim Dempsey: I tried both methods, unfortunately they don’t work. The printed index is always “1” for every three print cases.

 

I have the feeling, it could be an bug. I added this line

          weights( 2:12:2 ) = dataPerAxis( axisDataPointIntervalId( 1:12:2 ) - 1 + axisIntervalIds( 1:12:2 ) )    

before the critical one. It doesn’t cause an error, but the following one, which is strange to me.

0 Kudos
andrew_4619
Honored Contributor II
1,011 Views

It is clearly a compiler bug. 12.x is very very old, the bug does not exist in current version!

I would not spend any time thinking about this!  Either use a newer compiler or apply one of the many workarounds you already know.

0 Kudos
Rabindran
Beginner
1,011 Views

thanks for the replys. i will try to get my hands on a newer version.

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,011 Views

Updating from V12.x may not be possible for youu.

In your sample code, the size of the array dataPerAxis is quite small, and can be indexed with standard INTEGER indexes. Your axisDataPointerIntervaID and axisIntervalIds are INTEGER(8) arrays. Some of the older compilers had issues with indexing an array with an integer type that was not the integer type used for allocation/declaration. With this in mind either:

a) Define axisDataPointerIntervaID and axisIntervalIds as INTEGER(4) arrays
or
b) Allocate/define dataPerAxis array using INTEGER(8) values (or literals)

Jim Dempsey

0 Kudos
Rabindran
Beginner
1,011 Views

@Jim Dempsey: thank you for you suggestion. Unfortunately it didn’t work. I will try my luck with a newer version.

Is is possible to close this thread?

0 Kudos
Reply