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.

Memory alignment in derived types

Wee_Beng_T_
Beginner
2,216 Views
Hi,

If I have the derived type below :

type test

integer :: k,ij(2)

real :: xy(2)

end type test

type(test) :: t1(10,10)

Are the variables aligned in this way :

t1(1,1)%k,t1(1,1)%ij(1),t1(1,1)%ij(2),t1(1,1)%xy(1),t1(1,1)%xy(2)

t1(1,2)%k,t1(1,2)%ij(1),t1(1,2)%ij(2),t1(1,2)%xy(1),t1(1,2)%xy(2) ... ?

or

t1(1,1)%k,t1(1,2)%k,t1(1,3)%k, ...

t1(1,1)%ij(1),t1(1,2)%ij(1),t1(1,3)%ij(1) ... ?

or even

t1(1,1)%k,t1(1,2)%k,t1(1,3)%k, ...

t1(1,1)%ij(1),t1(1,1)%ij(2),t1(1,2)%ij(1),t1(1,2)%ij(2) ... ?

Can I choose how the variables are stored in memory?

Also, do I need to use -align records during compilation whenever I have derived types?

thank you!







0 Kudos
1 Solution
Steven_L_Intel1
Employee
2,216 Views
SEQUENCE will give you the first layout. I would not use the word "aligned" here, as it means something different than I think you are using.

I don't see a problem, offhand, with the current layout and your proposed code. I suggest that you write your program for clarity and correctness first. Run the program through Intel VTune Amplifier to see if you are getting memory contention or cache miss issues. Ideally you want to keep your memory references near each other if they are small.

View solution in original post

0 Kudos
4 Replies
jimdempseyatthecove
Honored Contributor III
2,216 Views
Look in the documentation for SEQUENCE

Jim Dempsey
0 Kudos
Steven_L_Intel1
Employee
2,216 Views
Without SEQUENCE or BIND(C), you cannot depend on the relative order of components within the derived type, but the answer to your general question is that the first layout you show is what you get. Within each element of t1, each of its components are together.

SEQUENCE prevents the compiler from rearranging the components, but does not necessarily prevent padding between components for alignment. Intel Fortran does not add padding, by default, for SEQUENCE types, though you can request that with -align sequence.

BIND(C) specifies that the order of the components, and alignment padding, is exactly the same as what the "companion C compiler" would do for an equivalent struct (though array components are still laid out in Fortran's column-major order.)

You do not "need" to use -align records - but you can if you wish. For the type you show, no alignment would be added even if you added that option, since all the components are naturally aligned.
0 Kudos
Wee_Beng_T_
Beginner
2,216 Views
Hi,

I googled for the use of SEQUENCE and it gives me the idea that it will ensure that the memory will be aligned according to the first layout, is that so?

Is there anyway I can get layout 2 or even 3 thru some means?

This is because in my code, I have something like:

do j = 1,10

do i = 1,10

ii = t1(i,j)%ij(1); jj = t1(i,j)%ij(2)

expression involving ii,jj ....

end do

end do

Is it better/faster if I just declare the array ij(2,10,10)?

thank you!

0 Kudos
Steven_L_Intel1
Employee
2,217 Views
SEQUENCE will give you the first layout. I would not use the word "aligned" here, as it means something different than I think you are using.

I don't see a problem, offhand, with the current layout and your proposed code. I suggest that you write your program for clarity and correctness first. Run the program through Intel VTune Amplifier to see if you are getting memory contention or cache miss issues. Ideally you want to keep your memory references near each other if they are small.
0 Kudos
Reply