There is a very common mistake in your code that has nothing to do with MPI itself but with the proper use of pointers in C/C++. All MPI communication calls in C expect a pointer to the data and most examples are shown like MPI_Somecall(&var, ...)
but there var
is usually a scalar integer or floating-point variable. While &var
also works if var
is a static array, it doesn't work if var
is a pointer since &var
then gives the location of the pointer itself and not the address that it points to.
In a nutshell:
MPI_Somecall(&var, ...)
when var
is:
- scalar global/static or automatic variable, e.g.
int var;
- a global/static or an automatic instance of a structure, e.g.
struct foo var;
Otherwise:
MPI_Somecall(var, ...)
when var
is:
- a static array, e.g.
int var[10];
- a pointer to a heap variable or array, e.g.
int *var = malloc(...);
- a pointer in general, e.g.
int *var = &some_int_var;
- a function argument of a pointer type, e.g.
void func(..., int *var, ...);
Replace all instances of &array
in your code with array
.