Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can Somebody explain me this MPI code properly. (Please be as descriptive as possible) -> #include mpi.h #include #include #define NRA 10 /* number of

Can Somebody explain me this MPI code properly. (Please be as descriptive as possible) ->

#include "mpi.h"

#include

#include

#define NRA 10 /* number of rows in matrix A */

#define NCA 10 /* number of columns in matrix A */

#define NCB 5 /* 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 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 */

MPI_Status status;

MPI_Init(&argc,&argv);

MPI_Comm_rank(MPI_COMM_WORLD,&taskid);

MPI_Comm_size(MPI_COMM_WORLD,&numtasks);

if (numtasks

printf("Need at least two MPI tasks. Quitting... ");

MPI_Abort(MPI_COMM_WORLD, rc);

exit(1);

}

numworkers = numtasks-1;

/**************************** master task ************************************/

if (taskid == MASTER)

{

printf("mpi_mm has started with %d tasks. ",numtasks);

printf("Initializing arrays... ");

for (i=0; i

for (j=0; j

a[i][j]= i+j;

for (i=0; i

for (j=0; j

b[i][j]= i*j;

/* Send matrix data to the worker tasks */

averow = NRAumworkers;

extra = NRA%numworkers;

offset = 0;

mtype = FROM_MASTER;

for (dest=1; dest

{

rows = (dest

printf("Sending %d rows to task %d offset=%d ",rows,dest,offset);

MPI_Send(&offset, 1, MPI_INT, dest, mtype, MPI_COMM_WORLD);

MPI_Send(&rows, 1, MPI_INT, dest, mtype, MPI_COMM_WORLD);

MPI_Send(&a[offset][0], rows*NCA, MPI_DOUBLE, dest, mtype,

MPI_COMM_WORLD);

MPI_Send(&b, NCA*NCB, MPI_DOUBLE, dest, mtype, MPI_COMM_WORLD);

offset = offset + rows;

}

/* Receive results from worker tasks */

mtype = FROM_WORKER;

for (i=1; i

{

source = i;

MPI_Recv(&offset, 1, MPI_INT, source, mtype, MPI_COMM_WORLD, &status);

MPI_Recv(&rows, 1, MPI_INT, source, mtype, MPI_COMM_WORLD, &status);

MPI_Recv(&c[offset][0], rows*NCB, MPI_DOUBLE, source, mtype,

MPI_COMM_WORLD, &status);

printf("Received results from task %d ",source);

}

/* Print results */

printf("****************************************************** ");

printf("Result Matrix: ");

for (i=0; i

{

printf(" ");

for (j=0; j

printf("%6.2f ", c[i][j]);

}

printf(" ****************************************************** ");

printf ("Done. ");

}

/**************************** worker task ************************************/

if (taskid > 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

for (i=0; i

{

c[i][k] = 0.0;

for (j=0; j

c[i][k] = c[i][k] + a[i][j] * b[j][k];

}

mtype = FROM_WORKER;

MPI_Send(&offset, 1, MPI_INT, MASTER, mtype, MPI_COMM_WORLD);

MPI_Send(&rows, 1, MPI_INT, MASTER, mtype, MPI_COMM_WORLD);

MPI_Send(&c, rows*NCB, MPI_DOUBLE, MASTER, mtype, MPI_COMM_WORLD);

}

MPI_Finalize();

}

image text in transcribed

CC:Windows system32\cmd.exe Microsoft Windows [Uersion 6.1.7601 1 Copyright (c) 2009 Microsoft Corporation. All rights re s erved C:\Users\1 6 BCE0747c d C:\Users\1 6 BCE0747\Documents\U isual Studio 2012\Projects atrix\Debug C:\Users\1 6 BCE0747\Documents\U isual Studio 2012Projects\matrix\Debugnpiexec -n 4 matrix.exe mpi mm has started with 4 tasks. Initializing arrays - - Sending 4 rows to task 1 offset-D Sending 3 rows to task 2 offset-4 Sending 3 rows to task 3 offset-7 Receiued results from task 1 Received results from task 2 Receiued results from task 3 Result Matrix: 0.00 285.00 570.00 855.00 1140.00 0.00 330.00 660.00 990.00 1320.00 0.00 375.00 750.00 1125.00 1500.00 0.00 420.00 840.00 1260.00 1680.00 0.00 465.00 930.00 1395.00 1860.00 0.00 510.00 1020.00 1530.00 2040.00 0.00 555.00 1110.00 1665.00 2220.00 0.00 600.0 1200.00 1800.00 2400.00 0.00 645.00 1290.00 1935.00 2580.00 0.00 690.00 1380.00 2070.00 276.00 Done. C:\Users 16BCE0747\DocumentsUisual Studio 2012 Projects matrix\Debug CC:Windows system32\cmd.exe Microsoft Windows [Uersion 6.1.7601 1 Copyright (c) 2009 Microsoft Corporation. All rights re s erved C:\Users\1 6 BCE0747c d C:\Users\1 6 BCE0747\Documents\U isual Studio 2012\Projects atrix\Debug C:\Users\1 6 BCE0747\Documents\U isual Studio 2012Projects\matrix\Debugnpiexec -n 4 matrix.exe mpi mm has started with 4 tasks. Initializing arrays - - Sending 4 rows to task 1 offset-D Sending 3 rows to task 2 offset-4 Sending 3 rows to task 3 offset-7 Receiued results from task 1 Received results from task 2 Receiued results from task 3 Result Matrix: 0.00 285.00 570.00 855.00 1140.00 0.00 330.00 660.00 990.00 1320.00 0.00 375.00 750.00 1125.00 1500.00 0.00 420.00 840.00 1260.00 1680.00 0.00 465.00 930.00 1395.00 1860.00 0.00 510.00 1020.00 1530.00 2040.00 0.00 555.00 1110.00 1665.00 2220.00 0.00 600.0 1200.00 1800.00 2400.00 0.00 645.00 1290.00 1935.00 2580.00 0.00 690.00 1380.00 2070.00 276.00 Done. C:\Users 16BCE0747\DocumentsUisual Studio 2012 Projects matrix\Debug

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Mannino Michael

5th Edition

0983332401, 978-0983332404

Students also viewed these Databases questions