#include "mpi.h" /* required MPI library */ #include #define NRA 100 /* number of rows in matrix A */ #define NCA 15 /* number of columns in matrix A */ #define NCB 7 /* 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, /* determines 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 */ MPI_Status status; /*----------------*/ /* initialize MPI */ /*----------------*/ rc = MPI_Init(&argc,&argv); /*-----------------------------------*/ /* get the size of the process group */ /*-----------------------------------*/ rc|= MPI_Comm_size(MPI_COMM_WORLD,&numtasks); /*---------------------------*/ /* get the process ID number */ /*---------------------------*/ rc|= MPI_Comm_rank(MPI_COMM_WORLD,&taskid); if (rc != 0) printf ("error initializing MPI and obtaining task ID info\n"); numworkers = numtasks-1; /*-------------------------------------------------------*/ /********************** master task **********************/ /*-------------------------------------------------------*/ if (taskid == MASTER) { printf("Number of worker tasks = %d\n",numworkers); for (i=0; i MASTER) { mtype = FROM_MASTER; /*------------------------------------------------------------*/ /* receive the initial offset position of the matrix at which */ /* I will start */ /*------------------------------------------------------------*/ MPI_Recv(&offset, 1, MPI_INT, MASTER, mtype, MPI_COMM_WORLD, &status); /*-----------------------------------------------------*/ /* receive the number of rows I am required to compute */ /*-----------------------------------------------------*/ MPI_Recv(&rows, 1, MPI_INT, MASTER, mtype, MPI_COMM_WORLD, &status); /*-----------------------------------------*/ /* receive the matrix A starting at offset */ /*-----------------------------------------*/ MPI_Recv(&a, rows*NCA, MPI_DOUBLE, MASTER, mtype, MPI_COMM_WORLD, &status); /*----------------------*/ /* receive the matrix B */ /*----------------------*/ MPI_Recv(&b, NCA*NCB, MPI_DOUBLE, MASTER, mtype, MPI_COMM_WORLD, &status); for (k=0; k