Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

Packing an allocatable derived type array gets messed up after deallocating the original array

kostas85
Beginner
551 Views

Hello All,

When an allocatable derived type array (say container) is packed (using the intrinsic pack function) then the content of the resulting array (say packed_container) change when deallocating the original array. To clarify, in the following program the array packed_container is printed before and after deallocating container. Please let me know if you can reproduce the result. Up to now I don't have any suggestion for a workaround I would  appreciate any help on this. Many thanks in advance.

Kostas

PS: The code and it'output follow, compiled by ifort 14.0.2.144

module my_storage_type

implicit none

type storage1
  integer :: id=0
  integer, dimension(:), allocatable :: lowkey
end type storage1
 
end module my_storage_type


program compress_test

use my_storage_type

implicit none

type(storage1), dimension(:), allocatable :: container, cont_he
integer :: i

! inits
 allocate(container(4)) 
 
 container(1)%id = 2
 allocate(container(1)%lowkey,source=(/1,2,3,4/))

 container(3)%id = 5
 allocate(container(3)%lowkey,source=(/5,6,7,8/))
! ------

allocate(cont_he,source=pack(container,container%id/=0))

print *, " output before deallocating container"
print *, cont_he

deallocate(container)

print *, " output after deallocating container"
print *, cont_he


end program compress_test

produces the output:

  output before deallocating container
           2           1           2           3           4           5
           5           6           7           8
  output after deallocating container
           2           0           0           3           4           5
    11551136           0           7           8

 

0 Kudos
4 Replies
kostas85
Beginner
551 Views

A Note

When I referred to a workaround I implied using the pack intrinsic. For example note that replacing the allocate(...,source=pack(...)) line of the code above with a "user" pack procedure such as:

allocate(cont_he(count(container%id/=0)))

cnt = 0
do i=1,size(container)
    
    if (container(i)%id/=0) then
    cnt = cnt + 1
    cont_he(cnt) = container(i)
    end if
    
end do

produces the correct result. I'm sorry, I forgot to mention this :)

0 Kudos
John4
Valued Contributor I
551 Views

Since the problem seems to be with the PACK intrinsic, I think your only workaround is not to use said intrinsic.  The code causes an ICE in gfortran 4.8, and gfortran 4.9+ doesn't seem to support SOURCE= in the allocate statement. sunf90 complains about the allocatable components in the PRINT statements, but after changing those to

print *, (cont_he(i)%id, cont_he(i)%lowkey, i = 1, SIZE(cont_he))

the code compiles just fine and produces the expected results:

...:~$ ll `which sunf90`
lrwxrwxrwx 1 root root 5 Mar 17 20:58 /opt/solarisstudio12.4/bin/sunf90 -> ./f90*
...:~$ sunf90 test_pack.f90 
...:~$ ./a.out 
  output before deallocating container
 2 1 2 3 4 5 5 6 7 8
  output after deallocating container
 2 1 2 3 4 5 5 6 7 8
...:~$ ll `which ifort`
-rwxr-xr-x 1 root root 4013312 Apr 25 08:19 /opt/intel/composer_xe_2013_sp1.3.174/bin/intel64/ifort*
...:~$ ifort test_pack.f90 
...:~$ ./a.out 
  output before deallocating container
           2           1           2           3           4           5
           5           6           7           8
  output after deallocating container
           2           0           0           3           4           5
    32641440           0           7           8


 



 

0 Kudos
kostas85
Beginner
551 Views

Thanks a lot John for your feedback! For now, I've coded the packing without the pack intrinsic. It's really interesting that both gfortran and ifort give the same error.

0 Kudos
Xiaoping_D_Intel
Employee
551 Views

It is a known issue in "PACK" and has been fixed in 15.0 beta compiler.

Thanks,

Xiaoping Duan

Intel Customer Support

0 Kudos
Reply