/************************************************************
* File: mm_speedup.ch
* Purpose: Parallel matrix multiplication using Ch arrays
* with measurement of the average time taken by
* each worker process to complete its computation.
************************************************************/
#include
#include
#include
#define NRA 2880 // number of rows in matrix A
#define NCA 2000 // number of columns in matrix A
#define NCB 450 // 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(int argc, char *argv[]) {
int numtasks, // number of tasks
taskid, // task identifier
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; // misc
double stime, etime, my_compute_time, compute_time;
FILE *stream;
char *file = "output_c.dat";
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
MPI_Comm_rank(MPI_COMM_WORLD, &taskid);
if(taskid == MASTER) {
rows = NRA;
}
else {
averow = NRA/(numtasks-1);
extra = NRA%(numtasks-1);
rows = (taskid <= extra) ? averow+1 : averow;
}
// declare matrices using Ch computational arrays
array double a[rows][NCA], // matrix A to be multiplied
b[NCA][NCB], // matrix B to be multiplied
c[rows][NCB]; // result matrix C
if(taskid == MASTER) {
printf("Number of worker tasks = %d\n", numtasks-1);
// initialize matrix a and matrix b
for(i=0; i