#include "headers.h" int main(int argc, char *argv[]){ int me, m, src, dst, row, fd[3], i, j, nproc, square; int *Arow, *Brow, *Crow, *A, *B, *C; MPI_Status status; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &me); MPI_Comm_size(MPI_COMM_WORLD, &nproc); allocate_rows(&Arow, &Brow, &Crow, nproc + 1); row = me + 1; square = nproc * nproc; dst = ((me == (nproc - 1)) ? 0 : me + 1 ); src = ((me == 0) ? (nproc - 1) : me - 1 ); if(me == BossMan){ /* parent code */ if(parse_args(argc, argv, fd, &nproc) < 0){ MPI_Finalize(); exit(EXIT_FAILURE); } /* make the matrices */ allocate_rows(&A, &B, &C, square); /* initialize A */ get_row(fd[0], square, row, A); /* initialize B */ get_row(fd[1], square, row, B); } /* Scatter A */ MPI_Scatter(A,nproc,MPI_INT,&Arow[1],nproc,MPI_INT,BossMan,MPI_COMM_WORLD); /* Scatter B */ MPI_Scatter(B,nproc,MPI_INT,&Brow[1],nproc,MPI_INT,BossMan,MPI_COMM_WORLD); for(j = 1; j <= nproc; j++) Crow[j] = Arow[row] * Brow[j]; /* pass the rows of B around the ring */ m = row; for(i = 1; i < nproc; i++){ MPI_Send(&Brow[1],nproc,MPI_INT,dst,ROWTAG,MPI_COMM_WORLD); MPI_Recv(&Brow[1],nproc,MPI_INT,src,ROWTAG,MPI_COMM_WORLD,&status); if((m -= 1) == 0) m = nproc; for(j = 1; j <= nproc; j++) { Crow[j] += Arow[m] * Brow[j]; } } MPI_Gather(&Crow[1],nproc,MPI_INT,C,nproc,MPI_INT,BossMan,MPI_COMM_WORLD); if(me == BossMan)set_row(fd[2], square, row, C); MPI_Finalize(); exit(EXIT_SUCCESS); }