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

Data alignment in common blocks

rfvujm
Beginner
924 Views

Hi,

I'm currently developing a static library in Fortran containing common blocks. These COMMONswill be accessed from C++ application, that links the fortran library. As the C++ app requirement is that the data from extern "C" is aligned by 1 byte, I was hoping to set this in VF11 compiler settings, however I have only found /allign: commons (4 bytes) and/allign: dcommons (8 bytes). Is there a different way of solving this problem (e.g. in-line allignment macro)? I would be most grateful for your help.

Regards,

Mario

0 Kudos
6 Replies
TimP
Honored Contributor III
924 Views

Generally speaking, it's unreliable to depend on schemes for including padding between variables in COMMON, particularly when interoperating between languages. Perhaps that is the point you are making.

The traditional recommendation to declare all data in a COMMON in decreasing order of size is still good, to avoid requirements for padding to produce correct alignments. This is satisfactory, in the absence of requirement for efficient vectorization.

In order to promote vectorization without inclusion of padding, we set arrays to specific sizes, such as multiples of 16. That may not be among your concerns.

0 Kudos
Steven_L_Intel1
Employee
924 Views

Declare your COMMON with the BIND(C) attribute, like this:

BIND(C) :: /COMMON_NAME/

This will make Fortran lay out the variables exactly as C would in a struct. Fortran does not pad COMMONs by default, which is why you don't see an option for it.

0 Kudos
rfvujm
Beginner
924 Views

My C++ appneeds to support the dataalignment by 1 for legacy reasons (porting from Alpha)and a chain structureof the set of software modules that passes this data. The fortran code is mid-way in this chain, it gets HUGE amount of REAL*4, INTEGER*2 and LOGICAL*1 variables and arrays and collects it in COMMONs in various order (so no natural alignment possible). Next, the C++ gets itby extern "C", wraps it into structures and passes it further on. At each stage, the offsets between each vaiable must be kept constant. I'd like to avoid low-level data manipulation and COMMONs redefinition, as the whole chain would be affected.

Any way to 'hack' :) the IVF11 compiler?

0 Kudos
TimP
Honored Contributor III
924 Views

As Steve pointed out, the iso_c_binding declaration guarantees consistency (almost certainly with no padding) between Fortran and extern "C"

The mis-alignment would have been problematical on Alpha, and the code was intentionally non-portable, but Steve's suggestion seems best.

0 Kudos
Steven_L_Intel1
Employee
924 Views
Even without BIND(C), Intel Fortran does not pad COMMONs unless you ask it to.
0 Kudos
rfvujm
Beginner
924 Views

Thanks guys!

Mario

0 Kudos
Reply