Write 3 MPI Programs for Matrix Multiplication Assume all pr

Write 3 MPI Programs for Matrix Multiplication Assume all process start with initial portion of data and hold results in the end Version One:All to all broudcast Version Two:Row/Column broudcast Version Three: Cannon\'s Alogorithm Compare Run on 16 processes With Matrix size N = 256 Compare running time of each Deliverables Source Code Analysis of running time Graph of time

Solution

#include <stdio.h>

#include <stdlib.h>

#include <mpi.h>

#define ms 2

int main(int argc,char* argv[])

{

    int i,j,k;

    int x,c;

    int matrix_a[ms][ms];

    int matrix_b[ms][ms];

    int matrix_c[ms][ms];

    int myrank, p;

    int NRPE;

    double starttime, endtime;

    MPI_Init(&argc, &argv);

    MPI_Comm_rank(MPI_COMM_WORLD, &myrank);

    MPI_Comm_size(MPI_COMM_WORLD, &p);

    MPI_Status status;

    NRPE = ms / p;

    if(myrank == 0)

    {

         printf(\"\ TCE 3411 Parellel Processing : Matrix Multiplication\ \");

         printf(\"\ by Sargunan Arujanan 1001110396\");

         printf(\"\    Muin bin Abdullah 1071119464\ \");

         printf(\"\ Date : %s\",__DATE__);

         printf(\"\ Time : %s\",__TIME__);

         printf(\"\ ====================================================\ \");

        //matrix a

        printf(\"\    matrix a \ \");

        printf(\"--------------\ \");

        for(i=0; i<ms; ++i)

            for(j=0; j<ms; ++j)

                matrix_a[i][j] = rand() % 10;

        for(i=0; i<ms; ++i)

        {

            for(j=0; j<ms; ++j)

                printf(\"%3d\", matrix_a[i][j]);

            printf(\"\ \");

        }

        //matrix b

        printf(\"\    matrix b \ \");

        printf(\"--------------\ \");

        for(x=0; x<ms; ++x)

            for(c=0; c<ms; ++c)

                matrix_b[x][c] = rand() % 10;

        for(x=0; x<ms; ++x)

        {

            for(c=0; c<ms; ++c)

                printf(\"%3d\", matrix_b[x][c]);

            printf(\"\ \");

        }

    }

    //Broadcast Matrix B values to all proccess

    for(i=0; i < ms; i++)

    {

        MPI_Bcast(matrix_b[i], ms*ms, MPI_INT, 0, MPI_COMM_WORLD);

    }

    printf(\"\ MATRIX B by Process: %d\ \", myrank);

    for(x=0; x<ms; ++x)

    {

        for(c=0; c<ms; ++c)

            printf(\"%3d\", matrix_b[x][c]);

        printf(\"\ \");

    }

    //End of Broadcast

    //Sending of each row of Matrix A to all process

    for(i=0; i<p; i++)

    {

      for(j=0; j<ms; j++)

       {

            MPI_Send(&matrix_a[j], ms*NRPE, MPI_INT, i, 0, MPI_COMM_WORLD);

            NRPE++;

       }

    }

    //end of sending

    //Receiving of each row of Matrix A per process

    MPI_Recv(matrix_a, ms*NRPE, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);

    //Multiplication Area

    starttime = MPI_Wtime();

    for (k=0; k<ms; k++)

    for (i=0; i<ms; i++) {

      matrix_c[i][k] = 0;

      for (j=0; j<ms; j++)

        matrix_c[i][k] = matrix_c[i][k] + matrix_a[i][j] * matrix_b[j][k];

      }

      endtime   = MPI_Wtime();

    MPI_Send(&matrix_c[i][k], ms*ms, MPI_INT, 0, 0, MPI_COMM_WORLD);

    //end of multiplication

    //Display area

    if(myrank == 0)

    {

        //MPI_Recv(matrix_c, ms*ms, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);

        printf(\"\ RESULT: matrix c \ \");

        printf(\"----------------\ \");

         for (i=0; i<ms; i++)

         {

          printf(\"\ \");

          for (k=0; k<ms; k++)

           printf(\"%3d \", matrix_c[i][k]);

         }

         printf(\"\ \ Parellel Time %f seconds\ \",endtime-starttime);

    }

  printf (\"\ \");

    return 0;

    MPI_Finalize();

}

 Write 3 MPI Programs for Matrix Multiplication Assume all process start with initial portion of data and hold results in the end Version One:All to all broudca
 Write 3 MPI Programs for Matrix Multiplication Assume all process start with initial portion of data and hold results in the end Version One:All to all broudca
 Write 3 MPI Programs for Matrix Multiplication Assume all process start with initial portion of data and hold results in the end Version One:All to all broudca
 Write 3 MPI Programs for Matrix Multiplication Assume all process start with initial portion of data and hold results in the end Version One:All to all broudca

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site