matrix multiplication using Mpi_Scatter and Mpi_Gather

后端 未结 2 1076
迷失自我
迷失自我 2020-12-20 02:14

I newbie to mpi programming. I was trying to write matrix multiplication. Went through the post MPI Matrix Multiplication with scatter gather about matrix multiplication usi

相关标签:
2条回答
  • 2020-12-20 02:26

    Call to mpi_finalize doesn't indicate that all the MPI processes are terminated like in OpenMP !

    In most of mpi implementation, all the processes execute the instruction before the MPI_init and after MPI_Finalized.

    A good practice is to do nothing before MPI_Init and after MPI_Finalized.

    0 讨论(0)
  • 2020-12-20 02:42

    The result matrix c is getting printed by all processes because every process executes the function void print_results(char *prompt, int a[N][N]). Since you are gathering at the process having rank 0, add a statement if (rank == 0) before calling the print_results(...) function. Further, the result is incorrect because of a wrong loop logic in :

                    for (j = 0; j < N; j++)
                    {
                            sum = sum + aa[j] * b[i][j];                
                    }
    

    This should be :

                    for (j = 0; j < N; j++)
                    {
                            sum = sum + aa[j] * b[j][i];                
                    }
    

    Also there is no need to broadcast b as all processes already already have a copy of it and you can avoid MPI_Barrier(). The complete program then becomes :

    #define N 4
    #include <stdio.h>
    #include <math.h>
    #include <sys/time.h>
    #include <stdlib.h>
    #include <stddef.h>
    #include "mpi.h"
    
    
    void print_results(char *prompt, int a[N][N]);
    
    int main(int argc, char *argv[])
    {
        int i, j, k, rank, size, tag = 99, blksz, sum = 0;
        int a[N][N]={{1,2,3,4},{5,6,7,8},{9,1,2,3},{4,5,6,7,}};
        int b[N][N]={{1,2,3,4},{5,6,7,8},{9,1,2,3},{4,5,6,7,}};
        int c[N][N];
        int aa[N],cc[N];
    
        MPI_Init(&argc, &argv);
        MPI_Comm_size(MPI_COMM_WORLD, &size);
        MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    
        //scatter rows of first matrix to different processes     
        MPI_Scatter(a, N*N/size, MPI_INT, aa, N*N/size, MPI_INT,0,MPI_COMM_WORLD);
    
        //broadcast second matrix to all processes
        MPI_Bcast(b, N*N, MPI_INT, 0, MPI_COMM_WORLD);
    
        MPI_Barrier(MPI_COMM_WORLD);
    
              //perform vector multiplication by all processes
              for (i = 0; i < N; i++)
                {
                        for (j = 0; j < N; j++)
                        {
                                sum = sum + aa[j] * b[j][i];  //MISTAKE_WAS_HERE               
                        }
                        cc[i] = sum;
                        sum = 0;
                }
    
        MPI_Gather(cc, N*N/size, MPI_INT, c, N*N/size, MPI_INT, 0, MPI_COMM_WORLD);
    
        MPI_Barrier(MPI_COMM_WORLD);        
        MPI_Finalize();
        if (rank == 0)                         //I_ADDED_THIS
            print_results("C = ", c);
    }
    
    void print_results(char *prompt, int a[N][N])
    {
        int i, j;
    
        printf ("\n\n%s\n", prompt);
        for (i = 0; i < N; i++) {
                for (j = 0; j < N; j++) {
                        printf(" %d", a[i][j]);
                }
                printf ("\n");
        }
        printf ("\n\n");
    }
    

    Then c =

    C = 
     54 37 47 57
    
     130 93 119 145
    
     44 41 56 71
    
     111 79 101 123 
    
    0 讨论(0)
提交回复
热议问题