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

Changing order of columns in a matrix

Robin_T_
Novice
1,336 Views

When I use the program

program subscript_array_test

  IMPLICIT NONE

  integer, allocatable :: mat (:, :)
  INTEGER :: loop
  
  allocate (mat (5, 5))
  mat = reshape ([(loop, loop = 1, 25)], [5, 5])

print *, mat  
  
print *, [mat([3, 1, 5, 4, 2], 1 : 5)]

  mat = mat([3, 1, 5, 4, 2], 1 : 5)
  
print *, mat  
  if (allocated (mat)) deallocate (mat)
        
end program subscript_array_test

with compiler options

ifort -m64 -O0 -g -traceback -check all -warn all -debug all -ftrapuv -stand f18 -c subscript_array_test.f90	
ifort subscript_array_test.o -m64 -O0 -g -traceback -check all -warn all -debug all -ftrapuv -stand f18 -o subscript_array_test.x

in version 19.0.4.243 of the ifort compiler, I get the result

           1           2           3           4           5           6
           7           8           9          10          11          12
          13          14          15          16          17          18
          19          20          21          22          23          24
          25
           3           1           5           4           2           8
           6          10           9           7          13          11
          15          14          12          18          16          20
          19          17          23          21          25          24
          22
           3           1           5           4           2  -858993460
  -858993460  -858993460  -858993460  -858993460  -858993460  -858993460
  -858993460  -858993460  -858993460  -858993460  -858993460  -858993460
  -858993460  -858993460  -858993460  -858993460  -858993460  -858993460
  -858993460

This is surprising to me, I would have expected the same output from the second and third print statement, or at least a warning. Is there an elegant way to change the order of columns in a matrix, or do I have to do it with a loop over all rows?

0 Kudos
9 Replies
jimdempseyatthecove
Honored Contributor III
1,336 Views

Try: mat = [mat([3, 1, 5, 4, 2], 1 : 5)]

Jim Dempsey

0 Kudos
JVanB
Valued Contributor II
1,336 Views

@jimdempseyatthecove: That can't work because you are trying to assign a 1-d expression to a 2-d array.

Both gfortran and ifort 16.0 get this right so the results as displayed are a regression.

BTW, the code given permutes rows, not columns.

 

0 Kudos
Robin_T_
Novice
1,336 Views

Repeat Offender wrote:

Both gfortran and ifort 16.0 get this right so the results as displayed are a regression.

Indeed, I remember this working as intended some time back. So is this a bug in ifort version 19?

0 Kudos
Johannes_Rieke
New Contributor III
1,336 Views

Hi, PSXE2019u3 and u5 (Windows) as well as gfortran 9.2 (Linux) print the expected results. I compiled with default compiler options. I never have installed u4, so I can't check. Mighbe only u4 has this regression? Have you tried to compile with default compiler options?

0 Kudos
Robin_T_
Novice
1,336 Views

johannes k. wrote:

Hi, PSXE2019u3 and u5 (Windows) as well as gfortran 9.2 (Linux) print the expected results. I compiled with default compiler options. I never have installed u4, so I can't check. Mighbe only u4 has this regression? Have you tried to compile with default compiler options?

I just checked.

The 

-check all

flag causes the unexpected behaviour.

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,336 Views

Oops, replace the enclosing []'s with ()'s

mat = (mat([3, 1, 5, 4, 2], 1 : 5))

This works...

... note, 2019u3 it also works without the ()'s, but the ()'s compile without error.

Jim Dempsey

 

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,336 Views

*** adding -check all causes the error to show up even with the ()'s

Jim Dempsey

0 Kudos
Robin_T_
Novice
1,336 Views

jimdempseyatthecove (Blackbelt) wrote:

Oops, replace the enclosing []'s with ()'s

mat = (mat([3, 1, 5, 4, 2], 1 : 5))

This works...

... note, 2019u3 it also works without the ()'s, but the ()'s compile without error.

Jim Dempsey

This is now valid code, but it still outputs the same as in the original post.

The issue here lies in the -check all compiler flag

0 Kudos
Johannes_Rieke
New Contributor III
1,336 Views

Strange that this compiler flag corrupts the results. In PSXE2019u5 the bug is still present. PSXE2017u6 and PSXE2018u3 works fine. The regression seems to be introduced with PSXE2019 family.

0 Kudos
Reply