Frage

In the context of MPI with derived data types, I've been told to be careful when using the bind(C) construct as it inhibits certain optimizations of the compiler. Consider this (rather unlikely example):

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

Without the bind(C) statement the compiler would probably reorder the structure and group the two integers for better alignment. Especially for large structures and when trying to use auto-vectorization this will be beneficial.

With bind(C), this regrouping is impossible (to stay compatible to C, where the compiler probably won't optimize that much). This would either lead to a large memory consumption (three words instead of two) if all elements are aligned to words, or to a loss of alignment. (At least, so I've been told. )

Until recently, I have never mixed C and Fortran, and I have never used derived types for MPI communications. In the near future, I'm going to look into mixed language programming, and these issues seem to be important.

So my question is two-fold:

  • bind(C): Does this misalignment play a role in "real-world" applications? Has anyone experienced performance/optimization issues here?
  • iso_c_binding: Are there additional pitfalls when (additionally) using the module iso_c_binding? Which restrictions are imposed on the code and which optimizations are disabled?
War es hilfreich?

Lösung

I wouldn't worry that much. C compilers also do insert padding into structures, especially when the elements are not multiples of 4 bytes. Much worse would be sequence, but that is something different.

Try 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(){
 t o;
 printf("%x %x %x \n",&o.a,&o.b,&o.c);
 return 0;
}

My compiler (gcc on x86_64) alligns the components to addresses that are multiples of 4 bytes even without any optimizations. Even more, the allignment is exactly the same that gfortran chose with all optimizations enabled.

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

  • The module iso_c_binding itself should have no impact on optimizations, bacuse it is only a collection of parameters, typedefs and procedures. It may force you to use more pointers than you would normally do though.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top