Matrix Multiplication

In order to save time executing complex tasks, it is often useful to utilize parallel computing concepts. For this project the goal was to use the functions of the MPI library in order to establish a communication between processors. The code starts by creating the 2 matrices and then broadcasts the whole matrix B to all processors. Then each processor receives nbln lines of matrix A, by using the MPI_Scatter function. Every processors now have nbln lines of matrix A written in their respective buffer, they then use that buffer and the matrix B to write the product of AxB in matrix C. The resulting matrix is then gathered by processor 0 and displayed. This project was done using C, and should be compiled with mpicc.


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

  int main(int argc,char *argv[])
    int p, myid, source, dest, nbln, deb_ligne,i,j,k,rc;
    const int nla=12;    /* Nombre de lignes dans la matrice A */
    const int nca=11;    /* Nombre de colonnes dans la matrice A */
    const int ncb=9;   /* Nombre de colonnes dans la matrice B */
    int  root=0, tag1=1, tag2=2;
    double	 a[nla][nca],           /* matrice A  */
      b[nca][ncb],           /* matrice B */
      c[nla][ncb],           /* matrice résultat  C */
      buff[nla][nca];	/* buff des comm. collectives*/
    MPI_Status status;
    /* on suppose nla divise p*/
    nbln  = nla/p;
    if (myid == root)
	for (i=0; i<nla; i++)
	  for (j=0; j<nca; j++)
	    a[i][j]= i+j;
	for (i=0; i<nca; i++)
	  for (j=0; j<ncb; j++)
	    b[i][j]= i*j;
    MPI_Bcast(&b ,nca*ncb, MPI_DOUBLE, 0, MPI_COMM_WORLD); /*Broadcast de la matrice B*/ 
    /*Scatter de nbln lignes au processeurs aux processeurs*/
    MPI_Scatter(&a, nca*nbln, MPI_DOUBLE, &buff, nca*nbln, MPI_DOUBLE, 0, MPI_COMM_WORLD);
    /*calcul du pr duit de la matrice AxB */
    for (k=0; k<ncb; k++)
      for (i=0; i < nbln; i++)
	  c[i][k] = 0.0;
	  for (j=0; j<nca; j++)
	    c[i][k] = c[i][k] + buff[i][j] * b[j][k];
    MPI_Gather(&c, nbln*ncb, MPI_DOUBLE, &c, nbln*ncb, MPI_DOUBLE, 0, MPI_COMM_WORLD);
    /* affichage des  résultats */
    if(myid == 0){
      printf("Matrice résultat \n");
      for (i=0; i<nla; i++)
	  for (j=0; j<ncb; j++)
	    printf("%6.2f   ", c[i][j]);
      printf ("\n");