I am trying to use MPI_Gather to gather individual two-dimensional arrays into the master process for it to then print out the contents of the the entire matrix. I split the workload across num_processes
processes and have each work on their own private matrix.
I'll give a snippet of my code along with some pseudo-code to demonstrate what I'm doing. Note I created my own MPI_Datatype as I am transferring struct types.
Point **matrix = malloc(sizeof(Point *) * (num_rows/ num_processes));
for (i = 0; i < num_rows/num_processes; i++)
matrix [i] = malloc(sizeof(Point) * num_cols);
for( i = 0; i< num_rows/num_processes; i++)
for (j = 0; j < num_cols; j++)
matrix[i][j] = *Point type*
if (processor == 0) {
full_matrix = malloc(sizeof(Point *) * num_rows);
for (i = 0; i < num_rows/num_processes; i++)
matrix [i] = malloc(sizeof(Point) * num_cols);
MPI_Gather(matrix, num_rows/num_processes*num_cols, MPI_POINT_TYPE, full_matrix, num_rows/num_processes*num_cols, MPI_POINT_TYPE, 0, MPI_COMM_WORLD);
} else {
MPI_Gather(matrix, num_rows/num_processes*num_cols, MPI_POINT_TYPE, NULL, 0, MPI_DATATYPE_NULL, 0, MPI_COMM_WORLD);
}
// ...print full_matrix...
The double for-loop prior to the gather computes the correct values as my own testing showed, but the gather onto full_matrix
only contains the data from its own processes, i.e. the master process, as its printing later showed.
I'm having trouble figuring out why this is given the master process transfers the data correctly. Is the problem how I allocate memory for each process?