Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

// c3090 pass4 key Parallelize the matrix code given #include <malloc.h> #includ

ID: 3910117 • Letter: #

Question

// c3090 pass4 key

Parallelize the matrix code given

#include <malloc.h>

#include <stdio.h>

#include <iostream>

#include <omp.h>

using namespace std;

#define ORDER 1000

#define AVAL 3.0

#define BVAL 5.0

#define TOL 0.001

int main(int argc, char *argv[])

{

int Ndim, Pdim, Mdim; /* A[N][P], B[P][M], C[N][M] */

int i, j, k;

double *A, *B, *C, cval, tmp, err, errsq;

double dN, mflops;

double start_time, run_time;

Ndim = ORDER;

Pdim = ORDER;

Mdim = ORDER;

A = (double *)malloc(Ndim*Pdim * sizeof(double));

B = (double *)malloc(Pdim*Mdim * sizeof(double));

C = (double *)malloc(Ndim*Mdim * sizeof(double));

/* Initialize matrices */

for (i = 0; i<Ndim; i++)

for (j = 0; j<Pdim; j++)

*(A + (i*Ndim + j)) = AVAL;

for (i = 0; i<Pdim; i++)

for (j = 0; j<Mdim; j++)

*(B + (i*Pdim + j)) = BVAL;

for (i = 0; i<Ndim; i++)

for (j = 0; j<Mdim; j++)

*(C + (i*Ndim + j)) = 0.0;

start_time = omp_get_wtime();

/* Do the matrix product */

for (i = 0; i<Ndim; i++) {

for (j = 0; j<Mdim; j++) {

tmp = 0.0;

for (k = 0; k<Pdim; k++) {

/* C(i,j) = sum(over k) A(i,k) * B(k,j) */

tmp += *(A + (i*Ndim + k)) * *(B + (k*Pdim + j));

}

*(C + (i*Ndim + j)) = tmp;

}

}

/* Check the answer */

run_time = omp_get_wtime() - start_time;

cout << " Order " << ORDER << " multiplication in " << run_time << " seconds ";

cout << " " << omp_get_max_threads() << " threads ";

dN = (double)ORDER;

mflops = 2.0 * dN * dN * dN / (1000000.0* run_time);

cout << " Order " << ORDER << " multiplication at " << mflops << " mflops ";

cval = Pdim * AVAL * BVAL;

errsq = 0.0;

for (i = 0; i<Ndim; i++) {

for (j = 0; j<Mdim; j++) {

err = *(C + i*Ndim + j) - cval;

errsq += err * err;

}

}

if (errsq > TOL)

cout << " Errors in multiplication: " << errsq << endl;

else

cout << " Successful Completion";

printf(" Exiting to OS ");

system("pause");

return 0;

}

Explanation / Answer

Hello,

The below code will satisfy your need.

Thank you :)