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.
29286 Discussions

size of allocatables in derived types

Lars_Jakobsen
Beginner
565 Views
Hi,

I have a question conserning the use of allocatables in derived types.

I have made this test case:

 

[fortran]program Dynamic_memory
  implicit none

  !-- Type1
  type :: T_myType1
    integer, dimension(:), allocatable :: myIntAll
  end type

  type(T_myType1)                        :: myType1_1 !  1 pointer member
  type(T_myType1), dimension(10)         :: myType1_2 ! 10 pointer members
  
!-- Type2  
  type :: T_myType2
    integer, dimension(1)  :: myIntAll
  end type

  type(T_myType2), dimension(10)  :: myType2_2

  integer         :: mem
  
  integer, dimension(:), allocatable :: myAlloc
  
!-- body  
  write(*,*) 'Welcome to Dynamic_memory 101'
  mem = sizeof(myType1_1)
  write(*,*) 'The sizeof myType1_1 is currently: ', mem
  mem = sizeof(myType1_2)
  write(*,*) 'The sizeof myType1_2 is currently: ', mem
  mem = sizeof(myType2_2)
  write(*,*) 'The sizeof myType2_2 is currently: ', mem  
  read(*,*)

  end program[/fortran]

 The output of this is:
 Welcome to Dynamic_memory 101
 The sizeof myType1_1 is currently:           36
 The sizeof myType1_2 is currently:          360
 The sizeof myType2_2 is currently:           40

Hence myType1_1 uses 36 bytes of memory without having allocated any room for myIntAll whereas myType2_2 only uses 40 bytes corresponding to the size of the 10 integers that I have made room for (I am using integer = integer(4)).

My originally thought was that I could save some memory by making the array myIntAll allocatable, however there seem to be some overhead involved in using allocatables in derived types that are not present with fixed size arrays. Is it possible to calculate this overhead or am I doing something wrong here?

Regards

Lars

0 Kudos
3 Replies
Steven_L_Intel1
Employee
565 Views
When you have deferred-shape arrays in a derived type, whether they are POINTER or ALLOCATABLE, what gets put in the derived type is the descriptor for the array.  This is of variable size depending on the number of dimensions.  An array of these would have an array of descriptors. Typically you would use allocatable arrays here when you don't know in advance the size of the arrays you need.  If you do know, then by all means make them fixed-size.
0 Kudos
Lars_Jakobsen
Beginner
565 Views
Thank you Steve.

In my case the array that I would put into the derived type would sometimes have size 2 and other times size 4 and in rare instances size 9. If I have to declare them as fixed-size I would have to declare them all as size 9 even though I only rarely need 9 entries.

The size of the descriptor according to the help file is (on IA-32) (6+3*x)*4 bytes, where x is the dimension of the array in the above example this equal 36 bytes.

But if the descriptor uses 36 bytes of memory, then there is no benefit in using deferred-shape arrays.

Regards

Lars

0 Kudos
Steven_L_Intel1
Employee
565 Views
Certainly no benefit for fixed-size arrays where the maximum size is 40 bytes or less.
0 Kudos
Reply