0
votes

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

1
num is not initialized before it is used. Always compile with -Wall, i.e. with all warnings. - Dmitri Chubarov
num is not declared, this is not a minimal reproducible example, please update the code. Also: in practice - use MPI_Scatter. - Zulan
Also the scope where mat_a is declared in rank0 is limited by the conditional. - Dmitri Chubarov
@DmitriChubarov ohh yeah. So stupid of me. Thanks. - Vishesh Chanana

1 Answers

1
votes

On rank 0 the scope where mat_a is initialized is limited to the conditional

  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);
        }
  }

Out of this scope the variable mat_a is declared not initialized.

That is why the output is correct for processes with positive rank and incorrect on the process with rank=0.

Unfortunately with GCC this mistake would not be recognized. Even though it is clear that the variable is used without initialization.

You can find a complete example at the following link that reproduces this error.

To fix this with minimal changes you might do it as follows:

  if(rank == 0){
        int tmp_a[9] = {1,1,1,2,2,2,3,3,3};
        int tmp_b[9] = {1,1,1,2,2,2,3,3,3};
        for(i=0;i<9;i++) {
                mat_a[i] = tmp_a[i];
                mat_b[i] = tmp_b[i];
        }
        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);
        }
  }