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

problem with automatic reallocation in statement "A = fun(A)"

riad_h_1
Beginner
409 Views

Dear all,

I am using ifort 19.0.5 for mac OS and I encountered a problem (that seems to be a bug) with statements of the type A = fun(A) where

- A is an allocatable array,

- the result of fun is of a shape (and/or a size) different from A,

like in statements A = reshape(A,..) or A = transpose(A).

I have not observed any problems with gfortran (9.2) and nagfor (6.2).

With ifort the array A is re-allocated to the right shape but its values are sometimes wrong. For example, with A = transpose(A) I always get wrong values.

This is not the case with A = reshape(A,...) which seems more insidious since the result is only sometimes wrong (the same test program run several times can give the right result or can give A = 0) 

My question is simply: is it really a bug? The option -assume realloc-lhs does not resolve this problem (if I understood this is now the default). 

Obviously I can use the workaround : tmp = f(A) ; call move_alloc (from=tmp, to=A) , but in the code I use, there is a significant number of this type of instructions (with fun intrinsic or user function) and therefore a lot of changes to be made...

Best regards,

Riad 

 

 

 

0 Kudos
6 Replies
Steve_Lionel
Honored Contributor III
409 Views

No example. Hmm..

Ok, let's try:

integer, allocatable :: a(:,:)

a = reshape ([1,2,3,4,5,6],[3,2])
a = transpose(a)
print *, shape(a)
print *, a


end

 

D:\Projects>ifort t.f90
Intel(R) Visual Fortran Intel(R) 64 Compiler for applications running on Intel(R) 64, Version 19.0.5.281 Build 20190815
Copyright (C) 1985-2019 Intel Corporation.  All rights reserved.

Microsoft (R) Incremental Linker Version 14.23.28107.0
Copyright (C) Microsoft Corporation.  All rights reserved.

-out:t.exe
-subsystem:console
t.obj

D:\Projects>t.exe
           2           3
           1           4           2           5           3           6

Looks ok to me. What do you see when you try this? If it works for you, please show us a complete test case that demonstrates the failure.

0 Kudos
riad_h_1
Beginner
409 Views

Hi Steve,

yes your example works. But when I change  a = reshape ([1,2,3,4,5,6],[3,2]) to a = reshape ([1,2,3,4,5,6],[2,3]) and I compile with -check all the result is not the expected one.

Here are two examples:

1) realloc_transpose.f90 (very similar to yours for which the problem arise when compiling with -check all)

! compilation: ifort -check all realloc_transpose.f90
! (if -check all is omited, the problem goes away!)
implicit none
integer :: mat(2,3) = reshape([1,4,2,5,3,6],[2,3]), trans_mat(3,2), i, j
integer, allocatable :: A(:,:)

trans_mat = transpose(mat)

A = mat
print*,'Let A:'
do i = 1, size(A,1) ; print*,(A(i,j),j=1,size(A,2)) ; end do

A = transpose(A)

if (any(A - trans_mat /= 0)) then
   print '(a   )','Error: A = transpose(A) does not give the expected result:'
   do i = 1, size(A,1) ;  print*,(A(i,j),j=1,size(A,2)) ; end do
end if

end

And here is the output:

 Let A:
           1           2           3
           4           5           6
Error: A = transpose(A) does not give the expected result:
           1           4
           2           5
           2           6

2) realloc_reshape.f90 where I run several times the same statements until an error is detected:

! compilation: ifort realloc_reshape.f90 

implicit none
integer, parameter   :: nrow = 2, ncol = 3, ntimes = 2000000
integer              :: mat(nrow,ncol), mat_resh(ncol,nrow)
integer, allocatable :: A(:,:)
integer              :: i, j, iter

mat = reshape([(i,i=1,nrow*ncol)],[nrow,ncol])
mat_resh = reshape(mat,[ncol,nrow])

do iter = 1, ntimes

   A = mat

!- Reshape A and put the result back into A (automatic re-allocation)
!  Sometimes the result is wrong (A=0) with ifort 19

   A = reshape(A,[ncol,nrow])

!- Stop if A /= mat_resh:

   if (any(A - mat_resh /= 0)) then
      print '(a,i0)','Error at iteration #',iter
      print*,'A = reshape(A,[ncol,nrow] gives:'
      do i = 1, size(A,1) ;  print*,(A(i,j),j=1,size(A,2)) ; end do
      print*,'which is not the expected result:'
      do i = 1, size(mat_resh,1) ;  print*,(mat_resh(i,j),j=1,size(mat_resh,2)) ; end do
      stop
   end if

   deallocate(A)
end do
print*,'Terminated with no error'

And here is the output:

Error at iteration #13722
 A = reshape(A,[ncol,nrow] gives:
           0           0
           0           0
           0           0
 which is not the expected result:
           1           4
           2           5
           3           6

Best regard,

Riad

0 Kudos
Ferdinand_T_
New Contributor II
409 Views
0 Kudos
Steve_Lionel
Honored Contributor III
409 Views

Thanks for providing a reproducer. I suggest you report it to Intel using the Online Service Center. And in the future, please provide test cases, no matter how simple you think the problem is!

0 Kudos
riad_h_1
Beginner
409 Views

Ferdinand T. wrote:

This may be related to the issues with -check shape reported earlier:
https://software.intel.com/en-us/forums/intel-fortran-compiler/topic/840198
https://software.intel.com/en-us/forums/intel-fortran-compiler/topic/831548

Thanks Ferdinand. Yes for the 1st case, it seems to be the same problem that appears only with the "check shape" option. 

However, the second case may be a different problem, as it occurs even without any options and is random. 

Best,

Riad

0 Kudos
riad_h_1
Beginner
409 Views

Steve Lionel (Ret.) (Blackbelt) wrote:

Thanks for providing a reproducer. I suggest you report it to Intel using the Online Service Center. And in the future, please provide test cases, no matter how simple you think the problem is!

Thank you Steve, I will report it. Best regards, Riad

0 Kudos
Reply