ISO_C_BINDING: performance impact / optimization

In the context of MPI with derived data types, I was told to be careful when using the bind(C) construct, since it blocks certain compiler optimizations. Consider this (a rather unlikely example):

 type, bind(C) :: myType integer(2) :: a complex :: z integer(2) :: b end type myType 

Without the bind(C) operator, the compiler is likely to change the order of the structure and group two integers for better alignment. Especially for large structures and when trying to use the auto-vector, this will be useful.

With bind(C) this rearrangement is not possible (in order to remain compatible with C , where the compiler probably won't optimize like that). This will lead either to a large memory consumption (three words instead of two) if all elements are word aligned or have lost alignment. (At least that's what they told me.)

Until recently, I had never mixed C and Fortran, and I had never used derived types for MPI exchange. In the near future I am going to study programming in a mixed language, and these problems seem to be important.

So my question is twofold:

  • bind(C) : Does this inconsistency play a role in real-world applications? Has anyone experienced performance / optimization issues here?
  • iso_c_binding : Are there additional errors when (optional) using the iso_c_binding module? What restrictions are imposed on the code and which optimizations are disabled?
+4
source share
1 answer

I wouldn’t worry so much. C compilers also insert padding into structures, especially when elements are not a multiple of 4 bytes. The sequence will be much worse, but it is something else.

Try using this equivalent of your derived type in C:

 #include <stdio.h> #include <stdint.h> #include <complex.h> typedef struct{ int16_t a; float complex b; int16_t c; } t; int main(){ to; printf("%x %x %x \n",&o.a,&o.b,&o.c); return 0; } 

My compiler ( gcc on x86_64) assigns components to addresses that are multiples of 4 bytes, even without any optimizations. Moreover, the alignment is exactly the same as gfortran selected with optimizations enabled.

The same destination is also used by icc , ifort , suncc and sunf90 .

  • The iso_c_binding module iso_c_binding should not affect optimization, since it is only a set of parameters, typedefs and procedures. This may force you to use more pointers than you usually do.
+5
source

Source: https://habr.com/ru/post/1502744/


All Articles