MAIN PROGRAM : matrices multiplication: C=A*B here we'd to keep C matrix "row by row" !!!


#include <stdio.h> #include <iostream.h> #include <mpi.h> #define N 400 // matrix dimension #define comm MPI_COMM_WORLD // standard MPI communicator static MPI_Status status; // current status of MPI operation static int IP,KP; // current process number and total number of processes static int tag=0; static double A[N][N], B[N][N], C[N][N]; // all the 3 needed matrices main (int argc, char **argv) { int i,j,k,nc1,nc,rep; double t,e; char pname[MPI_MAX_PROCESSOR_NAME]; MPI_Init(&argc, &argv); MPI_Comm_rank(comm,&IP); // who I am ? MPI_Comm_size(comm,&KP); // how manu of us are there in a team ? MPI_Get_processor_name(pname,&i); // what are processor names ? nc=N/KP; j=N%KP; // we consider N > KP !!! (IP<j) ? nc++,nc1=IP*nc : nc1=j*(nc+1)+nc*(IP-j); //distribute columns cout << "Process " << IP << " of " << KP << " started on " << pname << " with columns from " << nc1 << " to " << nc1+nc-1 << endl; if(IP==0) // process 0 sends the whole matrices A and B to others for(k=1;k<KP;k++) { MPI_Send(&A,N*N,MPI_DOUBLE,k,tag,comm); MPI_Send(&B,N*N,MPI_DOUBLE,k,tag,comm); } else // other processes receives the source matrices { MPI_Recv(&A,N*N,MPI_DOUBLE,0,tag,comm,&status); MPI_Recv(&B,N*N,MPI_DOUBLE,0,tag,comm,&status); } // ------------------ start multiplication for(i=0;i<N;i++) for(j=0;j<nc;j++) { t=0; for(k=0; k<N; k++) t+=(A[i][k])*(B[k][j+nc1]); C[j+nc1][i]=t; } // ------------------ finish multiplication if(IP==0) // process 0 receives the results for(k=1;k<KP;k++) { nc=N/KP; j=N%KP; (k<j) ? nc++,nc1=k*nc : nc1=j*(nc+1)+nc*(k-j); MPI_Recv(&(C[nc1][0]),N*nc,MPI_DOUBLE,k,tag,comm,&status); } else // others send their results (they know nc1 and nc!) MPI_Send(&(C[nc1][0]),N*nc,MPI_DOUBLE,0,tag,comm); MPI_Finalize(); }