/****************************************************************************** * FILE: matmul.c * DESCRIPTION: * The master task distributes a matrix multiply operation to numtasks-1 worker tasks. * THe matrices are stored in row-major fashion ******************************************************************************/ #include #include #include #include #define MASTER 0 /* my_rank of first task */ #define FROM_MASTER 1 /* setting a message type */ #define FROM_WORKER 2 /* setting a message type */ #define DEBUG 0 MPI_Status status; main(int argc, char **argv) { int numtasks, /* number of tasks in partition */ my_rank, /* a task identifier */ numworkers, /* number of worker tasks */ source, /* task id of message source */ dest, /* task id of message destination */ nbytes, /* number of bytes in message */ tag, /* 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, /* misc */ count, seed, NRA, NCA, NCB; if (argc != 4) /*escape sequence for command line*/ { printf("correct command is srun -N \n"); return 0; } /*read inputs*/ NRA = atoi(argv[1]); /* number of rows in matrix A */ NCA = atoi(argv[2]); /* number of columns in matrix A */ NCB = atoi(argv[3]); /* number of columns in matrix B */ #ifdef DEBUG printf("NRA: %d NCA: %d NCB: %d\n", NRA, NCA, NCB); #endif double a[NRA][NCA], /* matrix A to be multiplied */ b[NCA][NCB], /* matrix B to be multiplied */ c[NRA][NCB]; /* result matrix C */ double start, /*start time*/ end, /*end time*/ time; /*time elapsed*/ MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); MPI_Comm_size(MPI_COMM_WORLD, &numtasks); numworkers = numtasks-1; start=MPI_Wtime(); //record start time /**************************** master side ************************************/ if (my_rank == MASTER) { #ifdef DEBUG printf("Number of worker tasks = %d\n",numworkers); #endif for (i=0; i MASTER) { tag = FROM_MASTER; source = MASTER; MPI_Recv(&offset, 1, MPI_INT, source, tag, MPI_COMM_WORLD, &status); MPI_Recv(&rows, 1, MPI_INT, source, tag, MPI_COMM_WORLD, &status); count = rows*NCA; MPI_Recv(&a, count, MPI_DOUBLE, source, tag, MPI_COMM_WORLD, &status); count = NCA*NCB; MPI_Recv(&b, count, MPI_DOUBLE, source, tag, MPI_COMM_WORLD, &status); #ifdef DEBUG printf ("SLAVE(%d): source: %d tag: %d ", my_rank, source, tag); printf ("offset: %d ", offset); printf ("rows: %d ", rows); printf ("a[0][0]: %e ", a[0][0]); printf ("b[0][0]: %e ", b[0][0]); printf("\n"); #endif int kmax, imax, jmax; kmax = NCB; imax = rows; jmax = NCA; #ifdef DEBUG printf ("SLAVE(%d): kmax(NCB): %d imax(NRA): %d jmax(NCA): %d\n", my_rank, kmax, imax, jmax); #endif for (k=0; k