Lab05: Matrix Multiply Part 1

Objective
Apply parallel techniques to matrix algebra
Assignment for Part 1

  • Create a matrix of size maxn.
  • Send each row of the matrix to a different process
  • Print each row from each process,
    IDENTIFY THE PROCESS THAT IS PRINTING THE ROW
  • Print the entire matrix from process 0
    
    #include <stdio.h>
    #include "mpi.h"
    
    #define maxn 5
    
    int main(int argc, char **argv )
    {
        int rank, size, i, j;
        MPI_Status status;
        double x[maxn][maxn];
        double recvbuf[maxn];
        int row,col;
        MPI_Datatype Col;
    
        MPI_Init( &argc, &argv );
    
        MPI_Comm_rank( MPI_COMM_WORLD, &rank );
        MPI_Comm_size( MPI_COMM_WORLD, &size );
    
        if (maxn % (size-1) != 0 ) {
           if (rank==0) printf("Sorry, np-1 must divide into %d\n", maxn);
           MPI_Abort( MPI_COMM_WORLD, 1 );
        }
    
        if (rank == 0) {
           /* Fill the data as specified */
           for (i=0; i <maxn; i++)
               for (j=0; j<maxn; j++)
                   x[i][j] = rand() % 10;
    
           printf("Matrix X:\n");
           for (i=0; i<maxn; i++) {
               for (j=0; j<maxn; j++)
                   printf("%4.1f", x[i][j]);
               printf("\n");
           }
    
        }
        count = maxn/(size-1);
    
        if (rank==0) {
    
             //SEND EACH ROW OF MATRIX X TO A SEPARATE PROCESS
    	 //PRINT EACH ROW FROM EACH PROCESS
    	 //MPI_Send(&x[0],1, MPI_DOUBLE,1 , 0, MPI_COMM_WORLD);
    	 //The above sends row 1 (0th row) to process 1
    	 //Use a loop to send to each process
            }
        }
    
       if (rank>0) {
    
        printf("Process: %d, RECVBUF:\n", rank);
        //PRINT OUT EACH ROW FROM A DIFFERENT PROCESS
        // printf("%4.1f", recvbuf[ ]);
    
       }
     */
        MPI_Finalize( );
        return 0;
    }