C: scatterv/gatherv a 2D array

Hello everyone,

I am new to MPI in C, and i am trying to scatter the rows in a 2D array of char, do some computations and after gather the 2D array again. Let's say that i use 4 processes:

I declare the global array and the local array here:

char global[20][3];
char local[5][3];
int my_rank;
int p;

and after i initialize mpi and scatter:

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &p);
MPI_Scatter(global, 15, MPI_CHAR, local, 15, MPI_CHAR, 0, MPI_COMM_WORLD);

And that works just fine.
But what if the user gives the size of the arrays, and i malloc the arrays ?

char **global;
global = (char **)malloc(20*sizeof(char*));
for(int i=0;i<3;i++) 
    global = (char *)malloc(3*sizeof(char));
char **local;
local = (char **)malloc(5*sizeof(char*));
for(int i=0;i<3;i++)
    local = (char *)malloc(3*sizeof(char));

Anyone ?

There is more to this than just one small example of code can fix. I found a pretty complete discussion here. READ the whole thing because there are several gotchas.
Try:

Malloc and MPI

1 Like