I am trying to do a matrix matrix multiplication using a MPI. I am right now using a very hard coded approach. I have initialized an array in the processor ranked 0. I send this array to all the other processors i.e the processor with rank 1 and rank 2.
Now, when I try to extract some information in a temporary array, I am getting some garbage values. I cant figure out why.
Here is the code:
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&world_size);;
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
if(rank == 0){
int mat_a[9] = {1,1,1,2,2,2,3,3,3};
int mat_b[9] = {1,1,1,2,2,2,3,3,3};
for(i = 1;i<world_size;i++){
MPI_Send(&mat_a[0],9,MPI_INT,i,tag,MPI_COMM_WORLD);
//MPI_Send(&mat_b[0],9,MPI_INT,i,tag,MPI_COMM_WORLD);
}
}
if(rank!=0){
MPI_Recv(&mat_a[0],9,MPI_INT,0,tag,MPI_COMM_WORLD,&status);
//MPI_Recv(&mat_b[0],9,MPI_INT,0,tag,MPI_COMM_WORLD,&status);
}
for(i = rank;i<9;i=i+3){
temp_1[num] = mat_a[i];
num++;
}
num = 0;
/* for(i = rank;i<9;i=i*3){
temp_2[num] = mat_b[i];
num++;
}
num = 0;
*/
for(i = 0;i<3;i++){
printf("in rank %d with val at temp_1 = %d\n",rank,temp_1[i]);
}
Here is my output:
in rank 0 with val at temp_1 = 550149432
in rank 0 with val at temp_1 = 0
in rank 0 with val at temp_1 = 4965952
in rank 1 with val at temp_1 = 1
in rank 1 with val at temp_1 = 2
in rank 2 with val at temp_1 = 1
in rank 2 with val at temp_1 = 2
in rank 2 with val at temp_1 = 3
in rank 1 with val at temp_1 = 3
Job complete
Please help
num
is not initialized before it is used. Always compile with-Wall
, i.e. with all warnings. - Dmitri Chubarovnum
is not declared, this is not a minimal reproducible example, please update the code. Also: in practice - useMPI_Scatter
. - Zulanmat_a
is declared in rank0 is limited by the conditional. - Dmitri Chubarov