I am writing my first program using MPI and I am having hard time trying to properly send data to other processes using MPI_Scatter, modify them and receive the values using MPI_Gather. The code is as follows:
int** matrix;
int m = 2, n = 2;
int status;
// could have been int matrix[2][2];
matrix = malloc(m*sizeof(int*));
for(i = 0; i < m; i++) {
matrix[i] = malloc(n*sizeof(int));
}
matrix[0][0] = 1;
matrix[0][1] = 2;
matrix[1][0] = 2;
matrix[1][1] = 3;
MPI_Init( &argc, &argv );
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
MPI_Comm_size( MPI_COMM_WORLD, &size );
printf("My name is %d out of %d\n", rank, size);
MPI_Scatter(&matrix, 1, MPI_INT, &status, 1, MPI_INT, 0, MPI_COMM_WORLD);
printf("My name is %d and my status is %d\n", rank, status);
status += 1;
MPI_Gather(&status,1,MPI_INT,&matrix,1,MPI_INT,0,MPI_COMM_WORLD);
The results is as follows:
My name is 0 out of 4
My name is 1 out of 4
My name is 1 and my status is 0
My name is 2 out of 4
My name is 2 and my status is 120
My name is 0 and my status is 17773264
My name is 3 out of 4
My name is 3 and my status is 0
After MPI_Gather when I try to print the content of matrix I get segmentation fault...
Which is not what I expect... I suspect that the problem is I have a 2d array and want to send single values. Is this right? How to correct this?