First, I'm learning Message Passing Interface(MPI) from https://computing.llnl.gov/tutorials/mpi/
When it comes to creating your own MPI datatype, I'm having trouble with it.
My program is trying to get each quadrant. Say the following 4 x 4 matrix,
A = {
1.0, 2.0, 3.0, 4.0,
5.0, 6.0, 7.0, 8.0,
9.0, 10.0, 11.0, 12.0,
13.0, 14.0, 15.0, 16.0
}
So I want to divid it into 4 submatrices such that when master sends out 3 submatrices(submatrix 1, 2, 3), each worker can receive it's respective submatrix.
Submatrix 0 | Submatrix 1
Submatrix 2 | Submatrix 3
Now, my program only gets the first row of each submatrix and it prints out second row as zeros.
The following is current print out. (You can ignore submatrix 0)
3 4
0 0
9 10
0 0
11 12
0 0
Attached is my program. Any pointer will be greatly appreciated.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<mpi.h>
//matrix size
#define SIZE 4
double A[SIZE][SIZE] ={
1.0, 2.0, 3.0, 4.0,
5.0, 6.0, 7.0, 8.0,
9.0, 10.0, 11.0, 12.0,
13.0, 14.0, 15.0, 16.0
};
static double B[SIZE/2][SIZE/2];
MPI_Datatype QUAD;
#define QUADRANT(Q,y,x) (Q[y * SIZE/2]+(x * SIZE/2))
void printout(double Y[SIZE/2][SIZE/2]){
int i,j;
for(i=0;i< SIZE/2;i++){
for(j=0; j< SIZE/2; j++){
printf("%.0f ",Y[i][j]);
}
printf("\n");
}
}
int main(int argc, char** argv){
int rank, size, i, j;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Status stat;
//Define a MPI datatype, Quadrant
MPI_Type_vector(SIZE/2, SIZE/2, SIZE, MPI_DOUBLE, &QUAD);
MPI_Type_commit(&QUAD);
//master process
if(rank==0){
MPI_Send(QUADRANT(A,0,1),1,QUAD,1,0, MPI_COMM_WORLD);
MPI_Send(QUADRANT(A,1,0),1,QUAD,2,0,MPI_COMM_WORLD);
MPI_Send(QUADRANT(A,1,1),1,QUAD,3,0,MPI_COMM_WORLD);
}else{
MPI_Recv(B,1,QUAD,0,0,MPI_COMM_WORLD,&stat);
printout(B);
printf("\n");
}
MPI_Finalize();
}
There is similar program at https://computing.llnl.gov/tutorials/mpi/samples/C/mpi_vector.c
But trying to get all the numbers in the column matrix.