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

In this lab you will use OMP to parallelize the multiplication of two matrices B

ID: 3904656 • Letter: I

Question

In this lab you will use OMP to parallelize the multiplication of two matrices B and C of type double, A, B and C are all M x M matrices, where M 25. Implement the following functions double** allocateMat (?nt sz) ; // Returns pointer to a matrix void assignMat (double** mat, int sz) ; //All entries 2.0 double** matMult(double**, double**, int sz) //Multiply void fxeeMat (double**, int sz); I free a matrix void printMat double**, int sz); Use the omp get ntime() API to display the elapsed time for the multiplication operation only. Parallelize memory allocation and multiplication. The function matMult) allocates memory to the product matrix and returns a pointer to the matrix (of pointers). Pay attention to the scopes for all variables. Use static scheduling and different chunksize (from 1-4). Obtain elapsed times for the various schedules

Explanation / Answer


/*
* Parallel Matrix Multiplication using openMP
* Compile with -fopenmp flag
*
*/
#include <algorithm>
#include <omp.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <assert.h>


using namespace std;
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define pks printf("Case %d: ",++ks);

#define mx 1002                    

/*
here i have manually set the matrix location you can replace it with a function that returns the location of mat[0][0] for eg like: char * p = &a[0][0]; return p;
*/

int a[mx][mx];
int b[mx][mx];
int c[mx][mx];
int d[mx][mx];
void assignMat(int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=2.0;
b[i][j]=2.0;

}
}
}
void check(int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
assert(c[i][j]==d[i][j]);
}
}
void matMult(int n)
{
int i,j,k;
double st=omp_get_wtime();
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
double en=omp_get_wtime();
printf("Serial: %lf ",en-st);
}
void matrix_mult_parallel1(int n)
{
//Static Scheduler
memset(d,0,sizeof d);
int i,j,k;
double st=omp_get_wtime();
#pragma omp parallel for schedule(static,50) collapse(2) private(i,j,k)shared(a,b,c)
for(i=0;i<n;i++)for( j=0;j<n;j++)for(k=0;k<n;k++)d[i][j]+=a[i][k]*b[k][j];
double en=omp_get_wtime();
printf("Parallel-1(Static Scheduler) %lf ",en-st);
check(n);
}
void matrix_mult_parallel2(int n)
{
//Dynamic Scheduler
memset(d,0,sizeof d);
int i,j,k;
double st=omp_get_wtime();
#pragma omp parallel for schedule(dynamic,50) collapse(2) private(i,j,k) shared(a,b,c)
for(i=0;i<n;i++)for( j=0;j<n;j++)for(k=0;k<n;k++)d[i][j]+=a[i][k]*b[k][j];
double en=omp_get_wtime();
printf("Parallel-2(Dynamic Scheduler) %lf ",en-st);
check(n);
}
int main() {
//READ("in");
//WRITE("out2");
int n=500;
assignMat();
matMult(n);

matrix_mult_parallel1(n);

matrix_mult_parallel2(n);

return 0;

}


Note: To free the matrices you can just delare a function which takes the location of the matrix and its size and the inserts in eah block.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote