/****************************************************************************** * MPI Matrix Multiply - C Version * FILE: mpi_mm.c * OTHER FILES: make.mpi_mm.c * DESCRIPTION: * In this code, the master task distributes a matrix multiply * operation to numtasks-1 worker tasks. * NOTE1: C and Fortran versions of this code differ because of the way * arrays are stored/passed. C arrays are row-major order but Fortran * arrays are column-major order. * AUTHOR: Ros Leibensperger / Blaise Barney. Converted to MPI: George L. * Gusciora (1/25/95) * LAST REVISED: 12/14/95 Blaise Barney ******************************************************************************/ #include "mpi.h" #include #define NRA 1000 /* number of rows in matrix A */ #define NCA 1000 /* number of columns in matrix A */ #define NCB 1000 /* number of columns in matrix B */ #define MASTER 0 /* taskid of first task */ #define FROM_MASTER 1 /* setting a message type */ #define FROM_WORKER 2 /* setting a message type */ int main(argc,argv) int argc; char *argv[]; { int numtasks, /* number of tasks in partition */ taskid, /* a task identifier */ numworkers, /* number of worker tasks */ source, /* task id of message source */ dest, /* task id of message destination */ mtype, /* message type */ rows, /* rows of matrix A sent to each worker */ averow, extra, offset, /* used to determine rows sent to each worker */ i, j, k, rc; /* misc */ double a[NRA][NCA], /* matrix A to be multiplied */ b[NCA][NCB], /* matrix B to be multiplied */ c[NRA][NCB]; /* result matrix C */ double startwtime,endwtime; MPI_Status status; rc = MPI_Init(&argc,&argv); rc|= MPI_Comm_size(MPI_COMM_WORLD,&numtasks); rc|= MPI_Comm_rank(MPI_COMM_WORLD,&taskid); if (rc != 0) printf ("error initializing MPI and obtaining task ID information\n"); else printf ("task ID = %d\n", taskid); numworkers = numtasks-1; /**************************** master task ************************************/ if (taskid == MASTER) { printf("Number of worker tasks = %d\n",numworkers); for (i=0; i MASTER) { mtype = FROM_MASTER; MPI_Recv(&offset, 1, MPI_INT, MASTER, mtype, MPI_COMM_WORLD, &status); MPI_Recv(&rows, 1, MPI_INT, MASTER, mtype, MPI_COMM_WORLD, &status); MPI_Recv(&a, rows*NCA, MPI_DOUBLE, MASTER, mtype, MPI_COMM_WORLD, &status); MPI_Recv(&b, NCA*NCB, MPI_DOUBLE, MASTER, mtype, MPI_COMM_WORLD, &status); for (k=0; k