Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!
26747 Discussions

strange behavior while copying an array

Seungun_L_
Beginner
126 Views

Hi,

I'm experiencing strange behavior of the program while copying an array.

I made a simple test code to reproduce it.

program test

integer :: ni, nj, nk
integer, allocatable :: id(:)
real*8, allocatable :: a(:,:,:), b(:,:,:)

ni = 50
nj = 50
nk = 1

allocate(a(ni,nj,nk))
allocate(b(ni,nj,nk))
allocate(id(nk))

a = 1d0

do n = 1, nk
   id(n) = nk+1-n
enddo

call copy(a,b)

print*, sum(a), sum(b)

contains

subroutine copy(aa,bb)

real*8, intent(in)  :: aa(ni,nj,nk)
real*8, intent(out) :: bb(ni,nj,nk)

do n = 1, nk
   bb(:,:,id(n)) = aa(:,:,n)
enddo

end subroutine copy

end program test

I think the program should print out the sum value of 2500 for both array a and b.

However, the program prints out 2500 for array a, but 1250 for array b.

In this test, I used intel fortran compiler 2017 initial release.

Could you give me an advice for this problem?, or is there anything I'm doing wrong?

Thanks,

0 Kudos
5 Replies
jimdempseyatthecove
Black Belt
126 Views

You are addressing beyond the end of the arrays a and b.

the 3rd subscript of a and b have a dimension of 1. IOW dimensions are (50,50,1).

Your copy subroutine is specifying 3rd index values of 50:1 and 1:50.

I am surprised you did not crash your program.

Jim Dempsey

Seungun_L_
Beginner
126 Views

Hi.

The copy subroutine is specifying 3rd index value of 1, not 1 to 50.

The value of nk is just 1.

So, copy subroutine just copies aa(:,:,1) to bb(:,:,1).

Am I wrong?

jimdempseyatthecove
Black Belt
126 Views

You are wrong:

do n = 1, nk ! nk == 1
   bb(:,:,id(n)) = aa(:,:,n)
! n = 1  bb(1:50,1:50,50) = aa(1:50,1:50,1) ! bb out of allocation range
enddo

Edit *** my error, id(1) should contain nk-1+1=1

! n = 1  bb(1:50,1:50,1) = aa(1:50,1:50,1) ! bb inside of allocation range

mea culpa

Jim Dempsey

Seungun_L_
Beginner
126 Views

I don't understand you.

do n = 1, nk
   bb(:,:,id(n)) = aa(:,:,n)
enddo

in the above code, nk is 1 and id(1) is 1.

Do loop will be executed just once, not 50 times as you mentioned.

So I think, the above code is same as below code.

bb(:,:,1) = aa(:,:,1)

Isn't it?

 

Juergen_R_R
Valued Contributor I
126 Views

Indeed the code is correct. I was first puzzled whether the subroutine copy knows the actual values of ni, nj, nk, but it actually does via host association. That seems to be a bug in the first ifort 17 release. The latest update (update 4, 17.0.4) works fine. I remember that there were a few issues with arrays in the ifort 17 initial release, at least a submitted some bug reports. As I said, the newest update works, as do gfortran 4.8., 4.9., 5.x, 6.x, 7.x, 8.x, nagfor v6 and PGF v17. 

Reply