0
#include<stdio.h>
#include<mpi.h>

int main()
{
        int a_r = 0, a_c = 0, v_s = 0, i = 0, rank = 0, size = 0;
        int local_row = 0, partial_sum = 0, sum = 0, j = 0;
        int my_first_ele = 0, my_last_ele = 0;
        int a[10][10], v[10], partial_mul[10] = {0}, mul[10] = {0};

        MPI_Init(NULL, NULL);
        MPI_Comm_rank(MPI_COMM_WORLD, &rank);
        MPI_Comm_size(MPI_COMM_WORLD, &size);

        if(rank == 0)
        {
                printf("Enter the row of array A: ");
                scanf("%d", &a_r);
                printf("Enter the column of array A: ");
                scanf("%d", &a_c);
                printf("Enter the array A: ");

                for(i = 0; i < a_r; i++)
                {
                        for(j = 0; j < a_c; j++)
                                scanf("%d", &a[i][j]);
                }

                printf("Enter the size of vector array: ");
                scanf("%d", &v_s);
                printf("Enter the vector array: ");
                for(i = 0; i < v_s; i++)
                {
                        scanf("%d", &v[i]);
                }

                MPI_Bcast(&a_r, 1, MPI_INT, 0, MPI_COMM_WORLD);
                MPI_Bcast(&a_c, 1, MPI_INT, 0, MPI_COMM_WORLD);
                MPI_Bcast(&v_s, 1, MPI_INT, 0, MPI_COMM_WORLD);
                MPI_Bcast(a, a_r*a_c, MPI_INT, 0, MPI_COMM_WORLD);
                MPI_Bcast(v, v_s, MPI_INT, 0, MPI_COMM_WORLD);

                local_row = a_r / size;
                my_first_ele = rank * local_row;
                my_last_ele = (rank+1) * local_row;

                if(a_c == v_s)
                {      
                        for(i = my_first_ele; i < my_last_ele; i++)
                        {
                                for(j = 0; j < a_c; j++)
                                {
                                        partial_mul[i] = partial_mul[i] + (a[i][j]*v[j]);
                                }
                        }
                        printf("\nPartial multiplication in Rank 0: \n");
                        for(i = my_first_ele; i < my_last_ele; i++)
                                printf("%d \n", partial_mul[i]);

                        MPI_Gather(partial_mul, local_row, MPI_INT, mul, local_row, MPI_INT, 0, MPI_COMM_WORLD);

                        printf("\n \nGlobal Multiplication: \n");
                        for(i = 0; i < a_r; i++)
                        {
                                printf("%d \n", mul[i]);
                        }
                }
                else
                        printf("\nCan't multiply. \n");
        }

        else
        {
                MPI_Bcast(&a_r, 1, MPI_INT, 0, MPI_COMM_WORLD);
                MPI_Bcast(&a_c, 1, MPI_INT, 0, MPI_COMM_WORLD);
                MPI_Bcast(&v_s, 1, MPI_INT, 0, MPI_COMM_WORLD);
                MPI_Bcast(a, a_r*a_c, MPI_INT, 0, MPI_COMM_WORLD);
                MPI_Bcast(v, v_s, MPI_INT, 0, MPI_COMM_WORLD);

                local_row = a_r / size;
                my_first_ele = rank * local_row;
                my_last_ele = (rank+1) * local_row;
                if(a_c == v_s)
                {     
                        for(i = my_first_ele; i < my_last_ele; i++)
                        {
                                for(j = 0; j < a_c; j++)
                                {
                                        partial_mul[i] = partial_mul[i] + (a[i][j]*v[j]);
                                }
                        }
                        printf("\nPartial multiplication in Rank %d: \n", rank);
                        for(i = my_first_ele; i < my_last_ele; i++)
                                printf("%d \n", partial_mul[i]);

                        MPI_Gather(partial_mul, local_row, MPI_INT, mul, local_row, MPI_INT, 0, MPI_COMM_WORLD);

                }
                else
                        printf("\nCan't multiply. \n");
        }
        MPI_FINALIZE();
}

I have a problem with above code. My partial multiplication value is correct. But in my overall multiplication I can only gather rank 0 elements, rest of the values are being printed as 0. What is the problem can anyone explain.

Zulan
  • 21,896
  • 6
  • 49
  • 109
priya
  • 29
  • 7

1 Answers1

1

Looking at your data layout I think you misunderstand data structures in MPI: All data is kept separate in each rank, there is no implict sharing or distribution. Your vector partial_sum is separate on each rank, each with the full 10 elements. So assuming size=2, a_r=10 and zero initialization, after the computation the contents will look like this:

  • rank 0: {x0,x1,x2,x3,x4,0,0,0,0,0}
  • rank 1: {0,0,0,0,0,x5,x6,x7,x8,x9}

Where x is the correct computed value. Gather will then collect the first local_row=5 elements from each rank, resulting in {x0,x1,x2,x3,x4,0,0,0,0,0}.

You could just fix this by adding the correct offset:

MPI_Gather(&partial_mul[my_first_ele], local_row, MPI_INT, mul, local_row, MPI_INT, 0, MPI_COMM_WORLD);

But please don't do that. Instead you should reconsider your data structures to really distribute the data, reserve the correct sizes for each part of the vector / array. To send around parts of the data to each rank, use MPI_Scatter (the opposite of MPI_Gather). The most difficult is to get the matrix right. This is explained in detail by this excellent answer.

Community
  • 1
  • 1
Zulan
  • 21,896
  • 6
  • 49
  • 109