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

Is any difference for type real and complex in memory?

志强_赵_
Beginner
521 Views

I write a program to allocate memory, like ffree 10. Then the ffree will allocate 10G memory.

This is my code

program ffree
  character  :: buf * 255
  integer    :: size, info
  integer(8) :: num, i
  real(8),dimension(:),allocatable :: ff

  call getarg(1, buf)
  read(buf, *)size

  num = size * 1024 * 1024 * 1024_8 / 8
  print*, "total number", num

  allocate(ff(num), stat = info)
  if(info.ne.0) stop ("Don't have enough memory")
  ff = 0

  forall(i = 1:num)
     ff(i) = 0
  end forall

  print*, "there are ", size, " G memory at least."
  print*, "allocate finished"
  read*, i

  deallocate(ff)
  print*, "deallocate finished"
end program ffree

When the type of ff is real, the program is normal. I mean when I use ffree like `ffree 10`, the command free show 10G used.

But I change the type to complex. When I use free like `ffree 10`, the command free show 20 G used. Why?

Figures below(before I run the program, 4 G has been used)

real.png

cmplx.png

0 Kudos
3 Replies
JVanB
Valued Contributor II
521 Views

complex(kind=8) internally is an ordered pair of real(kind=8) variables, so it occupies 16 bytes rather than 8.

There is also complex*8 which internally is an ordered pair of real*4 variables, therefore 8 bytes in memory.

If you use the standard-conforming complex(kind=8) declaration style, you have to take into account the extra storage, while if you use the nonstandard complex*8 notation you have to take into account the lower precision.  There is the C_SIZEOF function from the standard intrinsic ISO_C_BINDING module and the almost equivalent intrinsic extension SIZEOF that can tell you the size of a data item.

 

0 Kudos
holmz
New Contributor I
521 Views

Sometimes printing out the size and value of the first element can be useful.

D       WRITE(*,*)' SIZEOF(C(1)=',SIZEOF(C(1)), '  C=',C

Using the "D" you can compile with -d-lines when you are debugging and then -no-d-lines when you get past that point,
(better check the -d-lines and -no-d-lines, as I usually get them misspelled...)

0 Kudos
Steven_L_Intel1
Employee
521 Views

D lines work only for fixed-form source.

0 Kudos
Reply